问题描述
我怎样才能通过索引交换字符。 我已经这样做了,但我想可能会有很多错误。
我现在正在努力的是将 0 更改为 1,以及在突变类的情况下将 1 更改为 0。
我尝试了很多方法,但我必须说我放弃了,因为我一直在犯奇怪的错误,尤其是我仍然从 0 开始学习。
import random
a = 4
b = 7
c = 2
def function(x):
return x[0]**2.0 + x[1]**2.0
# boots - how many times program should go on
boots = 40
number_pop = 4
pop_people = 4
pr_cross = 50
pr_mut = 40
#gener. 1st population
def population():
z = []
for i in range(pop_people):
r = f'{random.randint(0,255):08b}'
z.append(r)
print("New population: ",z)
return z
# population()
def crossing():
tab_cross = []
tab_cross = population()
a_cross = []
for o in range(0,pop_people,2):
w_lb_psl = random.randint(0,100)
if w_lb_psl > pr_cross:
pc = 3
p1_1 = tab_cross[o][0:pc]
p1_2 = tab_cross[o][pc:8]
p2_1 = tab_cross[o+1][0:pc]
p2_2 = tab_cross[o+1][pc:8]
a_cross.append(p1_1 + p2_2)
a_cross.append(p2_1 + p1_2)
else:
a_cross.append(tab_cross[o])
a_cross.append(tab_cross[o+1])
print("Population after cross: ",a_cross)
return a_cross
# crossing()
def mutation():
tab_mut = []
tab_mut = crossing()
for m in range(pop_people):
w_lb_psl = random.uniform(0,100)
if w_lb_psl < pr_mut:
for x in tab_mut:
if x == 1:
tab_mut[x] = 0
elif x == 0:
tab_mut[x] = 1
print("Population after mutation: ",tab_mut)
return tab_mut
mutation()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)