forked from PasaOpasen/geneticalgorithm2-6.8.7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.py
46 lines (36 loc) · 1.07 KB
/
callbacks.py
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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 31 19:54:11 2020
@author: qtckp
"""
import sys
sys.path.append('..')
import numpy as np
from geneticalgorithm2 import geneticalgorithm2 as ga
from geneticalgorithm2 import Callbacks, MiddleCallbacks, ActionConditions
dim = 16
def f(X):
pen=0
if np.sum(X) < 1:
pen=500+10*(1-np.sum(X))
return np.sum(X)+pen
varbound=np.array([[0,10]]*dim)
model=ga(function=f,
dimension=dim,
variable_type='real',
variable_boundaries=varbound,
algorithm_parameters={
'max_num_iteration': 2000
})
model.run(
callbacks=[
Callbacks.SavePopulation('./output/callback/pop_example', save_gen_step=500, file_prefix='constraints'),
Callbacks.PlotOptimizationProcess('./output/callback/plot_example', save_gen_step=300, show = False, main_color='red', file_prefix='plot')
]
)
# doing nothing, just for test
model.run(
middle_callbacks=[
MiddleCallbacks.UniversalCallback(lambda data: data, ActionConditions.EachGen(generation_step=1))
]
)