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