Home > matpower7.0 > lib > t > t_opf_model_legacy.m

t_opf_model_legacy

PURPOSE ^

T_OPF_MODEL_LEGACY Tests for OPF_MODEL, using the deprecated ADD_CONSTRAINTS.

SYNOPSIS ^

function t_opf_model_legacy(quiet)

DESCRIPTION ^

T_OPF_MODEL_LEGACY Tests for OPF_MODEL, using the deprecated ADD_CONSTRAINTS.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function t_opf_model_legacy(quiet)
0002 %T_OPF_MODEL_LEGACY Tests for OPF_MODEL, using the deprecated ADD_CONSTRAINTS.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2012-2017, Power Systems Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   This file is part of MATPOWER.
0009 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0010 %   See https://matpower.org for more info.
0011 
0012 if nargin < 1
0013     quiet = 0;
0014 end
0015 
0016 num_tests = 363;
0017 
0018 t_begin(num_tests, quiet);
0019 
0020 %% define named indices into data matrices
0021 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0022     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0023 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0024     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0025     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0026 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0027     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0028     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0029 
0030 %%-----  opt_model  -----
0031 t = 'constructor';
0032 om = opf_model;
0033 t_ok(isa(om, 'opf_model'), t);
0034 t_ok(isa(om, 'opt_model'), t);
0035 
0036 %%-----  add_vars  -----
0037 t = 'add_vars';
0038 vN = 0;
0039 vNS = 0;
0040 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0041 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0042 
0043 t = 'om.add_vars(''Va'', 4)';
0044 om.add_vars('Va', 4);
0045 vNS = vNS + 1; vN = vN + 4;
0046 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0047 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0048 
0049 t = 'om.add_vars(''Pg'', 3, Pg0, Pgmin, Pgmax)';
0050 om.add_vars('Pg', 3, [2;4;6], [1;2;3], [10;20;30]);
0051 vNS = vNS + 1; vN = vN + 3;
0052 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0053 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0054 
0055 t = 'om.add_vars(''Vm1'', 5, V0, Vmin, Vmax, ''I'')';
0056 V0 = [1;1;1;1;1];
0057 Vmin = zeros(5, 1);
0058 Vmax = 1 + 0.01*(1:5)';
0059 vt = 'I';
0060 om.add_vars('Vm1', 5, V0, Vmin, Vmax, vt);
0061 vNS = vNS + 1; vN = vN + 5;
0062 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0063 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0064 
0065 t = 'om.add_vars(''Vm2'', 5, V0, Vmin, Vmax, ''CIBIC'')';
0066 vt = 'CIBIC';
0067 om.add_vars('Vm2', 5, V0, Vmin, Vmax, vt);
0068 vNS = vNS + 1; vN = vN + 5;
0069 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0070 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0071 
0072 t = 'om.add_vars(''x'', dims)';
0073 om.add_vars('x', {2,2});
0074 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0075 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0076 
0077 t = 'om.add_vars(''x'', {1,1}, 2)';
0078 om.add_vars('x', {1,1}, 2);
0079 vNS = vNS + 1; vN = vN + 2;
0080 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0081 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0082 
0083 t = 'om.add_vars(''x'', {1,2}, 2, x0(1,2))';
0084 om.add_vars('x', {1,2}, 2, [-1;-2]);
0085 vNS = vNS + 1; vN = vN + 2;
0086 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0087 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0088 
0089 t = 'om.add_vars(''x'', {2,1}, 3)';
0090 om.add_vars('x', {2,1}, 3);
0091 vNS = vNS + 1; vN = vN + 3;
0092 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0093 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0094 
0095 t = 'om.add_vars(''x'', {2,2}, 2, x0(2,2), xmin(2,2), xmax(2,2))';
0096 om.add_vars('x', {2,2}, 2, [1;0],[0;-1],[2;1]);
0097 vNS = vNS + 1; vN = vN + 2;
0098 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0099 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0100 
0101 t = 'om.add_vars(''y'', {2,3,4})';
0102 om.add_vars('y', {2,3,4});
0103 vt0 = {'C', 'I', 'B'};
0104 for i = 1:2
0105     for j = 1:3
0106         for k = 1:4
0107             n = i+j+k;
0108             if i == 1
0109                 vt = vt0{j};
0110             else
0111                 vt = char(vt0{j} * ones(1, n));
0112                 vt(j+1) = vt0{1+rem(j,3)};
0113             end
0114 %             fprintf('%d %d %d : %s\n', i, j, k, vt);
0115             t = sprintf('om.add_vars(''y'', {%d,%d,%d}, y0, ymin, ymax, vt)', i,j,k);
0116             om.add_vars('y', {i,j,k}, n, 10*(n:-1:1)', -1*(n:-1:1)', 100+(n:-1:1)', vt);
0117             vNS = vNS + 1; vN = vN + n;
0118             t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0119             t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0120         end
0121     end
0122 end
0123 
0124 %%-----  getN  -----
0125 t = 'om.getN(''var'', ''Pg'') == 3';
0126 t_ok(om.getN('var', 'Pg') == 3, t);
0127 
0128 t = 'size(om.getN(''var'', ''x'')) == [2 2]';
0129 t_is(size(om.getN('var', 'x')), [2,2], 14, t);
0130 
0131 t = 'om.getN(''var'', ''x'')(1,2) == 2';
0132 N = om.getN('var', 'x');
0133 t_is(N(1,2), 2, 14, t);
0134 
0135 t = 'om.getN(''var'', ''x'', {2,1}) == 3';
0136 t_is(om.getN('var', 'x', {2,1}), 3, 14, t);
0137 
0138 t = 'om.getN(''var'', ''y'', {2,1,3}) == 6';
0139 t_is(om.getN('var', 'y', {2,1,3}), 6, 14, t);
0140 
0141 t = 'om.getN(''var'')';
0142 t_is(om.getN('var'), vN, 14, t);
0143 
0144 %%-----  get_idx  -----
0145 t = 'get_idx : var';
0146 vv = om.get_idx();
0147 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
0148 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
0149 t_is([vv.i1.x(2,1) vv.iN.x(2,1) vv.N.x(2,1)], [22 24 3], 14, [t ' : x(2,1)']);
0150 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
0151 t_is([vv.i1.y(2,2,4) vv.iN.y(2,2,4) vv.N.y(2,2,4)], [133 140 8], 14, [t ' : y(2,2,4)']);
0152 
0153 %%-----  getv  -----
0154 t = 'om.getv(''Va'')';
0155 [v0, vl, vu] = om.getv('Va');
0156 t_ok(~any(v0), [t ' : v0']);
0157 t_ok(all(isinf(vl) & vl < 0), [t ' : vl']);
0158 t_ok(all(isinf(vu) & vu > 0), [t ' : vu']);
0159 
0160 t = 'om.getv(''Pg'')';
0161 [v0, vl, vu] = om.getv('Pg');
0162 t_is(v0, [2;4;6], 14, [t ' : v0']);
0163 t_is(vl, [1;2;3], 14, [t ' : vl']);
0164 t_is(vu, [10;20;30], 14, [t ' : vu']);
0165 
0166 t = 'om.getv(''Vm1'')';
0167 [v0, vl, vu, vt] = om.getv('Vm1');
0168 t_is(double(vt), double('I'), 14, [t ' : vt']);
0169 
0170 t = 'om.getv(''Vm2'')';
0171 [v0, vl, vu, vt] = om.getv('Vm2');
0172 t_is(double(vt), double('CIBIC'), 14, [t ' : vt']);
0173 
0174 t = 'om.getv(''x'')';
0175 [v0, vl, vu, vt] = om.getv('x');
0176 t_is(size(v0), [2,2], 14, [t ' : size(v0)']);
0177 t_is(v0{2,2}, [1;0], 14, [t ' : v0{2,2}']);
0178 t_is(vl{2,2}, [0;-1], 14, [t ' : vl{2,2}']);
0179 t_is(vu{2,2}, [2;1], 14, [t ' : vu{2,2}']);
0180 t_is(double(vt{2,2}), double('C'), 14, [t ' : vt{2,2}']);
0181 
0182 for i = 1:2
0183     for j = 1:3
0184         for k = 1:4
0185             n = i+j+k;
0186             if i == 1
0187                 vt = vt0{j};
0188             else
0189                 vt = char(vt0{j} * ones(1, n));
0190                 vt(j+1) = vt0{1+rem(j,3)};
0191             end
0192             t = sprintf('om.getv(''y'', {%d,%d,%d})', i, j, k);
0193             [v0, vl, vu, gvt] = om.getv('y', {i,j,k});
0194             t_is(v0, 10*(n:-1:1)', 14, [t ' : v0']);
0195             t_is(vl, -1*(n:-1:1)', 14, [t ' : vl']);
0196             t_is(vu, 100+(n:-1:1)', 14, [t ' : vu']);
0197             t_is(gvt, vt, 14, [t ' : vt']);
0198         end
0199     end
0200 end
0201 
0202 t = 'om.getv()';
0203 [v0, vl, vu, vt] = om.getv();
0204 t_ok(length(v0) == om.getN('var'), [t ' : length(v0)']);
0205 t_ok(length(vl) == om.getN('var'), [t ' : length(vl)']);
0206 t_ok(length(vu) == om.getN('var'), [t ' : length(vu)']);
0207 t_is(v0(vv.i1.x(2,2):vv.iN.x(2,2)), [1;0], 14, [t ' : v0(vv.i1.x(2,2):vv.iN.x(2,2))']);
0208 t_is(vl(vv.i1.x(2,2):vv.iN.x(2,2)), [0;-1], 14, [t ' : vl(vv.i1.x(2,2):vv.iN.x(2,2))']);
0209 t_is(vu(vv.i1.x(2,2):vv.iN.x(2,2)), [2;1], 14, [t ' : vu(vv.i1.x(2,2):vv.iN.x(2,2))']);
0210 t_is(vt(vv.i1.x(2,2):vv.iN.x(2,2)), 'C', 14, [t ' : vt(vv.i1.x(2,2):vv.iN.x(2,2))']);
0211 n = 8;
0212 t_is(v0(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), 10*(n:-1:1)', 14, [t ' : v0(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0213 t_is(vl(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), -1*(n:-1:1)', 14, [t ' : vl(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0214 t_is(vu(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), 100+(n:-1:1)', 14, [t ' : vu(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0215 t_is(vt(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), 'IIBIIIII', 14, [t ' : vt(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0216 vt0 = 'CCCCCCCIIIIICIBICCCCCCCCCCCCCCCCCCCCCCCCCCCCIIIIIIIIIIIIIIIIIIIIIIBBBBBBBBBBBBBBBBBBBBBBBBBBCICCCICCCCICCCCCICCCCCIIBIIIIBIIIIIBIIIIIIBIIIIIBBBCBBBBBCBBBBBBCBBBBBBBCBBBBB';
0217 t_is(vt, vt0, 14, [t ' : vt']);
0218 
0219 %%-----  add_constraints (linear)  -----
0220 t = 'add_constraints';
0221 lN = 0;
0222 lNS = 0;
0223 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0224 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0225 
0226 t = 'om.add_constraints(''Pmis'', A, l, u, {''Va'', ''Pg''})';
0227 A = sparse([1:3 1:3 1:3]', [1:3 4:6 7 7 7]', [1 1 1 -1 -1 -1 2 3 4]', 3, 7);
0228 l = -(1:3)'; u = (1:3)';
0229 om.add_constraints('Pmis', A, l, u, {'Va', 'Pg'});
0230 lNS = lNS + 1; lN = lN + 3;
0231 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0232 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0233 
0234 t = 'om.add_constraints(''Qmis'', A, l, u)';
0235 A = sparse([1:3 1:3 1:3]', [1:3 4:6 7 7 7]', [1 1 1 -1 -1 -1 2 3 4]', 3, vN);
0236 om.add_constraints('Qmis', A, l, u);
0237 lNS = lNS + 1; lN = lN + 3;
0238 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0239 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0240 
0241 t = 'om.add_constraints(''mylin'', {2, 2})';
0242 om.add_constraints('mylin', {2, 2});
0243 % om.add_constraints('mylin', {2, 2}, 'lin');
0244 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0245 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0246 
0247 for i = 1:2
0248     for j = 1:2
0249         t = sprintf('om.add_constraints(''mylin'', {%d,%d}, A, l, u, vs)', i,j);
0250         A = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0251             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0252         l = -ones(i+j, 1); u = [];
0253         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0254         om.add_constraints('mylin', {i, j}, A, l, u, vs);
0255         lNS = lNS + 1; lN = lN + i+j;
0256         t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0257         t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0258     end
0259 end
0260 
0261 %%-----  add_constraints (nonlinear, equality)  -----
0262 t = 'add_constraints (nonlinear, equality)';
0263 neN = 0;
0264 neNS = 0;
0265 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0266 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0267 
0268 t = 'om.add_constraints(''Pmise'', N, 1, fcn, hess, {''Pg'', ''Va''})';
0269 N = 4;
0270 fcn = @(x)my_fcn(x, N, 2);
0271 hess = @(x, lam)my_hess(x, lam, 10);
0272 om.add_constraints('Pmise', N, 1, fcn, hess, {'Pg', 'Va'});
0273 neNS = neNS + 1; neN = neN + N;
0274 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0275 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0276 
0277 t = 'om.add_constraints(''Qmise'', N, 1, fcn, hess)';
0278 N = 3;
0279 fcn = @(x)my_fcn(x, N, 2);
0280 hess = @(x, lam)my_hess(x, lam, 10);
0281 om.add_constraints('Qmise', N, 1, fcn, hess);
0282 neNS = neNS + 1; neN = neN + N;
0283 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0284 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0285 
0286 t = 'om.add_constraints(''mynle'', {2, 2})';
0287 om.add_constraints('mynle', {2, 2}, 'nle');
0288 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0289 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0290 
0291 for i = 1:2
0292     for j = 1:2
0293         t = sprintf('om.add_constraints(''mynle'', {%d,%d}, N, 1, fcn, hess, vs)', i,j);
0294         N = i+j;
0295         fcn = @(x)my_fcn(x, N, i);
0296         hess = @(x, lam)my_hess(x, lam, j);
0297         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0298         om.add_constraints('mynle', {i, j}, N, 1, fcn, hess, vs);
0299         neNS = neNS + 1; neN = neN + N;
0300         t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0301         t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0302     end
0303 end
0304 
0305 %%-----  add_constraints (nonlinear, inequality)  -----
0306 t = 'add_constraints (nonlinear, inequality)';
0307 niN = 0;
0308 niNS = 0;
0309 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0310 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0311 
0312 t = 'om.add_constraints(''Pmisi'', N, 0, fcn, hess, {''Pg'', ''Va''})';
0313 N = 3;
0314 fcn = @(x)my_fcn(x, N, -2);
0315 hess = @(x, lam)my_hess(x, lam, -10);
0316 om.add_constraints('Pmisi', N, 0, fcn, hess, {'Pg', 'Va'});
0317 niNS = niNS + 1; niN = niN + N;
0318 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0319 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0320 
0321 t = 'om.add_constraints(''Qmisi'', N, 0, fcn, hess)';
0322 N = 2;
0323 fcn = @(x)my_fcn(x, N, -2);
0324 hess = @(x, lam)my_hess(x, lam, -10);
0325 om.add_constraints('Qmisi', N, 0, fcn, hess);
0326 niNS = niNS + 1; niN = niN + N;
0327 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0328 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0329 
0330 t = 'om.add_constraints(''mynli'', {2, 2})';
0331 om.add_constraints('mynli', {2, 2}, 'nli');
0332 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0333 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0334 
0335 for i = 1:2
0336     for j = 1:2
0337         t = sprintf('om.add_constraints(''mynli'', {%d,%d}, N, 0, fcn, hess, vs)', i,j);
0338         N = i+j-1;
0339         fcn = @(x)my_fcn(x, N, i);
0340         hess = @(x, lam)my_hess(x, lam, j);
0341         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0342         om.add_constraints('mynli', {i, j}, N, 0, fcn, hess, vs);
0343         niNS = niNS + 1; niN = niN + N;
0344         t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0345         t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0346     end
0347 end
0348 
0349 %%-----  get_idx  -----
0350 t = 'get_idx : lin';
0351 [vv, ll] = om.get_idx();
0352 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0353 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0354 t_is([ll.i1.mylin(2,1) ll.iN.mylin(2,1) ll.N.mylin(2,1)], [12 14 3], 14, [t ' : mylin(2,1)']);
0355 
0356 %%-----  linear_constraints  -----
0357 t = 'linear_constraints';
0358 [A, l, u] = om.linear_constraints();
0359 t_ok(issparse(A), [t ' : issparse(A)']);
0360 t_is(size(A), [lN, vN], 14, [t ' : size(A)']);
0361 t_is(length(l), lN, 14, [t ' : length(l)']);
0362 t_is(length(u), lN, 14, [t ' : length(u)']);
0363 AA = sparse([1:3 1:3 1:3]', [1:3 4:6 7 7 7]', [1 1 1 -1 -1 -1 2 3 4]', 3, vN);
0364 t_is(full(A(ll.i1.Qmis:ll.iN.Qmis, :)), full(AA), 14, [t ' : A(<Qmis>,:)']);
0365 t_is(full(A(ll.i1.mylin(2,1):ll.iN.mylin(2,1), vv.i1.Pg:vv.iN.Pg)), eye(3,3), 14, [t ' : A(<mylin(2,1)>,<Pg>)']);
0366 t_is(full(A(ll.i1.mylin(2,1):ll.iN.mylin(2,1), vv.i1.x(2,1):vv.iN.x(2,1))), [0 -1 0;0 -1 0;0 -1 0], 14, [t ' : A(<mylin(2,1)>,<x(2,1)>)']);
0367 
0368 %om
0369 
0370 %%-----  eval_nln_constraint  -----
0371 t = 'eval_nln_constraint';
0372 x = (1:om.getN('var'))';
0373 [g, dg] = om.eval_nln_constraint(x, 1);
0374 t_is(length(g), neN, 14, [t ' : length(g)']);
0375 t_ok(issparse(dg), [t ' : issparse(dg)']);
0376 t_is(size(dg), [neN, vN], 14, [t ' : size(dg)']);
0377 t_is(g, [7 8 9 3 3 4 5 6 7 6 7 8 7 8 9 7 8 9 27]', 14, [t ' : g']);
0378 e = [[  1 2 3 4 7 6 7;
0379         0 0 0 0 0 2 0;
0380         0 0 0 0 0 0 2;
0381         2 0 0 0 0 0 0 ] zeros(4, vN-7) ];
0382 t_is(full(dg(1:4, :)), e, 14, [t ' : dg(1:4, :)   [Pmise]']);
0383 e = [[3 2:vN]; [0 2 0 zeros(1, vN-3)]; [0 0 2 zeros(1, vN-3)]];
0384 t_is(full(dg(5:7, :)), e, 14, [t ' : dg(5:7, :)   [Qmise]']);
0385 e = [[0 0 0 0 6 6 7; 0 0 0 0 0 1 0] zeros(2, 10) [18 19; 0 0] zeros(2, vN-19)];
0386 t_is(full(dg(8:9, :)), e, 14, [t ' : dg(8:9, :)   [mynle(1,1)]']);
0387 e = [[0 0 0 0 6 6 7; 0 0 0 0 0 1 0; 0 0 0 0 0 0 1] zeros(3, 12) [20 21; 0 0; 0 0] zeros(3, vN-21)];
0388 t_is(full(dg(10:12, :)), e, 14, [t ' : dg(10:12, :) [mynle(1,2)]']);
0389 e = [[0 0 0 0 7 6 7; 0 0 0 0 0 2 0; 0 0 0 0 0 0 2] zeros(3, 14) [22 23 24; 0 0 0; 0 0 0] zeros(3, vN-24)];
0390 t_is(full(dg(13:15, :)), e, 14, [t ' : dg(13:15, :) [mynle(2,1)]']);
0391 e = [[0 0 0 0 7 6 7; 0 0 0 0 0 2 0; 0 0 0 0 0 0 2; 0 0 0 0 0 0 0] zeros(4, 17) [25 26; 0 0; 0 0; 2 0] zeros(4, vN-26)];
0392 t_is(full(dg(16:19, :)), e, 14, [t ' : dg(16:19, :) [mynle(2,2)]']);
0393 % g
0394 % full(dg)
0395 % full(dg)'
0396 
0397 [h, dh] = om.eval_nln_constraint(x, 0);
0398 t_is(length(h), niN, 14, [t ' : length(h)']);
0399 t_ok(issparse(dh), [t ' : issparse(dh)']);
0400 t_is(size(dh), [niN, vN], 14, [t ' : size(dh)']);
0401 t_is(h, [3 4 5 -1 0 6 6 7 7 8 7 8 9]', 14, [t ' : h']);
0402 e = [[  1 2 3 4 3 6 7;
0403         0 0 0 0 0 -2 0;
0404         0 0 0 0 0 0 -2 ] zeros(3, vN-7) ];
0405 t_is(full(dh(1:3, :)), e, 14, [t ' : dh(1:3, :)   [Pmisi]']);
0406 e = [[-1 2:vN]; [0 -2 zeros(1, vN-2)]];
0407 t_is(full(dh(4:5, :)), e, 14, [t ' : dh(5:7, :)   [Qmisi]']);
0408 e = [[0 0 0 0 6 6 7] zeros(1, 10) [18 19] zeros(1, vN-19)];
0409 t_is(full(dh(6, :)), e, 14, [t ' : dh(6, :)     [mynli(1,1)]']);
0410 e = [[0 0 0 0 6 6 7; 0 0 0 0 0 1 0] zeros(2, 12) [20 21; 0 0] zeros(2, vN-21)];
0411 t_is(full(dh(7:8, :)), e, 14, [t ' : dh(7:8, :)   [mynli(1,2)]']);
0412 e = [[0 0 0 0 7 6 7; 0 0 0 0 0 2 0] zeros(2, 14) [22 23 24; 0 0 0] zeros(2, vN-24)];
0413 t_is(full(dh(9:10, :)), e, 14, [t ' : dh(9:10, :)  [mynli(2,1)]']);
0414 e = [[0 0 0 0 7 6 7; 0 0 0 0 0 2 0; 0 0 0 0 0 0 2] zeros(3, 17) [25 26; 0 0; 0 0] zeros(3, vN-26)];
0415 t_is(full(dh(11:13, :)), e, 14, [t ' : dh(11:13, :) [mynli(2,2)]']);
0416 % h
0417 %full(dh)'
0418 
0419 t = 'eval_nln_constraint_hess';
0420 lam = (1:neN)'/100;
0421 d2G = om.eval_nln_constraint_hess(x, lam, 1);
0422 t_ok(issparse(d2G), [t ' : issparse(d2G)']);
0423 t_is(size(d2G), [vN, vN], 14, [t ' : size(d2G)']);
0424 % t_is(full(d2G(27:end, :)), zeros(vN-26, vN), 14, [t ' : d2G(27:end, :)']);
0425 % t_is(full(d2G(:, 27:end)), zeros(vN, vN-26), 14, [t ' : d2G(:, 27:end)']);
0426 e = sparse([1:3 5:7 25], [1:3 5:7 25], [22.09 12.06 13.07 41.48 46.53 43.48 27.19], vN, vN);
0427 t_is(d2G, e, 13, [t ' : d2G']);
0428 
0429 %d2G
0430 
0431 lam = -(1:niN)'/100;
0432 d2H = om.eval_nln_constraint_hess(x, lam, 0);
0433 t_ok(issparse(d2H), [t ' : issparse(d2H)']);
0434 t_is(size(d2H), [vN, vN], 14, [t ' : size(d2H)']);
0435 % t_is(full(d2H(27:end, :)), zeros(vN-26, vN), 14, [t ' : d2H(27:end, :)']);
0436 % t_is(full(d2H(:, 27:end)), zeros(vN, vN-26), 14, [t ' : d2H(:, 27:end)']);
0437 e = sparse([1:2 5:7], [1:2 5:7], [-9.04 -8.05 20.66 18.68 5.84], vN, vN);
0438 t_is(d2H, e, 13, [t ' : d2H']);
0439 
0440 %d2H
0441 
0442 %%-----  add_costs  -----
0443 t = 'add_costs';
0444 cN = 0;
0445 cNS = 0;
0446 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0447 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0448 
0449 t = 'om.add_costs(''ucost'', cp, {''Va'', ''Pg''})';
0450 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
0451             'Cw', [2;3]);
0452 om.add_costs('ucost', cp, {'Va', 'Pg'});
0453 cNS = cNS + 1; cN = cN + 2;
0454 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0455 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0456 
0457 t = 'om.add_costs(''vcost'', cp)';
0458 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
0459             'Cw', [2;3]);
0460 om.add_costs('vcost', cp);
0461 cNS = cNS + 1; cN = cN + 2;
0462 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0463 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0464 
0465 t = 'om.add_costs(''wc'', {2,2})';
0466 om.add_costs('wc', {2,2});
0467 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0468 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0469 
0470 for i = 1:2
0471     for j = 1:2
0472         t = 'om.add_costs(''wc'', {i, j}, cp, vs)';
0473         cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0474             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0475         cp.Cw = (i+j:-1:1)';
0476         if i == 2
0477             cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
0478         end
0479         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0480         om.add_costs('wc', {i, j}, cp, vs);
0481         cNS = cNS + 1; cN = cN + i+j;
0482         t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0483         t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0484     end
0485 end
0486 
0487 t = 'build_cost_params';
0488 om.build_cost_params();
0489 cp = om.get_cost_params();
0490 t_ok(isfield(cp, 'N'), t);
0491 
0492 %%-----  get_idx  -----
0493 t = 'get_idx : cost';
0494 [vv, ll, nne, nni, cc] = om.get_idx();
0495 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 14, [t ' : vcost']);
0496 t_is(size(cc.i1.wc), [2, 2], 14, [t ' : size(cc.i1.wc)']);
0497 t_is([cc.i1.wc(2,1) cc.iN.wc(2,1) cc.N.wc(2,1)], [10 12 3], 14, [t ' : wc(2,1)']);
0498 
0499 %%-----  get_cost_params  -----
0500 t = 'om.get_cost_params(''ucost'')';
0501 cp = om.get_cost_params('ucost');
0502 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0503 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0504 t_is(cp.Cw, [2;3], 14, [t, ' : Cw']);
0505 t_is(full(cp.H), zeros(2,2), 14, [t, ' : H']);
0506 t_is(cp.dd, ones(2,1),  14, [t, ' : dd']);
0507 t_is(cp.rh, zeros(2,1), 14, [t, ' : rh']);
0508 t_is(cp.kk, zeros(2,1), 14, [t, ' : kk']);
0509 t_is(cp.mm, ones(2,1),  14, [t, ' : mm']);
0510 
0511 t = 'om.get_cost_params(''vcost'')';
0512 cp = om.get_cost_params('vcost');
0513 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0514 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0515 t_is(cp.Cw, [2;3], 14, [t, ' : Cw']);
0516 t_is(full(cp.H), zeros(2,2), 14, [t, ' : H']);
0517 t_is(cp.dd, ones(2,1),  14, [t, ' : dd']);
0518 t_is(cp.rh, zeros(2,1), 14, [t, ' : rh']);
0519 t_is(cp.kk, zeros(2,1), 14, [t, ' : kk']);
0520 t_is(cp.mm, ones(2,1),  14, [t, ' : mm']);
0521 
0522 t = 'om.get_cost_params(''wc'') : error';
0523 try
0524     cp = om.get_cost_params('wc')
0525     t_ok(0, t);
0526 catch
0527     t_ok(strfind(lasterr, '@opt_model/get_cost_params: cost set ''wc'' requires an idx arg'), t);
0528 end
0529 
0530 t = 'om.get_cost_params(''wc'', {1,2})';
0531 cp = om.get_cost_params('wc', {1,2});
0532 N = sparse([1:3 1:3]', [vv.i1.Pg-1+(1:3) vv.i1.x(1,2)+ones(1,3)]', [ones(3,1);-ones(3,1)], 3, vN);
0533 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0534 t_is(cp.Cw, [3;2;1], 14, [t, ' : Cw']);
0535 t_is(full(cp.H), zeros(3,3), 14, [t, ' : H']);
0536 t_is(cp.dd, ones(3,1),  14, [t, ' : dd']);
0537 t_is(cp.rh, zeros(3,1), 14, [t, ' : rh']);
0538 t_is(cp.kk, zeros(3,1), 14, [t, ' : kk']);
0539 t_is(cp.mm, ones(3,1),  14, [t, ' : mm']);
0540 
0541 t = 'om.get_cost_params(''wc'', {2,1})';
0542 cp = om.get_cost_params('wc', {2,1});
0543 N = sparse([1:3 1:3]', [vv.i1.Pg-1+(1:3) vv.i1.x(2,1)+ones(1,3)]', [ones(3,1);-ones(3,1)], 3, vN);
0544 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0545 t_is(cp.Cw, [3;2;1], 14, [t, ' : Cw']);
0546 H = sparse(1:3, 1:3, 1:3, 3, 3);
0547 t_is(full(cp.H), full(H), 14, [t, ' : H']);
0548 t_is(cp.dd, ones(3,1),  14, [t, ' : dd']);
0549 t_is(cp.rh, zeros(3,1), 14, [t, ' : rh']);
0550 t_is(cp.kk, zeros(3,1), 14, [t, ' : kk']);
0551 t_is(cp.mm, ones(3,1),  14, [t, ' : mm']);
0552 
0553 t = 'om.get_cost_params()';
0554 cp = om.get_cost_params();
0555 t_ok(issparse(cp.N), [t ' : issparse(cp.N)']);
0556 t_is(size(cp.N), [cN, vN], 14, [t ' : size(cp.N)']);
0557 t_is(size(cp.H), [cN, cN], 14, [t ' : size(cp.H)']);
0558 t_is(length(cp.Cw), cN, 14, [t ' : length(cp.Cw)']);
0559 t_is(length(cp.dd), cN, 14, [t ' : length(cp.dd)']);
0560 t_is(length(cp.rh), cN, 14, [t ' : length(cp.rh)']);
0561 t_is(length(cp.kk), cN, 14, [t ' : length(cp.kk)']);
0562 t_is(length(cp.mm), cN, 14, [t ' : length(cp.mm)']);
0563 N = sparse([1:2 1:2]', [1:4]', [1 1 -1 -1]', 2, 4);
0564 Cw = [2;3];
0565 H = zeros(2,2);
0566 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Va:vv.iN.Va)), full(N), 14, [t ' : N(<vcost>,<Va>)']);
0567 N = sparse([1:2]', [1 3]', [2 2]', 2, 3);
0568 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Pg:vv.iN.Pg)), full(N), 14, [t ' : N(<vcost>,<Pg>)']);
0569 t_is(full(cp.Cw(cc.i1.vcost:cc.iN.vcost)), Cw, 14, [t ' : Cw(<vcost>)']);
0570 t_is(full(cp.H(cc.i1.vcost:cc.iN.vcost, cc.i1.vcost:cc.iN.vcost)), full(H), 14, [t ' : H(<vcost>,<vcost>)']);
0571 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
0572 t_is(full(cp.N(cc.i1.wc(1,2):cc.iN.wc(1,2), vv.i1.Pg:vv.iN.Pg)), full(N), 14, [t ' : N(<wc(1,2)>,<Pg>)']);
0573 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 2);
0574 Cw = [3;2;1];
0575 H = zeros(3,3);
0576 t_is(full(cp.N(cc.i1.wc(1,2):cc.iN.wc(1,2), vv.i1.x(1,2):vv.iN.x(1,2))), full(N), 14, [t ' : N(<wc(1,2)>,<x(1,2)>)']);
0577 t_is(full(cp.Cw(cc.i1.wc(1,2):cc.iN.wc(1,2))), Cw, 14, [t ' : Cw(<wc(1,2)>)']);
0578 t_is(full(cp.H(cc.i1.wc(1,2):cc.iN.wc(1,2), cc.i1.wc(1,2):cc.iN.wc(1,2))), full(H), 14, [t ' : H(<wc(1,2)>,<wc(1,2)>)']);
0579 
0580 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
0581 t_is(full(cp.N(cc.i1.wc(2,1):cc.iN.wc(2,1), vv.i1.Pg:vv.iN.Pg)), full(N), 14, [t ' : N(<wc(2,1)>,<Pg>)']);
0582 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 3);
0583 Cw = [3;2;1];
0584 H = full(sparse((1:3)', (1:3)', (1:3)', 3, 3));
0585 t_is(full(cp.N(cc.i1.wc(2,1):cc.iN.wc(2,1), vv.i1.x(2,1):vv.iN.x(2,1))), full(N), 14, [t ' : N(<wc(2,1)>,<x(2,1)>)']);
0586 t_is(full(cp.Cw(cc.i1.wc(2,1):cc.iN.wc(2,1))), Cw, 14, [t ' : Cw(<wc(2,1)>)']);
0587 t_is(full(cp.H(cc.i1.wc(2,1):cc.iN.wc(2,1), cc.i1.wc(2,1):cc.iN.wc(2,1))), full(H), 14, [t ' : H(<wc(2,1)>,<wc(2,1)>)']);
0588 
0589 t_is(cp.dd, ones(cN,1),  14, [t, ' : dd']);
0590 t_is(cp.rh, zeros(cN,1), 14, [t, ' : rh']);
0591 t_is(cp.kk, zeros(cN,1), 14, [t, ' : kk']);
0592 t_is(cp.mm, ones(cN,1),  14, [t, ' : mm']);
0593 
0594 %%-----  compute_cost  -----
0595 % f = om.compute_cost(x, name, idx)
0596 t = 'om.compute_cost(x)';
0597 x = [1:7 rand(1,10) 8:(vN-10)]';
0598 f = om.compute_cost(x);
0599 t_is(f, 343, 14, t);
0600 
0601 t = 'om.compute_cost(''ucost'')';
0602 f = om.compute_cost(x, 'ucost');
0603 t_is(f, 52, 14, t);
0604 
0605 t = 'om.compute_cost(''wc'', {2,1})';
0606 f = om.compute_cost(x, 'wc', {2,1});
0607 t_is(f, 91, 14, t);
0608 
0609 t = 'om.compute_cost(''wc'')';
0610 f = om.compute_cost(x, 'wc');
0611 t_is(f, 239, 14, t);
0612 
0613 % om
0614 % om = struct(om);
0615 
0616 t_end
0617 
0618 function [g, dg] = my_fcn(x, p1, p2)
0619 if iscell(x)
0620     xx = [];
0621     for k = 1:length(x)
0622         xx = [xx; x{k}];
0623     end
0624 else
0625     xx = x;
0626 end
0627 M = p1;
0628 N = length(xx);
0629 if M > N
0630     error('M <= length(x)');
0631 end
0632 g = xx(1:M) + p2;
0633 dg = sparse(1:M, 1:M, p2, M, N) + sparse(1, 1:N, xx, M, N);
0634 
0635 function d2G = my_hess(x, lam, p3)
0636 if iscell(x)
0637     xx = [];
0638     for k = 1:length(x)
0639         xx = [xx; x{k}];
0640     end
0641 else
0642     xx = x;
0643 end
0644 N = length(xx);
0645 M = length(lam);
0646 MM = min(M, N);
0647 d2G = sparse(1:MM, 1:MM, xx(1:MM) + lam(1:MM) + p3, N, N);
0648 %full(d2G(1:MM,1:MM))

Generated on Mon 24-Jun-2019 15:58:45 by m2html © 2005