forked from DEAP/deap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DEAP#175 from fmv1992/master
Fixed bin2float behavior and added test suite for benchamarks.py
- Loading branch information
Showing
10 changed files
with
142 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"""Test functions from deap/benchmarks.""" | ||
import sys | ||
import unittest | ||
from nose import with_setup | ||
from past.builtins import xrange | ||
|
||
from deap import base | ||
from deap import benchmarks | ||
from deap import creator | ||
from deap.benchmarks import binary | ||
|
||
|
||
class BenchmarkTest(unittest.TestCase): | ||
"""Test object for unittest of deap/benchmarks.""" | ||
|
||
def setUp(self): | ||
|
||
@binary.bin2float(0, 1023, 10) | ||
def evaluate(individual): | ||
"""Simplest evaluation function.""" | ||
return individual | ||
|
||
creator.create("FitnessMin", base.Fitness, weights=(-1.0,)) | ||
creator.create("Individual", list, fitness=creator.FitnessMin) | ||
self.toolbox = base.Toolbox() | ||
self.toolbox.register("evaluate", evaluate) | ||
|
||
def tearDown(self): | ||
del creator.FitnessMin | ||
|
||
def test_bin2float(self): | ||
|
||
# Correct evaluation of bin2float. | ||
zero_individual = creator.Individual([0 for x in xrange(10)]) | ||
full_individual = creator.Individual([1 for x in xrange(10)]) | ||
two_individiual = creator.Individual(8*[0] + [1, 0]) | ||
population = [zero_individual, full_individual, two_individiual] | ||
fitnesses = map(self.toolbox.evaluate, population) | ||
for ind, fit in zip(population, fitnesses): | ||
ind.fitness.values = fit | ||
assert population[0].fitness.values == (0.0, ) | ||
assert population[1].fitness.values == (1023.0, ) | ||
assert population[2].fitness.values == (2.0, ) | ||
|
||
# Incorrect evaluation of bin2float. | ||
wrong_size_individual = creator.Individual([0, 1, 0, 1, 0, 1, 0, 1, 1]) | ||
wrong_population = [wrong_size_individual] | ||
# It is up the user to make sure that bin2float gets an individual with | ||
# an adequate length; no exceptions are raised. | ||
fitnesses = map(self.toolbox.evaluate, wrong_population) | ||
for ind, fit in zip(wrong_population, fitnesses): | ||
# In python 2.7 operator.mul works in a different way than in | ||
# python3. Thus an error occurs in python2.7 but an assignment is | ||
# correctly executed in python3. | ||
if sys.version_info < (3, ): | ||
with self.assertRaises(TypeError): | ||
ind.fitness.values = fit | ||
else: | ||
assert wrong_population[0].fitness.values == () | ||
|
||
|
||
if __name__ == "__main__": | ||
suite = unittest.TestLoader().loadTestsFromTestCase(BenchmarkTest) | ||
unittest.TextTestRunner(verbosity=2).run(suite) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.