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