(norganizer) web status filter
[experiments.git] / evolution / main.py
1
2 import copy
3 import logging
4 import random
5 import time
6
7 from entities import GENES, Bot, Good
8 from scene import Scene
9 from systems import PygameRenderer, Movement, Behavior
10 from utils import Vector
11
12 def generateRandomGenes(entity, size):
13 for _ in range(size):
14 entity.addGene((random.choice(GENES.values()))())
15 logging.debug("Creation: %s[%s]", entity, entity.getGenes())
16
17
18 if __name__ == "__main__":
19 # logging.basicConfig(format='%(module)s:%(levelname)s:%(message)s', filename='output.log',level=logging.DEBUG)
20 logging.basicConfig(format='%(module)s:%(levelname)s:%(message)s', level=logging.INFO)
21
22 random.seed(1)
23 scene = Scene("mainScene", 1000, 1000)
24 renderer = PygameRenderer(1024, 800, "Evolution")
25 movement = Movement()
26 behavior = Behavior()
27 framerate = 40
28 population = 16
29 food = 1800
30 genePool = 6
31 for _ in xrange(0, population):
32 b = Bot(Vector(random.randint(1, scene.w - 1), random.randint(1, scene.h - 1))).addToScene(scene)
33 generateRandomGenes(b, genePool)
34
35 for _ in xrange(0, food):
36 o = Good(Vector(random.randint(1, scene.w - 1), random.randint(1, scene.h - 1))).addToScene(scene)
37
38 renderer.update(scene)
39 movement.update(scene)
40
41 isRunning = True
42 startTime = time.time()
43 while isRunning:
44 for event in renderer.pygame.event.get():
45 if event.type == renderer.pygame.QUIT:
46 isRunning = False
47
48 behavior.update(scene)
49 movement.update(scene)
50 renderer.update(scene)
51 renderer.clock.tick(framerate)
52
53 if (len(scene.getByType(Scene.Types.ALIVE)) == 0):
54 isRunning = False
55
56 logging.info("Simulation took %f seconds", time.time() - startTime)
57
58 renderer.close()
59 movement.close()
60 behavior.close()
61