我的带有 in 函数的代码不执行任务,但在它外面执行相同的操作?
My code with in function does not perform the task but does the same while out of it?
这是我的代码,我稍微翻转一下,跨越两个列表并选择列表的随机元素:
def selRandom(individuals, k):
return [random.choice(individuals) for i in range(k)]
def cxOnePoint(ind1, ind2):
size = min(len(ind1), len(ind2))
cxpoint = random.randint(1, size - 1)
ind1[cxpoint:], ind2[cxpoint:] = ind2[cxpoint:], ind1[cxpoint:]
return ind1, ind2
def mutFlipBit(individual, indpb):
for i in range(len(individual)):
if random.random() < indpb:
individual[i] = type(individual[i])(not individual[i])
return individual,
def operators(selection, crossover, mutation, parent, k, indvpb):
select = ['randomSelection']
cx = ['OnePoint']
mutate = ['flipBitMutate']
if selection not in select:
return "invalid"
else:
if selection == 'randomSelection':
(parent) = selRandom(parent, k)
if crossover not in cx:
return "invalid"
else:
if crossover == 'OnePoint':
ind = cxOnePoint(parent[0], parent[1])
if mutation not in mutate:
return "not valid"
else:
if mutation == 'flipBitMutate':
mutatedIndvidual = mutFlipBit(ind[0], indvpb)
return parent, ind, mutatedIndvidual
我运行执行代码:
indv = ([1,0,1,0,1,0,1,1],[0,1,0,1,0,0,0,1],[0,0,1,1,1,1,0,0],[0,1,1,1,0,0,0,1],[1,0,0,0,1,1,1,1])
selection = 'randomSelection'
crossover = 'OnePoint'
mutation = 'flipBitMutate'
selected_parent, ind, mutatedIndvidual = operators(selection = selection , crossover = crossover, mutation = mutation, parent = indv, k = 3, indvpb = 0.1 )
print("Parents:\n",indv)
print("Selected parent to reproduce:\n",selected_parent)
print("Crossover offsprings:\n",ind)
print("Mutated offsprings",mutatedIndvidual)
并得到结果:
Parents:
([1, 0, 1, 0, 1, 0, 1, 1], [1, 1, 1, 1, 0, 0, 1, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 1, 1, 1])
Selected parent to reproduce:
[[1, 1, 1, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0, 0, 1], [1, 1, 1, 1, 0, 0, 1, 0]]
Crossover offsprings:
([1, 1, 1, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0, 0, 1])
Mutated offsprings ([1, 1, 1, 1, 0, 0, 1, 0],)
所以代码正在执行但没有运行。它从元组中随机选择,然后不交叉(混合来自两个列表的位)或翻转位。如果我单独测试 运行 代码(在运算符方法之外)它有效:
a = [1,1,1,1,1,1,1,1]
b = [0,0,0,0,0,0,0,0]
c = [1,0,0,0,1,1,0,1]
d= (a,b,c)
print("selecting randomely:\n",selRandom(d,1))
print("TESTING CROSSOVER\n", cxOnePoint(a,b))
print("Mutate:\n",mutFlipBit(a,0.4))
并得到正确的结果:
selecting randomely:
[[0, 0, 0, 0, 0, 0, 0, 0]]
TESTING CROSSOVER
([1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
Mutate:
([0, 1, 1, 1, 1, 1, 1, 1],)
我在这里犯的逻辑错误是什么?
谢谢!
回答我自己的问题:
我已经在 mutFlipBit()
和 cxOnePoint
中分配了原始列表,并且我将 'mutFlipBit()' 更改为:
def mutation(individual, indp):
return [not ind if random.random() < indp else ind for ind in individual]
这对我有用
这是我的代码,我稍微翻转一下,跨越两个列表并选择列表的随机元素:
def selRandom(individuals, k):
return [random.choice(individuals) for i in range(k)]
def cxOnePoint(ind1, ind2):
size = min(len(ind1), len(ind2))
cxpoint = random.randint(1, size - 1)
ind1[cxpoint:], ind2[cxpoint:] = ind2[cxpoint:], ind1[cxpoint:]
return ind1, ind2
def mutFlipBit(individual, indpb):
for i in range(len(individual)):
if random.random() < indpb:
individual[i] = type(individual[i])(not individual[i])
return individual,
def operators(selection, crossover, mutation, parent, k, indvpb):
select = ['randomSelection']
cx = ['OnePoint']
mutate = ['flipBitMutate']
if selection not in select:
return "invalid"
else:
if selection == 'randomSelection':
(parent) = selRandom(parent, k)
if crossover not in cx:
return "invalid"
else:
if crossover == 'OnePoint':
ind = cxOnePoint(parent[0], parent[1])
if mutation not in mutate:
return "not valid"
else:
if mutation == 'flipBitMutate':
mutatedIndvidual = mutFlipBit(ind[0], indvpb)
return parent, ind, mutatedIndvidual
我运行执行代码:
indv = ([1,0,1,0,1,0,1,1],[0,1,0,1,0,0,0,1],[0,0,1,1,1,1,0,0],[0,1,1,1,0,0,0,1],[1,0,0,0,1,1,1,1])
selection = 'randomSelection'
crossover = 'OnePoint'
mutation = 'flipBitMutate'
selected_parent, ind, mutatedIndvidual = operators(selection = selection , crossover = crossover, mutation = mutation, parent = indv, k = 3, indvpb = 0.1 )
print("Parents:\n",indv)
print("Selected parent to reproduce:\n",selected_parent)
print("Crossover offsprings:\n",ind)
print("Mutated offsprings",mutatedIndvidual)
并得到结果:
Parents:
([1, 0, 1, 0, 1, 0, 1, 1], [1, 1, 1, 1, 0, 0, 1, 0], [0, 0, 1, 1, 1, 1, 0, 0], [0, 1, 0, 1, 0, 0, 0, 1], [1, 0, 0, 0, 1, 1, 1, 1])
Selected parent to reproduce:
[[1, 1, 1, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0, 0, 1], [1, 1, 1, 1, 0, 0, 1, 0]]
Crossover offsprings:
([1, 1, 1, 1, 0, 0, 1, 0], [0, 1, 0, 1, 0, 0, 0, 1])
Mutated offsprings ([1, 1, 1, 1, 0, 0, 1, 0],)
所以代码正在执行但没有运行。它从元组中随机选择,然后不交叉(混合来自两个列表的位)或翻转位。如果我单独测试 运行 代码(在运算符方法之外)它有效:
a = [1,1,1,1,1,1,1,1]
b = [0,0,0,0,0,0,0,0]
c = [1,0,0,0,1,1,0,1]
d= (a,b,c)
print("selecting randomely:\n",selRandom(d,1))
print("TESTING CROSSOVER\n", cxOnePoint(a,b))
print("Mutate:\n",mutFlipBit(a,0.4))
并得到正确的结果:
selecting randomely:
[[0, 0, 0, 0, 0, 0, 0, 0]]
TESTING CROSSOVER
([1, 1, 1, 1, 1, 1, 1, 0], [0, 0, 0, 0, 0, 0, 0, 1])
Mutate:
([0, 1, 1, 1, 1, 1, 1, 1],)
我在这里犯的逻辑错误是什么?
谢谢!
回答我自己的问题:
我已经在
mutFlipBit()
和cxOnePoint
中分配了原始列表,并且我将 'mutFlipBit()' 更改为:def mutation(individual, indp): return [not ind if random.random() < indp else ind for ind in individual]
这对我有用