root / Final-Parameters_sm_fsn_fss_fsv / pulvinus-optimize.py @ 16
Historique | Voir | Annoter | Télécharger (3,69 ko)
1 |
import os |
---|---|
2 |
import numpy as np |
3 |
from scipy.optimize import minimize |
4 |
outputfile='temporary.csv'
|
5 |
|
6 |
|
7 |
# initial parameters
|
8 |
sm=0.95
|
9 |
fsn=0.5
|
10 |
fss=0.5
|
11 |
fsv=0.5
|
12 |
|
13 |
x0=[sm, fsn, fss, fsv] |
14 |
|
15 |
# parameters obey the following bounds
|
16 |
#bnds = ((0, 1), (0, 1), (0, 1))
|
17 |
|
18 |
#bnds = ((0.5, 1.0), (0.1, 0.9), (0.1, 1.5), (0.1, 0.5))
|
19 |
bnds = ((0.25, 1.0), (0.1, 1.5), (0.1, 1.5), (0.1, 0.75)) |
20 |
|
21 |
# possible target quantities
|
22 |
# --------------------------
|
23 |
theta0=1 #38 |
24 |
# region deformation
|
25 |
am0=1.672631 # Mesophyl |
26 |
an0=1.608788 # Nectary |
27 |
as0=1.790434 # Side |
28 |
av0=1.294615 # Vasculature |
29 |
# strain anisotropy
|
30 |
anim=0.6247 # Mesophyl |
31 |
anin=0.6172 # Nectary |
32 |
anis=0.5489 # Side |
33 |
aniv=0.6958 # Vasculature |
34 |
|
35 |
# experimental standard deviation
|
36 |
stdtheta=0.21 #38 |
37 |
stdam=0.23 # Mesophyl |
38 |
stdan=0.11 # Nectary |
39 |
stdas=0.21 # Side |
40 |
stdav=0.06 # Vasculature |
41 |
#
|
42 |
stdanim=0.0532 # Mesophyl |
43 |
stdanin=0.0378 # Nectary |
44 |
stdanis=0.0741 # Side |
45 |
stdaniv=0.0333 # Vasculature |
46 |
|
47 |
a0=[theta0, am0, an0, as0, av0, anim, anin, anis, aniv] |
48 |
stda=[stdtheta, stdam, stdan, stdas, stdav, stdanim, stdanin, stdanis, stdaniv] |
49 |
|
50 |
# target quantities included in the objective function
|
51 |
var=np.array([1, 2, 3, 4]) # here theta is not included |
52 |
#var=np.array([0, 1, 2, 3, 4]) # here theta (side angle) is included
|
53 |
#var=np.array([0, 1, 2, 3, 4, 5, 6, 7, 8]) # side angle, deformation and anisotropy are included
|
54 |
#var=np.array([5, 6, 7, 8]) # side angle, deformation and anisotropy are included
|
55 |
|
56 |
'''
|
57 |
# objectiv function 1
|
58 |
def f(x):
|
59 |
res=0
|
60 |
for i in range(len(x)):
|
61 |
res+=(x[i]-a0[var[i]])**2/(x[i]+a0[var[i]])**2
|
62 |
return res
|
63 |
'''
|
64 |
|
65 |
# objectiv function taking into account the experimental standard deviation for the weight
|
66 |
def f(x): |
67 |
res=0
|
68 |
for i in range(len(x)): |
69 |
res+=(x[i]-a0[var[i]])**2/(stda[var[i]])**2 |
70 |
return res
|
71 |
|
72 |
|
73 |
|
74 |
def objective(x): |
75 |
[sm, fsn, fss, fsv] = x |
76 |
os.system("FreeFem++ pulvinus4optimize.cpp -fsn "+str(fsn)+" -fss "+str(fss)+" -fsv "+str(fsv)+" -sm "+str(sm)+" -outfile "+outputfile) |
77 |
data=np.loadtxt(outputfile, delimiter=';', dtype=float) |
78 |
xvar=data[var+4]
|
79 |
print(xvar) |
80 |
return f(xvar)
|
81 |
|
82 |
#
|
83 |
# GLOBAL OPTIMIZATION
|
84 |
#
|
85 |
|
86 |
from scipy.optimize import differential_evolution #New in version 0.15.0. |
87 |
|
88 |
res = differential_evolution(objective, bounds=bnds) |
89 |
res |
90 |
|
91 |
|
92 |
'''
|
93 |
|
94 |
###############################################################
|
95 |
###############################################################
|
96 |
# fitting the observed expansions K=lambda+2*mu/3 (d=3 formula)
|
97 |
###############################################################
|
98 |
###############################################################
|
99 |
|
100 |
Ok: Normal End
|
101 |
[1.67263 1.60879 1.79043 1.29461]
|
102 |
Out[2]:
|
103 |
fun: 7.656738339961133e-09
|
104 |
message: 'Optimization terminated successfully.'
|
105 |
nfev: 4745
|
106 |
nit: 78
|
107 |
success: True
|
108 |
x: array([0.54006051, 0.95448193, 1.21647743, 0.50956303])
|
109 |
|
110 |
|
111 |
Results in the results file:
|
112 |
|
113 |
/*
|
114 |
sm;fsn;fss;fsv;sideangle;am;an;as;av;sam;san;sas;sav;
|
115 |
*/
|
116 |
-0.540061;0.954482;1.21648;0.509563;0.477428;1.67263;1.60879;1.79043;1.29461;0.963544;0.973407;0.942094;0.935191
|
117 |
|
118 |
|
119 |
###############################################################
|
120 |
###############################################################
|
121 |
# fitting the observed expansions K=lambda+mu (d=2 formula)
|
122 |
###############################################################
|
123 |
###############################################################
|
124 |
Ok: Normal End
|
125 |
[1.67263 1.60879 1.79043 1.29461]
|
126 |
Out[3]:
|
127 |
fun: 7.656738339961133e-09
|
128 |
message: 'Optimization terminated successfully.'
|
129 |
nfev: 5825
|
130 |
nit: 96
|
131 |
success: True
|
132 |
x: array([0.46445277, 0.95448186, 1.21647529, 0.5095638 ])
|
133 |
|
134 |
|
135 |
Results in the results file:
|
136 |
|
137 |
/*
|
138 |
sm;fsn;fss;fsv;sideangle;am;an;as;av;sam;san;sas;sav;
|
139 |
*/
|
140 |
-0.464453;0.954482;1.21648;0.509564;0.477427;1.67263;1.60879;1.79043;1.29461;0.963544;0.973407;0.942095;0.935191
|
141 |
|
142 |
|
143 |
|
144 |
'''
|