-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (67 loc) · 2.34 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import tkinter as tk
import random
import os
import cv2
import PIL.Image, PIL.ImageTk
from classes.target import Target
from classes.heightmap import HeightMap
from classes.dot import Dot
from classes.dots_collection import bread
#Contact http://labos.ulg.ac.be/etho/brotcorne/
dots_number_init = 500
HM_object = HeightMap(os.path.dirname(os.path.realpath(__file__)))
cv_img = cv2.cvtColor(cv2.imread("image.bmp"), cv2.COLOR_BGR2RGB)
height, width, no_channels = cv_img.shape
root = tk.Tk()
root.title("Genetic Algorithm in Heightmaps")
canvas = tk.Canvas(master = root, width = width, height = height)
photo = PIL.ImageTk.PhotoImage(image = PIL.Image.fromarray(cv_img))
canvas.create_image(0, 0, image=photo, anchor=tk.NW)
target_radius = 5
target_x = width/2
target_y = 25
target = Target(canvas.create_oval(target_x-target_radius, target_y-target_radius, target_x+target_radius, target_y+target_radius, width=0, fill=('red')), [target_x, 25], target_radius)
canvas.pack()
new_generation = ''
previous_generation = ''
generation = 1
n = 0
while generation <= 150:
previous_generation = new_generation
dots_number = dots_number_init
for x in previous_generation:
canvas.delete(x.ball)
if generation == 1:
new_generation = bread(canvas, '', dots_number, width, height)
else:
new_generation = bread(canvas, previous_generation, dots_number, width, height)
for x in previous_generation:
canvas.delete(x.ball)
while (dots_number > 0):
dots_number = dots_number_init
for x in new_generation:
x.frame_collision(target, width, height)
if(x.isDead != True and x.energy >= 0):
x.frame_move(canvas)
x.consume_energy(HM_object)
else:
dots_number -= 1
root.update() # update the display
root.after(16) # wait 1 ms
for x in new_generation:
x.calculate_fitness(target)
report = '- Generation: ' + str(generation)
print(report)
reached = 0
moves = []
for x in new_generation:
if x.reachedTarget:
reached += 1
moves.append(x.move)
print('Points to Goal: ' + str(reached))
if moves != []:
print('Mean moves: ' + str(round(sum(moves) / len(moves))))
else:
print('Mean moves: 0')
generation += 1
root.mainloop()