Skip to content

Commit 57ad2f4

Browse files
committed
FIXED GA: Handled Assertion Errors gracefully
1 parent 2454f7b commit 57ad2f4

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

search/genetic_algorithm.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def run(self, trials: int, verbose: bool = False, writer: Optional[SummaryWriter
7575
print(f"Using {self.n_jobs} parallel workers")
7676

7777
all_params = self._initialize_population(self.populationSize) # Initial Population
78+
assert len(all_params) == self.populationSize, "Initial population size should match the defined population size"
7879
results = []
7980
evals_done = 0 # To control the budget with `trials`
8081

@@ -177,8 +178,14 @@ def evaluate_population(evals_done, params, eval_cache):
177178
num_parents=len(all_params) // 2 # Elitism: shortlisting top 50%, also suggested by the paper
178179
)
179180
# A brief Checking
180-
assert len(elites) < len(all_params), "Chosen Elites should be fewer than the total population"
181-
assert len(elites) < self.populationSize, "Number of selected Elites should be less than the population size"
181+
if len(elites) > len(all_params):
182+
if verbose:
183+
print("Number of Elites exceeded total population; trimming elites.")
184+
elites = elites[:len(all_params)]
185+
if len(elites) > self.populationSize:
186+
if verbose:
187+
print("Number of Elites exceeded population size; trimming elites.")
188+
elites = elites[:self.populationSize]
182189

183190
# 2. Crossover
184191
offspring: List[Dict[str, Any]] = [] # They are evolved children
@@ -216,7 +223,10 @@ def evaluate_population(evals_done, params, eval_cache):
216223
mutated_child = self._mutate(child)
217224
mutated_offspring.append(mutated_child)
218225
# A Brief Checking
219-
assert len(mutated_offspring) == len(offspring), "Number of Mutated Offsprings should match the Offsprings"
226+
if len(mutated_offspring) != len(offspring):
227+
if verbose:
228+
print("Number of Mutated Offsprings does not match the Offsprings; adjusting accordingly.")
229+
mutated_offspring = random.sample(mutated_offspring, len(offspring))
220230

221231
# Replacing the population with new generation: elites + mutated_offspring
222232
new_generation = elites + mutated_offspring
@@ -228,7 +238,10 @@ def evaluate_population(evals_done, params, eval_cache):
228238
if len(new_generation) > self.populationSize:
229239
new_generation = new_generation[:self.populationSize]
230240
all_params = new_generation
231-
assert len(all_params) == self.populationSize, "New generation population size should match the defined population size"
241+
if len(all_params) != self.populationSize:
242+
if verbose:
243+
print("Adjusting new generation to match population size.")
244+
all_params = random.sample(all_params, self.populationSize)
232245

233246
if verbose:
234247
print(f"Generation {gen+1} completed. Current Population size: {len(all_params)}")

0 commit comments

Comments
 (0)