유전 알고리즘을 사용했다


흑돌은 단순 추적 알고리즘을 준 상태고, 백돌은 1세대에는 랜덤으로 움직이다가 점점 자연선택에 따라 덜맞은 놈만 형질유전 되면서 진화해 나간다. 결국 요리조리 잘 피해다니는 똑똑한 백돌로 거듭나게 됌


보기 편하게 흑돌한테 잡혔을때 빨간색으로 변하게 했다. 


또한 진화과정을 계속 보기가 귀찮다면 화살표 위 키를 눌러서 가속시킬 수 있음. 돌아오려면 아래키 누르면 된다


그럼 즐감


pygame 라이브러리가 필요할거임 코드는 2.7버전에서 쓰여졌다



-----------------------------------

import random, time
# Maker function will give you the list of generation
def maker(a,b):# a = object num, b = their gen num
    sub = []
    result = []
    for i in range(a):
        for j in range(b):
            sub.append(random.randrange(4))
        result.append(sub)
        sub = []
    return result

def chance(a):# a is percentage.
    num = random.random()

    if num <= float(a)/100:
        return 1
    else:
        return 0

def evol(evlist,a,b,mutchance,mutnum):# evlist = parents list, a and b is same with maker's
    result = []
    son = []
    for i in evlist:
        result.append(i)
    if a-len(evlist) == 0:
        return 0
    # Making son {
    for i in range(a-len(evlist)):
        for j in range(b):
            if chance(50)==1:
                son.append(evlist[0][j])
            else:
                son.append(evlist[1][j])
        result.append(son)
        son = []
    # } Making son
    # Mutant {
    if chance(mutchance)==1:
        for hh in range(mutnum):
            mutpos1 = random.randrange(a)
            mutpos2 = random.randrange(b)
            if result[mutpos1][mutpos2] == 0:
                result[mutpos1][mutpos2] = random.randrange(1,4)
            else:
                result[mutpos1][mutpos2] = random.randrange(0,4)
        print 'MUTANT!!!'*1000
    # } Mutant
    return result


--------------- 여기까지가 evol 모듈입니다. 이걸 파이썬 폴더에 저장한후 아래 본코드를 실행하시면 됩니다.

# -*- coding: utf-8 -*-
import pygame, evol, random, time
pygame.init()
screenx = 400
screeny = 300
blackx = 100
blacky = 150
bspeed = 50
hcount = 0
wspeed = 50
whitex = 300
whitey = 100
screen = pygame.display.set_mode((screenx,screeny))
count1 = 0
count2 = 0
obnum = 20
generation = 0
gennum = 20
frame = 0.1
bmove = 0
av = 0
wcolor = [255,255,255]
gen = evol.maker(obnum,gennum)
hitlist = []
average = []
mutchance = 1#단위는 %
def goodness(list):
    rullet = random.randrange(1,101)
    list2 = []
    for i in list:
        list2.append(i)
    list2.sort
    list2.reverse()

    mother = list.index(list2[0])
    father = list.index(list2[1])



    return [mother,father]

while 1:
    #백돌 움직임{
    if gen[count1][count2]==0 and whitey-20>=0:
        whitey -= wspeed
    if gen[count1][count2]==1 and whitey+20<=screeny:
        whitey += wspeed
    if gen[count1][count2]==2 and whitex-20>=0:
        whitex -= wspeed
    if gen[count1][count2]==3 and whitex+20<=screenx:
        whitex += wspeed
    #}백돌 움직임

    #흑돌움직임{
    if whitey<blacky:
        bmove = 0
    if whitey>blacky:
        bmove = 1
    if whitex<blackx:
        bmove = 2
    if whitex>blackx:
        bmove = 3
    if bmove==0 and blacky-20>=0:
        blacky -= bspeed
    if bmove==1 and blacky+20<=screeny:
        blacky += bspeed
    if bmove==2 and blackx-20>=0:
        blackx -= bspeed
    if bmove==3 and blackx+20<=screenx:
        blackx += bspeed
    #}흑돌움직임
    #그리기{
    screen.fill((180,180,180))
    for i in range(0,(screenx/50)+1):
        pygame.draw.line(screen,[0,0,0],(i*50,0),(i*50,screeny),2)
    for i in range(0,(screeny/50)+1):
        pygame.draw.line(screen,[0,0,0],(0,i*50),(screenx,i*50),2)

    for i in pygame.event.get():
        if i.type == pygame.QUIT:
            pygame.quit()
            exit()
        if i.type == pygame.KEYDOWN:
            if i.key == pygame.K_UP:
                frame = 0
            if i.key == pygame.K_DOWN:
                frame = 0.1
    if blackx == whitex and blacky == whitey:
        wcolor = [255,0,0]
        hcount += 1

    else:
        wcolor = [255,255,255]
    pygame.draw.circle(screen,[0,0,0],(blackx,blacky),20,0)
    pygame.draw.circle(screen,wcolor,(whitex,whitey),20,0)
    #}그리기
    count2 += 1
    distance = ((blackx - whitex)**2 + (blacky-whitey)**2)**0.5

    average.append(distance)
    #개체교체{
    if count2 == gennum:
        count2 = 0
        count1 += 1
        blackx = 100
        blacky = 150
        whitex = 300
        whitey = 100
        for i in average:
            av += i
        av = float(av)/gennum
        hitlist.append(av)
        av = 0
        average = []
        print hcount
        hcount = 0
    #}개체교체
    #세대교체{
    if count1 == obnum:
        count1 = 0
        father = gen[goodness(hitlist)[0]]
        mother = gen[goodness(hitlist)[1]]
        gen = evol.evol([father,mother],obnum,gennum,mutchance,2)
        generation += 1

        print('%dGENERATION'%generation)
        hitlist = []
    #}세대교체

    time.sleep(frame)
    pygame.display.update()