Skip to content

Commit

Permalink
Add example for finding the first population of gbest
Browse files Browse the repository at this point in the history
  • Loading branch information
thieu1995 committed Nov 24, 2023
1 parent d49ad6e commit b403561
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions examples/utils/run_g_best.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/env python
# Created by "Thieu" at 16:09, 24/11/2023 ----------%
# Email: [email protected] %
# Github: https://github.com/thieu1995 %
# --------------------------------------------------%

from opfunu.cec_based.cec2017 import F52017
from mealpy import FloatVar, BBO


# Find the index of population that contains the global best solution.
def get_first_best_population(opt):
for idx, pop_child in enumerate(opt.history.list_population):
for agent in pop_child:
if agent.target.fitness == opt.g_best.target.fitness:
return idx, pop_child
return None, None

## Define your own problems
f1 = F52017(30, f_bias=0)

p1 = {
"bounds": FloatVar(lb=f1.lb, ub=f1.ub),
"obj_func": f1.evaluate,
"minmax": "min",
"name": "F5",
"log_to": "console",
"save_population": True
}

optimizer = BBO.OriginalBBO(epoch=100, pop_size=30)
optimizer.solve(p1, seed=10) # Set seed for each solved problem

idx, my_first_found_best_pop = get_first_best_population(optimizer)
print(f"The global best solution found at generation: {idx}")
print(my_first_found_best_pop)

0 comments on commit b403561

Please sign in to comment.