Beispiel #1
0
	def fitness(self):
		if self.fit:
			return self.fit
		score = 0.0
		for J in xrange(config.simulations_per_individual):
			players = [[lambda x: self.string[len(x)], 'Individual']]
			players += [random.choice(bs) for i in xrange(6)]
			x = []
			record = []
			points = [0 for i in xrange(7)]
			for I in xrange(20):
				what = [p[0](x) for p in players]
				x.append([sum([1 if j == 'A' else 0 for j in what]), sum([1 if j == 'B' else 0 for j in what])])
				money = [700/x[-1][0] if j == 'A' else 300/x[-1][1] for j in what]
				points = [sum(a) for a in zip(points, money)]
				record.append(points)
			score += (float(record[-1][0])/float(max(record[-1])))**config.value_power
		self.fit = score
		return score
	
	def get_description(self):
		return " - (%s), fitness ~ %f\n"%(self.string, self.fitness())
		
if __name__ == '__main__':
	G = Generation(f20)
	cont = 0
	while True:
		print "Generation #%d"%cont
		cont += 1
		G = G.spawn_new_gen(Debug=True)
Beispiel #2
0
def make_plot(fittest):
    ret = ""
    for point in fittest.conjunto:
        ret += str(point[0]) + " " + str(point[1]) + "\n"
    return ret


def average(ls):
    return sum(ls) / len(ls)


cont = 0

while True:
    G = G.spawn_new_gen()
    cont += 1
    if cont % 40 == 1:
        fittest = max([(ind.fitness(), ind) for ind in G.individuals])[1]
        fitness = fittest.fitness()
        data_output = open("data.out", "w")
        graph_guide = open("graph.gnp", "w")
        data_output.write(make_plot(fittest))
        graph_guide.writelines(
            [
                "set terminal png size 800,600\n",
                "set output './images/fittest%d.png'\n" % (cont / 40),
                "set xrange [0:20]\n",
                "set yrange [0:20]\n",
                "set label 1 'Fitness = %f' at 1,1\n" % fitness,
                "plot './data.out' using 1:2 title 'Generation %d' with points pt 7 lt 2 lw 2 \n" % (cont / 40),