예제 #1
0
    def __init__(self, dna=None):
        self.pos = Vector(WIDTH // 2, HEIGHT - 40)
        self.vel = Vector(0, 0)
        self.acc = Vector(0, 0)

        self.reached = False
        self.crashed = False
        self.alive_tick = 0

        if dna == None:
            self.dna = DNA()
        else:
            self.dna = dna

        self.fitness = 0
예제 #2
0
class Starship:
    def __init__(self):
        self.pos = Vector(WIDTH // 2, HEIGHT - 40)
        self.vel = Vector(0, 0)
        self.acc = Vector(0, 0)

    def apply_force(self, force):
        self.acc += force

    def update(self):
        self.apply_force(Vector(random.uniform(-1, 1), random.uniform(-1, 1)))

        self.vel += self.acc
        self.pos += self.vel
        self.acc *= 0

    def show(self):
        c = app_hdl.canvas

        c.push()
        c.translate(self.pos.x, self.pos.y)
        c.rotate(self.vel.heading())
        img = 'rocket-moving.png'
        c.create_image(0, 0, IMAGE_DIR + img)
        c.pop()
예제 #3
0
def setup(app):
    global app_hdl
    app_hdl = app

    app.stars = []
    for i in range(200):
        app.stars.append([random.randint(0, WIDTH), random.randint(0, HEIGHT)])

    app.blackholes = []
    for i in range(BLACKHOLES_COUNT):
        app.blackholes.append(Vector(random.randint(100, WIDTH - 100), random.randint(300, HEIGHT - 300)))

    app.pop = Population()
예제 #4
0
def setup(app):
    global app_hdl
    app_hdl = app

    app.cnt = 0

    app.stars = []
    for i in range(200):
        app.stars.append([random.randint(0, WIDTH), random.randint(0, HEIGHT)])

    app.blackholes = []
    for i in range(BLACKHOLES_COUNT):
        app.blackholes.append(Vector(random.randint(100, WIDTH - 100), random.randint(300, HEIGHT - 300)))

    app.pop = Population()

    app.generation_index = 1
    app.canvas.font_family('Tahoma 14')
    app.canvas.font_color('white')
    app.canvas.text_anchor('sw')
예제 #5
0
    def update(self):
        self.apply_force(Vector(random.uniform(-1, 1), random.uniform(-1, 1)))

        self.vel += self.acc
        self.pos += self.vel
        self.acc *= 0
예제 #6
0
 def __init__(self):
     self.pos = Vector(WIDTH // 2, HEIGHT - 40)
     self.vel = Vector(0, 0)
     self.acc = Vector(0, 0)
예제 #7
0
from EasyDraw import EasyDraw
from EasyDraw.Vector import *

import math, random, os

WIDTH = 800
HEIGHT = 600

IMAGE_DIR = os.path.join(os.path.dirname(__file__), 'images/')

app_hdl = None

POPULATION_SIZE = 2
MARS_COORDS = Vector(WIDTH // 2, 60)
EARTH_COORDS = Vector(WIDTH // 2, HEIGHT + 80)
BLACKHOLES_COUNT = 3

class Population:
    def __init__(self):
        self.starships = []
        for i in range(POPULATION_SIZE):
            self.starships.append(Starship())

    def run(self):
        for s in self.starships:
            s.update()
            s.show()

class Starship:
    def __init__(self):
        self.pos = Vector(WIDTH // 2, HEIGHT - 40)
예제 #8
0
class Starship:
    def __init__(self, dna=None):
        self.pos = Vector(WIDTH // 2, HEIGHT - 40)
        self.vel = Vector(0, 0)
        self.acc = Vector(0, 0)

        self.reached = False
        self.crashed = False
        self.alive_tick = 0

        if dna == None:
            self.dna = DNA()
        else:
            self.dna = dna

        self.fitness = 0

    def apply_force(self, force):
        self.acc += force

    def update(self):
        dist = self.pos.distance_from(MARS_COORDS)
        self.alive_tick += 1

        if dist < 80:
            self.reached = True
            self.pos = MARS_COORDS

        for bh in app_hdl.blackholes:
            if self.pos.distance_from(bh) < 60:
                self.crashed = True

        if self.pos.x > WIDTH or self.pos.x < 0 or self.pos.y > HEIGHT or self.pos.y < 0:
            self.crashed = True

        self.apply_force(self.dna.genes[app_hdl.cnt])

        if self.reached == False:
            self.vel += self.acc
            self.pos += self.vel
            self.acc *= 0

    def calc_fitness(self):
        dist = self.pos.distance_from(MARS_COORDS)
        self.fitness = map(dist, 0, WIDTH, WIDTH, 0)
        if self.reached:
            self.fitness *= 20
        if self.crashed:
            self.fitness /= 10
        self.fitness *= self.alive_tick

    def show(self):
        if self.crashed:
            return

        c = app_hdl.canvas

        c.push()
        c.translate(self.pos.x, self.pos.y)
        c.rotate(self.vel.heading())

        if self.vel.length() < 1 or self.reached:
            img = 'rocket-idle.png'
        else:
            img = 'rocket-moving.png'

        c.create_image(0, 0, IMAGE_DIR + img)
        c.pop()