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

t_opf_model

PURPOSE ^

T_OPF_MODEL Tests for OPF_MODEL.

SYNOPSIS ^

function t_opf_model(quiet)

DESCRIPTION ^

T_OPF_MODEL Tests for OPF_MODEL.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function t_opf_model(quiet)
0002 %T_OPF_MODEL Tests for OPF_MODEL.
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 = 591;
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_var  -----
0037 t = 'add_var';
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_var(''Va'', 4)';
0044 nVa = 4;
0045 om.add_var('Va', nVa);
0046 vNS = vNS + 1; vN = vN + nVa;
0047 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0048 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0049 
0050 t = 'om.add_var(''Pg'', 3, Pg0, Pgmin, Pgmax)';
0051 nPg = 3;
0052 om.add_var('Pg', nPg, [2;4;6], [1;2;3], [10;20;30]);
0053 vNS = vNS + 1; vN = vN + nPg;
0054 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0055 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0056 
0057 t = 'om.add_var(''Vm1'', 5, V0, Vmin, Vmax, ''I'')';
0058 V0 = [1;1;1;1;1];
0059 Vmin = zeros(5, 1);
0060 Vmax = 1 + 0.01*(1:5)';
0061 vt = 'I';
0062 om.add_var('Vm1', 5, V0, Vmin, Vmax, vt);
0063 vNS = vNS + 1; vN = vN + 5;
0064 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0065 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0066 
0067 t = 'om.add_var(''Vm2'', 5, V0, Vmin, Vmax, ''CIBIC'')';
0068 nVm2 = 5;
0069 vt = 'CIBIC';
0070 om.add_var('Vm2', nVm2, V0, Vmin, Vmax, vt);
0071 vNS = vNS + 1; vN = vN + nVm2;
0072 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0073 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0074 
0075 t = 'om.init_indexed_name(''var'', ''x'', dims)';
0076 om.init_indexed_name('var', 'x', {2,2});
0077 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0078 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0079 
0080 t = 'om.add_var(''x'', {1,1}, 2)';
0081 om.add_var('x', {1,1}, 2);
0082 vNS = vNS + 1; vN = vN + 2;
0083 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0084 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0085 
0086 t = 'om.add_var(''x'', {1,2}, 2, x0(1,2))';
0087 om.add_var('x', {1,2}, 2, [-1;-2]);
0088 vNS = vNS + 1; vN = vN + 2;
0089 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0090 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0091 
0092 t = 'om.add_var(''x'', {2,1}, 3)';
0093 om.add_var('x', {2,1}, 3);
0094 vNS = vNS + 1; vN = vN + 3;
0095 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0096 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0097 
0098 t = 'om.add_var(''x'', {2,2}, 2, x0(2,2), xmin(2,2), xmax(2,2))';
0099 om.add_var('x', {2,2}, 2, [1;0],[0;-1],[2;1]);
0100 vNS = vNS + 1; vN = vN + 2;
0101 t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0102 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0103 
0104 t = 'om.init_indexed_name(''var'', ''y'', {2,3,4})';
0105 om.init_indexed_name('var', 'y', {2,3,4});
0106 vt0 = {'C', 'I', 'B'};
0107 for i = 1:2
0108     for j = 1:3
0109         for k = 1:4
0110             n = i+j+k;
0111             if i == 1
0112                 vt = vt0{j};
0113             else
0114                 vt = char(vt0{j} * ones(1, n));
0115                 vt(j+1) = vt0{1+rem(j,3)};
0116             end
0117 %             fprintf('%d %d %d : %s\n', i, j, k, vt);
0118             t = sprintf('om.add_var(''y'', {%d,%d,%d}, y0, ymin, ymax, vt)', i,j,k);
0119             om.add_var('y', {i,j,k}, n, 10*(n:-1:1)', -1*(n:-1:1)', 100+(n:-1:1)', vt);
0120             vNS = vNS + 1; vN = vN + n;
0121             t_ok(om.getN('var') == vN, sprintf('%s : var.N  = %d', t, vN));
0122             t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0123         end
0124     end
0125 end
0126 
0127 %%-----  getN  -----
0128 t = 'om.getN(''var'', ''Pg'') == 3';
0129 t_ok(om.getN('var', 'Pg') == 3, t);
0130 
0131 t = 'size(om.getN(''var'', ''x'')) == [2 2]';
0132 t_is(size(om.getN('var', 'x')), [2,2], 14, t);
0133 
0134 t = 'om.getN(''var'', ''x'')(1,2) == 2';
0135 N = om.getN('var', 'x');
0136 t_is(N(1,2), 2, 14, t);
0137 
0138 t = 'om.getN(''var'', ''x'', {2,1}) == 3';
0139 t_is(om.getN('var', 'x', {2,1}), 3, 14, t);
0140 
0141 t = 'om.getN(''var'', ''y'', {2,1,3}) == 6';
0142 t_is(om.getN('var', 'y', {2,1,3}), 6, 14, t);
0143 
0144 t = 'om.getN(''var'')';
0145 t_is(om.getN('var'), vN, 14, t);
0146 
0147 %%-----  get_idx  -----
0148 t = 'get_idx : var';
0149 vv = om.get_idx();
0150 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
0151 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
0152 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)']);
0153 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
0154 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)']);
0155 
0156 t = 'get_idx(''var'')';
0157 vv = om.get_idx('var');
0158 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
0159 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
0160 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)']);
0161 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
0162 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)']);
0163 
0164 %%-----  params_var  -----
0165 t = 'om.params_var(''Va'')';
0166 [v0, vl, vu] = om.params_var('Va');
0167 t_ok(~any(v0), [t ' : v0']);
0168 t_ok(all(isinf(vl) & vl < 0), [t ' : vl']);
0169 t_ok(all(isinf(vu) & vu > 0), [t ' : vu']);
0170 
0171 t = 'om.params_var(''Pg'')';
0172 [v0, vl, vu] = om.params_var('Pg');
0173 t_is(v0, [2;4;6], 14, [t ' : v0']);
0174 t_is(vl, [1;2;3], 14, [t ' : vl']);
0175 t_is(vu, [10;20;30], 14, [t ' : vu']);
0176 
0177 t = 'om.params_var(''Vm1'')';
0178 [v0, vl, vu, vt] = om.params_var('Vm1');
0179 t_is(double(vt), double('I'), 14, [t ' : vt']);
0180 
0181 t = 'om.params_var(''Vm2'')';
0182 [v0, vl, vu, vt] = om.params_var('Vm2');
0183 t_is(double(vt), double('CIBIC'), 14, [t ' : vt']);
0184 
0185 t = 'om.params_var(''x'')';
0186 [v0, vl, vu, vt] = om.params_var('x');
0187 t_is(size(v0), [2,2], 14, [t ' : size(v0)']);
0188 t_is(v0{2,2}, [1;0], 14, [t ' : v0{2,2}']);
0189 t_is(vl{2,2}, [0;-1], 14, [t ' : vl{2,2}']);
0190 t_is(vu{2,2}, [2;1], 14, [t ' : vu{2,2}']);
0191 t_is(double(vt{2,2}), double('C'), 14, [t ' : vt{2,2}']);
0192 
0193 for i = 1:2
0194     for j = 1:3
0195         for k = 1:4
0196             n = i+j+k;
0197             if i == 1
0198                 vt = vt0{j};
0199             else
0200                 vt = char(vt0{j} * ones(1, n));
0201                 vt(j+1) = vt0{1+rem(j,3)};
0202             end
0203             t = sprintf('om.params_var(''y'', {%d,%d,%d})', i, j, k);
0204             [v0, vl, vu, gvt] = om.params_var('y', {i,j,k});
0205             t_is(v0, 10*(n:-1:1)', 14, [t ' : v0']);
0206             t_is(vl, -1*(n:-1:1)', 14, [t ' : vl']);
0207             t_is(vu, 100+(n:-1:1)', 14, [t ' : vu']);
0208             t_is(gvt, vt, 14, [t ' : vt']);
0209         end
0210     end
0211 end
0212 
0213 t = 'om.params_var()';
0214 [v0, vl, vu, vt] = om.params_var();
0215 t_ok(length(v0) == om.getN('var'), [t ' : length(v0)']);
0216 t_ok(length(vl) == om.getN('var'), [t ' : length(vl)']);
0217 t_ok(length(vu) == om.getN('var'), [t ' : length(vu)']);
0218 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))']);
0219 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))']);
0220 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))']);
0221 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))']);
0222 n = 8;
0223 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))']);
0224 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))']);
0225 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))']);
0226 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))']);
0227 vt0 = 'CCCCCCCIIIIICIBICCCCCCCCCCCCCCCCCCCCCCCCCCCCIIIIIIIIIIIIIIIIIIIIIIBBBBBBBBBBBBBBBBBBBBBBBBBBCICCCICCCCICCCCCICCCCCIIBIIIIBIIIIIBIIIIIIBIIIIIBBBCBBBBBCBBBBBBCBBBBBBBCBBBBB';
0228 t_is(vt, vt0, 14, [t ' : vt']);
0229 
0230 %%-----  varsets_len  -----
0231 t = 'om.varsets_len(vs) : ';
0232 vs = om.varsets_cell2struct({'Pg'});
0233 t_is(om.varsets_len(vs), 3, 14, [t '{''Pg''}']);
0234 
0235 vs = om.varsets_cell2struct({'Pg', 'Va'});
0236 t_is(om.varsets_len(vs), 7, 14, [t '{''Pg'', ''Va''}']);
0237 
0238 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0239 t_is(om.varsets_len(vs), 5, 14, [t '''x'', {{1,1},{2,1}}']);
0240 
0241 vs = om.varsets_cell2struct({'x'});
0242 t_is(om.varsets_len(vs), 9, 14, [t '{''x''}']);
0243 
0244 vs = om.varsets_cell2struct({'x', 'y', 'Pg'});
0245 t_is(om.varsets_len(vs), 156, 14, [t '{''x'', ''y'', ''Pg''}']);
0246 
0247 vs = om.varsets_cell2struct({});
0248 t_is(om.varsets_len(vs), om.var.N, 14, [t '<all>']);
0249 
0250 %%-----  varsets_idx  -----
0251 t = 'om.varsets_idx(vs) : ';
0252 vv = om.get_idx('var');
0253 vs = om.varsets_cell2struct({'Pg'});
0254 t_is(om.varsets_idx(vs), [vv.i1.Pg:vv.iN.Pg], 14, [t '{''Pg''}']);
0255 
0256 vs = om.varsets_cell2struct({'Pg', 'Va'});
0257 t_is(om.varsets_idx(vs), [vv.i1.Pg:vv.iN.Pg vv.i1.Va:vv.iN.Va], 14, [t '{''Pg'', ''Va''}']);
0258 
0259 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0260 t_is(om.varsets_idx(vs), [vv.i1.x(1,1):vv.iN.x(1,1) vv.i1.x(2,1):vv.iN.x(2,1)], 14, [t '''x'', {{1,1},{2,1}}']);
0261 
0262 vs = om.varsets_cell2struct({'x'});
0263 t_is(om.varsets_idx(vs), [vv.i1.x(1,1):vv.iN.x(1,1) vv.i1.x(1,2):vv.iN.x(1,2) vv.i1.x(2,1):vv.iN.x(2,1) vv.i1.x(2,2):vv.iN.x(2,2)], 14, [t '{''x''}']);
0264 
0265 vs = om.varsets_cell2struct({});
0266 t_is(om.varsets_idx(vs), 1:om.var.N, 14, [t '<all>']);
0267 
0268 %%-----  varsets_x  -----
0269 t = 'varsets_x(x, vs) : ';
0270 x = (1:om.var.N)';
0271 vs = om.varsets_cell2struct({'Pg'});
0272 xx = om.varsets_x(x, vs);
0273 t_is(length(xx), 1, 14, [t '{''Pg''} : length']);
0274 t_is(xx{1}, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''Pg''} : 1']);
0275 
0276 vs = om.varsets_cell2struct({'Pg', 'Va'});
0277 xx = om.varsets_x(x, vs);
0278 t_is(length(xx), 2, 14, [t '{''Pg'', ''Va''} : length']);
0279 t_is(xx{1}, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''Pg'', ''Va''} : 1']);
0280 t_is(xx{2}, [vv.i1.Va:vv.iN.Va]', 14, [t '{''Pg'', ''Va''} : 2']);
0281 
0282 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0283 xx = om.varsets_x(x, vs);
0284 t_is(length(xx), 2, 14, [t '''x'', {{1,1},{2,1}} : length']);
0285 t_is(xx{1}, [vv.i1.x(1,1):vv.iN.x(1,1)]', 14, [t '''x'', {{1,1},{2,1}} : 1']);
0286 t_is(xx{2}, [vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '''x'', {{1,1},{2,1}} : 2']);
0287 
0288 vs = om.varsets_cell2struct({'x'});
0289 xx = om.varsets_x(x, vs);
0290 t_is(length(xx), 4, 14, [t '{''x''} : length']);
0291 t_is(xx{1}, [vv.i1.x(1,1):vv.iN.x(1,1)]', 14, [t '{''x''} : 1']);
0292 t_is(xx{2}, [vv.i1.x(1,2):vv.iN.x(1,2)]', 14, [t '{''x''} : 2']);
0293 t_is(xx{3}, [vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '{''x''} : 3']);
0294 t_is(xx{4}, [vv.i1.x(2,2):vv.iN.x(2,2)]', 14, [t '{''x''} : 4']);
0295 
0296 vs = om.varsets_cell2struct({'x', 'y', 'Pg'});
0297 xx = om.varsets_x(x, vs);
0298 t_is(length(xx), 29, 14, [t '{''x'', ''y'', ''Pg''} : length']);
0299 t_is(xx{ 1}, [vv.i1.x(1,1):vv.iN.x(1,1)]', 14, [t '{''x'', ''y'', ''Pg''} :  1']);
0300 t_is(xx{ 2}, [vv.i1.x(1,2):vv.iN.x(1,2)]', 14, [t '{''x'', ''y'', ''Pg''} :  2']);
0301 t_is(xx{ 3}, [vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '{''x'', ''y'', ''Pg''} :  3']);
0302 t_is(xx{ 4}, [vv.i1.x(2,2):vv.iN.x(2,2)]', 14, [t '{''x'', ''y'', ''Pg''} :  4']);
0303 t_is(xx{ 5}, [vv.i1.y(1,1,1):vv.iN.y(1,1,1)]', 14, [t '{''x'', ''y'', ''Pg''} :  5']);
0304 t_is(xx{ 6}, [vv.i1.y(1,1,2):vv.iN.y(1,1,2)]', 14, [t '{''x'', ''y'', ''Pg''} :  6']);
0305 t_is(xx{ 7}, [vv.i1.y(1,1,3):vv.iN.y(1,1,3)]', 14, [t '{''x'', ''y'', ''Pg''} :  7']);
0306 t_is(xx{ 8}, [vv.i1.y(1,1,4):vv.iN.y(1,1,4)]', 14, [t '{''x'', ''y'', ''Pg''} :  8']);
0307 t_is(xx{ 9}, [vv.i1.y(1,2,1):vv.iN.y(1,2,1)]', 14, [t '{''x'', ''y'', ''Pg''} :  9']);
0308 t_is(xx{10}, [vv.i1.y(1,2,2):vv.iN.y(1,2,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 10']);
0309 t_is(xx{11}, [vv.i1.y(1,2,3):vv.iN.y(1,2,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 11']);
0310 t_is(xx{12}, [vv.i1.y(1,2,4):vv.iN.y(1,2,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 12']);
0311 t_is(xx{13}, [vv.i1.y(1,3,1):vv.iN.y(1,3,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 13']);
0312 t_is(xx{14}, [vv.i1.y(1,3,2):vv.iN.y(1,3,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 14']);
0313 t_is(xx{15}, [vv.i1.y(1,3,3):vv.iN.y(1,3,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 15']);
0314 t_is(xx{16}, [vv.i1.y(1,3,4):vv.iN.y(1,3,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 16']);
0315 t_is(xx{17}, [vv.i1.y(2,1,1):vv.iN.y(2,1,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 17']);
0316 t_is(xx{18}, [vv.i1.y(2,1,2):vv.iN.y(2,1,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 18']);
0317 t_is(xx{19}, [vv.i1.y(2,1,3):vv.iN.y(2,1,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 19']);
0318 t_is(xx{20}, [vv.i1.y(2,1,4):vv.iN.y(2,1,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 20']);
0319 t_is(xx{21}, [vv.i1.y(2,2,1):vv.iN.y(2,2,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 21']);
0320 t_is(xx{22}, [vv.i1.y(2,2,2):vv.iN.y(2,2,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 22']);
0321 t_is(xx{23}, [vv.i1.y(2,2,3):vv.iN.y(2,2,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 23']);
0322 t_is(xx{24}, [vv.i1.y(2,2,4):vv.iN.y(2,2,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 24']);
0323 t_is(xx{25}, [vv.i1.y(2,3,1):vv.iN.y(2,3,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 25']);
0324 t_is(xx{26}, [vv.i1.y(2,3,2):vv.iN.y(2,3,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 26']);
0325 t_is(xx{27}, [vv.i1.y(2,3,3):vv.iN.y(2,3,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 27']);
0326 t_is(xx{28}, [vv.i1.y(2,3,4):vv.iN.y(2,3,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 28']);
0327 t_is(xx{29}, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''x'', ''y'', ''Pg''} : 29']);
0328 
0329 vs = om.varsets_cell2struct({});
0330 xx = om.varsets_x(x, vs);
0331 t_is(length(xx), om.var.N, 14, [t '<all> : length']);
0332 t_is(xx, [1:om.var.N]', 14, [t '<all>']);
0333 
0334 t = 'varsets_x(x, vs, ''vector'') : ';
0335 vs = om.varsets_cell2struct({'Pg'});
0336 xx = om.varsets_x(x, vs, 'vector');
0337 t_is(length(xx), vv.N.Pg, 14, [t '{''Pg''} : length']);
0338 t_is(xx, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''Pg''}']);
0339 
0340 vs = om.varsets_cell2struct({'Pg', 'Va'});
0341 xx = om.varsets_x(x, vs, 'vector');
0342 t_is(length(xx), vv.N.Va + vv.N.Pg, 14, [t '{''Pg'', ''Va''} : length']);
0343 t_is(xx, [vv.i1.Pg:vv.iN.Pg vv.i1.Va:vv.iN.Va]', 14, [t '{''Pg'', ''Va''}']);
0344 
0345 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0346 xx = om.varsets_x(x, vs, 'vector');
0347 t_is(length(xx), vv.N.x(1,1) + vv.N.x(2,1), 14, [t '''x'', {{1,1},{2,1}} : length']);
0348 t_is(xx, [vv.i1.x(1,1):vv.iN.x(1,1) vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '''x'', {{1,1},{2,1}}']);
0349 
0350 vs = om.varsets_cell2struct({'x'});
0351 xx = om.varsets_x(x, vs, 'vector');
0352 t_is(length(xx), sum(vv.N.x(:)), 14, [t '{''x''} : length']);
0353 t_is(xx, [vv.i1.x(1,1):vv.iN.x(1,1) vv.i1.x(1,2):vv.iN.x(1,2) vv.i1.x(2,1):vv.iN.x(2,1) vv.i1.x(2,2):vv.iN.x(2,2)]', 14, [t '{''x''}']);
0354 
0355 vs = om.varsets_cell2struct({'x', 'y', 'Pg'});
0356 xx = om.varsets_x(x, vs, 'vector');
0357 t_is(length(xx), sum(vv.N.x(:))+sum(vv.N.y(:))+vv.N.Pg, 14, [t '{''x'', ''y'', ''Pg''} : length']);
0358 t_is(xx, [  vv.i1.x(1,1):vv.iN.x(1,1) vv.i1.x(1,2):vv.iN.x(1,2) ...
0359             vv.i1.x(2,1):vv.iN.x(2,1) vv.i1.x(2,2):vv.iN.x(2,2) ...
0360             vv.i1.y(1,1,1):vv.iN.y(1,1,1) vv.i1.y(1,1,2):vv.iN.y(1,1,2) ...
0361             vv.i1.y(1,1,3):vv.iN.y(1,1,3) vv.i1.y(1,1,4):vv.iN.y(1,1,4) ...
0362             vv.i1.y(1,2,1):vv.iN.y(1,2,1) vv.i1.y(1,2,2):vv.iN.y(1,2,2) ...
0363             vv.i1.y(1,2,3):vv.iN.y(1,2,3) vv.i1.y(1,2,4):vv.iN.y(1,2,4) ...
0364             vv.i1.y(1,3,1):vv.iN.y(1,3,1) vv.i1.y(1,3,2):vv.iN.y(1,3,2) ...
0365             vv.i1.y(1,3,3):vv.iN.y(1,3,3) vv.i1.y(1,3,4):vv.iN.y(1,3,4) ...
0366             vv.i1.y(2,1,1):vv.iN.y(2,1,1) vv.i1.y(2,1,2):vv.iN.y(2,1,2) ...
0367             vv.i1.y(2,1,3):vv.iN.y(2,1,3) vv.i1.y(2,1,4):vv.iN.y(2,1,4) ...
0368             vv.i1.y(2,2,1):vv.iN.y(2,2,1) vv.i1.y(2,2,2):vv.iN.y(2,2,2) ...
0369             vv.i1.y(2,2,3):vv.iN.y(2,2,3) vv.i1.y(2,2,4):vv.iN.y(2,2,4) ...
0370             vv.i1.y(2,3,1):vv.iN.y(2,3,1) vv.i1.y(2,3,2):vv.iN.y(2,3,2) ...
0371             vv.i1.y(2,3,3):vv.iN.y(2,3,3) vv.i1.y(2,3,4):vv.iN.y(2,3,4) ...
0372             vv.i1.Pg:vv.iN.Pg]', 14, [t '{''x'', ''y'', ''Pg''}']);
0373 
0374 vs = om.varsets_cell2struct({});
0375 xx = om.varsets_x(x, vs, 'vector');
0376 t_is(length(xx), om.var.N, 14, [t '<all> : length']);
0377 t_is(xx, [1:om.var.N]', 14, [t '<all>']);
0378 
0379 %%-----  add_lin_constraint  -----
0380 t = 'add_lin_constraint';
0381 lN = 0;
0382 lNS = 0;
0383 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0384 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0385 
0386 t = 'om.add_lin_constraint(''Pmis'', A, l, u, {''Va'', ''Pg''})';
0387 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);
0388 l = -(1:3)'; u = (1:3)';
0389 om.add_lin_constraint('Pmis', A, l, u, {'Va', 'Pg'});
0390 lNS = lNS + 1; lN = lN + 3;
0391 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0392 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0393 
0394 t = 'om.add_lin_constraint(''Qmis'', A, l, u)';
0395 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);
0396 om.add_lin_constraint('Qmis', A, l, u);
0397 lNS = lNS + 1; lN = lN + 3;
0398 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0399 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0400 
0401 t = 'om.init_indexed_name(''lin'', ''mylin'', {2, 2})';
0402 om.init_indexed_name('lin', 'mylin', {2, 2});
0403 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0404 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0405 
0406 for i = 1:2
0407     for j = 1:2
0408         t = sprintf('om.add_lin_constraint(''mylin'', {%d,%d}, A, l, u, vs)', i,j);
0409         A = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0410             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0411         l = -ones(i+j, 1); u = [];
0412         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0413         om.add_lin_constraint('mylin', {i, j}, A, l, u, vs);
0414         lNS = lNS + 1; lN = lN + i+j;
0415         t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0416         t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0417     end
0418 end
0419 
0420 %%-----  add_nln_constraint (equality)  -----
0421 t = 'add_nln_constraint (equality)';
0422 neN = 0;
0423 neNS = 0;
0424 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0425 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0426 
0427 t = 'om.add_nln_constraint(''Pmise'', N, 1, fcn, hess, {''Pg'', ''Va''})';
0428 N = 4;
0429 fcn = @(x)my_fcn(x, N, 2);
0430 hess = @(x, lam)my_hess(x, lam, 10);
0431 om.add_nln_constraint('Pmise', N, 1, fcn, hess, {'Pg', 'Va'});
0432 neNS = neNS + 1; neN = neN + N;
0433 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0434 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0435 
0436 t = 'om.add_nln_constraint(''Qmise'', N, 1, fcn, hess)';
0437 N = 3;
0438 fcn = @(x)my_fcn(x, N, 2);
0439 hess = @(x, lam)my_hess(x, lam, 10);
0440 om.add_nln_constraint('Qmise', N, 1, fcn, hess);
0441 neNS = neNS + 1; neN = neN + N;
0442 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0443 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0444 
0445 t = 'om.add_nln_constraint({''P'',''Q'',''R''}, [3;2;1], 1, fcn, hess, {''Pg'', ''Va''})';
0446 N = [3;2;1];
0447 fcn = @(x)my_fcn(x, sum(N), 2);
0448 hess = @(x, lam)my_hess(x, lam, 10);
0449 om.add_nln_constraint({'P', 'Q', 'R'}, N, 1, fcn, hess, {'Pg', 'Va'});
0450 neNS = neNS + length(N); neN = neN + sum(N);
0451 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0452 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0453 
0454 t = 'om.init_indexed_name(''nle'', ''mynle'', {2, 2})';
0455 om.init_indexed_name('nle', 'mynle', {2, 2});
0456 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0457 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0458 
0459 for i = 1:2
0460     for j = 1:2
0461         t = sprintf('om.add_nln_constraint(''mynle'', {%d,%d}, N, 1, fcn, hess, vs)', i,j);
0462         N = i+j;
0463         fcn = @(x)my_fcn(x, N, i);
0464         hess = @(x, lam)my_hess(x, lam, j);
0465         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0466         om.add_nln_constraint('mynle', {i, j}, N, 1, fcn, hess, vs);
0467         neNS = neNS + 1; neN = neN + N;
0468         t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0469         t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0470     end
0471 end
0472 
0473 %%-----  add_nln_constraint (inequality)  -----
0474 t = 'add_nln_constraint (inequality)';
0475 niN = 0;
0476 niNS = 0;
0477 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0478 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0479 
0480 t = 'om.add_nln_constraint(''Pmisi'', N, 0, fcn, hess, {''Pg'', ''Va''})';
0481 N = 3;
0482 fcn = @(x)my_fcn(x, N, -2);
0483 hess = @(x, lam)my_hess(x, lam, -10);
0484 om.add_nln_constraint('Pmisi', N, 0, fcn, hess, {'Pg', 'Va'});
0485 niNS = niNS + 1; niN = niN + N;
0486 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0487 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0488 
0489 t = 'om.add_nln_constraint(''Qmisi'', N, 0, fcn, hess)';
0490 N = 2;
0491 fcn = @(x)my_fcn(x, N, -2);
0492 hess = @(x, lam)my_hess(x, lam, -10);
0493 om.add_nln_constraint('Qmisi', N, 0, fcn, hess);
0494 niNS = niNS + 1; niN = niN + N;
0495 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0496 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0497 
0498 t = 'om.init_indexed_name(''nli'', ''mynli'', {2, 2})';
0499 om.init_indexed_name('nli', 'mynli', {2, 2});
0500 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0501 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0502 
0503 for i = 1:2
0504     for j = 1:2
0505         t = sprintf('om.add_nln_constraint(''mynli'', {%d,%d}, N, 0, fcn, hess, vs)', i,j);
0506         N = i+j-1;
0507         fcn = @(x)my_fcn(x, N, i);
0508         hess = @(x, lam)my_hess(x, lam, j);
0509         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0510         om.add_nln_constraint('mynli', {i, j}, N, 0, fcn, hess, vs);
0511         niNS = niNS + 1; niN = niN + N;
0512         t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0513         t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0514     end
0515 end
0516 
0517 %%-----  get_idx  -----
0518 t = 'get_idx : lin';
0519 [vv, ll] = om.get_idx();
0520 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0521 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0522 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)']);
0523 
0524 t = 'get_idx(''lin'')';
0525 ll = om.get_idx('lin');
0526 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0527 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0528 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)']);
0529 
0530 %%-----  params_lin_constraint  -----
0531 t = 'params_lin_constraint';
0532 [A, l, u] = om.params_lin_constraint();
0533 t_ok(issparse(A), [t ' : issparse(A)']);
0534 t_is(size(A), [lN, vN], 14, [t ' : size(A)']);
0535 t_is(length(l), lN, 14, [t ' : length(l)']);
0536 t_is(length(u), lN, 14, [t ' : length(u)']);
0537 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);
0538 t_is(full(A(ll.i1.Qmis:ll.iN.Qmis, :)), full(AA), 14, [t ' : A(<Qmis>,:)']);
0539 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>)']);
0540 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)>)']);
0541 
0542 %om
0543 
0544 %%-----  eval_nln_constraint  -----
0545 t = 'eval_nln_constraint';
0546 x = (1:om.var.N)';
0547 [g, dg] = om.eval_nln_constraint(x, 1);
0548 t_is(length(g), neN, 14, [t ' : length(g)']);
0549 t_ok(issparse(dg), [t ' : issparse(dg)']);
0550 t_is(size(dg), [neN, vN], 14, [t ' : size(dg)']);
0551 t_is(g, [7 8 9 3 3 4 5 7 8 9 3 4 5 6 7 6 7 8 7 8 9 7 8 9 27]', 14, [t ' : g']);
0552 e = [[  1 2 3 4 7 6 7;
0553         0 0 0 0 0 2 0;
0554         0 0 0 0 0 0 2;
0555         2 0 0 0 0 0 0 ] zeros(4, vN-7) ];
0556 t_is(full(dg(1:4, :)), e, 14, [t ' : dg(1:4, :)   [Pmise]']);
0557 e = [[3 2:vN]; [0 2 0 zeros(1, vN-3)]; [0 0 2 zeros(1, vN-3)]];
0558 t_is(full(dg(5:7, :)), e, 14, [t ' : dg(5:7, :)   [Qmise]']);
0559 e = [[  1 2 3 4 7 6 7;
0560         0 0 0 0 0 2 0;
0561         0 0 0 0 0 0 2;
0562         2 0 0 0 0 0 0;
0563         0 2 0 0 0 0 0;
0564         0 0 2 0 0 0 0 ] zeros(6, vN-7) ];
0565 t_is(full(dg(8:13, :)), e, 14, [t ' : dg(8:13, :)  [mynle(1,1)]']);
0566 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)];
0567 t_is(full(dg(14:15, :)), e, 14, [t ' : dg(14:15, :) [mynle(1,1)]']);
0568 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)];
0569 t_is(full(dg(16:18, :)), e, 14, [t ' : dg(16:18, :) [mynle(1,2)]']);
0570 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)];
0571 t_is(full(dg(19:21, :)), e, 14, [t ' : dg(19:21, :) [mynle(2,1)]']);
0572 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)];
0573 t_is(full(dg(22:25, :)), e, 14, [t ' : dg(22:25, :) [mynle(2,2)]']);
0574 % g
0575 % full(dg)
0576 % full(dg)'
0577 
0578 [h, dh] = om.eval_nln_constraint(x, 0);
0579 t_is(length(h), niN, 14, [t ' : length(h)']);
0580 t_ok(issparse(dh), [t ' : issparse(dh)']);
0581 t_is(size(dh), [niN, vN], 14, [t ' : size(dh)']);
0582 t_is(h, [3 4 5 -1 0 6 6 7 7 8 7 8 9]', 14, [t ' : h']);
0583 e = [[  1 2 3 4 3 6 7;
0584         0 0 0 0 0 -2 0;
0585         0 0 0 0 0 0 -2 ] zeros(3, vN-7) ];
0586 t_is(full(dh(1:3, :)), e, 14, [t ' : dh(1:3, :)   [Pmisi]']);
0587 e = [[-1 2:vN]; [0 -2 zeros(1, vN-2)]];
0588 t_is(full(dh(4:5, :)), e, 14, [t ' : dh(5:7, :)   [Qmisi]']);
0589 e = [[0 0 0 0 6 6 7] zeros(1, 10) [18 19] zeros(1, vN-19)];
0590 t_is(full(dh(6, :)), e, 14, [t ' : dh(6, :)     [mynli(1,1)]']);
0591 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)];
0592 t_is(full(dh(7:8, :)), e, 14, [t ' : dh(7:8, :)   [mynli(1,2)]']);
0593 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)];
0594 t_is(full(dh(9:10, :)), e, 14, [t ' : dh(9:10, :)  [mynli(2,1)]']);
0595 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)];
0596 t_is(full(dh(11:13, :)), e, 14, [t ' : dh(11:13, :) [mynli(2,2)]']);
0597 % h
0598 %full(dh)'
0599 
0600 t = 'eval_nln_constraint_hess';
0601 lam = (1:neN)'/100;
0602 d2G = om.eval_nln_constraint_hess(x, lam, 1);
0603 t_ok(issparse(d2G), [t ' : issparse(d2G)']);
0604 t_is(size(d2G), [vN, vN], 14, [t ' : size(d2G)']);
0605 % t_is(full(d2G(27:end, :)), zeros(vN-26, vN), 14, [t ' : d2G(27:end, :)']);
0606 % t_is(full(d2G(:, 27:end)), zeros(vN, vN-26), 14, [t ' : d2G(:, 27:end)']);
0607 e = sparse([1:3 5:7 25], [1:3 5:7 25], [33.2 24.18 26.2 56.8 62.86 60.76 27.25], vN, vN);
0608 t_is(d2G, e, 13, [t ' : d2G']);
0609 
0610 % d2G
0611 
0612 lam = -(1:niN)'/100;
0613 d2H = om.eval_nln_constraint_hess(x, lam, 0);
0614 t_ok(issparse(d2H), [t ' : issparse(d2H)']);
0615 t_is(size(d2H), [vN, vN], 14, [t ' : size(d2H)']);
0616 % t_is(full(d2H(27:end, :)), zeros(vN-26, vN), 14, [t ' : d2H(27:end, :)']);
0617 % t_is(full(d2H(:, 27:end)), zeros(vN, vN-26), 14, [t ' : d2H(:, 27:end)']);
0618 e = sparse([1:2 5:7], [1:2 5:7], [-9.04 -8.05 20.66 18.68 5.84], vN, vN);
0619 t_is(d2H, e, 13, [t ' : d2H']);
0620 
0621 %d2H
0622 
0623 %%-----  add_quad_cost  -----
0624 t = 'add_quad_cost';
0625 qcN = 0;
0626 qcNS = 0;
0627 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0628 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0629 
0630 t = 'om.add_quad_cost(''qc1'', <mat>Q, c, k, {''Pg'', ''Va''})';
0631 n = nVa + nPg;
0632 Q1 = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n:-1:1, 1:n, n, n);
0633 c1 = 10*(1:n)';
0634 k1 = n;
0635 om.add_quad_cost('qc1', Q1, c1, k1, {'Pg', 'Va'});
0636 qcNS = qcNS + 1; qcN = qcN + 1;
0637 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0638 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0639 
0640 t = 'om.add_quad_cost(''qc2'', <mat>Q, c)';
0641 n = om.getN('var');
0642 Q2 = sparse(1, 1:n, 1:n, n, n) + sparse(1:n, 1, n:-1:1, n, n);
0643 c2 = 10*(n:-1:1)';
0644 om.add_quad_cost('qc2', Q2, c2);
0645 qcNS = qcNS + 1; qcN = qcN + 1;
0646 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0647 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0648 
0649 t = 'om.add_quad_cost(''qc3'', <vec>Q, c, k, {''Vm2'', ''Pg''})';
0650 n = nVm2 + nPg;
0651 Q3 = 2*(1:n)';
0652 c3 = -1*(1:n)';
0653 k3 = -n;
0654 om.add_quad_cost('qc3', Q3, c3, k3, {'Vm2', 'Pg'});
0655 qcNS = qcNS + 1; qcN = qcN + n;
0656 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0657 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0658 
0659 t = 'om.add_quad_cost(''qc4'', <vec>Q, [], 0, vs)';
0660 n = om.getN('var', 'x', {2,1}) + om.getN('var', 'y', {1,1,1});
0661 Q4 = 1./(1:n)';
0662 vs = struct('name', {'x', 'y'}, 'idx', {{2,1}, {1,1,1}});
0663 om.add_quad_cost('qc4', Q4, [], 0, vs);
0664 qcNS = qcNS + 1; qcN = qcN + n;
0665 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0666 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0667 
0668 t = 'om.add_quad_cost(''qc5'', [], c, k, {''Pg'', ''Va''})';
0669 n = nVa + nPg;
0670 c5 = 100*(1:n)';
0671 k5 = (1:n)';
0672 om.add_quad_cost('qc5', [], c5, k5, {'Pg', 'Va'});
0673 qcNS = qcNS + 1; qcN = qcN + n;
0674 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0675 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0676 
0677 t = 'om.add_quad_cost(''qc6'', [], c)';
0678 n = om.getN('var');
0679 c6 = -(1:n)';
0680 om.add_quad_cost('qc6', [], c6);
0681 qcNS = qcNS + 1; qcN = qcN + n;
0682 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0683 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0684 
0685 t = 'om.init_indexed_name(''qdc'', ''qc'', {2,2})';
0686 om.init_indexed_name('qdc', 'qc', {2,2});
0687 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0688 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0689 
0690 for i = 1:2
0691     for j = 1:2
0692         t = 'om.add_quad_cost(''qc'', {i, j}, cp, vs)';
0693         n = nPg + om.getN('var', 'x', {i,j});
0694         QQ = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n*ones(n,1), 1:n, n, n);
0695         cc = -2*(1:n)';
0696         kk = 1000;
0697         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0698         om.add_quad_cost('qc', {i, j}, QQ, cc, kk, vs);
0699         qcNS = qcNS + 1; qcN = qcN + 1;
0700         t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0701         t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0702     end
0703 end
0704 
0705 %%-----  params_quad_cost  -----
0706 t = 'om.params_quad_cost(''qc1'')';
0707 [Q, c, k, vs] = om.params_quad_cost('qc1');
0708 t_is(Q, Q1, 14, [t, ' : Q']);
0709 t_is(c, c1, 14, [t, ' : c']);
0710 t_is(k, k1, 14, [t, ' : k']);
0711 vs1 = struct('name', {'Pg', 'Va'}, 'idx', {{}, {}});
0712 t_ok(isequal(vs, vs1), [t, ' : vs']);
0713 
0714 t = 'om.params_quad_cost(''qc2'')';
0715 [Q, c, k, vs] = om.params_quad_cost('qc2');
0716 t_is(Q, Q2, 14, [t, ' : Q']);
0717 t_is(c, c2, 14, [t, ' : c']);
0718 t_is(k, 0, 14, [t, ' : k']);
0719 t_ok(isequal(vs, {}), [t, ' : vs']);
0720 
0721 t = 'om.params_quad_cost(''qc3'')';
0722 [Q, c, k] = om.params_quad_cost('qc3');
0723 t_is(Q, Q3, 14, [t, ' : Q']);
0724 t_is(c, c3, 14, [t, ' : c']);
0725 t_is(k, k3, 14, [t, ' : k']);
0726 
0727 t = 'om.params_quad_cost(''qc4'')';
0728 [Q, c] = om.params_quad_cost('qc4');
0729 t_is(Q, Q4, 14, [t, ' : Q']);
0730 t_ok(isempty(c), [t, ' : c']);
0731 % t_is(k, 0, 14, [t, ' : k']);
0732 
0733 t = 'om.params_quad_cost(''qc5'')';
0734 [Q, c, k] = om.params_quad_cost('qc5');
0735 t_ok(isempty(Q), [t, ' : Q']);
0736 t_is(c, c5, 14, [t, ' : c']);
0737 t_is(k, k5, 14, [t, ' : k']);
0738 
0739 t = 'om.params_quad_cost(''qc6'')';
0740 [Q, c, k] = om.params_quad_cost('qc6');
0741 t_ok(isempty(Q), [t, ' : Q']);
0742 t_is(c, c6, 14, [t, ' : c']);
0743 t_is(k, 0, 14, [t, ' : k']);
0744 
0745 for i = 1:2
0746     for j = 1:2
0747         t = sprintf('om.params_quad_cost(''qc'', {%d, %d})', i, j);
0748         n = nPg + om.getN('var', 'x', {i,j});
0749         QQ = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n*ones(n,1), 1:n, n, n);
0750         cc = -2*(1:n)';
0751         kk = 1000;
0752         [Q, c, k] = om.params_quad_cost('qc', {i,j});
0753         t_is(Q, QQ, 14, [t, ' : Q']);
0754         t_is(c, cc, 14, [t, ' : c']);
0755         t_is(k, kk, 14, [t, ' : k']);
0756     end
0757 end
0758 
0759 t = 'om.params_quad_cost()';
0760 [Q, c, k] = om.params_quad_cost();
0761 % [ii, jj, ss] = find(Q)
0762 ii = [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 1 2 7 1 3 6 1 4 5 1 4 5 1 3 6 1 2 7 1 1 1 1 1 1 13 1 14 1 15 1 16 1 17 1 18 1 5 6 7 18 19 1 20 1 5 6 7 20 21 1 22 1 23 1 5 6 7 22 23 24 1 25 1 5 6 7 25 26 1 27 1 28 1 29 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]';
0763 jj = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 8 9 10 11 12 13 13 14 14 15 15 16 16 17 17 18 18 19 19 19 19 19 19 20 20 21 21 21 21 21 21 22 22 23 23 24 24 24 24 24 24 24 25 25 26 26 26 26 26 26 27 27 28 28 29 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170]';
0764 ss = [179 169 168 167 166 165 164 163 162 161 160 159 158 157 156 155 154 153 152 151 150 149 148 147 146 145 144 143 142 141 140 139 138 137 136 135 134 133 132 131 130 129 128 127 126 125 124 123 122 121 120 119 118 117 116 115 114 113 112 111 110 109 108 107 106 105 104 103 102 101 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 2 5 3 3 6 2 4 7 1 5 7 17 6 6 24 7 5 31 8 9 10 11 12 13 2 14 4 15 6 16 8 17 10 18 4 19 1 2 3 4 10 20 4 21 1 2 3 4 10 22 5 23 5.5 24 1 2 3 4 5 12.3333333333333333 25 4 26 1 2 3 4 10 27 0.25 28 0.2 29 0.1666666666666667 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170];
0765 QQ = sparse(ii, jj, ss, om.var.N, om.var.N);
0766 cc = [ 2139 2238 2337 2436 1751 1841 1931 1622 1611 1600 1589 1578 1566 1554 1542 1530 1518 1504 1491 1482 1469 1460 1447 1434 1427 1414 1413 1402 1391 1380 1369 1358 1347 1336 1325 1314 1303 1292 1281 1270 1259 1248 1237 1226 1215 1204 1193 1182 1171 1160 1149 1138 1127 1116 1105 1094 1083 1072 1061 1050 1039 1028 1017 1006 995 984 973 962 951 940 929 918 907 896 885 874 863 852 841 830 819 808 797 786 775 764 753 742 731 720 709 698 687 676 665 654 643 632 621 610 599 588 577 566 555 544 533 522 511 500 489 478 467 456 445 434 423 412 401 390 379 368 357 346 335 324 313 302 291 280 269 258 247 236 225 214 203 192 181 170 159 148 137 126 115 104 93 82 71 60 49 38 27 16 5 -6 -17 -28 -39 -50 -61 -72 -83 -94 -105 -116 -127 -138 -149 -160]';
0767 t_is(Q, QQ, 14, [t, ' : Q']);
0768 t_is(c, cc, 14, [t, ' : c']);
0769 t_is(k, 4027, 14, [t, ' : k']);
0770 
0771 %%-----  eval_quad_cost  -----
0772 t = 'om.eval_quad_cost(x, ''qc1'')';
0773 x = (1:om.var.N)';
0774 [Q, c, k, vs] = om.params_quad_cost('qc1');
0775 xx = om.varsets_x(x, vs, 'vector');
0776 ef = 1/2 * xx'*Q*xx + c'*xx + k;
0777 edf = Q*xx + c;
0778 f = om.eval_quad_cost(x, 'qc1');
0779 t_is(f, ef, 14, [t, ' : f']);
0780 [f, df] = om.eval_quad_cost(x, 'qc1');
0781 t_is(f, ef, 14, [t, ' : f']);
0782 t_is(df, edf, 14, [t, ' : df']);
0783 [f, df, d2f] = om.eval_quad_cost(x, 'qc1');
0784 t_is(f, ef, 14, [t, ' : f']);
0785 t_is(df, edf, 14, [t, ' : df']);
0786 t_is(d2f, Q, 14, [t, ' : d2f']);
0787 
0788 t = 'om.eval_quad_cost(x, ''qc2'')';
0789 [Q, c, k, vs] = om.params_quad_cost('qc2');
0790 xx = om.varsets_x(x, vs, 'vector');
0791 ef = 1/2 * xx'*Q*xx + c'*xx + k;
0792 edf = Q*xx + c;
0793 [f, df, d2f] = om.eval_quad_cost(x, 'qc2');
0794 t_is(f, ef, 14, [t, ' : f']);
0795 t_is(df, edf, 14, [t, ' : df']);
0796 t_is(d2f, Q, 14, [t, ' : d2f']);
0797 
0798 t = 'om.eval_quad_cost(x, ''qc3'')';
0799 [Q, c, k, vs] = om.params_quad_cost('qc3');
0800 xx = om.varsets_x(x, vs, 'vector');
0801 ef = 1/2 * Q.*xx.^2 + c.*xx + k;
0802 edf = Q.*xx + c;
0803 [f, df, d2f] = om.eval_quad_cost(x, 'qc3');
0804 t_is(f, ef, 14, [t, ' : f']);
0805 t_is(df, edf, 14, [t, ' : df']);
0806 t_is(d2f, Q, 14, [t, ' : d2f']);
0807 
0808 t = 'om.eval_quad_cost(x, ''qc4'')';
0809 [Q, c, k, vs] = om.params_quad_cost('qc4');
0810 xx = om.varsets_x(x, vs, 'vector');
0811 ef = 1/2 * Q.*xx.^2 + k;
0812 edf = Q.*xx;
0813 [f, df, d2f] = om.eval_quad_cost(x, 'qc4');
0814 t_is(f, ef, 14, [t, ' : f']);
0815 t_is(df, edf, 14, [t, ' : df']);
0816 t_is(d2f, Q, 14, [t, ' : d2f']);
0817 
0818 t = 'om.eval_quad_cost(x, ''qc5'')';
0819 [Q, c, k, vs] = om.params_quad_cost('qc5');
0820 xx = om.varsets_x(x, vs, 'vector');
0821 ef = c.*xx + k;
0822 edf = c;
0823 [f, df, d2f] = om.eval_quad_cost(x, 'qc5');
0824 t_is(f, ef, 14, [t, ' : f']);
0825 t_is(df, edf, 14, [t, ' : df']);
0826 t_is(d2f, sparse(length(xx), 1), 14, [t, ' : d2f']);
0827 
0828 t = 'om.eval_quad_cost(x, ''qc6'')';
0829 [Q, c, k, vs] = om.params_quad_cost('qc6');
0830 xx = x;
0831 ef = c.*xx + k;
0832 edf = c;
0833 [f, df, d2f] = om.eval_quad_cost(x, 'qc6');
0834 t_is(f, ef, 14, [t, ' : f']);
0835 t_is(df, edf, 14, [t, ' : df']);
0836 t_is(d2f, sparse(length(x), 1), 14, [t, ' : d2f']);
0837 
0838 for i = 1:2
0839     for j = 1:2
0840         t = sprintf('om.eval_quad_cost(x, ''qc'', {%d, %d})', i, j);
0841         [Q, c, k, vs] = om.params_quad_cost('qc', {i,j});
0842         xx = om.varsets_x(x, vs, 'vector');
0843         ef = 1/2 * xx'*Q*xx + c'*xx + k;
0844         edf = Q*xx + c;
0845         [f, df, d2f] = om.eval_quad_cost(x, 'qc', {i,j});
0846         t_is(f, ef, 14, [t, ' : f']);
0847         t_is(df, edf, 14, [t, ' : df']);
0848         t_is(d2f, Q, 14, [t, ' : d2f']);
0849     end
0850 end
0851 
0852 t = 'om.eval_quad_cost(x)';
0853 [Q, c, k] = om.params_quad_cost();
0854 xx = x;
0855 ef = 1/2 * xx'*Q*xx + c'*xx + k;
0856 edf = Q*xx + c;
0857 [f, df, d2f] = om.eval_quad_cost(x);
0858 t_is(f, ef, 14, [t, ' : f']);
0859 t_is(df, edf, 14, [t, ' : df']);
0860 t_is(d2f, Q, 14, [t, ' : d2f']);
0861 
0862 
0863 %%-----  add_legacy_cost  -----
0864 t = 'add_legacy_cost';
0865 cN = 0;
0866 cNS = 0;
0867 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0868 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0869 
0870 t = 'om.add_legacy_cost(''ucost'', cp, {''Va'', ''Pg''})';
0871 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
0872             'Cw', [2;3]);
0873 om.add_legacy_cost('ucost', cp, {'Va', 'Pg'});
0874 cNS = cNS + 1; cN = cN + 2;
0875 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0876 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0877 
0878 t = 'om.add_legacy_cost(''vcost'', cp)';
0879 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
0880             'Cw', [2;3]);
0881 om.add_legacy_cost('vcost', cp);
0882 cNS = cNS + 1; cN = cN + 2;
0883 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0884 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0885 
0886 t = 'om.init_indexed_name(''cost'', ''wc'', {2,2})';
0887 om.init_indexed_name('cost', 'wc', {2,2});
0888 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0889 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0890 
0891 for i = 1:2
0892     for j = 1:2
0893         t = 'om.add_legacy_cost(''wc'', {i, j}, cp, vs)';
0894         cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0895             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0896         cp.Cw = (i+j:-1:1)';
0897         if i == 2
0898             cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
0899         end
0900         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0901         om.add_legacy_cost('wc', {i, j}, cp, vs);
0902         cNS = cNS + 1; cN = cN + i+j;
0903         t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0904         t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0905     end
0906 end
0907 
0908 t = 'params_legacy_cost';
0909 cp = om.params_legacy_cost();
0910 t_ok(isfield(cp, 'N'), t);
0911 
0912 %%-----  get_idx  -----
0913 t = 'get_idx : cost';
0914 [vv, ll, nne, nni, cc] = om.get_idx();
0915 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 14, [t ' : vcost']);
0916 t_is(size(cc.i1.wc), [2, 2], 14, [t ' : size(cc.i1.wc)']);
0917 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)']);
0918 
0919 t = 'get_idx(''var'', ''cost'', ''lin'')';
0920 [vv, cc, ll] = om.get_idx('var', 'cost', 'lin');
0921 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
0922 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
0923 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)']);
0924 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
0925 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)']);
0926 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 14, [t ' : vcost']);
0927 t_is(size(cc.i1.wc), [2, 2], 14, [t ' : size(cc.i1.wc)']);
0928 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)']);
0929 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0930 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0931 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)']);
0932 
0933 %%-----  params_legacy_cost  -----
0934 t = 'om.params_legacy_cost(''ucost'')';
0935 cp = om.params_legacy_cost('ucost');
0936 n = nVa + nPg;
0937 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, n);
0938 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0939 t_is(cp.Cw, [2;3], 14, [t, ' : Cw']);
0940 t_is(full(cp.H), zeros(2,2), 14, [t, ' : H']);
0941 t_is(cp.dd, ones(2,1),  14, [t, ' : dd']);
0942 t_is(cp.rh, zeros(2,1), 14, [t, ' : rh']);
0943 t_is(cp.kk, zeros(2,1), 14, [t, ' : kk']);
0944 t_is(cp.mm, ones(2,1),  14, [t, ' : mm']);
0945 
0946 t = 'om.params_legacy_cost(''vcost'')';
0947 cp = om.params_legacy_cost('vcost');
0948 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0949 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0950 t_is(cp.Cw, [2;3], 14, [t, ' : Cw']);
0951 t_is(full(cp.H), zeros(2,2), 14, [t, ' : H']);
0952 t_is(cp.dd, ones(2,1),  14, [t, ' : dd']);
0953 t_is(cp.rh, zeros(2,1), 14, [t, ' : rh']);
0954 t_is(cp.kk, zeros(2,1), 14, [t, ' : kk']);
0955 t_is(cp.mm, ones(2,1),  14, [t, ' : mm']);
0956 
0957 t = 'om.params_legacy_cost(''wc'') : error';
0958 try
0959     cp = om.params_legacy_cost('wc')
0960     t_ok(0, t);
0961 catch
0962     t_ok(strfind(lasterr, '@opt_model/params_legacy_cost: legacy cost set ''wc'' requires an IDX arg'), t);
0963 end
0964 
0965 t = 'om.params_legacy_cost(''wc'', {1,2})';
0966 cp = om.params_legacy_cost('wc', {1,2});
0967 n = nPg + om.getN('var', 'x', {1,2});
0968 N = sparse([1:3 1:3]', [1:3 nPg+2*ones(1,3)]', [ones(3,1);-ones(3,1)], 3, n);
0969 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0970 t_is(cp.Cw, [3;2;1], 14, [t, ' : Cw']);
0971 t_is(full(cp.H), zeros(3,3), 14, [t, ' : H']);
0972 t_is(cp.dd, ones(3,1),  14, [t, ' : dd']);
0973 t_is(cp.rh, zeros(3,1), 14, [t, ' : rh']);
0974 t_is(cp.kk, zeros(3,1), 14, [t, ' : kk']);
0975 t_is(cp.mm, ones(3,1),  14, [t, ' : mm']);
0976 
0977 t = 'om.params_legacy_cost(''wc'', {2,1})';
0978 cp = om.params_legacy_cost('wc', {2,1});
0979 n = nPg + om.getN('var', 'x', {2,1});
0980 N = sparse([1:3 1:3]', [1:3 nPg+2*ones(1,3)]', [ones(3,1);-ones(3,1)], 3, n);
0981 t_is(full(cp.N), full(N), 14, [t, ' : N']);
0982 t_is(cp.Cw, [3;2;1], 14, [t, ' : Cw']);
0983 H = sparse(1:3, 1:3, 1:3, 3, 3);
0984 t_is(full(cp.H), full(H), 14, [t, ' : H']);
0985 t_is(cp.dd, ones(3,1),  14, [t, ' : dd']);
0986 t_is(cp.rh, zeros(3,1), 14, [t, ' : rh']);
0987 t_is(cp.kk, zeros(3,1), 14, [t, ' : kk']);
0988 t_is(cp.mm, ones(3,1),  14, [t, ' : mm']);
0989 
0990 t = 'om.params_legacy_cost()';
0991 cp = om.params_legacy_cost();
0992 t_ok(issparse(cp.N), [t ' : issparse(cp.N)']);
0993 t_is(size(cp.N), [cN, vN], 14, [t ' : size(cp.N)']);
0994 t_is(size(cp.H), [cN, cN], 14, [t ' : size(cp.H)']);
0995 t_is(length(cp.Cw), cN, 14, [t ' : length(cp.Cw)']);
0996 t_is(length(cp.dd), cN, 14, [t ' : length(cp.dd)']);
0997 t_is(length(cp.rh), cN, 14, [t ' : length(cp.rh)']);
0998 t_is(length(cp.kk), cN, 14, [t ' : length(cp.kk)']);
0999 t_is(length(cp.mm), cN, 14, [t ' : length(cp.mm)']);
1000 N = sparse([1:2 1:2]', [1:4]', [1 1 -1 -1]', 2, 4);
1001 Cw = [2;3];
1002 H = zeros(2,2);
1003 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Va:vv.iN.Va)), full(N), 14, [t ' : N(<vcost>,<Va>)']);
1004 N = sparse([1:2]', [1 3]', [2 2]', 2, 3);
1005 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Pg:vv.iN.Pg)), full(N), 14, [t ' : N(<vcost>,<Pg>)']);
1006 t_is(full(cp.Cw(cc.i1.vcost:cc.iN.vcost)), Cw, 14, [t ' : Cw(<vcost>)']);
1007 t_is(full(cp.H(cc.i1.vcost:cc.iN.vcost, cc.i1.vcost:cc.iN.vcost)), full(H), 14, [t ' : H(<vcost>,<vcost>)']);
1008 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
1009 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>)']);
1010 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 2);
1011 Cw = [3;2;1];
1012 H = zeros(3,3);
1013 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)>)']);
1014 t_is(full(cp.Cw(cc.i1.wc(1,2):cc.iN.wc(1,2))), Cw, 14, [t ' : Cw(<wc(1,2)>)']);
1015 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)>)']);
1016 
1017 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
1018 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>)']);
1019 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 3);
1020 Cw = [3;2;1];
1021 H = full(sparse((1:3)', (1:3)', (1:3)', 3, 3));
1022 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)>)']);
1023 t_is(full(cp.Cw(cc.i1.wc(2,1):cc.iN.wc(2,1))), Cw, 14, [t ' : Cw(<wc(2,1)>)']);
1024 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)>)']);
1025 
1026 t_is(cp.dd, ones(cN,1),  14, [t, ' : dd']);
1027 t_is(cp.rh, zeros(cN,1), 14, [t, ' : rh']);
1028 t_is(cp.kk, zeros(cN,1), 14, [t, ' : kk']);
1029 t_is(cp.mm, ones(cN,1),  14, [t, ' : mm']);
1030 
1031 %%-----  eval_legacy_cost  -----
1032 t = 'om.eval_legacy_cost(x)';
1033 x = [1:7 rand(1,10) 8:(vN-10)]';
1034 f = om.eval_legacy_cost(x);
1035 t_is(f, 343, 14, t);
1036 
1037 t = 'om.eval_legacy_cost(''ucost'')';
1038 f = om.eval_legacy_cost(x, 'ucost');
1039 t_is(f, 52, 14, t);
1040 
1041 t = 'om.eval_legacy_cost(''wc'', {2,1})';
1042 f = om.eval_legacy_cost(x, 'wc', {2,1});
1043 t_is(f, 91, 14, t);
1044 
1045 t = 'om.eval_legacy_cost(''wc'')';
1046 f = om.eval_legacy_cost(x, 'wc');
1047 t_is(f, 239, 14, t);
1048 
1049 %%-----  add_nln_cost  -----
1050 t = 'add_nln_cost';
1051 nlcN = 0;
1052 nlcNS = 0;
1053 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1054 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1055 
1056 t = 'om.add_nln_cost(''ucost'', 1, fcn, {''Va'', ''Pg''})';
1057 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
1058             'Cw', [2;3]);
1059 fcn = @(x)my_legacy_cost_fcn(x, cp, om, {'Va', 'Pg'});
1060 om.add_nln_cost('ucost', 1, fcn, {'Va', 'Pg'});
1061 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1062 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1063 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1064 
1065 t = 'om.add_nln_cost(''vcost'', cp)';
1066 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
1067             'Cw', [2;3]);
1068 fcn = @(x)my_legacy_cost_fcn(x, cp, om);
1069 om.add_nln_cost('vcost', 1, fcn);
1070 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1071 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1072 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1073 
1074 t = 'om.init_indexed_name(''nlc'', ''wc'', {2,2})';
1075 om.init_indexed_name('nlc', 'wc', {2,2});
1076 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1077 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1078 
1079 for i = 1:2
1080     for j = 1:2
1081         t = 'om.add_nln_cost(''wc'', {i, j}, cp, vs)';
1082         cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
1083             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
1084         cp.Cw = (i+j:-1:1)';
1085         if i == 2
1086             cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
1087         end
1088         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
1089         fcn = @(x)my_legacy_cost_fcn(x, cp, om, vs);
1090         om.add_nln_cost('wc', {i, j}, 1, fcn, vs);
1091         nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1092         t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1093         t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1094     end
1095 end
1096 
1097 %%-----  params_nln_cost  -----
1098 t = 'om.params_nln_cost(''ucost'')';
1099 [N, fcn] = om.params_nln_cost('ucost');
1100 t_is(N, 1, 14, [t, ' : N']);
1101 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1102 
1103 t = 'om.params_nln_cost(''vcost'')';
1104 [N, fcn] = om.params_nln_cost('vcost');
1105 t_is(N, 1, 14, [t, ' : N']);
1106 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1107 
1108 t = 'om.params_nln_cost(''wc'') : error';
1109 try
1110     [N, fcn] = om.params_nln_cost('wc')
1111     t_ok(0, t);
1112 catch
1113     t_ok(strfind(lasterr, '@opt_model/params_nln_cost: general nonlinear cost set ''wc'' requires an IDX arg'), t);
1114 end
1115 
1116 t = 'om.params_nln_cost(''wc'', {1,2})';
1117 [N, fcn] = om.params_nln_cost('wc', {1,2});
1118 t_is(N, 1, 14, [t, ' : N']);
1119 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1120 
1121 t = 'om.params_nln_cost(''wc'', {2,1})';
1122 [N, fcn] = om.params_nln_cost('wc', {2,1});
1123 t_is(N, 1, 14, [t, ' : N']);
1124 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1125 
1126 %%-----  eval_nln_cost  -----
1127 t = 'om.eval_nln_cost(x) : ';
1128 x = [1:7 rand(1,10) 8:(vN-10)]';
1129 f = om.eval_nln_cost(x);
1130 ef = 343;
1131 ii = [1 2 3 4 5 6 7 19 21 23 25 26];
1132 jj = [1 1 1 1 1 1 1 1 1 1 1 1];
1133 ss = [4 6 -4 -6 1 -26 -29 -3 -6 34 -3 52];
1134 edf = full(sparse(ii,jj,ss,vN,1));
1135 ii = [5 23 26 6 23 26 7 23 26 5 6 7 23 25 26 5 6 7 25 26];
1136 jj = [5 5 5 6 6 6 7 7 7 23 23 23 23 25 25 26 26 26 26 26];
1137 ss = [2 -1 -1 4 -2 -2 6 -3 -3 -1 -2 -3 6 4 -4 -1 -2 -3 -4 10];
1138 ed2f = full(sparse(ii,jj,ss,vN,vN));
1139 t_is(f, ef, 14, [t 'f']);
1140 [f, df] = om.eval_nln_cost(x);
1141 t_is(f, ef, 14, [t 'f']);
1142 t_is(df, edf, 14, [t 'df']);
1143 [f, df, d2f] = om.eval_nln_cost(x);
1144 t_is(f, ef, 14, [t 'f']);
1145 t_is(df, edf, 14, [t 'df']);
1146 t_is(d2f, ed2f, 14, [t 'd2f']);
1147 
1148 t = 'om.eval_nln_cost(''ucost'') : ';
1149 f = om.eval_nln_cost(x, 'ucost');
1150 ef = 52;
1151 edf = [2; 3; -2; -3; 4; 0; 6];
1152 t_is(f, ef, 14, [t 'f']);
1153 [f, df] = om.eval_nln_cost(x, 'ucost');
1154 t_is(f, ef, 14, [t 'f']);
1155 t_is(df, edf, 14, [t 'df']);
1156 [f, df, d2f] = om.eval_nln_cost(x, 'ucost');
1157 t_is(f, ef, 14, [t 'f']);
1158 t_is(df, edf, 14, [t 'df']);
1159 t_is(full(d2f), zeros(7,7), 14, [t 'd2f']);
1160 
1161 t = 'om.eval_nln_cost(''wc'', {2,1}) : ';
1162 f = om.eval_nln_cost(x, 'wc', {2,1});
1163 ef = 91;
1164 edf = [-5; -12; -17; 0; 34; 0];
1165 ii = [1 5 2 5 3 5 1 2 3 5];
1166 jj = [1 1 2 2 3 3 5 5 5 5];
1167 ss = [1 -1 2 -2 3 -3 -1 -2 -3 6];
1168 ed2f = full(sparse(ii,jj,ss,6,6));
1169 t_is(f, ef, 14, [t 'f']);
1170 [f, df] = om.eval_nln_cost(x, 'wc', {2,1});
1171 t_is(f, ef, 14, [t 'f']);
1172 t_is(df, edf, 14, [t 'df']);
1173 [f, df, d2f] = om.eval_nln_cost(x, 'wc', {2,1});
1174 t_is(f, ef, 14, [t 'f']);
1175 t_is(df, edf, 14, [t 'df']);
1176 t_is(d2f, ed2f, 14, [t 'd2f']);
1177 
1178 t = 'om.eval_nln_cost(''wc'') : ';
1179 f = om.eval_nln_cost(x, 'wc');
1180 t_is(f, 239, 14, [t 'f']);
1181 
1182 % om
1183 % om = struct(om);
1184 
1185 t_end
1186 
1187 function [g, dg] = my_fcn(x, p1, p2)
1188 if iscell(x)
1189     xx = [];
1190     for k = 1:length(x)
1191         xx = [xx; x{k}];
1192     end
1193 else
1194     xx = x;
1195 end
1196 M = p1;
1197 N = length(xx);
1198 if M > N
1199     error('M <= length(x)');
1200 end
1201 g = xx(1:M) + p2;
1202 dg = sparse(1:M, 1:M, p2, M, N) + sparse(1, 1:N, xx, M, N);
1203 
1204 function d2G = my_hess(x, lam, p3)
1205 if iscell(x)
1206     xx = [];
1207     for k = 1:length(x)
1208         xx = [xx; x{k}];
1209     end
1210 else
1211     xx = x;
1212 end
1213 N = length(xx);
1214 M = length(lam);
1215 MM = min(M, N);
1216 d2G = sparse(1:MM, 1:MM, xx(1:MM) + lam(1:MM) + p3, N, N);
1217 %full(d2G(1:MM,1:MM))
1218 
1219 function [varargout] = my_legacy_cost_fcn(x, cp, om, vs)
1220 [nw, nx] = size(cp.N);
1221 if ~isfield(cp, 'H') || isempty(cp.H)
1222     cp.H = sparse(nw, nw);
1223 end
1224 if ~isfield(cp, 'dd') || isempty(cp.dd)
1225     cp.dd = ones(nw, 1);
1226 end
1227 if ~isfield(cp, 'rh') || isempty(cp.rh)
1228     cp.rh = zeros(nw, 1);
1229 end
1230 if ~isfield(cp, 'kk') || isempty(cp.kk)
1231     cp.kk = zeros(nw, 1);
1232 end
1233 if ~isfield(cp, 'mm') || isempty(cp.mm)
1234     cp.mm = ones(nw, 1);
1235 end
1236 if nargin < 4
1237     vs = {};
1238 end
1239 if iscell(x)
1240     xx = vertcat(x{:});
1241 else
1242     xx = x;
1243 end
1244 vs = om.varsets_cell2struct(vs);
1245 [varargout{1:nargout}] = opf_legacy_user_cost_fcn(xx, cp);

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