我用Python写了一个相当简单的程序。代码如下:
import pygame
import time
from math import *
from random import *
from pygame.locals import *
def mutateBrain(brain):
a = 0
for element in brain:
brain[a][0] = element[0] + (1 * (0.5 - random()))
brain[a][1] = element[1] + (1 * (0.5 - random()))
a = a + 1
return brain;
def generateFirstBrain():
genbrain = []
h = 0;
while randint(0,5) != 0:
asd = [2 * random(), 2 * random()]
genbrain.insert(h, asd)
h = h + 1
return genbrain
pygame.init()
width, height = 640, 480
screen=pygame.display.set_mode((width, height))
screen.fill(pygame.Color(255,255,255))
pygame.draw.rect(screen,(255,0,0),(310,0,30,30))
movesa = generateFirstBrain()
movesb = generateFirstBrain()
movesc = generateFirstBrain()
cola = (255,255,0)
colb = (255,0,255)
colc = (0,255,255)
while 1:
movesa = mutateBrain(movesa)
movesb = mutateBrain(movesb)
movesc = mutateBrain(movesc)
step = 0
acurrentx = 320
acurrenty = 240
bcurrentx = 320
bcurrenty = 240
ccurrentx = 320
ccurrenty = 240
totalsn = 0
if (len(movesa) >= len(movesb)) and (len(movesa) >= len(movesc)):
totalsn = len(movesa)
elif (len(movesb) >= len(movesa)) and (len(movesb) >= len(movesc)):
totalsn = len(movesb)
else:
totalsn = len(movesc)
for g in range(totalsn):
screen.fill(pygame.Color(255,255,255))
pygame.draw.rect(screen,(255,0,0),(305,0,30,30))
try:
acurrentx = acurrentx + 1 - movesa[step][0]
acurrenty = acurrenty + 1 - movesa[step][1]
except IndexError:
acurrentx = acurrentx
在这里,在第一个 while 循环结束时,无论我运行突变大脑函数多少次,movel 总是得到相同的值。但它会发生变化,因此随机应该存在问题。有人可以帮我这个吗?谢谢。
波斯汪
相关分类