-
Notifications
You must be signed in to change notification settings - Fork 0
/
solver.py
846 lines (755 loc) · 34.9 KB
/
solver.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
import math
import util
import numpy as np
import random
import tqdm
import time
import ast
class GreatDelugeSolver:
"""A Great Deluge (Gunter Dueck) implementation for seating optimization."""
def __init__(
self,
guests,
affinities,
importances,
seated_apart_threshold,
seated_together_threshold,
n_seat,
water_level=0,
rain_speed="auto",
max_iterations=100000,
max_iterations_without_improvement=None,
silent=False,
):
"""Initializes the Great Deluge solver using the given parameters and problem.
Args:
guests (list): A list of guest IDs (from 0 to n-1).
affinities (numpy.ndarray): A numpy array that essentially keeps the interpersonal affinities in a matrix
format.
importances (numpy.ndarray): A numpy array that keeps the guest importance values.
seated_apart_threshold (float): An affinity threshold used to decide if two guests are considered enemies.
If one of them have an affinity below this value, they are enemies and they are not to be seated at the
same table.
seated_together_threshold (float): If two guests have an affinity value higher than this threshold for each
other, they must be seated at the same table.
n_seat (int): The number of seats per table.
water_level (float, optional): The water level of the algorithm. It indicates the initial quality threshold.
Defaults to 0.
rain_speed (float|"auto", optional): Rain speed value of the algorithm. Essentially, it indicates the amount
of increase for the threshold when a new solution is accepted. It automatically finds the best value
when set to "auto" based on Dueck's suggestion in his paper. Defaults to "auto".
max_iterations (int, optional): The maximum number of iterations the solver can try optimizing. Defaults to
100000.
max_iterations_without_improvement (int|None, optional): The maximum number of iterations the solver can go
without seeing an improvement. If None, it does not stop until it hits max_iterations. Defaults to None.
silent (bool, optional): Indicates whether the solver must run silently. Defaults to False.
Returns:
None
"""
self.guests = set(guests)
self.n_guest = len(guests)
self.affinities = affinities
self.importances = importances
self.seated_apart_threshold = seated_apart_threshold
self.seated_together_threshold = seated_together_threshold
self.n_seat = n_seat
self.n_table = math.ceil(self.n_guest / self.n_seat)
self.remaining_guests = self.guests.copy()
self.guest_tables = {guest: None for guest in self.guests}
self.best_happiness = -math.inf
self.best_arrangement = None
self.water_level = water_level
self.rain_speed = rain_speed
self.iterations = 0
self.max_iterations = max_iterations
if max_iterations_without_improvement is None:
self.max_iterations_without_improvement = self.max_iterations
else:
self.max_iterations_without_improvement = max_iterations_without_improvement
self.execution_time = None
self.happiness_history = []
self.threshold_history = []
self.silent = silent
def solve(self, initial_solution=None):
"""Solves the previously given problem.
Args:
initial_solution (list|None, optional): A list of table indices corresponding to guests. For example, the
first index of this list corresponds to the first guest. If None, a random initial solution is created.
Defaults to None.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
if initial_solution is None:
# Create a random initial solution
initial_solution = []
for table in range(self.n_table):
initial_solution.extend([table] * self.n_seat)
random.shuffle(initial_solution)
start = time.time()
self.best_happiness = util.calculate_solution_happiness(
initial_solution[: self.n_guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
self.best_arrangement = initial_solution
solution = initial_solution.copy()
last_happiness = self.best_happiness
self.iterations = 0
iterations_without_improvement = 0
progress_bar = tqdm.tqdm(total=self.max_iterations, disable=self.silent)
while (
self.iterations < self.max_iterations
and iterations_without_improvement < self.max_iterations_without_improvement
):
self.iterations += 1
progress_bar.update(1)
id_1, id_2 = random.sample(self.guests, 2)
while (id_1 >= self.n_guest and id_2 >= self.n_guest) or (
solution[id_1] == solution[id_2]
):
# If both seats are empty or both seats are from the same table, resample
id_1, id_2 = random.sample(self.guests, 2)
new_solution = solution.copy()
new_solution[id_1], new_solution[id_2] = (
new_solution[id_2],
new_solution[id_1],
)
new_happiness = util.calculate_solution_happiness(
new_solution[: self.n_guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
if new_happiness > self.best_happiness:
self.best_happiness = new_happiness
self.best_arrangement = new_solution
if new_happiness > self.water_level:
iterations_without_improvement = 0
solution = new_solution
last_happiness = new_happiness
if self.rain_speed == "auto":
self.water_level += (new_happiness - self.water_level) * 0.009
else:
self.water_level += self.rain_speed
else:
iterations_without_improvement += 1
self.happiness_history.append(last_happiness)
self.threshold_history.append(self.water_level)
end = time.time()
self.execution_time = end - start
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement[: self.n_guest],
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
def return_last_solution(self):
"""Returns the last obtained solution's details.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement[: self.n_guest],
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
class RecordToRecordSolver:
"""A Record-To-Record Travel (Gunter Dueck) implementation for seating optimization."""
def __init__(
self,
guests,
affinities,
importances,
seated_apart_threshold,
seated_together_threshold,
n_seat,
deviation,
max_iterations=100000,
max_iterations_without_improvement=None,
silent=False,
):
"""Initializes the Great Deluge solver using the given parameters and problem.
Args:
guests (list): A list of guest IDs (from 0 to n-1).
affinities (numpy.ndarray): A numpy array that essentially keeps the interpersonal affinities in a matrix
format.
importances (numpy.ndarray): A numpy array that keeps the guest importance values.
seated_apart_threshold (float): An affinity threshold used to decide if two guests are considered enemies.
If one of them have an affinity below this value, they are enemies and they are not to be seated at the
same table.
seated_together_threshold (float): If two guests have an affinity value higher than this threshold for each
other, they must be seated at the same table.
n_seat (int): The number of seats per table.
deviation (float): Allowed deviation amount of the algorithm. A new solution's quality must be at least
record quality - deviation (within the allowed deviation range) to be accepted.
max_iterations (int, optional): The maximum number of iterations the solver can try optimizing. Defaults to
100000.
max_iterations_without_improvement (int|None, optional): The maximum number of iterations the solver can go
without seeing an improvement. If None, it does not stop until it hits max_iterations. Defaults to None.
silent (bool, optional): Indicates whether the solver must run silently. Defaults to False.
Returns:
None
"""
self.guests = set(guests)
self.n_guest = len(guests)
self.affinities = affinities
self.importances = importances
self.seated_apart_threshold = seated_apart_threshold
self.seated_together_threshold = seated_together_threshold
self.n_seat = n_seat
self.n_table = math.ceil(self.n_guest / self.n_seat)
self.remaining_guests = self.guests.copy()
self.guest_tables = {guest: None for guest in self.guests}
self.best_happiness = -math.inf
self.best_arrangement = None
self.deviation = deviation
self.iterations = 0
self.max_iterations = max_iterations
if max_iterations_without_improvement is None:
self.max_iterations_without_improvement = self.max_iterations
else:
self.max_iterations_without_improvement = max_iterations_without_improvement
self.happiness_history = []
self.threshold_history = []
self.silent = silent
def solve(self, initial_solution=None):
"""Solves the previously given problem.
Args:
initial_solution (list|None, optional): A list of table indices corresponding to guests. For example, the
first index of this list corresponds to the first guest. If None, a random initial solution is created.
Defaults to None.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
if initial_solution is None:
# Create a random initial solution
initial_solution = []
for table in range(self.n_table):
initial_solution.extend([table] * self.n_seat)
random.shuffle(initial_solution)
start = time.time()
self.best_happiness = util.calculate_solution_happiness(
initial_solution[: self.n_guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
self.best_arrangement = initial_solution
solution = initial_solution.copy()
last_happiness = self.best_happiness
self.iterations = 0
iterations_without_improvement = 0
progress_bar = tqdm.tqdm(total=self.max_iterations, disable=self.silent)
threshold = self.best_happiness - self.deviation
while (
self.iterations < self.max_iterations
and iterations_without_improvement < self.max_iterations_without_improvement
):
self.iterations += 1
progress_bar.update(1)
id_1, id_2 = random.sample(self.guests, 2)
while (id_1 >= self.n_guest and id_2 >= self.n_guest) or (
solution[id_1] == solution[id_2]
):
# If both seats are empty or both seats are from the same table, resample
id_1, id_2 = random.sample(self.guests, 2)
new_solution = solution.copy()
new_solution[id_1], new_solution[id_2] = (
new_solution[id_2],
new_solution[id_1],
)
new_happiness = util.calculate_solution_happiness(
new_solution[: self.n_guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
if new_happiness > self.best_happiness:
self.best_happiness = new_happiness
self.best_arrangement = new_solution
threshold = self.best_happiness - self.deviation
if new_happiness > threshold:
iterations_without_improvement = 0
solution = new_solution
last_happiness = new_happiness
else:
iterations_without_improvement += 1
self.happiness_history.append(last_happiness)
self.threshold_history.append(threshold)
end = time.time()
self.execution_time = end - start
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement[: self.n_guest],
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
def return_last_solution(self):
"""Returns the last obtained solution's details.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement[: self.n_guest],
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
class SimulatedAnnealingSolver:
"""A Simulated Annealing implementation. It allows different types of cooling schedules."""
def __init__(
self,
guests,
affinities,
importances,
seated_apart_threshold,
seated_together_threshold,
n_seat,
temperature,
alpha,
max_iterations=100000,
max_iterations_without_improvement=None,
silent=False,
):
"""Initializes the Great Deluge solver using the given parameters and problem.
Args:
guests (list): A list of guest IDs (from 0 to n-1).
affinities (numpy.ndarray): A numpy array that essentially keeps the interpersonal affinities in a matrix
format.
importances (numpy.ndarray): A numpy array that keeps the guest importance values.
seated_apart_threshold (float): An affinity threshold used to decide if two guests are considered enemies.
If one of them have an affinity below this value, they are enemies and they are not to be seated at the
same table.
seated_together_threshold (float): If two guests have an affinity value higher than this threshold for each
other, they must be seated at the same table.
n_seat (int): The number of seats per table.
temperature (float): The initial temperature of the algorithm.
alpha (float|int|"auto"): The cooling factor of the algorithm. It is automatically set if set to "auto"
through dividing the temperature by the current number of iterations. If it is greater than or equal to
1, the alpha is used linearly by subtracting it from the temperature. Otherwise, it is used as usual (it
is multiplied with the temperature).
max_iterations (int, optional): The maximum number of iterations the solver can try optimizing. Defaults to
100000.
max_iterations_without_improvement (int|None, optional): The maximum number of iterations the solver can go
without seeing an improvement. If None, it does not stop until it hits max_iterations. Defaults to None.
silent (bool, optional): Indicates whether the solver must run silently. Defaults to False.
Returns:
None
"""
self.guests = set(guests)
self.n_guest = len(guests)
self.affinities = affinities
self.importances = importances
self.seated_apart_threshold = seated_apart_threshold
self.seated_together_threshold = seated_together_threshold
self.n_seat = n_seat
self.n_table = math.ceil(self.n_guest / self.n_seat)
self.remaining_guests = self.guests.copy()
self.guest_tables = {guest: None for guest in self.guests}
self.best_happiness = -math.inf
self.best_arrangement = None
self.temperature = temperature
self.alpha = alpha
self.max_iterations = max_iterations
self.iterations = 0
if max_iterations_without_improvement is None:
self.max_iterations_without_improvement = self.max_iterations
else:
self.max_iterations_without_improvement = max_iterations_without_improvement
self.happiness_history = []
self.temperature_history = []
self.execution_time = None
self.silent = silent
def solve(self, initial_solution=None):
"""Solves the previously given problem.
Args:
initial_solution (list|None, optional): A list of table indices corresponding to guests. For example, the
first index of this list corresponds to the first guest. If None, a random initial solution is created.
Defaults to None.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
if initial_solution is None:
# Create a random initial solution
initial_solution = []
for table in range(self.n_table):
initial_solution.extend([table] * self.n_seat)
random.shuffle(initial_solution)
start = time.time()
self.best_happiness = util.calculate_solution_happiness(
initial_solution[: self.n_guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
self.best_arrangement = initial_solution
solution = initial_solution.copy()
last_happiness = self.best_happiness
self.iterations = 0
iterations_without_improvement = 0
progress_bar = tqdm.tqdm(total=self.max_iterations, disable=self.silent)
while (
self.iterations < self.max_iterations
and self.temperature > 0
and iterations_without_improvement < self.max_iterations_without_improvement
):
self.iterations += 1
progress_bar.update(1)
id_1, id_2 = random.sample(self.guests, 2)
while id_1 >= self.n_guest and id_2 >= self.n_guest:
id_1, id_2 = random.sample(self.guests, 2)
new_solution = solution.copy()
new_solution[id_1], new_solution[id_2] = (
new_solution[id_2],
new_solution[id_1],
)
new_happiness = util.calculate_solution_happiness(
new_solution[: self.n_guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
if new_happiness > self.best_happiness:
self.best_happiness = new_happiness
self.best_arrangement = new_solution
happiness_diff = new_happiness - last_happiness
if happiness_diff > 0:
iterations_without_improvement = 0
solution = new_solution
last_happiness = new_happiness
else:
iterations_without_improvement += 1
sample = random.uniform(0.0, 1.0)
if sample < math.exp(happiness_diff / self.temperature):
solution = new_solution
last_happiness = new_happiness
self.happiness_history.append(last_happiness)
if self.alpha == "auto":
self.temperature /= self.iterations
elif self.alpha >= 1:
self.temperature -= self.alpha
else:
self.temperature *= self.alpha
self.temperature_history.append(self.temperature)
end = time.time()
self.execution_time = end - start
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement[: self.n_guest],
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
def return_last_solution(self):
"""Returns the last obtained solution's details.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement[: self.n_guest],
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
class ExhaustiveSolver:
"""An exhaustive solver that tries all possible solutions and finds the best one. Since recursion does not work well
especially with more complex networks, it uses the file that has all possible solutions and finds the one that has
the highest happiness.
"""
def __init__(
self,
guests,
affinities,
importances,
seated_apart_threshold,
seated_together_threshold,
n_seat,
possible_solutions_file,
):
"""Initializes the solver with the given parameters.
Args:
guests (list): A list of guest IDs (from 0 to n-1).
affinities (numpy.ndarray): A numpy array that essentially keeps the interpersonal affinities in a matrix
format.
importances (numpy.ndarray): A numpy array that keeps the guest importance values.
seated_apart_threshold (float): An affinity threshold used to decide if two guests are considered enemies.
If one of them have an affinity below this value, they are enemies and they are not to be seated at the
same table.
seated_together_threshold (float): If two guests have an affinity value higher than this threshold for each
other, they must be seated at the same table.
n_seat (int): The number of seats per table.
possible_solutions_file (str): Location of the file that stores all possible solutions.
"""
self.guests = set(guests)
self.n_guest = len(guests)
self.affinities = affinities
self.importances = importances
self.seated_apart_threshold = seated_apart_threshold
self.seated_together_threshold = seated_together_threshold
self.n_seat = n_seat
self.n_table = math.ceil(self.n_guest / self.n_seat)
self.best_happiness = -math.inf
self.best_arrangement = None
self.possible_solutions_file = possible_solutions_file
self.iterations = 0
self.execution_time = None
def solve(self):
"""Solves the previously given problem.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
start = time.time()
self.iterations = 0
with open(self.possible_solutions_file, "r") as file:
for line in tqdm.tqdm(file):
self.iterations += 1
tables = ast.literal_eval(line) # A safer alternative to eval
happiness = util.calculate_tables_happiness(
tables,
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
if happiness > self.best_happiness:
self.best_arrangement = tables
self.best_happiness = happiness
if self.best_arrangement is not None:
solution = [None] * self.n_guest
for table_id, table in enumerate(self.best_arrangement):
for guest in table:
solution[guest] = table_id
self.best_arrangement = solution
end = time.time()
self.execution_time = end - start
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement,
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
def return_last_solution(self):
"""Returns the last obtained solution's details.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement,
"Iterations": self.iterations,
"Execution time": self.execution_time,
}
class GreedySolver:
"""A greedy solver for comparison. It starts with the most important person, and finds the person that would
maximize their happiness. After seating this person, it then finds the person that would maximize the first two
people's happiness. This process is iterated until the table is full, and it starts this process again with the most
important non-seated person until everyone is seated. Obviously, it is far from being optimal.
"""
def __init__(
self,
guests,
affinities,
importances,
seated_apart_threshold,
seated_together_threshold,
n_seat,
):
"""Initializes the solver with the given parameters.
Args:
guests (list): A list of guest IDs (from 0 to n-1).
affinities (numpy.ndarray): A numpy array that essentially keeps the interpersonal affinities in a matrix
format.
importances (numpy.ndarray): A numpy array that keeps the guest importance values.
seated_apart_threshold (float): An affinity threshold used to decide if two guests are considered enemies.
If one of them have an affinity below this value, they are enemies and they are not to be seated at the
same table.
seated_together_threshold (float): If two guests have an affinity value higher than this threshold for each
other, they must be seated at the same table.
n_seat (int): The number of seats per table.
"""
self.guests = set(guests)
self.n_guest = len(guests)
self.affinities = affinities
self.importances = importances
self.seated_apart_threshold = seated_apart_threshold
self.seated_together_threshold = seated_together_threshold
self.n_seat = n_seat
self.n_table = math.ceil(self.n_guest / self.n_seat)
self.remaining_guests = self.guests.copy()
self.tables = []
self.guest_tables = {guest: None for guest in self.guests}
self.best_happiness = -math.inf
self.best_arrangement = None
self.execution_time = None
def solve(self):
"""Solves the previously given problem.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
start = time.time()
if len(self.remaining_guests) < 1:
return self.best_arrangement
current_table = len(self.tables)
progress_bar = tqdm.tqdm(total=self.n_table)
while len(self.remaining_guests) > 0:
if len(self.tables) <= current_table:
self.tables.append([])
if len(self.tables[current_table]) == 0:
# New table, start with the most important person
max_importance = 0
most_important_guest = None
for guest in self.guests:
if (
guest in self.remaining_guests
and self.importances[guest] > max_importance
):
max_importance = self.importances[guest]
most_important_guest = guest
self.remaining_guests.remove(most_important_guest)
self.tables[current_table].append(most_important_guest)
elif len(self.tables[current_table]) >= self.n_seat:
# The table is full, create a new table
current_table += 1
progress_bar.update(1)
self.tables.append([])
else:
# Add guests to the existing table
while (
len(self.tables[current_table]) < self.n_seat
and len(self.remaining_guests) > 0
):
max_affinity = -math.inf
max_affinity_guest = None
for guest in self.remaining_guests:
if (
guest != most_important_guest
and util.calculate_table_happiness(
self.tables[current_table] + [guest],
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
>= max_affinity
):
max_affinity = self.affinities[most_important_guest, guest]
max_affinity_guest = guest
if (
max_affinity_guest is None
): # Just in case, it should not be None
max_affinity_guest = self.remaining_guests.pop()
else:
self.remaining_guests.remove(max_affinity_guest)
self.tables[current_table].append(max_affinity_guest)
for table_id, table_guests in enumerate(self.tables):
for guest in table_guests:
self.guest_tables[guest] = table_id
self.best_arrangement = [self.guest_tables[i] for i in range(self.n_guest)]
self.best_happiness = util.calculate_solution_happiness(
self.best_arrangement,
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
end = time.time()
self.execution_time = end - start
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement,
"Iterations": None,
"Execution time": self.execution_time,
}
def return_last_solution(self):
"""Returns the last obtained solution's details.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement,
"Iterations": None,
"Execution time": self.execution_time,
}
class RandomSolver:
"""Generates a totally random solution for comparison."""
def __init__(
self,
guests,
affinities,
importances,
seated_apart_threshold,
seated_together_threshold,
n_seat,
):
"""Initializes the solver with the given parameters.
Args:
guests (list): A list of guest IDs (from 0 to n-1).
affinities (numpy.ndarray): A numpy array that essentially keeps the interpersonal affinities in a matrix
format.
importances (numpy.ndarray): A numpy array that keeps the guest importance values.
seated_apart_threshold (float): An affinity threshold used to decide if two guests are considered enemies.
If one of them have an affinity below this value, they are enemies and they are not to be seated at the
same table.
seated_together_threshold (float): If two guests have an affinity value higher than this threshold for each
other, they must be seated at the same table.
n_seat (int): The number of seats per table.
"""
self.guests = set(guests)
self.n_guest = len(guests)
self.affinities = affinities
self.importances = importances
self.seated_apart_threshold = seated_apart_threshold
self.seated_together_threshold = seated_together_threshold
self.n_seat = n_seat
self.n_table = math.ceil(self.n_guest / self.n_seat)
self.remaining_guests = self.guests.copy()
self.tables = []
self.guest_tables = {guest: None for guest in self.guests}
self.best_happiness = -math.inf
self.best_arrangement = None
self.execution_time = 0
def solve(self):
"""Solves the previously given problem.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
start = time.time()
initial_solution = []
for table in range(self.n_table):
initial_solution.extend([table] * self.n_seat)
random.shuffle(initial_solution)
self.best_arrangement = initial_solution[: self.n_guest]
self.best_happiness = util.calculate_solution_happiness(
self.best_arrangement,
self.affinities,
self.importances,
self.seated_apart_threshold,
self.seated_together_threshold,
)
end = time.time()
self.execution_time = end - start
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement,
"Iterations": None,
"Execution time": self.execution_time,
}
def return_last_solution(self):
"""Returns the last obtained solution's details.
Returns:
dict: A dictionary that has the solution's happiness, seating arrangement, iterations, and execution time.
"""
return {
"Best happiness": self.best_happiness,
"Best arrangement": self.best_arrangement,
"Iterations": None,
"Execution time": self.execution_time,
}