파이썬 코드인데 처음 만들어본거라 개뿌듯 ㅎㅎ


흑돌에는 기본적인 쫓아가는 로직을 줬고 백돌은 처음엔 랜덤한 움직임을 하다가, 점점 세대를 거치면서 흑돌한테 한대도 안맞는 경로를 찾아내여


적합도 평가는 흑돌과의 거리를 기준으로 했어요 아래는 코드에요



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(2))

        result.append(sub)

        sub = []

    return result


def chance(a):# a is percentage.

    num = random.randrange(1,101)

    if num <= a:

        return 1

    else:

        return 0


def evol(evlist,a,b,mutchance,mutnum):# evlist = parents list, a and b is same with maker's

    result = []

    for i in evlist:

        result.append(i)

    if a-len(evlist) == 0:

        return 0

    # Making son {

    for i in range(a-len(evlist)):

        mix = []

        for i in evlist:

            for j in i:

                mix.append(j)

        random.shuffle(mix)

        for i in range(b):



            mix.pop()

        result.append(mix)

    # } 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] = 1

            else:

                result[mutpos1][mutpos2] = 0

    # } Mutant

    return result





이게 evol 모듈이고


import pygame, evol, random, time

pygame.init()

screenx = 700

screeny = 500

blackx = 200

blacky = 150

whitex = 300

whitey = 150

screen = pygame.display.set_mode((screenx,screeny))


abcde = 0

frame = 0.1

obnum = 10

genum = 12

num = 0

distance = 0

wspeed = 15

bspeed = 50

hitlist = []

gen = evol.maker(obnum,genum)

generation = 1


def goodness(hitlist):#Goddness assessment function

    hitlist2 = []

    for i in hitlist:

        hitlist2.append(i)

    hitlist.sort()

    hitlist.reverse()

    a = hitlist2.index(hitlist[0])

    b = hitlist2.index(hitlist[1])



    return [gen[a],gen[b]]


while 1:


    screen.fill((125,125,125))

# You must in the scrren{

    if blackx<0:

        blackx = 0

    if blackx>screenx:

        blackx = screenx

    if blacky<0:

        blacky = 0

    if blacky>screeny:

        blacky=screeny

    if whitex<0:

        whitex = 0

    if whitex>screenx:

        whitex = screenx

    if whitey<0:

        whitey = 0

    if whitey>screeny:

        whitey=screeny

# } You must in the screen


# White moving{



    for i in range(genum/2):


        if gen[num][i] == 1:

            whitey -=wspeed


        else:

            whitey +=wspeed


    for i in range(genum/2,genum):

            if gen[num][i]:

                whitex -=wspeed


            else:

                whitex +=wspeed



# }White moving


# Black moving{

    if whitex<blackx:

        blackx -= bspeed

    if whitex>blackx:

        blackx += bspeed

    if whitey>blacky:

        blacky += bspeed

    if whitey<blacky:

        blacky -= bspeed


# }Black moving


    pygame.draw.circle(screen,(0,0,0),[blackx,blacky],30,0)

    pygame.draw.circle(screen,(255,255,255),[whitex,whitey],30,0)

#Check distance {


    distance = ((blackx - whitex)**2 + (blacky-whitey)**2)**0.5

    hitlist.append(int(distance))

    if distance<=60:

        print 'HITTED!!'

#} Check distance


# Num change{

    num +=1


    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 = frame/2

            elif i.key == pygame.K_DOWN:

                frame = frame*2

    pygame.display.update()

    time.sleep(frame)

# }Num change

#Generation change

    if num == obnum:

        blackx = 150

        blacky = 150

        whitex = 300

        whitey = 150


        gen = evol.evol(goodness(hitlist),obnum,genum,0.001,10)

        num = 0

        generation += 1

        print '%dGeneration'%generation

        hitlist = []

    abcde += 1



이게 본 소스인데 pygame 라이브러리가 필요할거에요