Skip to content

Commit

Permalink
2to3: apply range fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
keszybz committed Feb 27, 2022
1 parent 5b0865a commit 66cd64a
Show file tree
Hide file tree
Showing 19 changed files with 83 additions and 83 deletions.
4 changes: 2 additions & 2 deletions deap/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def varOr(population, toolbox, lambda_, cxpb, mutpb):
"or equal to 1.0.")

offspring = []
for _ in xrange(lambda_):
for _ in range(lambda_):
op_choice = random.random()
if op_choice < cxpb: # Apply crossover
ind1, ind2 = map(toolbox.clone, random.sample(population, 2))
Expand Down Expand Up @@ -481,7 +481,7 @@ def eaGenerateUpdate(toolbox, ngen, halloffame=None, stats=None,
logbook = tools.Logbook()
logbook.header = ['gen', 'nevals'] + (stats.fields if stats else [])

for gen in xrange(ngen):
for gen in range(ngen):
# Generate a new population
population = toolbox.generate()
# Evaluate the individuals
Expand Down
2 changes: 1 addition & 1 deletion deap/benchmarks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def dtlz1(individual, obj):
"""
g = 100 * (len(individual[obj-1:]) + sum((xi-0.5)**2 - cos(20*pi*(xi-0.5)) for xi in individual[obj-1:]))
f = [0.5 * reduce(mul, individual[:obj-1], 1) * (1 + g)]
f.extend(0.5 * reduce(mul, individual[:m], 1) * (1 - individual[m]) * (1 + g) for m in reversed(xrange(obj-1)))
f.extend(0.5 * reduce(mul, individual[:m], 1) * (1 - individual[m]) * (1 + g) for m in reversed(range(obj-1)))
return f

def dtlz2(individual, obj):
Expand Down
20 changes: 10 additions & 10 deletions deap/benchmarks/binary.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def wrapped_function(individual, *args, **kargs):
# User must take care to make nelem an integer.
nelem = len(individual)//nbits
decoded = [0] * nelem
for i in xrange(nelem):
for i in range(nelem):
gene = int("".join(map(str,
individual[i*nbits:i*nbits+nbits])),
2)
Expand Down Expand Up @@ -68,10 +68,10 @@ def chuang_f1(individual):
"""
total = 0
if individual[-1] == 0:
for i in xrange(0, len(individual)-1, 4):
for i in range(0, len(individual)-1, 4):
total += inv_trap(individual[i:i+4])
else:
for i in xrange(0, len(individual)-1, 4):
for i in range(0, len(individual)-1, 4):
total += trap(individual[i:i+4])
return total,

Expand All @@ -85,16 +85,16 @@ def chuang_f2(individual):
"""
total = 0
if individual[-2] == 0 and individual[-1] == 0:
for i in xrange(0, len(individual)-2, 8):
for i in range(0, len(individual)-2, 8):
total += inv_trap(individual[i:i+4]) + inv_trap(individual[i+4:i+8])
elif individual[-2] == 0 and individual[-1] == 1:
for i in xrange(0, len(individual)-2, 8):
for i in range(0, len(individual)-2, 8):
total += inv_trap(individual[i:i+4]) + trap(individual[i+4:i+8])
elif individual[-2] == 1 and individual[-1] == 0:
for i in xrange(0, len(individual)-2, 8):
for i in range(0, len(individual)-2, 8):
total += trap(individual[i:i+4]) + inv_trap(individual[i+4:i+8])
else:
for i in xrange(0, len(individual)-2, 8):
for i in range(0, len(individual)-2, 8):
total += trap(individual[i:i+4]) + trap(individual[i+4:i+8])
return total,

Expand All @@ -108,10 +108,10 @@ def chuang_f3(individual):
"""
total = 0
if individual[-1] == 0:
for i in xrange(0, len(individual)-1, 4):
for i in range(0, len(individual)-1, 4):
total += inv_trap(individual[i:i+4])
else:
for i in xrange(2, len(individual)-3, 4):
for i in range(2, len(individual)-3, 4):
total += inv_trap(individual[i:i+4])
total += trap(individual[-2:]+individual[:2])
return total,
Expand All @@ -125,7 +125,7 @@ def royal_road1(individual, order):
nelem = len(individual) // order
max_value = int(2**order - 1)
total = 0
for i in xrange(nelem):
for i in range(nelem):
value = int("".join(map(str, individual[i*order:i*order+order])), 2)
total += int(order) * int(value/max_value)
return total,
Expand Down
2 changes: 1 addition & 1 deletion deap/benchmarks/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def convergence(first_front, optimal_front):
distances.append(float("inf"))
for opt_ind in optimal_front:
dist = 0.
for i in xrange(len(opt_ind)):
for i in range(len(opt_ind)):
dist += (ind.fitness.values[i] - opt_ind[i])**2
if dist < distances[-1]:
distances[-1] = dist
Expand Down
4 changes: 2 additions & 2 deletions deap/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,8 @@ def cxOnePoint(ind1, ind2):
types2 = defaultdict(list)
if ind1.root.ret == __type__:
# Not STGP optimization
types1[__type__] = xrange(1, len(ind1))
types2[__type__] = xrange(1, len(ind2))
types1[__type__] = range(1, len(ind1))
types2[__type__] = range(1, len(ind2))
common_types = [__type__]
else:
for idx, node in enumerate(ind1[1:], 1):
Expand Down
12 changes: 6 additions & 6 deletions deap/tools/_hypervolume/pyhv.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def compute(self, front):
"""

def weaklyDominates(point, other):
for i in xrange(len(point)):
for i in range(len(point)):
if point[i] > other[i]:
return False
return True
Expand Down Expand Up @@ -152,7 +152,7 @@ def hvRecursive(self, dimIndex, length, bounds):
hvol = qPrevDimIndex.volume[dimIndex] + qPrevDimIndex.area[dimIndex] * (qCargo[dimIndex] - qPrevDimIndex.cargo[dimIndex])
else:
qArea[0] = 1
qArea[1:dimIndex+1] = [qArea[i] * -qCargo[i] for i in xrange(dimIndex)]
qArea[1:dimIndex+1] = [qArea[i] * -qCargo[i] for i in range(dimIndex)]
q.volume[dimIndex] = hvol
if q.ignore >= dimIndex:
qArea[dimIndex] = qPrevDimIndex.area[dimIndex]
Expand Down Expand Up @@ -184,7 +184,7 @@ def preProcess(self, front):
dimensions = len(self.referencePoint)
nodeList = _MultiList(dimensions)
nodes = [_MultiList.Node(dimensions, point) for point in front]
for i in xrange(dimensions):
for i in range(dimensions):
self.sortByDimension(nodes, i)
nodeList.extend(nodes, i)
self.list = nodeList
Expand Down Expand Up @@ -239,7 +239,7 @@ def __init__(self, numberLists):

def __str__(self):
strings = []
for i in xrange(self.numberLists):
for i in range(self.numberLists):
currentList = []
node = self.sentinel.next[i]
while node != self.sentinel:
Expand Down Expand Up @@ -292,7 +292,7 @@ def extend(self, nodes, index):

def remove(self, node, index, bounds):
"""Removes and returns 'node' from all lists in [0, 'index'[."""
for i in xrange(index):
for i in range(index):
predecessor = node.prev[i]
successor = node.next[i]
predecessor.next[i] = successor
Expand All @@ -309,7 +309,7 @@ def reinsert(self, node, index, bounds):
nodes of the node that is reinserted are in the list.
"""
for i in xrange(index):
for i in range(index):
node.prev[i].next[i] = node
node.next[i].prev[i] = node
if bounds[i] > node.cargo[i]:
Expand Down
14 changes: 7 additions & 7 deletions deap/tools/crossover.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def cxUniform(ind1, ind2, indpb):
:mod:`random` module.
"""
size = min(len(ind1), len(ind2))
for i in xrange(size):
for i in range(size):
if random.random() < indpb:
ind1[i], ind2[i] = ind2[i], ind1[i]

Expand Down Expand Up @@ -115,7 +115,7 @@ def cxPartialyMatched(ind1, ind2):
p1, p2 = [0] * size, [0] * size

# Initialize the position of each indices in the individuals
for i in xrange(size):
for i in range(size):
p1[ind1[i]] = i
p2[ind2[i]] = i
# Choose crossover points
Expand All @@ -127,7 +127,7 @@ def cxPartialyMatched(ind1, ind2):
cxpoint1, cxpoint2 = cxpoint2, cxpoint1

# Apply crossover between cx points
for i in xrange(cxpoint1, cxpoint2):
for i in range(cxpoint1, cxpoint2):
# Keep track of the selected values
temp1 = ind1[i]
temp2 = ind2[i]
Expand Down Expand Up @@ -166,11 +166,11 @@ def cxUniformPartialyMatched(ind1, ind2, indpb):
p1, p2 = [0] * size, [0] * size

# Initialize the position of each indices in the individuals
for i in xrange(size):
for i in range(size):
p1[ind1[i]] = i
p2[ind2[i]] = i

for i in xrange(size):
for i in range(size):
if random.random() < indpb:
# Keep track of the selected values
temp1 = ind1[i]
Expand Down Expand Up @@ -209,7 +209,7 @@ def cxOrdered(ind1, ind2):
optimization and machine learning. Addison Wesley, 1989
"""
size = min(len(ind1), len(ind2))
a, b = random.sample(xrange(size), 2)
a, b = random.sample(range(size), 2)
if a > b:
a, b = b, a

Expand Down Expand Up @@ -321,7 +321,7 @@ def cxSimulatedBinaryBounded(ind1, ind2, eta, low, up):
elif len(up) < size:
raise IndexError("up must be at least the size of the shorter individual: %d < %d" % (len(up), size))

for i, xl, xu in zip(xrange(size), low, up):
for i, xl, xu in zip(range(size), low, up):
if random.random() <= 0.5:
# This epsilon should probably be changed for 0 since
# floating point arithmetic in Python is safer
Expand Down
42 changes: 21 additions & 21 deletions deap/tools/emo.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def assignCrowdingDist(individuals):

nobj = len(individuals[0].fitness.values)

for i in xrange(nobj):
for i in range(nobj):
crowd.sort(key=lambda element: element[0][i])
distances[crowd[0][1]] = float("inf")
distances[crowd[-1][1]] = float("inf")
Expand Down Expand Up @@ -186,7 +186,7 @@ def tourn(ind1, ind2):
individuals_2 = random.sample(individuals, len(individuals))

chosen = []
for i in xrange(0, k, 4):
for i in range(0, k, 4):
chosen.append(tourn(individuals_1[i], individuals_1[i+1]))
chosen.append(tourn(individuals_1[i+2], individuals_1[i+3]))
chosen.append(tourn(individuals_2[i], individuals_2[i+1]))
Expand Down Expand Up @@ -620,7 +620,7 @@ def associate_to_niche(fitnesses, reference_points, best_point, intercepts):

# Retrieve min distance niche index
niches = numpy.argmin(distances, axis=1)
distances = distances[range(niches.shape[0]), niches]
distances = distances[list(range(niches.shape[0])), niches]
return niches, distances


Expand Down Expand Up @@ -710,7 +710,7 @@ def selSPEA2(individuals, k):
K = math.sqrt(N)
strength_fits = [0] * N
fits = [0] * N
dominating_inds = [list() for i in xrange(N)]
dominating_inds = [list() for i in range(N)]

for i, ind_i in enumerate(individuals):
for j, ind_j in enumerate(individuals[i+1:], i+1):
Expand All @@ -721,19 +721,19 @@ def selSPEA2(individuals, k):
strength_fits[j] += 1
dominating_inds[i].append(j)

for i in xrange(N):
for i in range(N):
for j in dominating_inds[i]:
fits[i] += strength_fits[j]

# Choose all non-dominated individuals
chosen_indices = [i for i in xrange(N) if fits[i] < 1]
chosen_indices = [i for i in range(N) if fits[i] < 1]

if len(chosen_indices) < k: # The archive is too small
for i in xrange(N):
for i in range(N):
distances = [0.0] * N
for j in xrange(i + 1, N):
for j in range(i + 1, N):
dist = 0.0
for l in xrange(L):
for l in range(L):
val = individuals[i].fitness.values[l] - \
individuals[j].fitness.values[l]
dist += val * val
Expand All @@ -742,20 +742,20 @@ def selSPEA2(individuals, k):
density = 1.0 / (kth_dist + 2.0)
fits[i] += density

next_indices = [(fits[i], i) for i in xrange(N)
next_indices = [(fits[i], i) for i in range(N)
if not i in chosen_indices]
next_indices.sort()
#print next_indices
chosen_indices += [i for _, i in next_indices[:k - len(chosen_indices)]]

elif len(chosen_indices) > k: # The archive is too large
N = len(chosen_indices)
distances = [[0.0] * N for i in xrange(N)]
sorted_indices = [[0] * N for i in xrange(N)]
for i in xrange(N):
for j in xrange(i + 1, N):
distances = [[0.0] * N for i in range(N)]
sorted_indices = [[0] * N for i in range(N)]
for i in range(N):
for j in range(i + 1, N):
dist = 0.0
for l in xrange(L):
for l in range(L):
val = individuals[chosen_indices[i]].fitness.values[l] - \
individuals[chosen_indices[j]].fitness.values[l]
dist += val * val
Expand All @@ -764,8 +764,8 @@ def selSPEA2(individuals, k):
distances[i][i] = -1

# Insert sort is faster than quick sort for short arrays
for i in xrange(N):
for j in xrange(1, N):
for i in range(N):
for j in range(1, N):
l = j
while l > 0 and distances[i][j] < distances[i][sorted_indices[i][l - 1]]:
sorted_indices[i][l] = sorted_indices[i][l - 1]
Expand All @@ -777,8 +777,8 @@ def selSPEA2(individuals, k):
while size > k:
# Search for minimal distance
min_pos = 0
for i in xrange(1, N):
for j in xrange(1, size):
for i in range(1, N):
for j in range(1, size):
dist_i_sorted_j = distances[i][sorted_indices[i][j]]
dist_min_sorted_j = distances[min_pos][sorted_indices[min_pos][j]]

Expand All @@ -789,11 +789,11 @@ def selSPEA2(individuals, k):
break

# Remove minimal distance from sorted_indices
for i in xrange(N):
for i in range(N):
distances[i][min_pos] = float("inf")
distances[min_pos][i] = float("inf")

for j in xrange(1, size - 1):
for j in range(1, size - 1):
if sorted_indices[i][j] == min_pos:
sorted_indices[i][j] = sorted_indices[i][j + 1]
sorted_indices[i][j + 1] = min_pos
Expand Down
4 changes: 2 additions & 2 deletions deap/tools/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def initRepeat(container, func, n):
See the :ref:`list-of-floats` and :ref:`population` tutorials for more examples.
"""
return container(func() for _ in xrange(n))
return container(func() for _ in range(n))

def initIterate(container, generator):
"""Call the function *container* with an iterable as
Expand Down Expand Up @@ -72,7 +72,7 @@ def initCycle(container, seq_func, n=1):
See the :ref:`funky` tutorial for an example.
"""
return container(func() for _ in xrange(n) for func in seq_func)
return container(func() for _ in range(n) for func in seq_func)

__all__ = ['initRepeat', 'initIterate', 'initCycle']

Expand Down
8 changes: 4 additions & 4 deletions deap/tools/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ def migRing(populations, k, selection, replacement=None, migarray=None):
"""
nbr_demes = len(populations)
if migarray is None:
migarray = range(1, nbr_demes) + [0]
migarray = list(range(1, nbr_demes)) + [0]

immigrants = [[] for i in xrange(nbr_demes)]
emigrants = [[] for i in xrange(nbr_demes)]
immigrants = [[] for i in range(nbr_demes)]
emigrants = [[] for i in range(nbr_demes)]

for from_deme in xrange(nbr_demes):
for from_deme in range(nbr_demes):
emigrants[from_deme].extend(selection(populations[from_deme], k))
if replacement is None:
# If no replacement strategy is selected, replace those who migrate
Expand Down
Loading

0 comments on commit 66cd64a

Please sign in to comment.