- Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path1_solve_simple.py
70 lines (51 loc) · 1.78 KB
/
1_solve_simple.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
fromsolver.Duelist_AlgorithmimportDuelistAlgorithm
'''
Example 1
Solving single variable optimization problem
'''
'''
Here we have a simple squared equation that we want to optimize (minimize).
The equation is y=(x1)^2+2
x1 is bounded from -2 to 10 (-2 is min value of x1 and 10 is max value of x1)
'''
'''
First we define the function:
The equation is f=(x1,x2) = y = (x1)^2+(x2)^2
'''
deff(x1,x2):
returnx1*x1+2
'''
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"]
'''
The thirds step is to specify the boundary for x1
x1 is bounded from -2 to 10 (-2 is min value of x1 and 10 is max value of x1)
'''
xmin=[-2]
xmax=[10]
'''
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=100)
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.
'''