簡體   English   中英

遺傳編程pyeasyGA和Zelle圖形在Python上

[英]Genetic Programming pyeasyGA and Zelle graphics on Python

我想制作一個簡單的程序來提高我對這類編程的了解。 我發現了一個非常有用的庫,pyeasyGA,並且我嘗試使用graphics.py創建一個簡單的程序,從隨機生成的“pass”序列創建一個收斂到一個點的序列。

這就是它的工作原理:

def create_individual(data):
    a = [(randint(0,5),randint(0,5)) for n in range(len(data))]
    print(a)
    return a

此函數創建一系列傳遞,因為graphics.py庫允許您通過為對象移動它想要移動它來移動對象。 那是我的“個人”。

為了計算健身,我使用了這個:

def fitness(individual, data):
    totX=0
    totY=0
    for elem in individual:

        totX+=elem[0]
        totY+=elem[1]

    tot = (totX,totY)

    return distEuclidea(arrivo, tot)

def distEuclidea(p1,p2):
    x1 = p1[0]
    y1 = p1[1]
    x2 = p2[0]
    y2 = p2[1]

    return ((x2-x1)**2+(y2-y1)**2)**(1/2)

此功能計算距所需到達點的距離。

在這些過程之后,程序會產生很多代,並且會使個體具有最低的適應度,但它不起作用。

它沒有發展。 每個傳遞序列似乎都是隨機生成的。

有人能幫助我嗎?

這是完整的代碼

編輯:

該計划似乎有效。 唯一的問題是幾代人。

我發現你的健身功能最難理解。 而不是平均角落或找到中心,它將角落相加然后找到距離。 什么是幾何解釋?

此外,您的代碼是指ga.logGenerations,它不是當前pyeasyga 0.3.1版本的一部分。

以下是我對你的要求的近似。 如果它沒有標記,那么請用示例和/或圖表來擴充您的解釋:

from time import sleep
from random import randint
from itertools import cycle
from graphics import *
from pyeasyga import pyeasyga

NUMBER_OF_RECTANGLES = 4  # make one more than what you want to see
NUMBER_OF_POINTS = 2

arrivo = (90, 90)

colori = ["red", "green", "blue", "cyan", "magenta", "yellow"]

X, Y = 0, 1

def distEuclidea(p1, p2):
    x1, y1 = p1
    x2, y2 = p2

    return ((x2 - x1) ** 2 + (y2 - y1) ** 2) ** 0.5

def create_individual(colors):
    color = next(colors)

    while color in rectangles and rectangles[color] is None:  # skip over deleted rectangle
        color = next(colors)

    if color in rectangles:
        rectangle = rectangles[color]
        p1, p2 = rectangle.getP1(), rectangle.getP2()
        points = [[p1.getX(), p1.getY()], [p2.getX(), p2.getY()]]
    else:
        points = [[randint(0, 20), randint(0, 20)] for _ in range(NUMBER_OF_POINTS)]

        rectangle = Rectangle(*[Point(x, y) for x, y in points])
        rectangle.setOutline(color)
        rectangle.draw(win)

        rectangles[color] = rectangle

    return [color, points]

def fitness(individual, colors):
    _, points = individual

    rectangle = Rectangle(*[Point(x, y) for x, y in points])

    center = rectangle.getCenter()

    return distEuclidea(arrivo, (center.getX(), center.getY()))

def mutate(individual):
    _, points = individual
    mutate_index = randint(0, NUMBER_OF_POINTS - 1)
    points[mutate_index][X] += randint(-1, 1)
    points[mutate_index][Y] += randint(-1, 1)

def is_point_inside_rectangle(point, rectangle):
    p1, p2 = rectangle.getP1(), rectangle.getP2()

    return min(p1.getX(), p2.getX()) < point.getX() < max(p1.getX(), p2.getX()) and \
        min(p1.getY(), p2.getY()) < point.getY() < max(p1.getY(), p2.getY())

win = GraphWin("Genetic Graphics", 500, 500)
win.setCoords(0, 0, 100, 100)

rectangles = {}
color_generator = cycle(colori[0:NUMBER_OF_RECTANGLES])

arrivoC = Circle(Point(*arrivo), 1)
arrivoC.setFill("orange")
arrivoC.draw(win)

number_of_rectangles = NUMBER_OF_RECTANGLES

while True:

    ga = pyeasyga.GeneticAlgorithm(color_generator, \
        elitism=False, \
        maximise_fitness=False, \
        crossover_probability=0.0, \
        population_size=number_of_rectangles)

    ga.create_individual = create_individual
    ga.fitness_function = fitness
    ga.mutate_function = mutate

    ga.run()

    for member in ga.last_generation():
        my_fitness, (my_color, my_points) = member
        if rectangles[my_color] is None:
            continue  # skip over deleted rectangle

        rectangle = Rectangle(*[Point(x, y) for x, y in my_points])
        rectangle.setOutline(my_color)
        rectangle.draw(win)
        rectangles[my_color] = rectangle

        if is_point_inside_rectangle(arrivoC.getCenter(), rectangle):
            rectangles[my_color] = None  # delete finished rectangle
            number_of_rectangles -= 1

    if number_of_rectangles < 2:
        break

    sleep(0.1)

for value in rectangles.values():
    if value is not None:
        value.undraw()  # delete unfinished rectangle

win.getMouse()
win.close()

以上是粗略的代碼(例如,它並不總是保持通用域點和矩形獨立於graphics.py點和矩形。)但它應該給你一些實驗:

在此輸入圖像描述

它在窗口的左下角創建矩形,遺傳算法在右上角向目標突變,當它們到達目標時丟棄矩形。

我的代碼的一部分復雜性是pyeasyga沒有提供一個功能鈎子來可視化每一代發生的事情。 更好的方法可能是將pyeasyga子類化為添加這樣的鈎子以簡化代碼的邏輯。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM