-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
127 lines (90 loc) · 2.56 KB
/
main.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#This is an example on how to use the optimizers. The data
#are fitted to an exponential model, which can be seen in
#the mycostfunction.py file. The answers should be close to
#1 and 0.5.
#
#Run this by opening python (version 2.7.x) and typing:
#
# execfile("main.py:)"
#
import libUnfitPython
import mycostfunction
guess = libUnfitPython.std_vector_double()
guess[:] = [0,0]
lower_bounds = libUnfitPython.std_vector_double()
upper_bounds = libUnfitPython.std_vector_double()
lower_bounds[:] = [-10, -10]
upper_bounds[:] = [10, 10]
t_data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_data = [1.0, 0.6065, 0.3679, 0.2231, 0.1353, 0.0821, 0.0498, 0.0302, 0.0183, 0.0111, 0.0067]
cf = mycostfunction.MyCostFunction(t_data,y_data)
##############################################
print("LevenbergMarquardt:")
guess[:] = [0,0]
lm = libUnfitPython.LevenbergMarquardt()
rc = lm.FindMin(cf, guess)
if rc == 0:
for x in guess:
print(x)
else:
print('Error encountered. Error code: ' + str(rc))
print('')
##############################################
print("NelderMead:")
guess[:] = [0,0]
nm = libUnfitPython.NelderMead()
rc = nm.FindMin(cf, guess)
if rc == 0:
for x in guess:
print(x)
else:
print('Error encountered. Error code: ' + str(rc))
print('')
##############################################
print("DifferentialEvolution:")
guess[:] = [0,0]
de = libUnfitPython.DifferentialEvolution()
de.bounds.SetBounds(lower_bounds, upper_bounds)
rc = de.FindMin(cf, guess)
if rc == 0:
for x in guess:
print(x)
else:
print('Error encountered. Error code: ' + str(rc))
print('')
##############################################
print("GeneticAlgorithm:")
guess[:] = [0,0]
ga = libUnfitPython.GeneticAlgorithm()
ga.bounds.SetBounds(lower_bounds, upper_bounds)
rc = ga.FindMin(cf, guess)
if rc == 0:
for x in guess:
print(x)
else:
print('Error encountered. Error code: ' + str(rc))
print('')
##############################################
print("ParticleSwarm:")
guess[:] = [0,0]
ps = libUnfitPython.ParticleSwarm()
ps.bounds.SetBounds(lower_bounds, upper_bounds)
rc = ps.FindMin(cf, guess)
if rc == 0:
for x in guess:
print(x)
else:
print('Error encountered. Error code: ' + str(rc))
print('')
##############################################
print("SimulatedAnnealing:")
guess[:] = [0,0]
sa = libUnfitPython.SimulatedAnnealing()
sa.bounds.SetBounds(lower_bounds, upper_bounds)
rc = sa.FindMin(cf, guess)
if rc == 0:
for x in guess:
print(x)
else:
print('Error encountered. Error code: ' + str(rc))
print('')