Remove xrange in one example

This commit is contained in:
Thomas Nagy 2019-01-18 18:47:33 +01:00
parent 17e973a7d3
commit 3bf0e7049b
No known key found for this signature in database
GPG Key ID: 49B4C67C05277AAA
1 changed files with 8 additions and 8 deletions

View File

@ -43,20 +43,20 @@ def gen(lst, options):
LEN = len(lst) LEN = len(lst)
POP = 3*LEN + 1 POP = 3*LEN + 1
popul = [range(LEN) for x in xrange(POP)] popul = [range(LEN) for x in range(POP)]
fitn = [0 for x in xrange(POP)] fitn = [0 for x in range(POP)]
def rnd(): def rnd():
return random.randint(0, LEN -1) return random.randint(0, LEN -1)
def mutate(): def mutate():
for x in xrange(LEN): for x in range(LEN):
# rotate the previous element by one # rotate the previous element by one
v = popul[x+LEN] = popul[x+LEN - 1] v = popul[x+LEN] = popul[x+LEN - 1]
a = v.pop(0) a = v.pop(0)
v.append(a) v.append(a)
for x in xrange(LEN): for x in range(LEN):
# swap elements # swap elements
a = rnd() a = rnd()
b = rnd() b = rnd()
@ -66,7 +66,7 @@ def gen(lst, options):
v[a] = v[b] v[a] = v[b]
v[b] = c v[b] = c
for x in xrange(LEN): for x in range(LEN):
# get one element out, add at the end # get one element out, add at the end
v = popul[x+2*LEN] v = popul[x+2*LEN]
@ -79,7 +79,7 @@ def gen(lst, options):
best = opti_ref best = opti_ref
pos = -1 pos = -1
for x in xrange(len(popul)): for x in range(len(popul)):
v = popul[x] v = popul[x]
arr = [lst[a] for a in v] arr = [lst[a] for a in v]
tmp = '%s %s' % (cmd, ' '.join(arr)) tmp = '%s %s' % (cmd, ' '.join(arr))
@ -99,14 +99,14 @@ def gen(lst, options):
assert (sum(popul[x]) == sum(range(LEN))) assert (sum(popul[x]) == sum(range(LEN)))
#print pos #print pos
for x in xrange(len(popul)): for x in range(len(popul)):
if x == pos: if x == pos:
continue continue
popul[x] = popul[pos][:] popul[x] = popul[pos][:]
assert(len(popul[x]) == LEN) assert(len(popul[x]) == LEN)
return best return best
for i in xrange(10000): for i in range(10000):
mutate() mutate()
print(evil()) print(evil())