Оновити 'main.py'

This commit is contained in:
Serhii Hromov 2020-05-11 14:21:53 +00:00
parent e7347a0062
commit 66040a6456

50
main.py
View File

@ -23,7 +23,6 @@ WIDTH = 10
KITCHEN = (1, 1)
MIDDLE = (floor(WIDTH / 2), floor(HEIGHT / 2))
display = pygame.display.set_mode((WIDTH * 32 + 200, HEIGHT * 32))
# eating time
@ -38,6 +37,14 @@ menu = Context.fromstring(''' |meat|salad|meal|drink|cold|hot |
Pizza | | | X | | | X |''')
#menu.lattice.graphviz()
#Digraph.render('Lattice.gv', view=True)
#print(menu.extension(['meal',]))
###
#genetic algos
gen_num = 20 #generations
gen_sol = 6 #solutions
@ -46,6 +53,7 @@ gen_par_mating = 2 #how many solutions we select
mut_per_gen = 10
mut_num_gen = None
par_selc_type = "tournament"
crossover = "two_points"
muta_type = "scramble"
par_keep = 1 #keep only one parent
@ -54,12 +62,12 @@ init_range_l = -2 #low
init_range_h = -5 #high
func_input = ['meal']
func_output = []
right_output = []
for e in menu.extension(['meal',]):
func_output.append(e)
right_output.append(e)
def fittnes_f(sol):
def fitness_f(sol):
output = 0
if sol in menu.extension([sol],):
output = 0.5
@ -67,7 +75,27 @@ def fittnes_f(sol):
fitness = 1.0 / np.abs(output - r_out)
return fitness
####
ga_inst = pygad.GA(num_generations=gen_num,
sol_per_pop=gen_sol,
num_parents_mating=gen_par_mating,
num_genes=len(func_input),
fitness_func=fitness_f,
mutation_percent_genes=mut_per_gen,
mutation_num_genes=mut_num_gen,
init_range_low=init_range_l,
init_range_high=init_range_l,
parent_selection_type=par_selc_type,
crossover_type=crossover,
mutation_type=muta_type,
keep_parents=par_keep,
K_tournament=4)
#ga_inst.run()
#ga_inst.plot_result()
#print(func_output)
###
class Tile:
def __init__(self, x, y, canwalk, table, kitchen):
self.x = x
@ -80,6 +108,8 @@ class Tile:
self.visited = False
self.path = False
self.parent = (0, 0)
class Restaurant:
def __init__(self, tables, clients):
self.h = HEIGHT
@ -118,6 +148,7 @@ class Restaurant:
self.tables.append((w, h))
self.tiles[1][1].kitchen = True
def putClient(self):
for t in self.tables:
if not self.tiles[t[1]][t[0]].clientState:
@ -149,6 +180,7 @@ def heuristic(a, b):
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
class Agent:
def __init__(self, x, y):
self.x = x
@ -157,12 +189,14 @@ class Agent:
self.idle = True
self.orders = []
self.food = False
def walk(self):
t = self.path.pop(0)
self.x = t[0]
self.y = t[1]
if not self.path:
self.idle = True
def BFS(self, goal):
restaurant.flush()
queue = [(self.x, self.y)]
@ -181,8 +215,10 @@ class Agent:
if restaurant.tiles[y][x].canwalk and not restaurant.tiles[y][x].visited:
queue.append((x, y))
restaurant.tiles[y][x].parent = n
def wait(self):
self.idle = True
def getTask(self):
if waiter.orders:
self.BFS(KITCHEN)
@ -249,12 +285,12 @@ def drawScreen():
textsurface = font.render(str(ticks), False, (255, 255, 255))
display.blit(textsurface, (WIDTH * 32 + 80, 332))
restaurant = Restaurant(3, 5)
waiter = Agent(2, 2)
clientTime = 10
totaltime = 0
clock = pygame.time.Clock()
ticks = 0
# draw info
@ -317,7 +353,6 @@ while True:
if t[2] > 0:
t[2] = t[2] - 1
# update tables
for table in restaurant.tables:
if restaurant.tiles[table[1]][table[0]].clientState:
@ -352,7 +387,6 @@ while True:
IDLE = random.choice(S_IDLE)
FIRST = random.choice(S_FIRST)
# update waiter
if waiter.idle:
if not waiter.getTask():