-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneticAlgorithm.py
More file actions
1450 lines (1220 loc) · 62.1 KB
/
GeneticAlgorithm.py
File metadata and controls
1450 lines (1220 loc) · 62.1 KB
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
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import logging
from GameOfLife import GameOfLife
import random
import numpy as np
import collections
from functools import lru_cache
class GeneticAlgorithm:
"""
A genetic algorithm implementation for optimizing Conway's Game of Life configurations.
Uses evolutionary principles to evolve configurations that maximize desired properties
like lifespan, cell growth, and pattern stability.
Attributes:
grid_size (int): Dimensions of the NxN grid for Game of Life configurations.
population_size (int): Number of configurations in each generation.
generations (int): Total number of generations to simulate.
mutation_rate_upper_limit (float): Starting mutation probability.
mutation_rate_lower_limit (float): Minimum allowed mutation rate.
alive_cells_weight (float): Fitness weight for maximum number of living cells.
lifespan_weight (float): Fitness weight for configuration lifespan.
alive_growth_weight (float): Fitness weight for cell growth ratio.
initial_living_cells_count_penalty_weight (float): Penalty weight for large initial configurations.
predefined_configurations (optional): Pre-made Game of Life patterns to include.
population (set[tuple]): Current generation's configurations.
configuration_cache (dict): Cache of evaluated configurations and their metrics.
generations_statistics (dict): Statistics for each generation.
mutation_rate_history (list): Track mutation rate changes over time.
diversity_history (list): Track diversity metrics over time.
"""
def __init__(self, grid_size, population_size, generations, mutation_rate_upper_limit, mutation_rate_lower_limit,
alive_cells_weight, lifespan_weight, alive_growth_weight,
initial_living_cells_count_penalty_weight, boundary_type, predefined_configurations=None):
"""
Initialize the genetic algorithm with configuration parameters.
Args:
grid_size (int): Size of the NxN grid.
population_size (int): Number of configurations per generation.
generations (int): Number of generations to evolve.
mutation_rate_upper_limit (float): Starting mutation probability.
mutation_rate_lower_limit (float): Minimum mutation rate.
alive_cells_weight (float): Weight for maximum living cells in fitness.
lifespan_weight (float): Weight for configuration lifespan in fitness.
alive_growth_weight (float): Weight for cell growth ratio in fitness.
initial_living_cells_count_penalty_weight (float): Weight for penalizing large initial patterns.
predefined_configurations (optional): Predefined Game of Life patterns to include.
"""
logging.info("""Initializing GeneticAlgorithm.""")
self.grid_size = grid_size
self.total_cells = grid_size ** 2
self.population_size = population_size
self.generations = generations
self.mutation_rate_lower_limit = mutation_rate_lower_limit
self.mutation_rate = mutation_rate_upper_limit
self.mutation_rate_upper_limit = mutation_rate_upper_limit
self.alive_cells_weight = alive_cells_weight
self.lifespan_weight = lifespan_weight
self.initial_living_cells_count_penalty_weight = initial_living_cells_count_penalty_weight
self.alive_growth_weight = alive_growth_weight
self.configuration_cache = collections.defaultdict(dict)
self.generations_statistics = collections.defaultdict(dict)
self.canonical_forms_cache = collections.defaultdict(
tuple) # Cache for canonical forms
self.block_frequencies_cache = collections.defaultdict(
dict) # Cache for block frequencies
self.population = set()
self.initial_population = set()
self.mutation_rate_history = []
self.diversity_history = [] # Track diversity metrics over generations
self.min_fitness = float('inf') # Minimum fitness score
self.max_fitness = float('-inf') # Maximum fitness score
self.min_uniqueness_score = float('inf') # Minimum uniqueness score
self.max_uniqueness_score = float('-inf') # Maximum uniqueness score
self.boundary_type = boundary_type
self.diversity_threshold = 1 / (grid_size ** 2)
self.predefined_configurations = predefined_configurations
def generate_varied_random_configurations(self, clusters_type_amount, scatter_type_amount, basic_patterns_type_amount):
"""
Generate diverse configurations using three distinct pattern types, ensuring uniqueness:
1. Clustered: Groups of adjacent living cells with potential holes.
2. Scattered: Randomly distributed living cells.
3. Basic patterns: Structured geometric arrangements.
Args:
clusters_type_amount (int): Number of cluster-based configurations.
scatter_type_amount (int): Number of scattered configurations.
basic_patterns_type_amount (int): Number of basic pattern configurations.
Returns:
list[tuple[int]]: Collection of unique initial configurations.
"""
total_cells = self.total_cells
max_cluster_size = self.grid_size
min_cluster_size = 3
max_scattered_cells = self.grid_size * 2
min_scattered_cells = 0
max_pattern_cells = self.grid_size * 2
min_pattern_cells = 0
population_pool = []
unique_canonical_forms = set()
# Generate Cluster Configurations
for _ in range(clusters_type_amount):
while True:
configuration = [0] * total_cells
cluster_size = random.randint(
min_cluster_size, max_cluster_size)
# Start with a random central point
center_row = random.randint(0, self.grid_size - 1)
center_col = random.randint(0, self.grid_size - 1)
# Define a set for added cells to ensure they are unique
added_cells = set()
cells_to_expand = [(center_row, center_col)]
# Expand the cluster with potential "holes"
while len(added_cells) < cluster_size and cells_to_expand:
current_row, current_col = cells_to_expand.pop(
random.randint(0, len(cells_to_expand) - 1))
# Try to add neighboring cells
for offset_row, offset_col in [(-1, 0), (1, 0), (0, -1), (0, 1)]:
new_row = (current_row + offset_row) % self.grid_size
new_col = (current_col + offset_col) % self.grid_size
new_index = new_row * self.grid_size + new_col
if new_index not in added_cells:
added_cells.add(new_index)
cells_to_expand.append((new_row, new_col))
for index in added_cells:
if random.uniform(0, 1) < 0.8:
configuration[index] = 1
# Check for uniqueness
canonical_form = self.pad_cannonical_form(tuple(configuration))
if canonical_form not in unique_canonical_forms:
unique_canonical_forms.add(canonical_form)
population_pool.append(tuple(canonical_form))
break
# Generate Scattered Configurations
for _ in range(scatter_type_amount):
while True:
configuration = [0] * total_cells
scattered_cells = random.randint(
min_scattered_cells, max_scattered_cells)
scattered_indices = random.sample(
range(total_cells), scattered_cells)
for index in scattered_indices:
configuration[index] = 1
# Check for uniqueness
canonical_form = self.pad_cannonical_form(tuple(configuration))
if canonical_form not in unique_canonical_forms:
unique_canonical_forms.add(canonical_form)
population_pool.append(tuple(canonical_form))
break
# Generate Basic Patterns Configuration
for _ in range(basic_patterns_type_amount):
while True:
configuration = [0] * total_cells
pattern_cells = random.randint(
min_pattern_cells, max_pattern_cells)
start_row = random.randint(0, self.grid_size - 3)
start_col = random.randint(0, self.grid_size - 3)
for i in range(self.grid_size):
for j in range(self.grid_size):
if random.uniform(0, 1) < 0.5:
row = (start_row + i) % self.grid_size
col = (start_col + j) % self.grid_size
index = row * self.grid_size + col
configuration[index] = 1
current_live_cells = sum(configuration)
if current_live_cells < pattern_cells:
additional_cells = random.sample(
[i for i in range(total_cells)
if configuration[i] == 0],
pattern_cells - current_live_cells
)
for index in additional_cells:
configuration[index] = 1
# Check for uniqueness
canonical_form = self.pad_cannonical_form(tuple(configuration))
if canonical_form not in unique_canonical_forms:
unique_canonical_forms.add(canonical_form)
population_pool.append(tuple(canonical_form))
break
# logging.debug("""Enriched population with variety.""")
return population_pool
def create_new_population(self, amount):
clusters_type_amount = amount // 2 + amount % 2
scatter_type_amount = amount // 4
basic_patterns_type_amount = amount // 4
population = self.generate_varied_random_configurations(
clusters_type_amount=clusters_type_amount,
scatter_type_amount=scatter_type_amount,
basic_patterns_type_amount=basic_patterns_type_amount
)
return population
def calc_fitness(self, lifespan, max_alive_cells_count, alive_growth, stableness, initial_living_cells_count):
"""
Calculate weighted fitness score combining multiple optimization objectives.
Combines multiple metrics into a single fitness value that balances:
- Configuration longevity through lifespan
- Peak population through maximum alive cells
- Growth dynamics through alive growth ratio
- Efficiency through initial size penalty
Args:
lifespan (int): Number of unique states before stopping.
max_alive_cells_count (int): Maximum living cells in any generation.
alive_growth (float): Ratio of max to min living cells.
stableness (int): 1 for configuration that stablizes and 0 for one who do not
initial_living_cells_count (int): Starting number of living cells.
Returns:
float: Combined fitness score weighted by configuration parameters.
"""
lifespan_score = lifespan * self.lifespan_weight
alive_cells_score = max_alive_cells_count * self.alive_cells_weight
growth_score = alive_growth * self.alive_growth_weight
large_configuration_penalty = (
1 / max(1, initial_living_cells_count * self.initial_living_cells_count_penalty_weight))
fitness = ((lifespan_score + alive_cells_score + growth_score)
* (large_configuration_penalty) * stableness)
return fitness
def calculate_corrected_scores(self):
"""
Calculate corrected scores by combining penalties for canonical form frequency,
block frequency across configurations, and cell frequency.
Returns:
list[tuple[int, float]]: List of configurations with corrected scores.
"""
# Reset the global block frequency cache
self.block_frequencies_cache = {}
global_block_frequency = {}
total_cells = self.total_cells
frequency_vector = np.zeros(total_cells)
uniqueness_scores = []
# First pass: Calculate global frequencies
for config in self.population:
frequency_vector += np.array(config)
# Detect unique blocks and update global block frequency
unique_blocks = self.detect_recurrent_blocks(config)
for block in unique_blocks:
if block not in global_block_frequency:
global_block_frequency[block] = 0
global_block_frequency[block] += 1
# Second pass: Calculate penalties and corrected scores
for config in self.population:
# Use normalized fitness score
normalized_fitness = self.configuration_cache[config]['normalized_fitness_score']
active_cells = [i for i, cell in enumerate(config) if cell == 1]
# Cell frequency penalty
if len(active_cells) == 0:
cell_frequency_penalty = 1 # Avoid division by zero
else:
total_frequency = sum(
frequency_vector[i] for i in active_cells)
cell_frequency_penalty = (
total_frequency / len(active_cells))
# Block recurrence penalty (penalizing blocks that appear in multiple configurations)
block_frequency_penalty = 1
unique_blocks = self.block_frequencies_cache[config]
for block in unique_blocks:
global_block_count = global_block_frequency.get(block, 1)
block_frequency_penalty *= global_block_count
block_frequency_penalty = block_frequency_penalty
# Combine penalties into a uniqueness score
uniqueness_score = (cell_frequency_penalty *
block_frequency_penalty)
uniqueness_scores.append(uniqueness_score)
# Update min/max uniqueness scores globally
self.min_uniqueness_score = min(
uniqueness_scores) if uniqueness_scores else 0
self.max_uniqueness_score = max(
uniqueness_scores) if uniqueness_scores else 1
corrected_scores = []
# Normalize uniqueness scores and calculate corrected scores
for config, uniqueness_score in zip(self.population, uniqueness_scores):
normalized_uniqueness = (uniqueness_score - self.min_uniqueness_score) / \
(self.max_uniqueness_score - self.min_uniqueness_score) \
if self.max_uniqueness_score != self.min_uniqueness_score else 1.0
corrected_score = (
normalized_fitness if normalized_fitness is not None else 0) / max(1, normalized_uniqueness)
corrected_scores.append((config, corrected_score))
# logging.debug("""Calculated corrected scores for parent selection.""")
return corrected_scores
def evaluate(self, configuration):
"""
Evaluate a configuration by simulating its evolution and calculating fitness.
Simulates the configuration through Conway's Game of Life and computes
various metrics including lifespan, population dynamics, and stability.
Results are cached to avoid redundant calculations.
Args:
configuration (tuple[int]): Flattened 1D representation of Game of Life grid.
Returns:
dict: Configuration evaluation results including:
- fitness_score: Overall fitness value
- history: Complete evolution history
- lifespan: Number of unique states
- alive_growth: Cell growth ratio
- max_alive_cells_count: Peak population
- is_static: Whether pattern becomes static
- is_periodic: Whether pattern becomes periodic
- stableness: Stability measure
- initial_living_cells_count: Starting population
"""
configuration_tuple = tuple(configuration)
# Check if the configuration is already cached
if configuration_tuple in self.configuration_cache:
# Recalculate normalized fitness if min/max fitness changed
if self.max_fitness != self.min_fitness:
self.configuration_cache[configuration_tuple]['normalized_fitness_score'] = (
(self.configuration_cache[configuration_tuple]['fitness_score'] - self.min_fitness) /
(self.max_fitness - self.min_fitness)
)
else:
self.configuration_cache[configuration_tuple]['normalized_fitness_score'] = 1.0
# logging.debug(
# """Configuration already evaluated. Retrieved from cache.""")
return self.configuration_cache[configuration_tuple]
expected_size = self.total_cells
if len(configuration_tuple) != expected_size:
raise ValueError("""Configuration size must be {}, but got {}""".format(
expected_size, len(configuration_tuple)))
def max_difference_with_distance(lst):
max_value = float('-inf')
dis = 0
min_index = 0
for j in range(1, len(lst)):
diff = (lst[j] - lst[min_index]) * (j - min_index)
if diff > max_value:
dis = j - min_index
max_value = diff
if lst[j] < lst[min_index]:
min_index = j
return max(max_value, 0) / dis if dis != 0 else 0
game = GameOfLife(self.grid_size, configuration_tuple,
boundary_type=self.boundary_type)
game.run()
max_alive_cells_count = max(
game.alive_history) if game.alive_history else 0
initial_living_cells_count = sum(configuration_tuple)
alive_growth = max_difference_with_distance(
game.alive_history) if game.alive_history else 0
stableness = 1 if game.is_methuselah else 0
fitness_score = self.calc_fitness(
lifespan=game.lifespan,
max_alive_cells_count=max_alive_cells_count,
alive_growth=alive_growth,
stableness=stableness,
initial_living_cells_count=initial_living_cells_count
)
# Update global min/max fitness values
self.min_fitness = min(self.min_fitness, fitness_score)
self.max_fitness = max(self.max_fitness, fitness_score)
# Calculate normalized fitness for this configuration
normalized_fitness = (
(fitness_score - self.min_fitness) /
(self.max_fitness - self.min_fitness)
if self.max_fitness != self.min_fitness else 1.0
)
self.configuration_cache[configuration_tuple] = {
'fitness_score': fitness_score,
'normalized_fitness_score': normalized_fitness,
'lifespan': game.lifespan,
'alive_growth': alive_growth,
'max_alive_cells_count': max_alive_cells_count,
'is_static': game.is_static,
'is_periodic': game.is_periodic,
'stableness': stableness,
'initial_living_cells_count': initial_living_cells_count
}
logging.debug("""Evaluated configuration and cached results.""")
return self.configuration_cache[configuration_tuple]
def initialize(self):
"""
Initialize the population with diverse configurations.
Creates initial population using three equal parts:
- Clustered configurations
- Scattered configurations
- Basic pattern configurations
Evaluates and caches fitness for all initial configurations.
Records initial generation statistics.
"""
logging.info(
"""Initializing population with diverse configurations.""")
population_pool = self.create_new_population(
amount=self.population_size)
self.initial_population.update(population_pool)
self.population.update(population_pool)
self.compute_generation(generation=0)
def populate(self, generation):
"""
Generate the next generation of configurations for the genetic algorithm.
This function dynamically evolves the population based on diversity metrics
and genetic operations. If diversity falls below a critical threshold, or
if the generation number is a multiple of 10, fresh configurations are
injected into the population to maintain genetic diversity.
Steps:
1. Evaluate the population's diversity and adjust the threshold dynamically.
2. If diversity is low or the generation is a multiple of 10:
- Inject fresh, unique configurations into the population.
3. Otherwise:
- Perform genetic operations (selection, crossover, mutation) to produce offspring.
- Add offspring to the population if they meet diversity criteria.
4. Combine the new and existing population and retain the top configurations.
Args:
generation (int): The current generation number.
Updates:
self.population (set): The updated population for the next generation.
Logs:
- Diversity metrics, threshold adjustments, and injection of new diversity.
- Selection, mutation, and crossover operations for genetic diversity.
- Rejections due to low diversity or duplication.
"""
new_population = set()
# Calculate diversity threshold based on normalized diversity metric
if self.diversity_history:
normalized_diversity = self.diversity_history[-1] / max(
self.diversity_history)
logging.debug(f"""Generation {generation}:
Normalized Diversity is {normalized_diversity:.3f}
""")
# Check if diversity falls below a critical threshold
low_diversity = normalized_diversity < self.diversity_threshold if self.diversity_history else False
if generation % 10 == 0 or (low_diversity and np.random.uniform(0, 1) < 0.25):
# Introduce fresh diversity by generating a new population
logging.debug(f"""Introducing fresh diversity for generation {
generation + 1}.""")
new_population_pool = self.create_new_population(
amount=self.population_size)
# Filter by unique canonical forms, including the existing population
existing_canonical_forms = self.population
for candidate in new_population_pool:
if candidate not in existing_canonical_forms:
new_population.add(candidate)
existing_canonical_forms.add(candidate)
else:
logging.debug("""Candidate rejected due to duplication.""")
else:
# Generate offspring for the current generation
num_children = self.population_size // 4
existing_canonical_forms = {config for config in self.population}
for _ in range(num_children):
parent1, parent2 = self.select_parents()
child = self.crossover(parent1, parent2)
if random.uniform(0, 1) < self.mutation_rate:
child = self.mutate(child)
# Compute canonical forms for diversity check
child_cannonical = self.pad_cannonical_form(child)
parent1_cannonical = parent1
parent2_cannonical = parent2
# Calculate normalized Hamming distances to parents
dis_parent1 = self.hamming_distance(
child_cannonical, parent1_cannonical)
dis_parent2 = self.hamming_distance(
child_cannonical, parent2_cannonical)
avg_dis = (dis_parent1 + dis_parent2) / 2
logging.debug(f"""Child avg_dis: {avg_dis:.3f}, dis_parent1: {
dis_parent1:.3f}, dis_parent2: {dis_parent2:.3f}.""")
# Add child to the new population if diversity criteria are met
if avg_dis > self.diversity_threshold and child_cannonical not in existing_canonical_forms:
new_population.add(child_cannonical)
existing_canonical_forms.add(child_cannonical)
else:
logging.debug(
"""Child rejected due to low diversity or duplication.""")
# Combine new and existing population, then filter based on fitness
combined = list(new_population) + list(self.population)
combined = [(config, self.evaluate(config)['normalized_fitness_score'])
for config in combined]
combined.sort(key=lambda x: x[1], reverse=True)
# Retain the top configurations to form the new population
self.population = set(
[config for config, _ in combined[:self.population_size]])
logging.debug(f"""Generation {generation}: Population updated with {
len(self.population)} configurations.""")
def select_parents(self):
"""
main parent selection integrating all selection strategies.
Args:
generation (int): Current generation number.
Returns:
tuple: Two parent configurations for crossover.
"""
corrected_scores = self.calculate_corrected_scores()
selection_methods = [
self.select_parents_normalized_probability,
self.select_parents_tournament,
self.select_parents_rank_based
]
selected_method = random.choices(selection_methods, weights=[
0.5, 0.25, 0.25], k=1)[0]
parents = selected_method(corrected_scores, num_parents=2)
return parents
def crossover(self, parent1, parent2):
"""
Create child configurations using one of three crossover methods:
1. Basic crossover (30% chance):
- Alternates cells from parents
- Simple but effective mixing strategy
2. Simple crossover (30% chance):
- Alternates rows from parents
- Preserves horizontal patterns
3. Complex crossover (40% chance):
- Selects blocks based on living cell density
- Intelligently combines high-fitness regions
Args:
parent1 (tuple[int]): First parent configuration
parent2 (tuple[int]): Second parent configuration
Returns:
tuple[int]: Child configuration created through crossover
"""
crossover_methods = [self.crossover_basic,
self.crossover_simple, self.crossover_complex]
crossover_probabilities = [0.3, 0.3, 0.4]
selected_crossover_method = random.choices(
crossover_methods, weights=crossover_probabilities, k=1)[0]
child_configuration = selected_crossover_method(parent1, parent2)
logging.debug("""Crossover created child configuration: {}""".format(
child_configuration))
return child_configuration
def mutate(self, configuration):
"""
Apply one of three mutation strategies to a configuration. When diversity is low,
the probabilities of harsher mutations increase to encourage exploration and
prevent premature convergence.
Mutation Strategies:
1. Basic mutation:
- Random cell flips with mutation rate probability.
- Uniform distribution of changes.
2. Cluster mutation:
- Flips cells in random 3x3 neighborhoods.
- Creates localized pattern changes.
3. Harsh mutation:
- Flips large contiguous blocks of cells.
- Enables major pattern alterations.
Args:
configuration (tuple[int]): The configuration to mutate.
Returns:
tuple[int]: The mutated configuration.
"""
# Adjust mutation probabilities based on diversity
if self.diversity_history:
normalized_diversity = self.diversity_history[-1] / max(
self.diversity_history)
else:
normalized_diversity = 1.0 # Assume high diversity for the first generation
if normalized_diversity < self.diversity_threshold:
# Low diversity: favor harsh mutations
mutation_probabilities = [0.2, 0.3, 0.5]
elif normalized_diversity < 10 * self.diversity_threshold:
# Moderate diversity: balance mutation strategies
mutation_probabilities = [0.3, 0.4, 0.3]
else:
# High diversity: favor basic and cluster mutations
mutation_probabilities = [0.4, 0.4, 0.2]
mutation_methods = [self.mutate_basic,
self.mutate_clusters, self.mutate_harsh]
selected_mutation_method = random.choices(
mutation_methods, weights=mutation_probabilities, k=1)[0]
mutated_configuration = selected_mutation_method(configuration)
logging.debug(f"""Applied mutation strategy: {selected_mutation_method.__name__} with probabilities
(basic: {mutation_probabilities[0]:.2f}, cluster: {mutation_probabilities[1]:.2f}, harsh: {mutation_probabilities[2]:.2f}).""")
return mutated_configuration
def crossover_basic(self, parent1, parent2):
"""
Perform basic crossover by alternating cells from each parent.
Args:
parent1 (tuple[int]): First parent configuration.
parent2 (tuple[int]): Second parent configuration.
Returns:
tuple[int]: Child configuration.
"""
N = self.grid_size
total_cells = N * N
child = []
for i in range(total_cells):
child.append(parent1[i] if i % 2 == 0 else parent2[i])
child_configuration = tuple(child)
# Assert child size
assert len(child_configuration) == total_cells, """Child size mismatch in basic crossover: expected {}, got {}""".format(
total_cells, len(child_configuration))
logging.debug("""Crossover applied (Basic): {}""".format(
child_configuration))
return child_configuration
def crossover_simple(self, parent1, parent2):
"""
Perform simple crossover by alternating entire rows from each parent.
Args:
parent1 (tuple[int]): First parent configuration.
parent2 (tuple[int]): Second parent configuration.
Returns:
tuple[int]: Child configuration.
Raises:
ValueError: If parent configurations do not match the expected grid size.
"""
N = self.grid_size
total_cells = N * N
if len(parent1) != total_cells or len(parent2) != total_cells:
logging.error("""Parent configurations must be {}., but got sizes: {} and {}""".format(
total_cells, len(parent1), len(parent2)))
raise ValueError("""Parent configurations must be {}, but got sizes: {} and {}""".format(
total_cells, len(parent1), len(parent2)))
blocks_parent1 = [parent1[i * N: (i + 1) * N] for i in range(N)]
blocks_parent2 = [parent2[i * N: (i + 1) * N] for i in range(N)]
child_blocks = []
for i in range(N):
if i % 2 == 0:
child_blocks.extend(blocks_parent2[i])
else:
child_blocks.extend(blocks_parent1[i])
child_configuration = tuple(child_blocks)
# Assert child size
assert len(child_configuration) == total_cells, """Child size mismatch in simple crossover: expected {}, got {}""".format(
total_cells, len(child_configuration))
logging.debug("""Crossover applied (Simple): {}""".format(
child_configuration))
return child_configuration
def crossover_complex(self, parent1, parent2):
"""
Perform complex crossover by selecting blocks based on living cell density.
Args:
parent1 (tuple[int]): First parent configuration.
parent2 (tuple[int]): Second parent configuration.
Returns:
tuple[int]: Child configuration.
Raises:
ValueError: If parent configurations do not match the expected grid size.
"""
N = self.grid_size
total_cells = N * N
number_of_blocks = N # Each block is a row
block_size = N
remainder = 0 # Since block_size * number_of_blocks == total_cells
if len(parent1) != total_cells or len(parent2) != total_cells:
logging.error("""Parent configurations must be {}., but got sizes: {} and {}""".format(
total_cells, len(parent1), len(parent2)))
raise ValueError("""Parent configurations must be {}, but got sizes: {} and {}""".format(
total_cells, len(parent1), len(parent2)))
blocks_parent1 = [
parent1[i * block_size:(i + 1) * block_size] for i in range(number_of_blocks)]
blocks_parent2 = [
parent2[i * block_size:(i + 1) * block_size] for i in range(number_of_blocks)]
block_alive_counts_parent1 = [sum(block) for block in blocks_parent1]
block_alive_counts_parent2 = [sum(block) for block in blocks_parent2]
max_alive_cells_parent1 = sum(block_alive_counts_parent1)
max_alive_cells_parent2 = sum(block_alive_counts_parent2)
if max_alive_cells_parent1 > 0:
probabilities_parent1 = [(alive_count / max_alive_cells_parent1) if alive_count > 0 else (1 / number_of_blocks)
for alive_count in block_alive_counts_parent1]
else:
probabilities_parent1 = [1 / number_of_blocks] * number_of_blocks
if max_alive_cells_parent2 > 0:
probabilities_parent2 = [(alive_count / max_alive_cells_parent2) if alive_count > 0 else (1 / number_of_blocks)
for alive_count in block_alive_counts_parent2]
else:
probabilities_parent2 = [1 / number_of_blocks] * number_of_blocks
# Ensure probabilities sum to 1
sum_prob_parent1 = sum(probabilities_parent1)
probabilities_parent1 = [
p / sum_prob_parent1 for p in probabilities_parent1]
sum_prob_parent2 = sum(probabilities_parent2)
probabilities_parent2 = [
p / sum_prob_parent2 for p in probabilities_parent2]
selected_blocks_parent1 = random.choices(
range(number_of_blocks), weights=probabilities_parent1, k=(number_of_blocks // 2) + remainder)
remaining_blocks_parent2 = [i for i in range(
number_of_blocks) if i not in selected_blocks_parent1]
selected_blocks_parent2 = random.choices(
remaining_blocks_parent2,
weights=[probabilities_parent2[i]
for i in remaining_blocks_parent2],
k=number_of_blocks // 2
)
child_blocks = []
for i in range(number_of_blocks):
if i in selected_blocks_parent1:
child_blocks.extend(blocks_parent1[i])
elif i in selected_blocks_parent2:
child_blocks.extend(blocks_parent2[i])
else:
# This case should not occur, but handle it just in case
selected_parent = random.choice([1, 2])
if selected_parent == 1:
child_blocks.extend(blocks_parent1[i])
else:
child_blocks.extend(blocks_parent2[i])
child_configuration = tuple(child_blocks)
# Assert child size
assert len(child_configuration) == total_cells, """Child size mismatch in complex crossover: expected {}, got {}""".format(
total_cells, len(child_configuration))
logging.debug("""Crossover applied (Complex): {}""".format(
child_configuration))
return child_configuration
def mutate_basic(self, configuration):
"""
Perform basic mutation by flipping cells with a probability proportional to the mutation rate.
Args:
configuration (tuple[int]): Configuration to mutate.
Returns:
tuple[int]: Mutated configuration.
"""
new_configuration = list(configuration)
for i in range(len(configuration)):
if random.uniform(0, 1) < min(0.5, self.mutation_rate * 5):
new_configuration[i] = 0 if configuration[i] else 1
mutated_configuration = tuple(new_configuration)
# Assert child size
expected_size = self.total_cells
assert len(mutated_configuration) == expected_size, """Mutated configuration size mismatch: expected {}, got {}""".format(
expected_size, len(mutated_configuration))
logging.debug("""Mutation applied (Basic): {}""".format(
mutated_configuration))
return mutated_configuration
def mutate_harsh(self, configuration):
"""
Perform harsh mutation by flipping large contiguous blocks of cells.
Args:
configuration (tuple[int]): Configuration to mutate.
Returns:
tuple[int]: Mutated configuration.
"""
new_configuration = list(configuration)
cluster_size = random.randint(1, len(new_configuration))
start = random.randint(0, len(new_configuration) - 1)
value = random.randint(0, 1)
for j in range(cluster_size):
idx = (start + j) % len(new_configuration)
new_configuration[idx] = value
mutated_configuration = tuple(new_configuration)
# Assert child size
expected_size = self.total_cells
assert len(mutated_configuration) == expected_size, """Mutated configuration size mismatch: expected {}, got {}""".format(
expected_size, len(mutated_configuration))
logging.debug("""Mutation applied (Harsh): {}""".format(
mutated_configuration))
return mutated_configuration
def mutate_clusters(self, configuration):
"""
Perform cluster mutation by flipping cells in random neighborhoods.
Args:
configuration (tuple[int]): Configuration to mutate.
mutation_rate (float): Probability of mutating a cluster.
cluster_size (int): Size of the cluster to mutate.
Returns:
tuple[int]: Mutated configuration.
"""
N = self.grid_size
mutated = list(configuration)
for _ in range(random.randint(1, N)):
if random.uniform(0, 1) < self.mutation_rate:
center_row = random.randint(0, N - 1)
center_col = random.randint(0, N - 1)
for i in range(-1, 2):
for j in range(-1, 2):
row = (center_row + i) % N
col = (center_col + j) % N
index = row * N + col
mutated[index] = 1 if mutated[index] == 0 else 0
mutated_configuration = tuple(mutated)
# Assert child size
expected_size = self.total_cells
assert len(mutated_configuration) == expected_size, """Mutated configuration size mismatch: expected {}, got {}""".format(
expected_size, len(mutated_configuration))
logging.debug("""Mutation applied (Cluster): {}""".format(
mutated_configuration))
return mutated_configuration
def select_parents_normalized_probability(self, corrected_scores, num_parents=2):
"""
Select parents using Normalized Probability (Roulette Wheel) Selection.
Args:
corrected_scores (list[tuple[int, float]]): List of tuples containing configurations and their corrected scores.
num_parents (int): Number of parents to select.
Returns:
list[tuple[int]]: Selected parent configurations.
"""
configs, scores = zip(*corrected_scores)
total_score = sum(scores)
if total_score == 0:
logging.debug("""Total score is 0, selecting parents randomly.""")
return random.choices(configs, k=num_parents)
probabilities = [score / total_score for score in scores]
selected_parents = random.choices(
configs, weights=probabilities, k=num_parents)
logging.debug(
"""Selected parents (Normalized Probability): {}""".format(selected_parents))
return selected_parents
def select_parents_tournament(self, corrected_scores, tournament_size=3, num_parents=2):
"""
Select parents using Tournament Selection.
Args:
corrected_scores (list[tuple[int, float]]): List of tuples containing configurations and their corrected scores.
tournament_size (int): Number of individuals competing in each tournament.
num_parents (int): Number of parents to select.
Returns:
list[tuple[int]]: Selected parent configurations.
"""
selected_parents = []
population_size = len(corrected_scores)
for _ in range(num_parents):
# Randomly select tournament_size individuals
tournament = random.sample(corrected_scores, k=min(
tournament_size, population_size))
parent = max(tournament, key=lambda x: x[1])[0]
selected_parents.append(parent)
logging.debug(
"""Selected parent from tournament: {}""".format(parent))
logging.debug(
"""Selected parents (Tournament): {}""".format(selected_parents))
return selected_parents
def select_parents_rank_based(self, corrected_scores, num_parents=2):
"""
Select parents using Rank-Based Selection.
Args:
corrected_scores (list[tuple[int, float]]): List of tuples containing configurations and their corrected scores.
num_parents (int): Number of parents to select.
Returns:
list[tuple[int]]: Selected parent configurations.
"""
sorted_scores = sorted(
corrected_scores, key=lambda x: x[1], reverse=True)
ranks = range(1, len(sorted_scores) + 1)
total_rank = sum(ranks)
probabilities = [rank / total_rank for rank in ranks]
selected_parents = random.choices(
[config for config, _ in sorted_scores],
weights=probabilities,
k=num_parents
)
logging.debug(
"""Selected parents (Rank-Based): {}""".format(selected_parents))
return selected_parents
def pad_cannonical_form(self, config):
canonical = self.get_canonical_form(config)
total_size = self.total_cells
current_size = len(canonical)
return canonical + (0,) * (total_size - current_size)
def get_canonical_form(self, config):
"""
Compute the canonical form of a configuration by normalizing its position and rotation.
Args:
config (tuple[int]): Flattened 1D representation of the grid or a sub-grid.
Returns:
tuple[int]: Canonical form of the configuration.
"""
if config in self.canonical_forms_cache:
logging.debug("""Configuration found in cache.""")
return self.canonical_forms_cache[config]
grid_size = int(np.sqrt(len(config)))
grid = np.array(config).reshape(grid_size, grid_size)