【发布时间】:2014-09-27 20:01:03
【问题描述】:
我正在尝试实现最初由Roger Alsing 创建的程序。我已经对其他人实施的内容进行了相当多的研究。我决定用 python 编写我的程序,并使用基本的三角形作为形状。当我运行程序时,经过几代后它并没有显示出改进(三角形往往会消失)。我假设我的 mutate 函数有问题。谁能告诉我为什么它产生的结果不尽如人意?
我的代码:
import random
import copy
from PIL import Image, ImageDraw
optimal = Image.open("mona_lisa.png")
optimal = optimal.convert("RGBA")
size = width, height = optimal.size
num_shapes = 128
generations = 50000
def random_genome():
elements = []
for i in range(num_shapes):
x = (random.randint(0, width), random.randint(0, height))
y = (random.randint(0, width), random.randint(0, height))
z = (random.randint(0, width), random.randint(0, height))
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
alpha = random.randint(10, 255)
elements.append([x, y, z, r, g, b, alpha])
return elements
def render_daughter(dna):
image = Image.new("RGBA", (width, height), "white")
draw = ImageDraw.Draw(image)
for item in dna:
x = item[0]
y = item[1]
z = item[2]
r = item[3]
g = item[4]
b = item[5]
alpha = item[6]
color = (r, g, b, alpha)
draw.polygon([x, y, z], fill = color)
return image
def mutate(dna):
dna_copy = copy.deepcopy(dna)
shape_index = random.randint(0, len(dna) - 1)
roulette = random.random() * 2
if roulette < 1:
if roulette < 0.25:
dna_copy[shape_index][3] = int(random.triangular(255, dna_copy[shape_index][3]))
elif roulette < 0.5:
dna_copy[shape_index][4] = int(random.triangular(255, dna_copy[shape_index][4]))
elif roulette < 0.75:
dna_copy[shape_index][5] = int(random.triangular(255, dna_copy[shape_index][5]))
elif roulette < 1.0:
dna_copy[shape_index][6] = int(0.00390625 * random.triangular(255, dna_copy[shape_index][6] * 255))
else:
if roulette < 1.25:
dna_copy[shape_index][0] = (int(random.triangular(width, dna_copy[shape_index][0][0])), int(random.triangular(height, dna_copy[shape_index][0][1])))
elif roulette < 1.5:
dna_copy[shape_index][2] = (int(random.triangular(width, dna_copy[shape_index][3][0])), int(random.triangular(height, dna_copy[shape_index][4][1])))
elif roulette < 1.75:
dna_copy[shape_index][3] = (int(random.triangular(width, dna_copy[shape_index][4][0])), int(random.triangular(height, dna_copy[shape_index][5][1])))
return dna_copy
def fitness(original, new):
fitness = 0
for x in range(0, width):
for y in range(0, height):
r1, g1, b1, a1 = original.getpixel((x, y))
r2, g2, b2, a2 = new.getpixel((x, y))
deltaRed = r1 - r2
deltaGreen = g1 - g2
deltaBlue = b1 - b2
deltaAlpha = a1 - a2
pixelFitness = deltaRed + deltaGreen + deltaBlue + deltaAlpha
fitness += pixelFitness
return fitness
def generate():
mother = random_genome()
best_genome = mother
best_fitness = fitness(optimal, render_daughter(best_genome))
for i in range(generations):
daughter = copy.deepcopy(best_genome)
daughter = mutate(daughter)
daughter_fitness = fitness(optimal, render_daughter(daughter))
if daughter_fitness < best_fitness:
best_genome = daughter
best_fitness = daughter_fitness
if i % 50 == 0:
print i
if i % 1000 == 0:
render_daughter(best_genome).save("iterations/output_" + str(i) + ".png")
if __name__ == "__main__":
generate()
我正在使用的开始图片:
1000代后的输出图像:
5000 代后的输出图像:
【问题讨论】: