Skip to content

Commit

Permalink
there may be no best model at the end of iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Mazur committed Sep 22, 2023
1 parent f29c7cf commit a3501c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
8 changes: 3 additions & 5 deletions src/darwin/algorithms/GA.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def run_ga(model_template: Template) -> ModelRun:
return GlobalVars.best_run

generations_no_change = 0
overall_best_fitness = options.crash_value

# Begin evolution
while runner.run_generation():
Expand All @@ -122,22 +121,21 @@ def run_ga(model_template: Template) -> ModelRun:
runner.run_downhill(population)

best_run = population.get_best_run()

best_fitness = best_run.result.fitness

log.message(f"Current generation best genome = {best_run.model.model_code.FullBinCode},"
f" best fitness = {best_fitness:.4f}")

best_run_overall = GlobalVars.best_run
best_run_overall = GlobalVars.best_run or best_run
overall_best_fitness = best_run_overall.result.fitness

log.message(f"Best overall fitness = {best_run_overall.result.fitness:4f},"
f" iteration {best_run_overall.generation}, model {best_run_overall.model_num}")

if best_fitness < overall_best_fitness:
generations_no_change = 0
log.message(f"Better fitness found, generation = {runner.generation},"
f" new best fitness = {best_fitness:.4f}")
overall_best_fitness = best_fitness
generations_no_change = 0
else:
generations_no_change += 1
log.message(f"No change in fitness for {generations_no_change} generations,"
Expand Down
12 changes: 7 additions & 5 deletions src/darwin/algorithms/OPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,20 @@ def run_skopt(model_template: Template) -> ModelRun:

log.message(f"Done telling")

best_fitness = heapq.nsmallest(1, fitnesses)[0]
best_run = population.get_best_run()

best_run = GlobalVars.best_run
best_fitness = best_run.result.fitness

if best_fitness < best_run.result.fitness:
best_run_overall = GlobalVars.best_run or best_run

if best_fitness < best_run_overall.result.fitness:
niter_no_change = 0
else:
niter_no_change += 1

log.message(f"Best fitness this iteration = {best_fitness:4f} at {time.asctime()}")
log.message(f"Best overall fitness = {best_run.result.fitness:4f},"
f" iteration {best_run.generation}, model {best_run.model_num}")
log.message(f"Best overall fitness = {best_run_overall.result.fitness:4f},"
f" iteration {best_run_overall.generation}, model {best_run_overall.model_num}")

if options.final_downhill_search and keep_going():
log.message(f"Starting final downhill")
Expand Down

0 comments on commit a3501c6

Please sign in to comment.