在下面的代码中,我有一个我填充的列表。
填充之后,我再次访问它,但是列表被重置。在
为什么会这样?在#Yonatan Oren 12/20/2013
#NIFTY GA Problem
from random import shuffle, randint, randrange
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
def getDistance(self, point):
distance = (((point.getX() - self.x) ** 2) + ((point.getY() - self.y) ** 2)) ** (0.5)
return distance
class Value:
def __init__(self, pointsList):
self.pointsList = pointsList
self.fitness = 0
def getFitness(self):
#add up all of the distances
fitness = sum([a.getDistance(b) for a,b in zip(*[self.pointsList[i:] for i in range(2)])])
return fitness
def getPointsList(self):
return self.pointsList
def __lt__(self, other):
return self.getFitness() < other.getFitness()
class Environment:
def __init__(self):
self.values = []
def createNewEnvironment(self, pointsList):
self.pointsList = pointsList
#create 10 new organisms
for i in range(10):
org = Value(pointsList)
print org.getFitness()
self.values.append(org)
shuffle(pointsList)
for o in self.values:
print "point_listed: %s" % o.getFitness()
env = Environment()
env.createNewEnvironment([Point(0,0), Point(5,0), Point(10,0), Point(10,5), Point(10,10), Point(5,10), Point(0, 10), Point(0, 5), Point(0,0)])
输出如下:
^{pr2}$
如您所见,所有的值都被重置为“82.7160188343”,我甚至不知道
那个号码是从哪里来的。在
有什么想法吗?在
谢谢。在