Skip to content

Commit

Permalink
added callback example
Browse files Browse the repository at this point in the history
  • Loading branch information
zStupan committed Oct 23, 2023
1 parent 67fd6b2 commit ea82514
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/callback.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from niapy.callbacks import Callback
from niapy.algorithms.basic import BatAlgorithm
from niapy.task import Task
# This is temporary fix to import module from parent folder
# It will be removed when package is published on PyPI
import sys

sys.path.append('../')
# End of fix


class PrintMeanFitness(Callback):
def __init__(self):
super().__init__()

def after_iteration(self, population, fitness, best_x, best_fitness, **params):
print(fitness.mean())


ba = BatAlgorithm(callbacks=[PrintMeanFitness()])
griewank = Task('griewank', max_evals=1000)

ba.run(griewank)

0 comments on commit ea82514

Please sign in to comment.