-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubsolv.jl
243 lines (242 loc) · 9.43 KB
/
subsolv.jl
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
########################################################################################################
### GCMMA-MMA-Julia ###
### ###
### This file is part of GCMMA-MMA-Julia. GCMMA-MMA-Julia is licensed under the terms of GNU ###
### General Public License as published by the Free Software Foundation. For more information and ###
### the LICENSE file, see <https://github.com/pollinico/GCMMA-MMA-Julia/blob/main/LICENSE>. ###
### ###
### The orginal work is written by Krister Svanberg in MATLAB. ###
### This is the Julia version of the code written by Nicolò Pollini. ###
### version 18-05-2023 ###
########################################################################################################
#-------------------------------------------------------------
#
# Copyright (C) 2006 Krister Svanberg
#
# This file, subsolv.m, is part of GCMMA-MMA-code.
#
# GCMMA-MMA-code is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of
# the License, or (at your option) any later version.
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# (file COPYING) along with this file. If not, see
# <http://www.gnu.org/licenses/>.
#
# You should have received a file README along with this file,
# containing contact information. If not, see
# <http://www.smoptit.se/> or e-mail [email protected] or [email protected].
#
# Version Dec 2006.
#
#
function subsolv(m,n,epsimin,low,upp,alfa,beta,p0,q0,P,Q,a0,a,b,c,d)
#
# This function subsolv solves the MMA subproblem:
#
# minimize SUM[ p0j/(uppj-xj) + q0j/(xj-lowj) ] + a0*z +
# + SUM[ ci*yi + 0.5*di*(yi)^2 ],
#
# subject to SUM[ pij/(uppj-xj) + qij/(xj-lowj) ] - ai*z - yi <= bi,
# alfaj <= xj <= betaj, yi >= 0, z >= 0.
#
# Input: m, n, low, upp, alfa, beta, p0, q0, P, Q, a0, a, b, c, d.
# Output: xmma,ymma,zmma, slack variables and Lagrange multiplers.
#
een = ones(n)
eem = ones(m)
epsi = 1
epsvecn = epsi*een
epsvecm = epsi*eem
x = 0.5*(alfa+beta)
y = copy(eem)
z = 1
lam = copy(eem)
xsi = een./(x-alfa)
xsi = max.(xsi,een)
eta = een./(beta-x)
eta = max.(eta,een)
mu = max.(eem,0.5*c)
zet = 1
s = copy(eem)
itera = 0
while epsi > epsimin
epsvecn = epsi*een
epsvecm = epsi*eem
ux1 = upp-x
xl1 = x-low
ux2 = ux1.*ux1
xl2 = xl1.*xl1
uxinv1 = een./ux1
xlinv1 = een./xl1
plam = p0 + P'*lam
qlam = q0 + Q'*lam
gvec = P*uxinv1 + Q*xlinv1
dpsidx = plam./ux2 - qlam./xl2
rex = dpsidx - xsi + eta
rey = c + d.*y - mu - lam
rez = a0 - zet - a'*lam
relam = gvec - a*z - y + s - b
rexsi = xsi.*(x-alfa) - epsvecn
reeta = eta.*(beta-x) - epsvecn
remu = mu.*y - epsvecm
rezet = zet*z - epsi
res = lam.*s - epsvecm
residu1 = [rex' rey' rez]'
residu2 = [relam' rexsi' reeta' remu' rezet res']'
residu = [residu1' residu2']'
residunorm = sqrt(residu'*residu)
residumax = maximum(abs.(residu))
ittt = 0;
while (residumax > 0.9*epsi) & (ittt < 200)
ittt=ittt + 1
itera=itera + 1
ux1 = upp-x
xl1 = x-low
ux2 = ux1.*ux1
xl2 = xl1.*xl1
ux3 = ux1.*ux2
xl3 = xl1.*xl2
uxinv1 = een./ux1
xlinv1 = een./xl1
uxinv2 = een./ux2
xlinv2 = een./xl2
plam = p0 + P'*lam
qlam = q0 + Q'*lam
gvec = P*uxinv1 + Q*xlinv1
GG = P*spdiagm(n,n,uxinv2) - Q*spdiagm(n,n,xlinv2)
dpsidx = plam./ux2 - qlam./xl2
delx = dpsidx - epsvecn./(x-alfa) + epsvecn./(beta-x)
dely = c + d.*y - lam - epsvecm./y
delz = a0 - a'*lam - epsi/z
dellam = gvec - a*z - y - b + epsvecm./lam
diagx = plam./ux3 + qlam./xl3
diagx = 2*diagx + xsi./(x-alfa) + eta./(beta-x)
diagxinv = een./diagx
diagy = d + mu./y
diagyinv = eem./diagy
diaglam = s./lam
diaglamyi = diaglam+diagyinv
if m < n
blam = dellam + dely./diagy - GG*(delx./diagx)
bb = [blam' delz]'
Alam = spdiagm(m,m,diaglamyi) + GG*spdiagm(n,n,diagxinv)*GG'
AA = [Alam a;
a' -zet/z ]
solut = AA\bb
dlam = solut[1:m]
dz = solut[m+1]
dx = -delx./diagx - (GG'*dlam)./diagx
else
diaglamyiinv = eem./diaglamyi
dellamyi = dellam + dely./diagy
Axx = spdiagm(n,n,diagx) + GG'*spdiagm(m,m,diaglamyiinv)*GG
azz = zet/z + a'*(a./diaglamyi)
axz = -GG'*(a./diaglamyi)
bx = delx + GG'*(dellamyi./diaglamyi)
bz = delz - a'*(dellamyi./diaglamyi)
AA = [Axx axz;
axz' azz ]
bb = [-bx' -bz]'
solut = AA\bb
dx = solut[1:n]
dz = solut[n+1]
dlam = (GG*dx)./diaglamyi - dz*(a./diaglamyi) + dellamyi./diaglamyi
end
#
dy = -dely./diagy + dlam./diagy
dxsi = -xsi + epsvecn./(x-alfa) - (xsi.*dx)./(x-alfa)
deta = -eta + epsvecn./(beta-x) + (eta.*dx)./(beta-x)
dmu = -mu + epsvecm./y - (mu.*dy)./y
dzet = -zet + epsi/z - zet*dz/z
ds = -s + epsvecm./lam - (s.*dlam)./lam
xx = [ y' z lam' xsi' eta' mu' zet s']'
dxx = [dy' dz dlam' dxsi' deta' dmu' dzet ds']'
#
stepxx = -1.01*dxx./xx
stmxx = maximum(stepxx)
stepalfa = -1.01*dx./(x-alfa)
stmalfa = maximum(stepalfa)
stepbeta = 1.01*dx./(beta-x)
stmbeta = maximum(stepbeta)
stmalbe = max(stmalfa,stmbeta)
stmalbexx = max(stmalbe,stmxx)
stminv = max(stmalbexx,1)
steg = 1/stminv
#
xold = copy(x)
yold = copy(y)
zold = copy(z)
lamold = copy(lam)
xsiold = copy(xsi)
etaold = copy(eta)
muold = copy(mu)
zetold = copy(zet)
sold = copy(s)
#
itto = 0
resinew = 2*residunorm
while (resinew > residunorm) & (itto < 50)
itto = itto+1
x = xold + steg*dx
y = yold + steg*dy
z = zold + steg*dz
lam = lamold + steg*dlam
xsi = xsiold + steg*dxsi
eta = etaold + steg*deta
mu = muold + steg*dmu
zet = zetold + steg*dzet
s = sold + steg*ds
ux1 = upp-x
xl1 = x-low
ux2 = ux1.*ux1
xl2 = xl1.*xl1
uxinv1 = een./ux1
xlinv1 = een./xl1
plam = p0 + P'*lam
qlam = q0 + Q'*lam
gvec = P*uxinv1 + Q*xlinv1
dpsidx = plam./ux2 - qlam./xl2
rex = dpsidx - xsi + eta
rey = c + d.*y - mu - lam
rez = a0 - zet - a'*lam
relam = gvec - a*z - y + s - b
rexsi = xsi.*(x-alfa) - epsvecn
reeta = eta.*(beta-x) - epsvecn
remu = mu.*y - epsvecm
rezet = zet*z - epsi
res = lam.*s - epsvecm
residu1 = [rex' rey' rez]'
residu2 = [relam' rexsi' reeta' remu' rezet res']'
residu = [residu1' residu2']'
resinew = sqrt(residu'*residu)
steg = steg/2
end
residunorm = copy(resinew)
residumax = maximum(abs.(residu))
steg = 2*steg
end
if ittt > 198
println("epsi: ", epsi)
println("ittt: ", ittt)
end
epsi = 0.1*epsi
end
xmma = copy(x)
ymma = copy(y)
zmma = copy(z)
lamma = copy(lam)
xsimma = copy(xsi)
etamma = copy(eta)
mumma = copy(mu)
zetmma = copy(zet)
smma = copy(s)
#-------------------------------------------------------------
return xmma,ymma,zmma,lamma,xsimma,etamma,mumma,zetmma,smma
end