-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
206 lines (158 loc) · 6.65 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/bash
# -*- coding: utf-8 -*
import turtle
import turtleWrap
import warehouse
import Population
import Individual
import random
import copy
class Point:
index_x = None
index_y = None
def __init__(self, x, y):
self.index_x = x
self.index_y = y
class Main:
turtleWrapper = None
warehouse = None
screen = None
current_position = None
data_sample = None #tablica populacji
input_vector = None
def __init__(self):
self.turtleWrapper = turtleWrap.TurtleWrapper()
self.warehouse = warehouse.Warehouse()
self.screen = turtle.Screen()
self.current_position = Point(round(self.warehouse.WIDTH/2), round(self.warehouse.HEIGHT/2))
self.init_screen()
self.input_vector = []
def init_screen(self):
self.screen = turtle.Screen()
self.screen.setup(1000, 1000)
self.screen.title("Algorytm ewolucyjny")
def up_pressed(self):
if not self.turtleWrapper.drawing_finished and self.turtleWrapper.move_up():
self.warehouse.set_way_on_matrix_when_moving_up()
def down_pressed(self):
if not self.turtleWrapper.drawing_finished and self.turtleWrapper.move_down():
self.warehouse.set_way_on_matrix_when_moving_down()
def left_pressed(self):
if not self.turtleWrapper.drawing_finished and self.turtleWrapper.move_left():
self.warehouse.set_way_on_matrix_when_moving_left()
def right_pressed(self):
if not self.turtleWrapper.drawing_finished and self.turtleWrapper.move_right():
self.warehouse.set_way_on_matrix_when_moving_right()
#tworzenie magazynu, tworzenie zestawu populacji
def finish_drawing(self):
if self.turtleWrapper.drawing_finished:
self.warehouse.fill_warehouse()
self.data_sample = [Population.Population(self.input_vector, self.warehouse.matrix) for i in range(100)]
for population in self.data_sample:
population.putCargosIntoWarehouse()
def iteration(self):
for population in self.data_sample:
population.evaluateObjectiveFunction(self.warehouse.HEIGHT, self.warehouse.WIDTH)
self.sort()
for i in range(50):
a = random.randrange(0, 50, 1)
b = random.randrange(0, 50, 1)
while a == b:
b = random.randrange(0, 50, 1)
for j in range(50):
self.data_sample.append(self.crossPopulations(self.data_sample[a], self.data_sample[b]))
for population in self.data_sample:
for cargo in population.individuals:
cargo.mutate()
population.warehouse = copy.deepcopy(self.warehouse.matrix)
population.putCargosIntoWarehouse()
self.print_warehouse()
def print_warehouse(self):
self.data_sample[0].debug_warehouse_shape()
for i in range(100):
print(self.data_sample[i].objectiveFunctionValue)
#takie tam wczytywanie towarow, nie wiazace
def getInput(self):
for i in range(20):
self.input_vector.append(1)
self.input_vector.append(4)
self.input_vector.append(7)
for i in range(10):
self.input_vector.append(2)
self.input_vector.append(3)
self.input_vector.append(5)
self.input_vector.append(6)
#powinno byc ok, nie sprawdzane, bo nie dziala wpisywanie towarow
#def reproduce(self):
def crossPopulations(self, mother, father):
x = [i for i in range(100)]
child = Population.Population(x,self.warehouse.matrix)
for k in range(100):
child.individuals.pop(0)
for i in range(100):
crossed_item = Individual.Individual(0)
crossed_item.cargo = mother.individuals[i].cargo
a = random.choice(["mother", "father"])
if a == "mother":
crossed_item.topLeftCorner_X = mother.individuals[i].topLeftCorner_X
else:
crossed_item.topLeftCorner_X = father.individuals[i].topLeftCorner_X
a = random.choice(["mother", "father"])
if a == "mother":
crossed_item.topLeftCorner_Y = mother.individuals[i].topLeftCorner_Y
else:
crossed_item.topLeftCorner_Y = father.individuals[i].topLeftCorner_Y
a = random.choice(["mother", "father"])
if a == "mother":
crossed_item.isVisible = mother.individuals[i].isVisible
else:
crossed_item.isVisible = father.individuals[i].isVisible
child.individuals.append(crossed_item)
return child
'''
tu zaczyna sie jazda bez trzymanki xd
mapujemy wartosci funkcji celu, do pozycji populacji w naszej probce
wyciagamy klucz funkcji o najmniejszej wartosci
przepisujemy te populacje do nowej probki
usuwamy populacje z mapy
podmieniamy probki
'''
def sort(self):
value_map = {a: self.data_sample[a].objectiveFunctionValue for a in range(100)}
new_sample = []
# 50 best populations
for i in range(50):
key = value_map[self.keyWithMinValue(value_map)]
new_sample.append(self.data_sample[key])
self.data_sample.pop(key)
self.data_sample = new_sample
#najszybsze wyszukiwanie maxa w mapie wg goscia ze stackoverflow, mial dowody
def keyWithMinValue(self, value_map):
v = list(value_map.values())
k = list(value_map.keys())
return k[v.index(min(v))]
def start(self):
self.screen.onkey(self.exit_program, "q")
self.screen.onkey(self.up_pressed, "Up")
self.screen.onkey(self.down_pressed, "Down")
self.screen.onkey(self.left_pressed, "Left")
self.screen.onkey(self.right_pressed, "Right")
self.screen.onkey(self.finish_drawing, "k")
self.screen.onkey(self.start_test_rysowania_cargo, "a")
self.screen.onkey(self.iteration, "i")
self.screen.onkey(self.print_warehouse, "w")
#self.screen.onkey(self.reproduce, "space")
self.screen.listen()
#self.screen.mainloop() #odrzucało mi to w necie znalazlem ze ma byc tak jak na dole
turtle.mainloop()
def start_test_rysowania_cargo(self):
self.turtleWrapper.drawCargo(0, 0, 50, 100)
self.turtleWrapper.drawCargo(200, -200, 200, 100)
self.screen.exitonclick()
def exit_program(self):
self.screen.bye()
################# PROGRAM ###################
program = Main()
program.getInput()
program.start()
#program.start_test_rysowania_cargo() #należy wykomentować tylko program.start()