diff --git a/deap/algorithms.py b/deap/algorithms.py index 56e1a344..2883f5bb 100644 --- a/deap/algorithms.py +++ b/deap/algorithms.py @@ -74,9 +74,9 @@ def varAnd(population, toolbox, cxpb, mutpb): offspring[i]) del offspring[i - 1].fitness.values, offspring[i].fitness.values - for i in range(len(offspring)): + for i, off in enumerate(offspring): if random.random() < mutpb: - offspring[i], = toolbox.mutate(offspring[i]) + offspring[i], = toolbox.mutate(off) del offspring[i].fitness.values return offspring diff --git a/deap/base.py b/deap/base.py index f9d84ba6..b7aa22c7 100644 --- a/deap/base.py +++ b/deap/base.py @@ -298,9 +298,9 @@ def __le__(self, other): if self_violates_constraints and other_violates_constraints: return True - elif self_violates_constraints: + if self_violates_constraints: return True - elif other_violates_constraints: + if other_violates_constraints: return False return self.wvalues <= other.wvalues @@ -311,9 +311,9 @@ def __lt__(self, other): if self_violates_constraints and other_violates_constraints: return False - elif self_violates_constraints: + if self_violates_constraints: return True - elif other_violates_constraints: + if other_violates_constraints: return False return self.wvalues < other.wvalues @@ -324,9 +324,9 @@ def __eq__(self, other): if self_violates_constraints and other_violates_constraints: return True - elif self_violates_constraints: + if self_violates_constraints: return False - elif other_violates_constraints: + if other_violates_constraints: return False return self.wvalues == other.wvalues @@ -340,9 +340,9 @@ def dominates(self, other): if self_violates_constraints and other_violates_constraints: return False - elif self_violates_constraints: + if self_violates_constraints: return False - elif other_violates_constraints: + if other_violates_constraints: return True return super(ConstrainedFitness, self).dominates(other) diff --git a/deap/cma.py b/deap/cma.py index 03f69175..a2a78b7f 100644 --- a/deap/cma.py +++ b/deap/cma.py @@ -406,7 +406,7 @@ def generate(self, ind_init): of its parent. """ arz = numpy.random.randn(self.lambda_, self.dim) - individuals = list() + individuals = [] # Make sure every parent has a parent tag and index for i, p in enumerate(self.parents): @@ -436,9 +436,9 @@ def _select(self, candidates): pareto_fronts = tools.sortLogNondominated(candidates, len(candidates)) - chosen = list() + chosen = [] mid_front = None - not_chosen = list() + not_chosen = [] # Fill the next population (chosen) with the fronts until there is not enough space # When an entire front does not fit in the space left we rely on the hypervolume @@ -641,7 +641,7 @@ def __init__(self, parent, sigma, steps, **kargs): < self.S_int) self.constraint_vecs = None - self.ancestors_fitness = list() + self.ancestors_fitness = [] @property def lambda_(self): @@ -703,7 +703,7 @@ def _integer_mutation(self): # the last best solution. We skip this last best solution part. if n_I_R == 0: return numpy.zeros((self.lambda_, self.dim)) - elif n_I_R == self.dim: + if n_I_R == self.dim: p = self.lambda_ / 2.0 / self.lambda_ # lambda_int = int(numpy.floor(self.lambda_ / 2)) else: diff --git a/deap/gp.py b/deap/gp.py index ccc47b6b..bbac8ef6 100644 --- a/deap/gp.py +++ b/deap/gp.py @@ -211,8 +211,7 @@ def __eq__(self, other): if type(self) is type(other): return all(getattr(self, slot) == getattr(other, slot) for slot in self.__slots__) - else: - return NotImplemented + return NotImplemented class Terminal(object): @@ -238,8 +237,7 @@ def __eq__(self, other): if type(self) is type(other): return all(getattr(self, slot) == getattr(other, slot) for slot in self.__slots__) - else: - return NotImplemented + return NotImplemented class MetaEphemeral(type): @@ -296,7 +294,7 @@ def __init__(self, name, in_types, ret_type, prefix="ARG"): # being polluted by builtins function when evaluating # GP expression. self.context = {"__builtins__": None} - self.mapping = dict() + self.mapping = {} self.terms_count = 0 self.prims_count = 0 @@ -639,7 +637,7 @@ def generate(pset, min_, max_, condition, type_=None): raise IndexError("The gp.generate function tried to add " "a terminal of type '%s', but there is " "none available." % (type_,)).with_traceback(traceback) - if type(term) is MetaEphemeral: + if isinstance(term, MetaEphemeral): term = term() expr.append(term) else: @@ -772,7 +770,7 @@ def mutUniform(individual, expr, pset): slice_ = individual.searchSubtree(index) type_ = individual[index].ret individual[slice_] = expr(pset=pset, type_=type_) - return individual, + return (individual,) def mutNodeReplacement(individual, pset): @@ -784,21 +782,21 @@ def mutNodeReplacement(individual, pset): :returns: A tuple of one tree. """ if len(individual) < 2: - return individual, + return (individual,) index = random.randrange(1, len(individual)) node = individual[index] if node.arity == 0: # Terminal term = random.choice(pset.terminals[node.ret]) - if type(term) is MetaEphemeral: + if isinstance(term, MetaEphemeral): term = term() individual[index] = term else: # Primitive prims = [p for p in pset.primitives[node.ret] if p.args == node.args] individual[index] = random.choice(prims) - return individual, + return (individual,) def mutEphemeral(individual, mode): @@ -826,7 +824,7 @@ def mutEphemeral(individual, mode): for i in ephemerals_idx: individual[i] = type(individual[i])() - return individual, + return (individual,) def mutInsert(individual, pset): @@ -850,7 +848,7 @@ def mutInsert(individual, pset): primitives = [p for p in pset.primitives[node.ret] if node.ret in p.args] if len(primitives) == 0: - return individual, + return (individual,) new_node = choice(primitives) new_subtree = [None] * len(new_node.args) @@ -866,7 +864,7 @@ def mutInsert(individual, pset): new_subtree[position:position + 1] = individual[slice_] new_subtree.insert(0, new_node) individual[slice_] = new_subtree - return individual, + return (individual,) def mutShrink(individual): @@ -878,7 +876,7 @@ def mutShrink(individual): """ # We don't want to "shrink" the root if len(individual) < 3 or individual.height <= 1: - return individual, + return (individual,) iprims = [] for i, node in enumerate(individual[1:], 1): @@ -897,7 +895,7 @@ def mutShrink(individual): slice_ = individual.searchSubtree(index) individual[slice_] = subtree - return individual, + return (individual,) ###################################### @@ -1057,8 +1055,7 @@ def _genpop(n, pickfrom=[], acceptfunc=lambda s: True, producesizes=False): if producesizes: return producedpop, producedpopsizes - else: - return producedpop + return producedpop def halflifefunc(x): return x * float(alpha) + beta @@ -1210,8 +1207,8 @@ def graph(expr): out of order when using `NetworX `_. """ nodes = list(range(len(expr))) - edges = list() - labels = dict() + edges = [] + labels = {} stack = [] for i, node in enumerate(expr): @@ -1282,7 +1279,7 @@ def mutSemantic(individual, gen_func=genGrow, pset=None, ms=None, min=2, max=6): new_ind.extend(tr1) new_ind.extend(tr2) - return new_ind, + return (new_ind,) def cxSemantic(ind1, ind2, gen_func=genGrow, pset=None, min=2, max=6): diff --git a/examples/bbob.py b/examples/bbob.py index c51b3de0..e4953109 100644 --- a/examples/bbob.py +++ b/examples/bbob.py @@ -43,7 +43,7 @@ def tupleize(func): when the evaluation function returns a single value. """ def wrapper(*args, **kargs): - return func(*args, **kargs), + return (func(*args, **kargs),) return wrapper @@ -71,7 +71,7 @@ def main(func, dim, maxfuncevals, ftarget=None): # Evolve until ftarget is reached or the number of evaluation # is exhausted (maxfuncevals) - for g in range(1, maxfuncevals): + for _ in range(1, maxfuncevals): toolbox.update(worst, best, sigma) worst.fitness.values = toolbox.evaluate(worst) if best.fitness <= worst.fitness: diff --git a/setup.py b/setup.py index 142833f1..990f918d 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ import deap -warnings = list() +warnings = [] try: from setuptools import setup, Extension, find_packages @@ -59,7 +59,7 @@ def build_extension(self, ext): def run_setup(build_ext): extra_modules = None if build_ext: - extra_modules = list() + extra_modules = [] hv_module = Extension("deap.tools._hypervolume.hv", sources=["deap/tools/_hypervolume/_hv.c", "deap/tools/_hypervolume/hv.cpp"]) extra_modules.append(hv_module) diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index c917ceec..abbd4e4c 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -100,7 +100,7 @@ def test_nsga2(setup_teardown_multi_obj): ind.fitness.values = fit pop = toolbox.select(pop, len(pop)) - for gen in range(1, NGEN): + for _ in range(1, NGEN): offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] @@ -169,7 +169,7 @@ def valid(individual): toolbox.register("generate", strategy.generate, creator.__dict__[INDCLSNAME]) toolbox.register("update", strategy.update) - for gen in range(NGEN): + for _ in range(NGEN): # Generate a new population population = toolbox.generate() @@ -222,7 +222,7 @@ def test_nsga3(setup_teardown_multi_obj): pop = toolbox.select(pop, len(pop)) # Begin the generational process - for gen in range(1, NGEN): + for _ in range(1, NGEN): offspring = algorithms.varAnd(pop, toolbox, 1.0, 1.0) # Evaluate the individuals with an invalid fitness diff --git a/tests/test_convergence.py b/tests/test_convergence.py index c6a7892d..480dd2bc 100644 --- a/tests/test_convergence.py +++ b/tests/test_convergence.py @@ -82,7 +82,7 @@ def test_cma_mixed_integer_1_p_1_no_constraint(self): best = None - for gen in range(NGEN): + for _ in range(NGEN): # Generate a new population population = toolbox.generate() @@ -118,7 +118,7 @@ def test_cma_mixed_integer_1_p_20_no_constraint(self): best = None - for gen in range(NGEN): + for _ in range(NGEN): # Generate a new population population = toolbox.generate() @@ -174,7 +174,7 @@ def c2(individual): best = None - for gen in range(NGEN): + for _ in range(NGEN): # Generate a new population population = toolbox.generate() @@ -234,7 +234,7 @@ def c2(individual): best = None - for gen in range(NGEN): + for _ in range(NGEN): # Generate a new population population = toolbox.generate() @@ -291,7 +291,7 @@ def test_nsga2(self): ind.fitness.values = fit pop = toolbox.select(pop, len(pop)) - for gen in range(1, NGEN): + for _ in range(1, NGEN): offspring = tools.selTournamentDCD(pop, len(pop)) offspring = [toolbox.clone(ind) for ind in offspring] @@ -344,7 +344,7 @@ def test_nsga3(self): pop = toolbox.select(pop, len(pop)) # Begin the generational process - for gen in range(1, NGEN): + for _ in range(1, NGEN): # Vary the individuals offspring = list(islice(algorithms.varAnd(pop, toolbox, 1.0, 1.0), len(pop))) @@ -414,7 +414,7 @@ def valid(individual): toolbox.register("generate", strategy.generate, creator.__dict__[INDCLSNAME]) toolbox.register("update", strategy.update) - for gen in range(NGEN): + for _ in range(NGEN): # Generate a new population population = toolbox.generate() diff --git a/tests/test_multiproc.py b/tests/test_multiproc.py index 08616d48..9688fd70 100644 --- a/tests/test_multiproc.py +++ b/tests/test_multiproc.py @@ -5,7 +5,7 @@ def _evalOneMax(individual): - return sum(individual), + return (sum(individual),) def test_multiproc():