This repository has been archived by the owner on Apr 13, 2024. It is now read-only.
forked from rmsolgi/geneticalgorithm
-
Notifications
You must be signed in to change notification settings - Fork 14
/
optimization_test_functions.py
78 lines (61 loc) · 2.04 KB
/
optimization_test_functions.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 19 16:25:17 2020
@author: qtckp
"""
import sys
sys.path.append('..')
import numpy as np
from geneticalgorithm2 import geneticalgorithm2 as ga
from OptimizationTestFunctions import Sphere, Ackley, AckleyTest, Rosenbrock, Fletcher, Griewank, Penalty2, Quartic, Rastrigin, SchwefelDouble, SchwefelMax, SchwefelAbs, SchwefelSin, Stairs, Abs, Michalewicz, Scheffer, Eggholder, Weierstrass
dim = 2
functions = [
Sphere(dim, degree = 2),
Ackley(dim),
AckleyTest(dim),
Rosenbrock(dim),
Fletcher(dim, seed = 1488),
Griewank(dim),
Penalty2(dim),
Quartic(dim),
Rastrigin(dim),
SchwefelDouble(dim),
SchwefelMax(dim),
SchwefelAbs(dim),
SchwefelSin(dim),
Stairs(dim),
Abs(dim),
Michalewicz(),
Scheffer(dim),
Eggholder(dim),
Weierstrass(dim)
]
for f in functions:
xmin, xmax, ymin, ymax = f.bounds
varbound = np.array([[xmin, xmax], [ymin, ymax]])
model = ga(function=f,
dimension = dim,
variable_type='real',
variable_boundaries=varbound,
algorithm_parameters = {
'max_num_iteration': 500,
'population_size': 100,
'mutation_probability': 0.1,
'elit_ratio': 0.01,
'crossover_probability': 0.5,
'parents_portion': 0.3,
'crossover_type':'uniform',
'mutation_type': 'uniform_by_center',
'selection_type': 'roulette',
'max_iteration_without_improv':100
}
)
model.run(no_plot = True,
stop_when_reached = (f.f_best + 1e-5/(xmax - xmin)) if f.f_best is not None else None
)
title = f"Optimization process for {type(f).__name__}"
model.plot_results(
title = title,
save_as = f"./output/opt_test_funcs/{title}.png",
main_color = 'green'
)