-
Notifications
You must be signed in to change notification settings - Fork 0
/
optimal_seating.py
144 lines (121 loc) · 4.32 KB
/
optimal_seating.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
import base64
import datetime
import io
import shapely
import cProfile
import room_factory
import table_factory
from evaluator import Evaluator
from mutator import Mutator
from plt import visualize_solution, animate
from searcher import Searcher
from seating_plan import SeatingPlan
import numpy as np
import pandas as pd
def read_room(df):
room_width = -1
room_height = -1
if df.loc[0][0] == 'o':
room = room_factory.create(
df.loc[0][0],
width=df.loc[0][1],
inner_width=df.loc[0][2]
)
room_width = int(df.loc[0][1][:-1])
room_height = int(df.loc[0][1][:-1])
elif df.loc[0][0] == 'circle':
room = room_factory.create(
df.loc[0][0],
width=df.loc[0][1],
)
room_width = int(df.loc[0][1][:-1])
room_height = int(df.loc[0][1][:-1])
elif df.loc[0][0] == 'delta' or df.loc[0][0] == 'rect':
room = room_factory.create(
df.loc[0][0],
width=df.loc[0][1],
height=df.loc[0][2]
)
room_width = int(df.loc[0][1][:-1])
room_height = int(df.loc[0][2][:-1])
elif df.loc[0][0] == 'L':
room = room_factory.create(
df.loc[0][0],
width=df.loc[0][1],
height=df.loc[0][2],
smaller_width=df.loc[0][3],
smaller_height=df.loc[0][4]
)
room_width = int(df.loc[0][1][:-1])
room_height = int(df.loc[0][2][:-1])
return room, room_width, room_height
def put_in_file(filename):
if 'circle' in filename:
return 'circle'
if 'rect' in filename or 'squared' in filename:
return 'rect'
if 'L_shape' in filename:
return 'L_shape'
if 'o_shape' in filename:
return 'o_shape'
if 'delta' in filename:
return 'delta'
return 'other'
def print_to_file(fitness: float, plan: SeatingPlan, time_in_seconds: float, filename='calculated.csv', iterations = 10_000):
path = f'./results/output-{put_in_file(filename)}.csv'
used_chairs = np.sum([len(table.template.chairs) for table in plan.tables[plan.used_tables_mask]])
total_chairs = np.sum([table.template.number_of_chairs for table in plan.tables])
import sys
import os.path
standard_output = sys.stdout
if (not os.path.exists(path)):
with open(path, 'a') as file:
sys.stdout = file
print(f"used,total,time,success")
with open(path, 'a') as file:
sys.stdout = file
print(f"{used_chairs},{total_chairs},{time_in_seconds},{fitness >= 0}")
sys.stdout = standard_output
def optimal_seating(df, filename='calculated.csv', iterations = 10_000):
room, room_width, room_height = read_room(df)
tables = ()
for index, row in df.iterrows():
if index == 0:
continue
tables = tables + (*table_factory.create_multiple(int(row[0]), row[1], room_dims=(room_width, room_height),
width=int(row[2]), height=int(row[3]),
ltrb=(int(row[4]), int(row[5]), int(row[6]), int(row[7]))),)
seating_plan = SeatingPlan(
np.array(tables),
np.repeat(True, len(tables)),
3000
)
mutator = Mutator(
room,
table_mutation_probability=.1,
table_mutation_offset_stdev=100,
table_mutation_angle_sigma=0,
)
evaluator = Evaluator(room)
def log_fn(i, evaluated_population):
if i % 100:
return
best_fitness, best_instance = evaluated_population[0]
worst_fitness, worst_instance = evaluated_population[-1]
print("no of used tables: ", best_instance.used_tables())
print(f"iteration: {i}, "
f"population size: {len(evaluated_population)}, "
f"fittness range: {abs(round(best_fitness, 2))}-{abs(round(worst_fitness, 2))}")
visualize_solution(room, best_instance, save=f'data/{i:05d}.png')
searcher = Searcher()
run = lambda: searcher(
mutate_fn=mutator,
evaluate_fn=evaluator,
log_fn=log_fn,
initial_population=(seating_plan,),
max_population_size=10,
num_iterations=iterations,
)
fitness, population, time = run()
animate()
print_to_file(fitness, population, time, filename, iterations)