Comments (3)
The current documentation clearly suggests using weights to vary the importance of each objective one against another. This is very misleading with selection operators that treat the fitness vector in a lexographic fashion.
from deap.
Just stopping by to say I agree with @kanosek and the original filer of this ticket.
Take for example selBest
vs. selTournament
.
selBest
uses sorted
with the reverse=True
argument. However, selTournament
uses max
.
This can lead to tricky behavior (note I'm using deap v1.3.0):
I would like to use deap to create a GA in which my weights represent, well, the weights of my objectives. All my weights will be negative, indicating I want the smallest possible value for each objective. So, I went ahead and subclassed base.Fitness like so to get around the lexicographical sorting (which doesn't meet my use case):
my_module.py
:
from deap import base
class MultiMinFitness(base.Fitness):
"""Compare with sum of wvalues. Smaller (more negative) is better."""
def __le__(self, other):
return sum(self.wvalues) <= sum(other.values)
def __lt__(self, other):
return sum(self.wvalues) < sum(other.values)
Now, if I create some unittests to ensure my subclass is behaving correctly, we get inconsistent results between selBest
and selTournament
:
test_my_module.py
:
import unittest
from unittest.mock import patch
from .my_module import MultiMinFitness
from deap import creator, tools
class MultiFitnessTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
"""Use the deap creator to set things up."""
creator.create('FitnessMin', MultiMinFitness, weights=(-2, -4, -8))
def test_selection(self):
class MyClass:
"""Stub in a class to assign fitnesses to."""
def __init__(self, f):
self.fitness = f
def __repr__(self):
return str('MyClass: ' + str(self.fitness))
# Initialize some fitness objects. Note that f2 is "more fit"
# since the weighted sum is "more negative"
f1 = creator.FitnessMin((1, 2, 3))
f2 = creator.FitnessMin((2, 4, 6))
# Create a couple class members.
c1 = MyClass(f1)
c2 = MyClass(f2)
# Use selBest to ensure c2 (and thus f2) is best.
best = tools.selBest([c1, c2], k=2)
# c2 is the 'most negative' and therefore the best.
self.assertIs(best[0], c2) # Succeeds
# Now perform a tournament selection. Patch selRandom so it just
# returns our list of MyClass objects.
with patch('deap.tools.selection.selRandom', return_value=[c1, c2]):
best_tourn = tools.selTournament([c1, c2], k=1, tournsize=2)
self.assertIs(best_tourn[0], c2) # Fails.
Results of running test_my_module.py
:
Failure
Traceback (most recent call last):
File "/usr/local/lib/python3.7/unittest/case.py", line 59, in testPartExecutor
yield
File "/usr/local/lib/python3.7/unittest/case.py", line 615, in run
testMethod()
File "/opt/project/tmp/test_my_module.py", line 39, in test_selection
self.assertIs(best_tourn[0], c2) # Fails.
File "/usr/local/lib/python3.7/unittest/case.py", line 1120, in assertIs
self.fail(self._formatMessage(msg, standardMsg))
File "/usr/local/lib/python3.7/unittest/case.py", line 680, in fail
raise self.failureException(msg)
AssertionError: MyClass: (1.0, 2.0, 3.0) is not MyClass: (2.0, 4.0, 6.0)
from deap.
Excellent point, thanks for your contribution. We'll consider this as part of the 2.0 refactoring that is underway.
from deap.
Related Issues (20)
- MultiStatistics class supports different functions for each Statistics object
- Support for Linux ARM aarch64 HOT 5
- In the example bbob.py,how can I get the "fgeneric" and "bbobbenchmarks" module? HOT 1
- Improve convergence performance(array dimensions question)
- Support Python 3.11 and 3.12, including wheels on PyPI
- AttributeError: 'list' object has no attribute 'fitness' HOT 8
- scaling in deap.tools.uniform_reference_points HOT 1
- question about the height of gp tree HOT 1
- TypeError: Population must be a sequence. For dicts or sets, use sorted(d) HOT 7
- question about kfold
- Getting the Pareto Front for every generation
- Seeking Help for "ValueError: too many values to unpack" Issue in Python Program HOT 1
- cxSimulatedBinaryBounded raises Exception with various eta HOT 1
- selNSGA3WithMemory fails if passed uniform_reference_points where nobj = 1
- gp.cxSemantic causes polyploidy
- How to use GA to find optimal set of solutions for multi-objective functions HOT 6
- Bug in released deap 1.4.1 on PyPI
- Fitness Comparation using a aggregated variable instead of lexicographic comparation
- ImportError: No module named copyreg HOT 1
- Callback Function in Deap HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deap.