0001 function t_mips(quiet)
0002
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 if nargin < 1
0034 quiet = 0;
0035 end
0036
0037 t_begin(60, quiet);
0038
0039 t = 'unconstrained banana function : ';
0040
0041 f_fcn = @(x)f2(x);
0042 x0 = [-1.9; 2];
0043
0044 [x, f, s, out, lam] = mips(f_fcn, x0);
0045 t_is(s, 1, 13, [t 'success']);
0046 t_is(x, [1; 1], 13, [t 'x']);
0047 t_is(f, 0, 13, [t 'f']);
0048 t_is(out.hist(end).compcond, 0, 6, [t 'compcond']);
0049 t_ok(isempty(lam.mu_l), [t 'lam.mu_l']);
0050 t_ok(isempty(lam.mu_u), [t 'lam.mu_u']);
0051 t_is(lam.lower, zeros(size(x)), 13, [t 'lam.lower']);
0052 t_is(lam.upper, zeros(size(x)), 13, [t 'lam.upper']);
0053
0054 t = 'unconstrained 3-d quadratic : ';
0055
0056 f_fcn = @(x)f3(x);
0057 x0 = [0; 0; 0];
0058
0059 [x, f, s, out, lam] = mips(f_fcn, x0);
0060 t_is(s, 1, 13, [t 'success']);
0061 t_is(x, [3; 5; 7], 13, [t 'x']);
0062 t_is(f, -244, 13, [t 'f']);
0063 t_is(out.hist(end).compcond, 0, 6, [t 'compcond']);
0064 t_ok(isempty(lam.mu_l), [t 'lam.mu_l']);
0065 t_ok(isempty(lam.mu_u), [t 'lam.mu_u']);
0066 t_is(lam.lower, zeros(size(x)), 13, [t 'lam.lower']);
0067 t_is(lam.upper, zeros(size(x)), 13, [t 'lam.upper']);
0068
0069 t = 'constrained 4-d QP : ';
0070
0071 f_fcn = @(x)f4(x);
0072 x0 = [1; 0; 0; 1];
0073 A = [ 1 1 1 1;
0074 0.17 0.11 0.10 0.18 ];
0075 l = [1; 0.10];
0076 u = [1; Inf];
0077 xmin = zeros(4,1);
0078
0079 [x, f, s, out, lam] = mips(f_fcn, x0, A, l, u, xmin);
0080 t_is(s, 1, 13, [t 'success']);
0081 t_is(x, [0; 2.8; 0.2; 0]/3, 6, [t 'x']);
0082 t_is(f, 3.29/3, 6, [t 'f']);
0083 t_is(out.hist(end).compcond, 0, 6, [t 'compcond']);
0084 t_is(lam.mu_l, [6.58;0]/3, 6, [t 'lam.mu_l']);
0085 t_is(lam.mu_u, [0;0], 13, [t 'lam.mu_u']);
0086 t_is(lam.lower, [2.24;0;0;1.7667], 4, [t 'lam.lower']);
0087 t_is(lam.upper, zeros(size(x)), 13, [t 'lam.upper']);
0088
0089 H = [ 1003.1 4.3 6.3 5.9;
0090 4.3 2.2 2.1 3.9;
0091 6.3 2.1 3.5 4.8;
0092 5.9 3.9 4.8 10 ];
0093 c = zeros(4,1);
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 t = 'constrained 2-d nonlinear : ';
0105
0106 f_fcn = @(x)f5(x);
0107 gh_fcn = @(x)gh5(x);
0108 hess_fcn = @(x, lam, cost_mult)hess5(x, lam, cost_mult);
0109 x0 = [1.1; 0];
0110 xmin = zeros(2, 1);
0111
0112
0113 [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], xmin, [], gh_fcn, hess_fcn);
0114 t_is(s, 1, 13, [t 'success']);
0115 t_is(x, [1; 1], 6, [t 'x']);
0116 t_is(f, -2, 6, [t 'f']);
0117 t_is(out.hist(end).compcond, 0, 6, [t 'compcond']);
0118 t_is(lam.ineqnonlin, [0;0.5], 6, [t 'lam.ineqnonlin']);
0119 t_ok(isempty(lam.mu_l), [t 'lam.mu_l']);
0120 t_ok(isempty(lam.mu_u), [t 'lam.mu_u']);
0121 t_is(lam.lower, zeros(size(x)), 13, [t 'lam.lower']);
0122 t_is(lam.upper, zeros(size(x)), 13, [t 'lam.upper']);
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132 t = 'constrained 3-d nonlinear : ';
0133
0134 f_fcn = @(x)f6(x);
0135 gh_fcn = @(x)gh6(x);
0136 hess_fcn = @(x, lam, cost_mult)hess6(x, lam, cost_mult);
0137 x0 = [1; 1; 0];
0138
0139 [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], [], [], gh_fcn, hess_fcn);
0140 t_is(s, 1, 13, [t 'success']);
0141 t_is(x, [1.58113883; 2.23606798; 1.58113883], 6, [t 'x']);
0142 t_is(f, -5*sqrt(2), 6, [t 'f']);
0143 t_is(out.hist(end).compcond, 0, 6, [t 'compcond']);
0144 t_is(lam.ineqnonlin, [0;sqrt(2)/2], 7, [t 'lam.ineqnonlin']);
0145 t_ok(isempty(lam.mu_l), [t 'lam.mu_l']);
0146 t_ok(isempty(lam.mu_u), [t 'lam.mu_u']);
0147 t_is(lam.lower, zeros(size(x)), 13, [t 'lam.lower']);
0148 t_is(lam.upper, zeros(size(x)), 13, [t 'lam.upper']);
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 t = 'constrained 3-d nonlinear (struct) : ';
0159 p = struct('f_fcn', f_fcn, 'x0', x0, 'gh_fcn', gh_fcn, 'hess_fcn', hess_fcn);
0160 [x, f, s, out, lam] = mips(p);
0161 t_is(s, 1, 13, [t 'success']);
0162 t_is(x, [1.58113883; 2.23606798; 1.58113883], 6, [t 'x']);
0163 t_is(f, -5*sqrt(2), 6, [t 'f']);
0164 t_is(out.hist(end).compcond, 0, 6, [t 'compcond']);
0165 t_is(lam.ineqnonlin, [0;sqrt(2)/2], 7, [t 'lam.ineqnonlin']);
0166 t_ok(isempty(lam.mu_l), [t 'lam.mu_l']);
0167 t_ok(isempty(lam.mu_u), [t 'lam.mu_u']);
0168 t_is(lam.lower, zeros(size(x)), 13, [t 'lam.lower']);
0169 t_is(lam.upper, zeros(size(x)), 13, [t 'lam.upper']);
0170
0171 t = 'constrained 4-d nonlinear : ';
0172
0173 f_fcn = @(x)f7(x);
0174 gh_fcn = @(x)gh7(x);
0175 hess_fcn = @(x, lam, sigma)hess7(x, lam, sigma);
0176 x0 = [1; 5; 5; 1];
0177 xmin = ones(4, 1);
0178 xmax = 5 * xmin;
0179
0180 [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], xmin, xmax, gh_fcn, hess_fcn);
0181 t_is(s, 1, 13, [t 'success']);
0182 t_is(x, [1; 4.7429994; 3.8211503; 1.3794082], 6, [t 'x']);
0183 t_is(f, 17.0140173, 6, [t 'f']);
0184 t_is(lam.eqnonlin, 0.1614686, 5, [t 'lam.eqnonlin']);
0185 t_is(lam.ineqnonlin, 0.55229366, 5, [t 'lam.ineqnonlin']);
0186 t_ok(isempty(lam.mu_l), [t 'lam.mu_l']);
0187 t_ok(isempty(lam.mu_u), [t 'lam.mu_u']);
0188 t_is(lam.lower, [1.08787121024; 0; 0; 0], 5, [t 'lam.lower']);
0189 t_is(lam.upper, zeros(size(x)), 7, [t 'lam.upper']);
0190
0191 t_end;
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218 function [f, df, d2f] = f2(x)
0219 a = 100;
0220 f = a*(x(2)-x(1)^2)^2+(1-x(1))^2;
0221 df = [ 4*a*(x(1)^3 - x(1)*x(2)) + 2*x(1)-2;
0222 2*a*(x(2) - x(1)^2) ];
0223 d2f = 4*a*[ 3*x(1)^2 - x(2) + 1/(2*a), -x(1);
0224 -x(1) 1/2 ];
0225
0226
0227
0228
0229 function [f, df, d2f] = f3(x)
0230 H = [5 -2 -1; -2 4 3; -1 3 5];
0231 c = [2; -35; -47];
0232 f = 1/2 * x'*H*x + c'*x + 5;
0233 df = H*x + c;
0234 d2f = H;
0235
0236
0237
0238
0239 function [f, df, d2f] = f4(x)
0240 H = [ 1003.1 4.3 6.3 5.9;
0241 4.3 2.2 2.1 3.9;
0242 6.3 2.1 3.5 4.8;
0243 5.9 3.9 4.8 10 ];
0244 c = zeros(4,1);
0245 f = 1/2 * x'*H*x + c'*x;
0246 df = H*x + c;
0247 d2f = H;
0248
0249
0250
0251
0252 function [f, df, d2f] = f5(x)
0253 c = -[1; 1];
0254 f = c'*x;
0255 df = c;
0256 d2f = zeros(2,2);
0257
0258 function [h, g, dh, dg] = gh5(x)
0259 h = [ -1 -1; 1 1] * x.^2 + [1; -2];
0260 dh = 2 * [-x(1) x(1); -x(2) x(2)];
0261 g = []; dg = [];
0262
0263 function Lxx = hess5(x, lam, cost_mult)
0264 mu = lam.ineqnonlin;
0265 Lxx = 2*[-1 1]*mu*eye(2);
0266
0267
0268
0269
0270 function [f, df, d2f] = f6(x)
0271 f = -x(1)*x(2) - x(2)*x(3);
0272 df = -[x(2); x(1)+x(3); x(2)];
0273 d2f = -[0 1 0; 1 0 1; 0 1 0];
0274
0275 function [h, g, dh, dg] = gh6(x)
0276 h = [ 1 -1 1; 1 1 1] * x.^2 + [-2; -10];
0277 dh = 2 * [x(1) x(1); -x(2) x(2); x(3) x(3)];
0278 g = []; dg = [];
0279
0280 function Lxx = hess6(x, lam, cost_mult)
0281 if nargin < 3, cost_mult = 1; end
0282 mu = lam.ineqnonlin;
0283 Lxx = cost_mult * [0 -1 0; -1 0 -1; 0 -1 0] + ...
0284 [2*[1 1]*mu 0 0; 0 2*[-1 1]*mu 0; 0 0 2*[1 1]*mu];
0285
0286
0287
0288
0289 function [f, df, d2f] = f7(x)
0290 f = x(1)*x(4)*sum(x(1:3)) + x(3);
0291 df = [ x(1)*x(4) + x(4)*sum(x(1:3));
0292 x(1)*x(4);
0293 x(1)*x(4) + 1;
0294 x(1)*sum(x(1:3)) ];
0295 d2f = sparse([ 2*x(4) x(4) x(4) 2*x(1)+x(2)+x(3);
0296 x(4) 0 0 x(1);
0297 x(4) 0 0 x(1);
0298 2*x(1)+x(2)+x(3) x(1) x(1) 0
0299 ]);
0300
0301 function [h, g, dh, dg] = gh7(x)
0302 g = sum(x.^2) - 40;
0303 h = -prod(x) + 25;
0304 dg = 2*x;
0305 dh = -prod(x)./x;
0306
0307 function Lxx = hess7(x, lam, sigma)
0308 if nargin < 3, sigma = 1; end
0309 lambda = lam.eqnonlin;
0310 mu = lam.ineqnonlin;
0311 [f, df, d2f] = f7(x);
0312 Lxx = sigma * d2f + lambda*2*speye(4) - ...
0313 mu*sparse([ 0 x(3)*x(4) x(2)*x(4) x(2)*x(3);
0314 x(3)*x(4) 0 x(1)*x(4) x(1)*x(3);
0315 x(2)*x(4) x(1)*x(4) 0 x(1)*x(2);
0316 x(2)*x(3) x(1)*x(3) x(1)*x(2) 0 ]);