0001 function [x, lambda, converged] = LPconstr(FUN,x,mpopt,step0,VLB,VUB,GRADFUN,LPEQUSVR, ...
0002 P1,P2,P3,P4,P5,P6,P7,P8,P9,P10,P11,P12,P13,P14,P15)
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 if nargin < 8, error('\ LPconstr needs more arguments ! \ '); end
0072
0073
0074 nvars = length(x);
0075 nequ = mpopt(15);
0076
0077
0078 if ~any(FUN<48)
0079 etype = 1;
0080 evalstr = [FUN,];
0081 evalstr=[evalstr, '(x'];
0082 for i=1:nargin - 8
0083 etype = 2;
0084 evalstr = [evalstr,',P',int2str(i)];
0085 end
0086 evalstr = [evalstr, ')'];
0087 else
0088 etype = 3;
0089 evalstr=[FUN,'; g=g(:);'];
0090 end
0091
0092
0093 if ~any(GRADFUN<48)
0094 gtype = 1;
0095 evalstr2 = [GRADFUN,'(x'];
0096 for i=1:nargin - 8
0097 gtype = 2;
0098 evalstr2 = [evalstr2,',P',int2str(i)];
0099 end
0100 evalstr2 = [evalstr2, ')'];
0101 else
0102 gtype = 3;
0103 evalstr2=[GRADFUN,';'];
0104 end
0105
0106
0107 if ~any(LPEQUSVR<48)
0108 lpeqtype = 1;
0109 evalstr3 = [LPEQUSVR,'(x'];
0110 for i=1:nargin - 8
0111 lpeqtype = 2;
0112 evalstr3 = [evalstr3,',P',int2str(i)];
0113 end
0114 evalstr3 = [evalstr3, ')'];
0115 else
0116 lpeqtype = 3;
0117 evalstr3=[LPEQUSVR,';'];
0118 end
0119
0120
0121 verbose = mpopt(31);
0122 itcounter = 0;
0123 runcounter = 1;
0124
0125 stepsize = step0 * 0.02;
0126
0127
0128
0129
0130 f_best = 9.9e15;
0131 f_best_run = 9.9e15;
0132 max_slackvar_last = 9.9e15;
0133 converged = 0;
0134
0135 if verbose
0136 fprintf(' it obj function max violation max slack var norm grad norm dx\n');
0137 fprintf('---- ------------- ------------- ------------- ------------- -------------\n');
0138 end
0139 while (converged == 0) && (itcounter < mpopt(22)) && (runcounter < mpopt(23))
0140
0141 itcounter = itcounter + 1;
0142 if verbose, fprintf('%3d ', itcounter); end
0143
0144
0145
0146 if lpeqtype == 1,
0147 [x, success_lf] = feval(LPEQUSVR,x);
0148 elseif lpeqtype == 2
0149 [x, success_lf] = eval(evalstr3);
0150 else
0151 eval(evalstr3);
0152 end
0153
0154
0155 if success_lf == 0;
0156 fprintf('\n Load flow did not converge. LPconstr restarted with reduced stepsize! ');
0157 x = xbackup;
0158 stepsize = 0.7*stepsize;
0159 end
0160
0161
0162
0163
0164 if etype == 1,
0165 [f, g] = feval(FUN,x);
0166 elseif etype == 2
0167 [f, g] = eval(evalstr);
0168 else
0169 eval(evalstr);
0170 end
0171 if gtype == 1
0172 [df_dx, dg_dx] = feval(GRADFUN, x);
0173 elseif gtype == 2
0174 [df_dx, dg_dx] = eval(evalstr2);
0175 else
0176 eval(evalstr2);
0177 end
0178 dg_dx = dg_dx';
0179 max_g = max(g);
0180
0181 if verbose, fprintf(' %-12.6g %-12.6g', f, max_g); end
0182
0183
0184
0185
0186 a_lp = dg_dx; f_lp = df_dx; rhs_lp = -g; vubdx = stepsize; vlbdx = -stepsize;
0187 if isempty(VUB) ~= 1 || isempty(VLB) ~= 1
0188 error('sorry, at this stage LPconstr can not solve a problem with VLB or VUB ');
0189 end
0190
0191
0192
0193 temp = find( ( g./(abs(g) + ones(length(g), 1) )) > 0.1*mpopt(16));
0194
0195
0196 if isempty(temp) ~= 1
0197 n_slack = length(temp);
0198 if issparse(a_lp)
0199 a_lp = [a_lp, sparse(temp, 1:n_slack, -1, size(a_lp,1), n_slack)];
0200 else
0201 a_lp = [a_lp, full(sparse(temp, 1:n_slack, -1, size(a_lp,1), n_slack))];
0202 end
0203 vubdx = [vubdx; g(temp) + 1.0e4*ones(n_slack, 1)];
0204 vlbdx = [vlbdx; zeros(n_slack, 1)];
0205 f_lp = [f_lp; 9.9e6 * max(df_dx) * ones(n_slack, 1)];
0206 end
0207
0208
0209
0210
0211 if itcounter ==1
0212 idx_workc = [];
0213 flag_workc = zeros(3 * length(rhs_lp) + 2 * nvars, 1);
0214 else
0215 flag_workc = flag_workc - 1;
0216 flag_workc(idx_bindc) = 20 * ones(size(idx_bindc));
0217
0218 if itcounter > 20
0219 idx_workc = find(flag_workc > 0);
0220 end
0221 end
0222
0223
0224
0225 [dx, lambda, idx_workc, idx_bindc] = LPsetup(a_lp, f_lp, rhs_lp, nequ, vlbdx, vubdx, idx_workc, mpopt);
0226
0227
0228 if length(dx) == nvars
0229 max_slackvar = 0;
0230 else
0231 max_slackvar = max(dx(nvars+1:length(dx))); if max_slackvar < 1.0e-8, max_slackvar = 0; end;
0232 end
0233
0234 if verbose, fprintf(' %-12.6g', max_slackvar); end
0235
0236
0237 dx = dx(1 : nvars);
0238
0239
0240
0241
0242 x = x + dx;
0243 xbackup = x;
0244 dL_dx = df_dx + dg_dx' * lambda;
0245
0246 norm_dL = norm(dL_dx, inf);
0247 if abs(f) < 1.0e-10
0248 norm_grad = norm_dL;
0249 else
0250 norm_grad = norm_dL/abs(f);
0251
0252
0253 end
0254 norm_dx = norm(dx ./ step0, inf);
0255
0256 if verbose, fprintf(' %-12.6g %-12.6g\n', norm_grad, norm_dx); end
0257
0258
0259
0260 if (norm_grad < mpopt(20)) && (max_g < mpopt(16)) && (norm_dx < mpopt(21))
0261 converged = 1; break;
0262 end
0263
0264
0265
0266
0267 if norm_dx < 0.05 * mpopt(21),
0268
0269 if max_g < mpopt(16) && abs(f_best - f_best_run)/f_best_run < 1.0e-4
0270
0271
0272
0273 converged = 1;
0274 break;
0275
0276 else
0277
0278 f_best_run = f_best;
0279 stepsize = 0.4* step0;
0280
0281 if verbose
0282 fprintf('\n----- restarted with larger stepsize\n');
0283 end
0284
0285 runcounter = runcounter + 1;
0286 end;
0287 end
0288
0289
0290
0291
0292 if itcounter == 1
0293
0294 if norm_grad < mpopt(20)
0295 stepsize = 0.015 * step0;
0296 elseif norm_grad < 2.0 * mpopt(20)
0297 stepsize = 0.05 * step0;
0298 elseif norm_grad < 4.0 * mpopt(20)
0299 stepsize = 0.3 * step0;
0300 elseif norm_grad < 6.0 * mpopt(20)
0301 stepsize = 0.6 * step0;
0302 else
0303 stepsize = step0;
0304 end
0305 end
0306
0307 if itcounter > 2
0308 if max_slackvar > max_slackvar_last + 1.0e-10
0309 stepsize = 0.7* stepsize;
0310 end
0311
0312 if max_slackvar < 1.0e-7
0313 actual_df = f_last - f;
0314 if abs(predict_df) > 1.0e-12
0315 ratio = actual_df/predict_df;
0316 else
0317 ratio = -99999;
0318 end
0319
0320 if ratio < 0.25 || f > f_last * 0.9999
0321 stepsize = 0.5 * stepsize;
0322 elseif ratio > 0.80
0323 stepsize = 1.05 * stepsize;
0324 end
0325
0326 if norm(stepsize ./ step0, inf) > 3.0, stepsize = 3*step0; end;
0327 end;
0328 end
0329
0330 max_slackvar_last = max_slackvar;
0331 f_best = min(f, f_best);
0332 f_last = f;
0333 predict_df = -(df_dx(1:nvars))' * dx(1:nvars);
0334 end
0335
0336
0337 if etype == 1,
0338 [f, g] = feval(FUN,x);
0339 elseif etype == 2
0340 [f, g] = eval(evalstr);
0341 else
0342 eval(evalstr);
0343 end
0344
0345 i = find(g < -mpopt(16));
0346 lambda(i) = zeros(size(i));