Home > matpower4.0 > t > t_mips.m

t_mips

PURPOSE ^

T_MIPS Tests of MIPS NLP solver.

SYNOPSIS ^

function t_mips(quiet)

DESCRIPTION ^

T_MIPS  Tests of MIPS NLP solver.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function t_mips(quiet)
0002 %T_MIPS  Tests of MIPS NLP solver.
0003 
0004 %   MIPS
0005 %   $Id: t_mips.m,v 1.14 2010/11/15 19:20:42 cvs Exp $
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %   Copyright (c) 2010 by Power System Engineering Research Center (PSERC)
0008 %
0009 %   This file is part of MIPS.
0010 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0011 %
0012 %   MIPS is free software: you can redistribute it and/or modify
0013 %   it under the terms of the GNU General Public License as published
0014 %   by the Free Software Foundation, either version 3 of the License,
0015 %   or (at your option) any later version.
0016 %
0017 %   MIPS is distributed in the hope that it will be useful,
0018 %   but WITHOUT ANY WARRANTY; without even the implied warranty of
0019 %   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0020 %   GNU General Public License for more details.
0021 %
0022 %   You should have received a copy of the GNU General Public License
0023 %   along with MIPS. If not, see <http://www.gnu.org/licenses/>.
0024 %
0025 %   Additional permission under GNU GPL version 3 section 7
0026 %
0027 %   If you modify MIPS, or any covered work, to interface with
0028 %   other modules (such as MATLAB code and MEX-files) available in a
0029 %   MATLAB(R) or comparable environment containing parts covered
0030 %   under other licensing terms, the licensors of MIPS grant
0031 %   you additional permission to convey the resulting work.
0032 
0033 if nargin < 1
0034     quiet = 0;
0035 end
0036 
0037 t_begin(60, quiet);
0038 
0039 t = 'unconstrained banana function : ';
0040 %% from MATLAB Optimization Toolbox's bandem.m
0041 f_fcn = @(x)f2(x);
0042 x0 = [-1.9; 2];
0043 % [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], [], [], [], [], struct('verbose', 2));
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 %% from http://www.akiti.ca/QuadProgEx0Constr.html
0056 f_fcn = @(x)f3(x);
0057 x0 = [0; 0; 0];
0058 % [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], [], [], [], [], struct('verbose', 2));
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 %% from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm
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 % [x, f, s, out, lam] = mips(f_fcn, x0, A, l, u, xmin, [], [], [], struct('verbose', 2));
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 % %% check with quadprog (for dev testing only)
0095 % [x, f, s, out, lam] = quadprog(H,c,-A(2,:), -0.10, A(1,:), 1, xmin);
0096 % t_is(s, 1, 13, [t 'success']);
0097 % t_is(x, [0; 2.8; 0.2; 0]/3, 6, [t 'x']);
0098 % t_is(f, 3.29/3, 6, [t 'f']);
0099 % t_is(lam.eqlin, -6.58/3, 6, [t 'lam.eqlin']);
0100 % t_is(lam.ineqlin, 0, 13, [t 'lam.ineqlin']);
0101 % t_is(lam.lower, [2.24;0;0;1.7667], 4, [t 'lam.lower']);
0102 % t_is(lam.upper, [0;0;0;0], 13, [t 'lam.upper']);
0103 
0104 t = 'constrained 2-d nonlinear : ';
0105 %% from http://en.wikipedia.org/wiki/Nonlinear_programming#2-dimensional_example
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 % xmax = 3 * ones(2, 1);
0112 % [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], xmin, [], gh_fcn, hess_fcn, struct('verbose', 2));
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 % %% check with fmincon (for dev testing only)
0124 % % fmoptions = optimset('Algorithm', 'interior-point');
0125 % % [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], xmin, [], gh_fcn, fmoptions);
0126 % [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], [], [], gh_fcn);
0127 % t_is(s, 1, 13, [t 'success']);
0128 % t_is(x, [1; 1], 4, [t 'x']);
0129 % t_is(f, -2, 6, [t 'f']);
0130 % t_is(lam.ineqnonlin, [0;0.5], 6, [t 'lam.ineqnonlin']);
0131 
0132 t = 'constrained 3-d nonlinear : ';
0133 %% from http://en.wikipedia.org/wiki/Nonlinear_programming#3-dimensional_example
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 % [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], [], [], gh_fcn, hess_fcn, struct('verbose', 2, 'comptol', 1e-9));
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 % %% check with fmincon (for dev testing only)
0150 % % fmoptions = optimset('Algorithm', 'interior-point');
0151 % % [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], xmin, [], gh_fcn, fmoptions);
0152 % [x, f, s, out, lam] = fmincon(f_fcn, x0, [], [], [], [], [], [], gh_fcn);
0153 % t_is(s, 1, 13, [t 'success']);
0154 % t_is(x, [1.58113883; 2.23606798; 1.58113883], 4, [t 'x']);
0155 % t_is(f, -5*sqrt(2), 8, [t 'f']);
0156 % t_is(lam.ineqnonlin, [0;sqrt(2)/2], 8, [t 'lam.ineqnonlin']);
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 %% Hock & Schittkowski test problem #71
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 % [x, f, s, out, lam] = mips(f_fcn, x0, [], [], [], xmin, xmax, gh_fcn, hess_fcn, struct('verbose', 2, 'comptol', 1e-9));
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 % %%-----  eg99 : linearly constrained fmincon example, mips can't solve  -----
0195 % function [f, df, d2f] = eg99(x)
0196 % f = -x(1)*x(2)*x(3);
0197 % df = -[ x(2)*x(3);
0198 %         x(1)*x(3);
0199 %         x(1)*x(2)   ];
0200 % d2f = -[    0       x(3)    x(2);
0201 %             x(3)    0       x(1);
0202 %             x(2)    x(1)    0   ];
0203 % end
0204 %
0205 % x0 = [10;10;10];
0206 % A = [1 2 2];
0207 % l = 0;
0208 % u = 72;
0209 % fmoptions = optimset('Display', 'testing');
0210 % fmoptions = optimset(fmoptions, 'Algorithm', 'interior-point');
0211 % [x, f, s, out, lam] = fmincon(f_fcn, x0, [-A; A], [-l; u], [], [], [], [], [], fmoptions);
0212 % t_is(x, [24; 12; 12], 13, t);
0213 % t_is(f, -3456, 13, t);
0214 
0215 
0216 %% unconstrained banana function
0217 %% from MATLAB Optimization Toolbox's bandem.m
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 %% unconstrained 3-d quadratic
0228 %% from http://www.akiti.ca/QuadProgEx0Constr.html
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 %% constrained 4-d QP
0238 %% from http://www.jmu.edu/docs/sasdoc/sashtml/iml/chap8/sect12.htm
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 %% constrained 2-d nonlinear
0251 %% from http://en.wikipedia.org/wiki/Nonlinear_programming#2-dimensional_example
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 %% constrained 3-d nonlinear
0269 %% from http://en.wikipedia.org/wiki/Nonlinear_programming#3-dimensional_example
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 %% constrained 4-d nonlinear
0288 %% Hock & Schittkowski test problem #71
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  ]);

Generated on Mon 26-Jan-2015 14:56:45 by m2html © 2005