- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path3_solve_stochastic.py
78 lines (57 loc) · 2.14 KB
/
3_solve_stochastic.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
fromsolver.Duelist_AlgorithmimportDuelistAlgorithm
importrandom
'''
EXAMPLE 3
For stochastic problems, we demonstrate this by adding probability distributions or random noises to the equation.
Random Uniform Noise is added to the previous example
'''
'''
Here we have a simple squared equation that we want to optimize (minimize).
The equation is y=(x1)^2+(x2)^2+ RandomUniformNoise(0,20)
x1 is bounded from -2 to 10 (-2 is min value of x1 and 10 is max value of x1)
x2 is bounded from 10 to 15 (10 is min value of x2 and 15 is max value of x2)
'''
'''
First we define the function:
The equation is f=(x1,x2) = y = (x1)^2+(x2)^2 + RandomUniformNoise(0,20)
'''
deff(x1,x2):
returnx1*x1+x2*x2+random.uniform(0,20)
'''
Secondly, we name the optimization variables and tell the solver how many variables to optimize.
We can do this by putting in the names of the variables as strings in an array.
'''
x=["x1","x2"]
'''
The thirds step is to specify the boundary for each variables
"x1" "x2"
min -2 5
max 10 15
This xmin and xmax array follows the position of the array you define at second step.
'''
xmin=[-2,5]
xmax=[10,15]
'''
DA.DuelistAlgorithm(function output=f,
names of manipulated variables= x,
lower limit of manipulated variables= xmin,
upper limit of manipulated variables = xmax,
population size=pop (default=200),
luck factor=luck (default=0.01),
innovative/mutation factor= mut (default=0.1),
learning probability= learn (default=0.8),
maximum generation= max_gen (default=500),
number of champions per selection=nc (default=5),
is shuffling required before duel?=shuffle (default=False)
)
You do not need to fill in variables with default values. They are for fine tuning the algorithm.
'''
#Increase the maximum generation for more precise answer
DA=DuelistAlgorithm(f,x,xmin,xmax,max_gen=1000)
DA.solve()
'''
You should see the answer, it should look something like the following:
Optimized using Duelist Algorithm. Answer is: [1.70085713e-03 5.00016813e+00] with fitness of 25.00168420123805
The analytical answer is [0,5] with fitness of 25.00
Duelist Algorithm performs well.
'''