Home > matpower4.1 > t > t_mips6.m

t_mips6

PURPOSE ^

------------------------------ deprecated ------------------------------

SYNOPSIS ^

function t_mips6(quiet)

DESCRIPTION ^

------------------------------  deprecated  ------------------------------
   MATLAB 6.x support to be removed in a future version.
--------------------------------------------------------------------------
T_MIPS6  Tests of MIPS NLP solver (for MATLAB 6).

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

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

Generated on Mon 26-Jan-2015 15:00:13 by m2html © 2005