some changes

This commit is contained in:
Vadzim Valchkovich 2023-06-17 17:16:58 +02:00
parent 57bc7f081f
commit 27050f5bfe
3 changed files with 50 additions and 21 deletions

View File

@ -14,7 +14,7 @@ kitchen = Kitchen([0, 0], 0, SQUARE_SIZE, SCREEN_SIZE, store)
engine = Engine(SCREEN_SIZE, SQUARE_SIZE, kitchen, waiter, ACTION_DURATION) engine = Engine(SCREEN_SIZE, SQUARE_SIZE, kitchen, waiter, ACTION_DURATION)
layout = LayoutController(engine, store) layout = LayoutController(engine, store)
params = layout.train_loop(10) params = layout.train_loop(12)
layout.create_and_subscribe( layout.create_and_subscribe(
params['radius'], params['radius'],

View File

@ -179,6 +179,7 @@ class Engine:
def unattainable_goal(self): def unattainable_goal(self):
if not self.is_simulation: if not self.is_simulation:
print(colored("Object unattainable", "red")) print(colored("Object unattainable", "red"))
self.objects.remove(self.goal.parent)
self.clock_increment(1000) self.clock_increment(1000)
self.revoke_goal() self.revoke_goal()

View File

@ -48,7 +48,7 @@ class LayoutController():
return action_clock < 1000 return action_clock < 1000
population = PriorityQueue() population = PriorityQueue()
for i in range(20): for i in range(15):
params = ( params = (
child_params[i] child_params[i]
if child_params if child_params
@ -89,35 +89,62 @@ class LayoutController():
def reproduction(self, parents): def reproduction(self, parents):
parents.queue = parents.queue[:5] parents.queue = parents.queue[:5]
children = [] children = []
while len(children) < 20: while len(children) < 10:
p1 = random.choice(parents.queue) p1 = random.choice(parents.queue)
p2 = random.choice(parents.queue) p2 = random.choice(parents.queue)
mask = {
'radius': random.randint(0, 1),
'neighbors_count': random.randint(0, 1),
'block_probability': random.randint(0, 1)
}
child1 = {
'radius': p1.o()['radius'] if mask['radius'] else p2.o()['radius'],
'neighbors_count': p1.o()['neighbors_count'] if mask['neighbors_count'] else p2.o()['neighbors_count'],
'block_probability': p1.o()['block_probability'] if mask['block_probability'] else p2.o()['block_probability'],
}
child2 = {
'radius': p1.o()['radius'] if not mask['radius'] else p2.o()['radius'],
'neighbors_count': p1.o()['neighbors_count'] if not mask['neighbors_count'] else p2.o()['neighbors_count'],
'block_probability': p1.o()['block_probability'] if not mask['block_probability'] else p2.o()['block_probability'],
}
children.append(child1)
children.append(child2)
for p in parents.queue[:5]:
child = { child = {
'radius': random.choice([p1.o()['radius'], p2.o()['radius']]), 'radius': p.o()['radius'],
'neighbors_count': random.choice([p1.o()['neighbors_count'], p2.o()['neighbors_count']]), 'neighbors_count': p.o()['neighbors_count'],
'block_probability': random.choice([p1.o()['block_probability'], p2.o()['block_probability']]) 'block_probability': p.o()['block_probability']
} }
children.append(child) children.append(child)
return children return children
def mutation(self, params): def mutation(self, params):
for _ in range(5): MUTATION_CHANCE = 10
unit = random.choice(params)
mutation_type = random.choice( for _ in range(10):
[ if random.randint(0, 100) <= MUTATION_CHANCE:
'radius',
'neighbors_count', unit = random.choice(params)
'block_probability' mutation_type = random.choice(
] [
) 'radius',
if mutation_type == 'radius': 'neighbors_count',
unit[mutation_type] = self.get_random_radius() 'block_probability'
elif mutation_type == 'neighbors_count': ]
unit[mutation_type] = self.get_random_neighbors_count() )
elif mutation_type == 'block_probability': if mutation_type == 'radius':
unit[mutation_type] = self.get_random_block_probability() unit[mutation_type] += self.get_random_radius()
elif mutation_type == 'neighbors_count':
unit[mutation_type] += self.get_random_neighbors_count()
elif mutation_type == 'block_probability':
unit[mutation_type] += self.get_random_block_probability()
unit[mutation_type] //= 2
return params return params
@ -129,6 +156,7 @@ class LayoutController():
while best < goal: while best < goal:
generation += 1 generation += 1
population = self.simulation(params) population = self.simulation(params)
print( print(
colored(f"generation #{generation}", "yellow"), colored(f"generation #{generation}", "yellow"),
colored("best fit:", "blue"), colored("best fit:", "blue"),