Home > matpower7.1 > 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-2020, 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 = 727;
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;             %% should get expanded to ones(5, 1)
0059 Vmin = 0;           %% should get expanded to 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 A1 = 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 l1 = -(1:3)'; u1 = (1:3)';
0389 om.add_lin_constraint('Pmis', A1, l1, u1, {'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 A2 = 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', A2, l1, u1);
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 = -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 t = 'om.add_lin_constraint(''onerow'', A, l, u)';
0421 A4 = sparse([1 1 1]', [1:3]', [-1 -2 -3]', 1, vN);
0422 om.add_lin_constraint('onerow', A4, 0, Inf);
0423 lNS = lNS + 1; lN = lN + 1;
0424 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0425 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0426 
0427 %%-----  add_nln_constraint (equality)  -----
0428 t = 'add_nln_constraint (equality)';
0429 neN = 0;
0430 neNS = 0;
0431 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0432 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0433 
0434 t = 'om.add_nln_constraint(''Pmise'', N, 1, fcn, hess, {''Pg'', ''Va''})';
0435 N = 4;
0436 fcn = @(x)my_fcn(x, N, 2);
0437 hess = @(x, lam)my_hess(x, lam, 10);
0438 om.add_nln_constraint('Pmise', N, 1, fcn, hess, {'Pg', 'Va'});
0439 neNS = neNS + 1; neN = neN + N;
0440 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0441 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0442 
0443 t = 'om.add_nln_constraint(''Qmise'', N, 1, fcn, hess)';
0444 N = 3;
0445 fcn = @(x)my_fcn(x, N, 2);
0446 hess = @(x, lam)my_hess(x, lam, 10);
0447 om.add_nln_constraint('Qmise', N, 1, fcn, hess);
0448 neNS = neNS + 1; neN = neN + N;
0449 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0450 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0451 
0452 t = 'om.add_nln_constraint({''P'',''Q'',''R''}, [3;2;1], 1, fcn, hess, {''Pg'', ''Va''})';
0453 N = [3;2;1];
0454 fcn = @(x)my_fcn(x, sum(N), 2);
0455 hess = @(x, lam)my_hess(x, lam, 10);
0456 om.add_nln_constraint({'P', 'Q', 'R'}, N, 1, fcn, hess, {'Pg', 'Va'});
0457 neNS = neNS + length(N); neN = neN + sum(N);
0458 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0459 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0460 
0461 t = 'om.init_indexed_name(''nle'', ''mynle'', {2, 2})';
0462 om.init_indexed_name('nle', 'mynle', {2, 2});
0463 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0464 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0465 
0466 for i = 1:2
0467     for j = 1:2
0468         t = sprintf('om.add_nln_constraint(''mynle'', {%d,%d}, N, 1, fcn, hess, vs)', i,j);
0469         N = i+j;
0470         fcn = @(x)my_fcn(x, N, i);
0471         hess = @(x, lam)my_hess(x, lam, j);
0472         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0473         om.add_nln_constraint('mynle', {i, j}, N, 1, fcn, hess, vs);
0474         neNS = neNS + 1; neN = neN + N;
0475         t_ok(om.getN('nle') == neN, sprintf('%s : nle.N  = %d', t, neN));
0476         t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0477     end
0478 end
0479 
0480 %%-----  add_nln_constraint (inequality)  -----
0481 t = 'add_nln_constraint (inequality)';
0482 niN = 0;
0483 niNS = 0;
0484 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0485 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0486 
0487 t = 'om.add_nln_constraint(''Pmisi'', N, 0, fcn, hess, {''Pg'', ''Va''})';
0488 N = 3;
0489 fcn = @(x)my_fcn(x, N, -2);
0490 hess = @(x, lam)my_hess(x, lam, -10);
0491 om.add_nln_constraint('Pmisi', N, 0, fcn, hess, {'Pg', 'Va'});
0492 niNS = niNS + 1; niN = niN + N;
0493 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0494 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0495 
0496 t = 'om.add_nln_constraint(''Qmisi'', N, 0, fcn, hess)';
0497 N = 2;
0498 fcn = @(x)my_fcn(x, N, -2);
0499 hess = @(x, lam)my_hess(x, lam, -10);
0500 om.add_nln_constraint('Qmisi', N, 0, fcn, hess);
0501 niNS = niNS + 1; niN = niN + N;
0502 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0503 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0504 
0505 t = 'om.init_indexed_name(''nli'', ''mynli'', {2, 2})';
0506 om.init_indexed_name('nli', 'mynli', {2, 2});
0507 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0508 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0509 
0510 for i = 1:2
0511     for j = 1:2
0512         t = sprintf('om.add_nln_constraint(''mynli'', {%d,%d}, N, 0, fcn, hess, vs)', i,j);
0513         N = i+j-1;
0514         fcn = @(x)my_fcn(x, N, i);
0515         hess = @(x, lam)my_hess(x, lam, j);
0516         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0517         om.add_nln_constraint('mynli', {i, j}, N, 0, fcn, hess, vs);
0518         niNS = niNS + 1; niN = niN + N;
0519         t_ok(om.getN('nli') == niN, sprintf('%s : nli.N  = %d', t, niN));
0520         t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0521     end
0522 end
0523 
0524 %%-----  get_idx  -----
0525 t = 'get_idx : lin';
0526 [vv, ll] = om.get_idx();
0527 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0528 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0529 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)']);
0530 
0531 t = 'get_idx(''lin'')';
0532 ll = om.get_idx('lin');
0533 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0534 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0535 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)']);
0536 
0537 %%-----  params_lin_constraint  -----
0538 t = 'om.params_lin_constraint(''Pmis'')';
0539 [A, l, u, vs] = om.params_lin_constraint('Pmis');
0540 t_is(A, A1, 14, [t, ' : A']);
0541 t_is(l, l1, 14, [t, ' : l']);
0542 t_is(u, u1, 14, [t, ' : u']);
0543 vs1 = struct('name', {'Va', 'Pg'}, 'idx', {{}, {}});
0544 t_ok(isequal(vs, vs1), [t, ' : vs']);
0545 
0546 t = 'om.params_lin_constraint(''Qmis'')';
0547 [A, l, u, vs] = om.params_lin_constraint('Qmis');
0548 t_is(A, A2, 14, [t, ' : A']);
0549 t_is(l, l1, 14, [t, ' : l']);
0550 t_is(u, u1, 14, [t, ' : u']);
0551 t_ok(isequal(vs, {}), [t, ' : vs']);
0552 
0553 for i = 1:2
0554     for j = 1:2
0555         t = sprintf('om.params_lin_constraint(''mylin'', {%d,%d})', i,j);
0556         A3 = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0557             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0558         l3 = -ones(i+j, 1); u = [];
0559         vvs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0560         [A, l, u, vs] = om.params_lin_constraint('mylin', {i,j});
0561         t_is(A, A3, 14, [t, ' : A']);
0562         t_is(l, l3, 14, [t, ' : l']);
0563         t_ok(all(isinf(u)) & all(u > 0), [t, ' : u']);
0564         t_ok(isequal(vs, vvs), [t, ' : vs']);
0565     end
0566 end
0567 
0568 t = 'om.params_lin_constraint(''onerow'')';
0569 [A, l, u, vs] = om.params_lin_constraint('onerow');
0570 t_is(A, A4, 14, [t, ' : A']);
0571 t_is(l, 0, 14, [t, ' : l']);
0572 t_ok(all(isinf(u)) & all(u > 0), [t, ' : u']);
0573 t_ok(isequal(vs, {}), [t, ' : vs']);
0574 
0575 t = 'om.params_lin_constraint()';
0576 [A, l, u] = om.params_lin_constraint();
0577 t_ok(issparse(A), [t ' : issparse(A)']);
0578 t_is(size(A), [lN, vN], 14, [t ' : size(A)']);
0579 t_is(length(l), lN, 14, [t ' : length(l)']);
0580 t_is(length(u), lN, 14, [t ' : length(u)']);
0581 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);
0582 t_is(full(A(ll.i1.Qmis:ll.iN.Qmis, :)), full(AA), 14, [t ' : A(<Qmis>,:)']);
0583 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>)']);
0584 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)>)']);
0585 
0586 %%-----  eval_lin_constraint  -----
0587 t = '[Ax_u, l_Ax, A] = om.eval_lin_constraint(x)';
0588 x = (1:om.var.N)';
0589 [Ax_u, l_Ax, AA] = om.eval_lin_constraint(x);
0590 t_is(Ax_u, A*x-u, 14, [t ' : Ax_u']);
0591 t_is(l_Ax, l-A*x, 14, [t ' : l_Ax']);
0592 t_is(AA, A, 14, [t ' : A']);
0593 
0594 t = 'Ax_u = om.eval_lin_constraint(x, ''Pmis'')';
0595 vs = om.varsets_cell2struct({'Va', 'Pg'});
0596 xx = om.varsets_x(x, vs, 'vector');
0597 Ax_u = om.eval_lin_constraint(x, 'Pmis');
0598 t_is(Ax_u, A1*xx-u1, 14, [t ' : Ax_u']);
0599 
0600 t = '[Ax_u, l_Ax] = om.eval_lin_constraint(x, ''Pmis'')';
0601 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'Pmis');
0602 t_is(Ax_u, A1*xx-u1, 14, [t ' : Ax_u']);
0603 t_is(l_Ax, l1-A1*xx, 14, [t ' : l_Ax']);
0604 
0605 t = '[Ax_u, l_Ax, A] = om.eval_lin_constraint(x, ''Pmis'')';
0606 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'Pmis');
0607 t_is(Ax_u, A1*xx-u1, 14, [t ' : Ax_u']);
0608 t_is(l_Ax, l1-A1*xx, 14, [t ' : l_Ax']);
0609 t_is(A, A1, 14, [t ' : A']);
0610 
0611 t = '[Ax_u, l_Ax, A] = om.eval_lin_constraint(x, ''Qmis'')';
0612 vs = om.varsets_cell2struct({'Va', 'Pg'});
0613 xx = om.varsets_x(x, vs, 'vector');
0614 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'Qmis');
0615 t_is(Ax_u, A2*x-u1, 14, [t ' : Ax_u']);
0616 t_is(l_Ax, l1-A2*x, 14, [t ' : l_Ax']);
0617 t_is(A, A2, 14, [t ' : A']);
0618 
0619 for i = 1:2
0620     for j = 1:2
0621         t = sprintf('om.params_lin_constraint(''mylin'', {%d,%d})', i,j);
0622         A3 = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0623             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0624         l3 = -ones(i+j, 1); u = [];
0625         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0626         xx = om.varsets_x(x, vs, 'vector');
0627         [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'mylin', {i,j});
0628         t_ok(all(isinf(Ax_u)) & all(Ax_u < 0), [t ' : Ax_u']);
0629         t_is(l_Ax, l3-A3*xx, 14, [t ' : l_Ax']);
0630         t_is(A, A3, 14, [t ' : A']);
0631     end
0632 end
0633 
0634 %%-----  params_nln_constraint  -----
0635 t = 'om.params_nln_constraint(1, ''Pmise'')';
0636 N = om.params_nln_constraint(1, 'Pmise');
0637 t_is(N, 4, 14, [t, ' : N']);
0638 [N, fcn] = om.params_nln_constraint(1, 'Pmise');
0639 t_is(N, 4, 14, [t, ' : N']);
0640 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0641 [N, fcn, hess] = om.params_nln_constraint(1, 'Pmise');
0642 t_is(N, 4, 14, [t, ' : N']);
0643 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0644 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0645 [N, fcn, hess, vs] = om.params_nln_constraint(1, 'Pmise');
0646 t_is(N, 4, 14, [t, ' : N']);
0647 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0648 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0649 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0650 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0651 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0652 t_ok(strcmp(vs(2).name, 'Va'), [t, ' : vs(2).name']);
0653 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0654 t_ok(isempty(vs(2).idx), [t, ' : vs(2).idx']);
0655 [N, fcn, hess, vs, include] = om.params_nln_constraint(1, 'Pmise');
0656 t_ok(strcmp(include, ''), [t, ' : include']);
0657 
0658 t = 'om.params_nln_constraint(1, ''P'')';
0659 [N, fcn, hess, vs, include] = om.params_nln_constraint(1, 'P');
0660 t_is(N, 3, 14, [t, ' : N']);
0661 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0662 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0663 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0664 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0665 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0666 t_ok(strcmp(vs(2).name, 'Va'), [t, ' : vs(2).name']);
0667 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0668 t_ok(isempty(vs(2).idx), [t, ' : vs(2).idx']);
0669 t_ok(isstruct(include), [t, ' : istruct(include)']);
0670 t_is(length(include.name), 2, 14, [t, ' : length(include.name)']);
0671 t_is(length(include.N), 2, 14, [t, ' : length(include.N)']);
0672 t_ok(strcmp(include.name{1}, 'Q'), [t, ' : include.name{1}']);
0673 t_ok(strcmp(include.name{2}, 'R'), [t, ' : include.name{2}']);
0674 t_is(include.N, [2 1], 14, [t, ' : include.N']);
0675 
0676 t = 'om.params_nln_constraint(1, ''mynle'') : error';
0677 try
0678     [N, fcn] = om.params_nln_constraint(1, 'mynle')
0679     t_ok(0, t);
0680 catch
0681     t_ok(strfind(lasterr, '@opt_model/params_nln_constraint: nonlinear constraint set ''mynle'' requires an IDX_LIST arg'), t);
0682 end
0683 
0684 t = 'om.params_nln_constraint(0, ''mynli'', {1,2})';
0685 [N, fcn, hess, vs] = om.params_nln_constraint(0, 'mynli', {1,2});
0686 t_is(N, 2, 14, [t, ' : N']);
0687 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0688 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0689 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0690 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0691 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0692 t_ok(strcmp(vs(2).name, 'x'), [t, ' : vs(2).name']);
0693 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0694 t_is(length(vs(2).idx), 2, 14, [t, ' : length(vs(2).idx)']);
0695 t_is(vs(2).idx{1}, 1, 14, [t, ' : vs(2).idx{1}']);
0696 t_is(vs(2).idx{2}, 2, 14, [t, ' : vs(2).idx{2}']);
0697 
0698 t = 'om.params_nln_constraint(0, ''mynli'', {2,2})';
0699 [N, fcn, hess, vs] = om.params_nln_constraint(0, 'mynli', {2,2});
0700 t_is(N, 3, 14, [t, ' : N']);
0701 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0702 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0703 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0704 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0705 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0706 t_ok(strcmp(vs(2).name, 'x'), [t, ' : vs(2).name']);
0707 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0708 t_is(length(vs(2).idx), 2, 14, [t, ' : length(vs(2).idx)']);
0709 t_is(vs(2).idx{1}, 2, 14, [t, ' : vs(2).idx{1}']);
0710 t_is(vs(2).idx{2}, 2, 14, [t, ' : vs(2).idx{2}']);
0711 
0712 %%-----  eval_nln_constraint  -----
0713 t = 'g = om.eval_nln_constraint';
0714 x = (1:om.var.N)';
0715 [g, dg] = om.eval_nln_constraint(x, 1);
0716 t_is(length(g), neN, 14, [t ' : length(g)']);
0717 eg = [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]';
0718 t_is(g, eg, 14, [t ' : g']);
0719 
0720 t = 'g = om.eval_nln_constraint(x, 1, ''Qmise'')';
0721 x = (1:om.var.N)';
0722 [g, dg] = om.eval_nln_constraint(x, 1, 'Qmise');
0723 t_is(length(g), 3, 14, [t ' : length(g)']);
0724 t_is(g, eg([5:7]), 14, [t ' : g']);
0725 
0726 t = 'g = om.eval_nln_constraint(x, 1, ''mynle'', {1,2})';
0727 x = (1:om.var.N)';
0728 [g, dg] = om.eval_nln_constraint(x, 1, 'mynle', {1,2});
0729 t_is(length(g), 3, 14, [t ' : length(g)']);
0730 t_is(g, eg(16:18), 14, [t ' : g']);
0731 
0732 t = '[g, dg] = om.eval_nln_constraint';
0733 x = (1:om.var.N)';
0734 [g, dg] = om.eval_nln_constraint(x, 1);
0735 t_is(length(g), neN, 14, [t ' : length(g)']);
0736 t_ok(issparse(dg), [t ' : issparse(dg)']);
0737 t_is(size(dg), [neN, vN], 14, [t ' : size(dg)']);
0738 t_is(g, eg, 14, [t ' : g']);
0739 ePmise = [[ 1 2 3 4 7 6 7;
0740             0 0 0 0 0 2 0;
0741             0 0 0 0 0 0 2;
0742             2 0 0 0 0 0 0 ] zeros(4, vN-7) ];
0743 t_is(full(dg(1:4, :)), ePmise, 14, [t ' : dg(1:4, :)   [Pmise]']);
0744 eQmise = [[3 2:vN]; [0 2 0 zeros(1, vN-3)]; [0 0 2 zeros(1, vN-3)]];
0745 t_is(full(dg(5:7, :)), eQmise, 14, [t ' : dg(5:7, :)   [Qmise]']);
0746 e = [[  1 2 3 4 7 6 7;
0747         0 0 0 0 0 2 0;
0748         0 0 0 0 0 0 2;
0749         2 0 0 0 0 0 0;
0750         0 2 0 0 0 0 0;
0751         0 0 2 0 0 0 0 ] zeros(6, vN-7) ];
0752 t_is(full(dg(8:13, :)), e, 14, [t ' : dg(8:13, :)  [mynle(1,1)]']);
0753 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)];
0754 t_is(full(dg(14:15, :)), e, 14, [t ' : dg(14:15, :) [mynle(1,1)]']);
0755 emynle12 = [[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)];
0756 t_is(full(dg(16:18, :)), emynle12, 14, [t ' : dg(16:18, :) [mynle(1,2)]']);
0757 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)];
0758 t_is(full(dg(19:21, :)), e, 14, [t ' : dg(19:21, :) [mynle(2,1)]']);
0759 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)];
0760 t_is(full(dg(22:25, :)), e, 14, [t ' : dg(22:25, :) [mynle(2,2)]']);
0761 
0762 t = '[g, dg] = om.eval_nln_constraint(x, 1, ''Pmise'')';
0763 x = (1:om.var.N)';
0764 [g, dg] = om.eval_nln_constraint(x, 1, 'Pmise');
0765 t_is(length(g), 4, 14, [t ' : length(g)']);
0766 t_is(g, eg([1:4]), 14, [t ' : g']);
0767 t_is(full(dg), ePmise(:, [5:7 1:4]), 14, [t ' : dg']);
0768 
0769 t = '[g, dg] = om.eval_nln_constraint(x, 1, ''Qmise'')';
0770 x = (1:om.var.N)';
0771 [g, dg] = om.eval_nln_constraint(x, 1, 'Qmise');
0772 t_is(length(g), 3, 14, [t ' : length(g)']);
0773 t_is(g, eg([5:7]), 14, [t ' : g']);
0774 t_is(full(dg), eQmise, 14, [t ' : dg']);
0775 
0776 t = '[g, dg] = om.eval_nln_constraint(x, 1, ''mynle'', {1,2})';
0777 x = (1:om.var.N)';
0778 [g, dg] = om.eval_nln_constraint(x, 1, 'mynle', {1,2});
0779 t_is(length(g), 3, 14, [t ' : length(g)']);
0780 t_is(g, eg(16:18), 14, [t ' : g']);
0781 t_is(full(dg), emynle12(:, [5:7 20:21]), 14, [t ' : dg']);
0782 
0783 % g
0784 % full(dg)
0785 % full(dg)'
0786 
0787 t = 'h = om.eval_nln_constraint';
0788 h = om.eval_nln_constraint(x, 0);
0789 t_is(length(h), niN, 14, [t ' : length(h)']);
0790 t_is(h, [3 4 5 -1 0 6 6 7 7 8 7 8 9]', 14, [t ' : h']);
0791 
0792 t = '[h, dh] = om.eval_nln_constraint';
0793 [h, dh] = om.eval_nln_constraint(x, 0);
0794 t_is(length(h), niN, 14, [t ' : length(h)']);
0795 t_ok(issparse(dh), [t ' : issparse(dh)']);
0796 t_is(size(dh), [niN, vN], 14, [t ' : size(dh)']);
0797 eh = [3 4 5 -1 0 6 6 7 7 8 7 8 9]';
0798 t_is(h, eh, 14, [t ' : h']);
0799 ePmisi = [[  1 2 3 4 3 6 7;
0800         0 0 0 0 0 -2 0;
0801         0 0 0 0 0 0 -2 ] zeros(3, vN-7) ];
0802 t_is(full(dh(1:3, :)), ePmisi, 14, [t ' : dh(1:3, :)   [Pmisi]']);
0803 eQmisi = [[-1 2:vN]; [0 -2 zeros(1, vN-2)]];
0804 t_is(full(dh(4:5, :)), eQmisi, 14, [t ' : dh(5:7, :)   [Qmisi]']);
0805 e = [[0 0 0 0 6 6 7] zeros(1, 10) [18 19] zeros(1, vN-19)];
0806 t_is(full(dh(6, :)), e, 14, [t ' : dh(6, :)     [mynli(1,1)]']);
0807 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)];
0808 t_is(full(dh(7:8, :)), e, 14, [t ' : dh(7:8, :)   [mynli(1,2)]']);
0809 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)];
0810 t_is(full(dh(9:10, :)), e, 14, [t ' : dh(9:10, :)  [mynli(2,1)]']);
0811 emynli22 = [[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)];
0812 t_is(full(dh(11:13, :)), emynli22, 14, [t ' : dh(11:13, :) [mynli(2,2)]']);
0813 
0814 t = '[h, dh] = om.eval_nln_constraint(x, 0, ''Pmisi'')';
0815 x = (1:om.var.N)';
0816 [h, dh] = om.eval_nln_constraint(x, 0, 'Pmisi');
0817 t_is(length(h), 3, 14, [t ' : length(h)']);
0818 t_is(h, eh([1:3]), 14, [t ' : h']);
0819 t_is(full(dh), ePmisi(:, [5:7 1:4]), 14, [t ' : dh']);
0820 
0821 t = '[h, dh] = om.eval_nln_constraint(x, 0, ''Qmisi'')';
0822 x = (1:om.var.N)';
0823 [h, dh] = om.eval_nln_constraint(x, 0, 'Qmisi');
0824 t_is(length(h), 2, 14, [t ' : length(h)']);
0825 t_is(h, eh([4:5]), 14, [t ' : h']);
0826 t_is(full(dh), eQmisi, 14, [t ' : dh']);
0827 
0828 t = '[h, dh] = om.eval_nln_constraint(x, 0, ''mynli'', {2,2})';
0829 x = (1:om.var.N)';
0830 [h, dh] = om.eval_nln_constraint(x, 0, 'mynli', {2,2});
0831 t_is(length(h), 3, 14, [t ' : length(h)']);
0832 t_is(h, eh(11:13), 14, [t ' : h']);
0833 t_is(full(dh), emynli22(:, [5:7 25:26]), 14, [t ' : dh']);
0834 
0835 % h
0836 % full(dh)'
0837 
0838 t = 'eval_nln_constraint_hess';
0839 lam = (1:neN)'/100;
0840 d2G = om.eval_nln_constraint_hess(x, lam, 1);
0841 t_ok(issparse(d2G), [t ' : issparse(d2G)']);
0842 t_is(size(d2G), [vN, vN], 14, [t ' : size(d2G)']);
0843 % t_is(full(d2G(27:end, :)), zeros(vN-26, vN), 14, [t ' : d2G(27:end, :)']);
0844 % t_is(full(d2G(:, 27:end)), zeros(vN, vN-26), 14, [t ' : d2G(:, 27:end)']);
0845 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);
0846 t_is(d2G, e, 13, [t ' : d2G']);
0847 
0848 % d2G
0849 
0850 lam = -(1:niN)'/100;
0851 d2H = om.eval_nln_constraint_hess(x, lam, 0);
0852 t_ok(issparse(d2H), [t ' : issparse(d2H)']);
0853 t_is(size(d2H), [vN, vN], 14, [t ' : size(d2H)']);
0854 % t_is(full(d2H(27:end, :)), zeros(vN-26, vN), 14, [t ' : d2H(27:end, :)']);
0855 % t_is(full(d2H(:, 27:end)), zeros(vN, vN-26), 14, [t ' : d2H(:, 27:end)']);
0856 e = sparse([1:2 5:7], [1:2 5:7], [-9.04 -8.05 20.66 18.68 5.84], vN, vN);
0857 t_is(d2H, e, 13, [t ' : d2H']);
0858 
0859 %d2H
0860 
0861 %%-----  add_quad_cost  -----
0862 t = 'add_quad_cost';
0863 qcN = 0;
0864 qcNS = 0;
0865 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0866 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0867 
0868 t = 'om.add_quad_cost(''qc1'', <mat>Q, c, k, {''Pg'', ''Va''})';
0869 n = nVa + nPg;
0870 Q1 = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n:-1:1, 1:n, n, n);
0871 c1 = 10*(1:n)';
0872 k1 = n;
0873 om.add_quad_cost('qc1', Q1, c1, k1, {'Pg', 'Va'});
0874 qcNS = qcNS + 1; qcN = qcN + 1;
0875 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0876 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0877 
0878 t = 'om.add_quad_cost(''qc2'', <mat>Q, c)';
0879 n = om.getN('var');
0880 Q2 = sparse(1, 1:n, 1:n, n, n) + sparse(1:n, 1, n:-1:1, n, n);
0881 c2 = 10*(n:-1:1)';
0882 om.add_quad_cost('qc2', Q2, c2);
0883 qcNS = qcNS + 1; qcN = qcN + 1;
0884 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0885 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0886 
0887 t = 'om.add_quad_cost(''qc3'', <vec>Q, c, k, {''Vm2'', ''Pg''})';
0888 n = nVm2 + nPg;
0889 Q3 = 2*(1:n)';
0890 c3 = -1*(1:n)';
0891 k3 = -n;
0892 om.add_quad_cost('qc3', Q3, c3, k3, {'Vm2', 'Pg'});
0893 qcNS = qcNS + 1; qcN = qcN + n;
0894 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0895 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0896 
0897 t = 'om.add_quad_cost(''qc4'', <vec>Q, [], 0, vs)';
0898 n = om.getN('var', 'x', {2,1}) + om.getN('var', 'y', {1,1,1});
0899 Q4 = 1./(1:n)';
0900 vs = struct('name', {'x', 'y'}, 'idx', {{2,1}, {1,1,1}});
0901 om.add_quad_cost('qc4', Q4, [], 0, vs);
0902 qcNS = qcNS + 1; qcN = qcN + n;
0903 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0904 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0905 
0906 t = 'om.add_quad_cost(''qc5'', [], c, k, {''Pg'', ''Va''})';
0907 n = nVa + nPg;
0908 c5 = 100*(1:n)';
0909 k5 = (1:n)';
0910 om.add_quad_cost('qc5', [], c5, k5, {'Pg', 'Va'});
0911 qcNS = qcNS + 1; qcN = qcN + n;
0912 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0913 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0914 
0915 t = 'om.add_quad_cost(''qc6'', [], c, <sclr>k)';
0916 n = om.getN('var');
0917 c6 = -(1:n)';
0918 k6 = 3;
0919 om.add_quad_cost('qc6', [], c6, k6);
0920 qcNS = qcNS + 1; qcN = qcN + n;
0921 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0922 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0923 
0924 t = 'om.init_indexed_name(''qdc'', ''qc'', {2,2})';
0925 om.init_indexed_name('qdc', 'qc', {2,2});
0926 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0927 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0928 
0929 for i = 1:2
0930     for j = 1:2
0931         t = 'om.add_quad_cost(''qc'', {i, j}, cp, vs)';
0932         n = nPg + om.getN('var', 'x', {i,j});
0933         QQ = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n*ones(n,1), 1:n, n, n);
0934         cc = -2*(1:n)';
0935         kk = 1000;
0936         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0937         om.add_quad_cost('qc', {i, j}, QQ, cc, kk, vs);
0938         qcNS = qcNS + 1; qcN = qcN + 1;
0939         t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N  = %d', t, qcN));
0940         t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0941     end
0942 end
0943 
0944 %%-----  params_quad_cost  -----
0945 t = 'om.params_quad_cost(''qc1'')';
0946 [Q, c, k, vs] = om.params_quad_cost('qc1');
0947 t_is(Q, Q1, 14, [t, ' : Q']);
0948 t_is(c, c1, 14, [t, ' : c']);
0949 t_is(k, k1, 14, [t, ' : k']);
0950 vs1 = struct('name', {'Pg', 'Va'}, 'idx', {{}, {}});
0951 t_ok(isequal(vs, vs1), [t, ' : vs']);
0952 
0953 t = 'om.params_quad_cost(''qc2'')';
0954 [Q, c, k, vs] = om.params_quad_cost('qc2');
0955 t_is(Q, Q2, 14, [t, ' : Q']);
0956 t_is(c, c2, 14, [t, ' : c']);
0957 t_is(k, 0, 14, [t, ' : k']);
0958 t_ok(isequal(vs, {}), [t, ' : vs']);
0959 
0960 t = 'om.params_quad_cost(''qc3'')';
0961 [Q, c, k] = om.params_quad_cost('qc3');
0962 t_is(Q, Q3, 14, [t, ' : Q']);
0963 t_is(c, c3, 14, [t, ' : c']);
0964 t_is(k, k3, 14, [t, ' : k']);
0965 
0966 t = 'om.params_quad_cost(''qc4'')';
0967 [Q, c] = om.params_quad_cost('qc4');
0968 t_is(Q, Q4, 14, [t, ' : Q']);
0969 t_ok(isempty(c), [t, ' : c']);
0970 % t_is(k, 0, 14, [t, ' : k']);
0971 
0972 t = 'om.params_quad_cost(''qc5'')';
0973 [Q, c, k] = om.params_quad_cost('qc5');
0974 t_ok(isempty(Q), [t, ' : Q']);
0975 t_is(c, c5, 14, [t, ' : c']);
0976 t_is(k, k5, 14, [t, ' : k']);
0977 
0978 t = 'om.params_quad_cost(''qc6'')';
0979 [Q, c, k] = om.params_quad_cost('qc6');
0980 t_ok(isempty(Q), [t, ' : Q']);
0981 t_is(c, c6, 14, [t, ' : c']);
0982 t_is(k, k6, 14, [t, ' : k']);
0983 
0984 for i = 1:2
0985     for j = 1:2
0986         t = sprintf('om.params_quad_cost(''qc'', {%d, %d})', i, j);
0987         n = nPg + om.getN('var', 'x', {i,j});
0988         QQ = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n*ones(n,1), 1:n, n, n);
0989         cc = -2*(1:n)';
0990         kk = 1000;
0991         [Q, c, k] = om.params_quad_cost('qc', {i,j});
0992         t_is(Q, QQ, 14, [t, ' : Q']);
0993         t_is(c, cc, 14, [t, ' : c']);
0994         t_is(k, kk, 14, [t, ' : k']);
0995     end
0996 end
0997 
0998 t = 'om.params_quad_cost()';
0999 [Q, c, k] = om.params_quad_cost();
1000 % [ii, jj, ss] = find(Q)
1001 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]';
1002 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]';
1003 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];
1004 QQ = sparse(ii, jj, ss, om.var.N, om.var.N);
1005 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]';
1006 t_is(Q, QQ, 14, [t, ' : Q']);
1007 t_is(c, cc, 14, [t, ' : c']);
1008 t_is(k, k1+k3*length(Q3)+sum(k5)+k6*length(c6)+4000, 14, [t, ' : k']);
1009 
1010 %%-----  eval_quad_cost  -----
1011 t = 'om.eval_quad_cost(x, ''qc1'')';
1012 x = (1:om.var.N)';
1013 [Q, c, k, vs] = om.params_quad_cost('qc1');
1014 xx = om.varsets_x(x, vs, 'vector');
1015 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1016 edf = Q*xx + c;
1017 f = om.eval_quad_cost(x, 'qc1');
1018 t_is(f, ef, 14, [t, ' : f']);
1019 [f, df] = om.eval_quad_cost(x, 'qc1');
1020 t_is(f, ef, 14, [t, ' : f']);
1021 t_is(df, edf, 14, [t, ' : df']);
1022 [f, df, d2f] = om.eval_quad_cost(x, 'qc1');
1023 t_is(f, ef, 14, [t, ' : f']);
1024 t_is(df, edf, 14, [t, ' : df']);
1025 t_is(d2f, Q, 14, [t, ' : d2f']);
1026 
1027 t = 'om.eval_quad_cost(x, ''qc2'')';
1028 [Q, c, k, vs] = om.params_quad_cost('qc2');
1029 xx = om.varsets_x(x, vs, 'vector');
1030 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1031 edf = Q*xx + c;
1032 [f, df, d2f] = om.eval_quad_cost(x, 'qc2');
1033 t_is(f, ef, 14, [t, ' : f']);
1034 t_is(df, edf, 14, [t, ' : df']);
1035 t_is(d2f, Q, 14, [t, ' : d2f']);
1036 
1037 t = 'om.eval_quad_cost(x, ''qc3'')';
1038 [Q, c, k, vs] = om.params_quad_cost('qc3');
1039 xx = om.varsets_x(x, vs, 'vector');
1040 ef = 1/2 * Q.*xx.^2 + c.*xx + k;
1041 edf = Q.*xx + c;
1042 [f, df, d2f] = om.eval_quad_cost(x, 'qc3');
1043 t_is(f, ef, 14, [t, ' : f']);
1044 t_is(df, edf, 14, [t, ' : df']);
1045 t_is(d2f, Q, 14, [t, ' : d2f']);
1046 
1047 t = 'om.eval_quad_cost(x, ''qc4'')';
1048 [Q, c, k, vs] = om.params_quad_cost('qc4');
1049 xx = om.varsets_x(x, vs, 'vector');
1050 ef = 1/2 * Q.*xx.^2 + k;
1051 edf = Q.*xx;
1052 [f, df, d2f] = om.eval_quad_cost(x, 'qc4');
1053 t_is(f, ef, 14, [t, ' : f']);
1054 t_is(df, edf, 14, [t, ' : df']);
1055 t_is(d2f, Q, 14, [t, ' : d2f']);
1056 
1057 t = 'om.eval_quad_cost(x, ''qc5'')';
1058 [Q, c, k, vs] = om.params_quad_cost('qc5');
1059 xx = om.varsets_x(x, vs, 'vector');
1060 ef = c.*xx + k;
1061 edf = c;
1062 [f, df, d2f] = om.eval_quad_cost(x, 'qc5');
1063 t_is(f, ef, 14, [t, ' : f']);
1064 t_is(df, edf, 14, [t, ' : df']);
1065 t_is(d2f, sparse(length(xx), 1), 14, [t, ' : d2f']);
1066 
1067 t = 'om.eval_quad_cost(x, ''qc6'')';
1068 [Q, c, k, vs] = om.params_quad_cost('qc6');
1069 xx = x;
1070 ef = c.*xx + k;
1071 edf = c;
1072 [f, df, d2f] = om.eval_quad_cost(x, 'qc6');
1073 t_is(f, ef, 14, [t, ' : f']);
1074 t_is(df, edf, 14, [t, ' : df']);
1075 t_is(d2f, sparse(length(x), 1), 14, [t, ' : d2f']);
1076 
1077 for i = 1:2
1078     for j = 1:2
1079         t = sprintf('om.eval_quad_cost(x, ''qc'', {%d, %d})', i, j);
1080         [Q, c, k, vs] = om.params_quad_cost('qc', {i,j});
1081         xx = om.varsets_x(x, vs, 'vector');
1082         ef = 1/2 * xx'*Q*xx + c'*xx + k;
1083         edf = Q*xx + c;
1084         [f, df, d2f] = om.eval_quad_cost(x, 'qc', {i,j});
1085         t_is(f, ef, 14, [t, ' : f']);
1086         t_is(df, edf, 14, [t, ' : df']);
1087         t_is(d2f, Q, 14, [t, ' : d2f']);
1088     end
1089 end
1090 
1091 t = 'om.eval_quad_cost(x)';
1092 [Q, c, k] = om.params_quad_cost();
1093 xx = x;
1094 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1095 edf = Q*xx + c;
1096 [f, df, d2f] = om.eval_quad_cost(x);
1097 t_is(f, ef, 14, [t, ' : f']);
1098 t_is(df, edf, 14, [t, ' : df']);
1099 t_is(d2f, Q, 14, [t, ' : d2f']);
1100 
1101 
1102 %%-----  add_legacy_cost  -----
1103 t = 'add_legacy_cost';
1104 cN = 0;
1105 cNS = 0;
1106 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
1107 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
1108 
1109 t = 'om.add_legacy_cost(''ucost'', cp, {''Va'', ''Pg''})';
1110 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
1111             'Cw', [2;3]);
1112 om.add_legacy_cost('ucost', cp, {'Va', 'Pg'});
1113 cNS = cNS + 1; cN = cN + 2;
1114 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
1115 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
1116 
1117 t = 'om.add_legacy_cost(''vcost'', cp)';
1118 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
1119             'Cw', [2;3]);
1120 om.add_legacy_cost('vcost', cp);
1121 cNS = cNS + 1; cN = cN + 2;
1122 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
1123 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
1124 
1125 t = 'om.init_indexed_name(''cost'', ''wc'', {2,2})';
1126 om.init_indexed_name('cost', 'wc', {2,2});
1127 t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
1128 t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
1129 
1130 for i = 1:2
1131     for j = 1:2
1132         t = 'om.add_legacy_cost(''wc'', {i, j}, cp, vs)';
1133         cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
1134             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
1135         cp.Cw = (i+j:-1:1)';
1136         if i == 2
1137             cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
1138         end
1139         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
1140         om.add_legacy_cost('wc', {i, j}, cp, vs);
1141         cNS = cNS + 1; cN = cN + i+j;
1142         t_ok(om.getN('cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
1143         t_ok(om.get('cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
1144     end
1145 end
1146 
1147 t = 'params_legacy_cost';
1148 cp = om.params_legacy_cost();
1149 t_ok(isfield(cp, 'N'), t);
1150 
1151 %%-----  get_idx  -----
1152 t = 'get_idx : cost';
1153 cc = om.get_idx('cost');
1154 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 14, [t ' : vcost']);
1155 t_is(size(cc.i1.wc), [2, 2], 14, [t ' : size(cc.i1.wc)']);
1156 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)']);
1157 
1158 t = 'get_idx(''var'', ''cost'', ''lin'')';
1159 [vv, cc, ll] = om.get_idx('var', 'cost', 'lin');
1160 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
1161 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
1162 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)']);
1163 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
1164 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)']);
1165 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 14, [t ' : vcost']);
1166 t_is(size(cc.i1.wc), [2, 2], 14, [t ' : size(cc.i1.wc)']);
1167 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)']);
1168 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
1169 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
1170 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)']);
1171 
1172 %%-----  params_legacy_cost  -----
1173 t = 'om.params_legacy_cost(''ucost'')';
1174 cp = om.params_legacy_cost('ucost');
1175 n = nVa + nPg;
1176 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, n);
1177 t_is(full(cp.N), full(N), 14, [t, ' : N']);
1178 t_is(cp.Cw, [2;3], 14, [t, ' : Cw']);
1179 t_is(full(cp.H), zeros(2,2), 14, [t, ' : H']);
1180 t_is(cp.dd, ones(2,1),  14, [t, ' : dd']);
1181 t_is(cp.rh, zeros(2,1), 14, [t, ' : rh']);
1182 t_is(cp.kk, zeros(2,1), 14, [t, ' : kk']);
1183 t_is(cp.mm, ones(2,1),  14, [t, ' : mm']);
1184 
1185 t = 'om.params_legacy_cost(''vcost'')';
1186 cp = om.params_legacy_cost('vcost');
1187 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
1188 t_is(full(cp.N), full(N), 14, [t, ' : N']);
1189 t_is(cp.Cw, [2;3], 14, [t, ' : Cw']);
1190 t_is(full(cp.H), zeros(2,2), 14, [t, ' : H']);
1191 t_is(cp.dd, ones(2,1),  14, [t, ' : dd']);
1192 t_is(cp.rh, zeros(2,1), 14, [t, ' : rh']);
1193 t_is(cp.kk, zeros(2,1), 14, [t, ' : kk']);
1194 t_is(cp.mm, ones(2,1),  14, [t, ' : mm']);
1195 
1196 t = 'om.params_legacy_cost(''wc'') : error';
1197 try
1198     cp = om.params_legacy_cost('wc')
1199     t_ok(0, t);
1200 catch
1201     t_ok(strfind(lasterr, '@opt_model/params_legacy_cost: legacy cost set ''wc'' requires an IDX_LIST arg'), t);
1202 end
1203 
1204 t = 'om.params_legacy_cost(''wc'', {1,2})';
1205 cp = om.params_legacy_cost('wc', {1,2});
1206 n = nPg + om.getN('var', 'x', {1,2});
1207 N = sparse([1:3 1:3]', [1:3 nPg+2*ones(1,3)]', [ones(3,1);-ones(3,1)], 3, n);
1208 t_is(full(cp.N), full(N), 14, [t, ' : N']);
1209 t_is(cp.Cw, [3;2;1], 14, [t, ' : Cw']);
1210 t_is(full(cp.H), zeros(3,3), 14, [t, ' : H']);
1211 t_is(cp.dd, ones(3,1),  14, [t, ' : dd']);
1212 t_is(cp.rh, zeros(3,1), 14, [t, ' : rh']);
1213 t_is(cp.kk, zeros(3,1), 14, [t, ' : kk']);
1214 t_is(cp.mm, ones(3,1),  14, [t, ' : mm']);
1215 
1216 t = 'om.params_legacy_cost(''wc'', {2,1})';
1217 cp = om.params_legacy_cost('wc', {2,1});
1218 n = nPg + om.getN('var', 'x', {2,1});
1219 N = sparse([1:3 1:3]', [1:3 nPg+2*ones(1,3)]', [ones(3,1);-ones(3,1)], 3, n);
1220 t_is(full(cp.N), full(N), 14, [t, ' : N']);
1221 t_is(cp.Cw, [3;2;1], 14, [t, ' : Cw']);
1222 H = sparse(1:3, 1:3, 1:3, 3, 3);
1223 t_is(full(cp.H), full(H), 14, [t, ' : H']);
1224 t_is(cp.dd, ones(3,1),  14, [t, ' : dd']);
1225 t_is(cp.rh, zeros(3,1), 14, [t, ' : rh']);
1226 t_is(cp.kk, zeros(3,1), 14, [t, ' : kk']);
1227 t_is(cp.mm, ones(3,1),  14, [t, ' : mm']);
1228 
1229 t = 'om.params_legacy_cost()';
1230 cp = om.params_legacy_cost();
1231 t_ok(issparse(cp.N), [t ' : issparse(cp.N)']);
1232 t_is(size(cp.N), [cN, vN], 14, [t ' : size(cp.N)']);
1233 t_is(size(cp.H), [cN, cN], 14, [t ' : size(cp.H)']);
1234 t_is(length(cp.Cw), cN, 14, [t ' : length(cp.Cw)']);
1235 t_is(length(cp.dd), cN, 14, [t ' : length(cp.dd)']);
1236 t_is(length(cp.rh), cN, 14, [t ' : length(cp.rh)']);
1237 t_is(length(cp.kk), cN, 14, [t ' : length(cp.kk)']);
1238 t_is(length(cp.mm), cN, 14, [t ' : length(cp.mm)']);
1239 N = sparse([1:2 1:2]', [1:4]', [1 1 -1 -1]', 2, 4);
1240 Cw = [2;3];
1241 H = zeros(2,2);
1242 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Va:vv.iN.Va)), full(N), 14, [t ' : N(<vcost>,<Va>)']);
1243 N = sparse([1:2]', [1 3]', [2 2]', 2, 3);
1244 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Pg:vv.iN.Pg)), full(N), 14, [t ' : N(<vcost>,<Pg>)']);
1245 t_is(full(cp.Cw(cc.i1.vcost:cc.iN.vcost)), Cw, 14, [t ' : Cw(<vcost>)']);
1246 t_is(full(cp.H(cc.i1.vcost:cc.iN.vcost, cc.i1.vcost:cc.iN.vcost)), full(H), 14, [t ' : H(<vcost>,<vcost>)']);
1247 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
1248 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>)']);
1249 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 2);
1250 Cw = [3;2;1];
1251 H = zeros(3,3);
1252 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)>)']);
1253 t_is(full(cp.Cw(cc.i1.wc(1,2):cc.iN.wc(1,2))), Cw, 14, [t ' : Cw(<wc(1,2)>)']);
1254 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)>)']);
1255 
1256 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
1257 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>)']);
1258 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 3);
1259 Cw = [3;2;1];
1260 H = full(sparse((1:3)', (1:3)', (1:3)', 3, 3));
1261 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)>)']);
1262 t_is(full(cp.Cw(cc.i1.wc(2,1):cc.iN.wc(2,1))), Cw, 14, [t ' : Cw(<wc(2,1)>)']);
1263 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)>)']);
1264 
1265 t_is(cp.dd, ones(cN,1),  14, [t, ' : dd']);
1266 t_is(cp.rh, zeros(cN,1), 14, [t, ' : rh']);
1267 t_is(cp.kk, zeros(cN,1), 14, [t, ' : kk']);
1268 t_is(cp.mm, ones(cN,1),  14, [t, ' : mm']);
1269 
1270 %%-----  eval_legacy_cost  -----
1271 t = 'om.eval_legacy_cost(x)';
1272 x = [1:7 rand(1,10) 8:(vN-10)]';
1273 f = om.eval_legacy_cost(x);
1274 t_is(f, 343, 14, t);
1275 
1276 t = 'om.eval_legacy_cost(''ucost'')';
1277 f = om.eval_legacy_cost(x, 'ucost');
1278 t_is(f, 52, 14, t);
1279 
1280 t = 'om.eval_legacy_cost(''wc'', {2,1})';
1281 f = om.eval_legacy_cost(x, 'wc', {2,1});
1282 t_is(f, 91, 14, t);
1283 
1284 t = 'om.eval_legacy_cost(''wc'')';
1285 f = om.eval_legacy_cost(x, 'wc');
1286 t_is(f, 239, 14, t);
1287 
1288 %%-----  add_nln_cost  -----
1289 t = 'add_nln_cost';
1290 nlcN = 0;
1291 nlcNS = 0;
1292 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1293 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1294 
1295 t = 'om.add_nln_cost(''ucost'', 1, fcn, {''Va'', ''Pg''})';
1296 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
1297             'Cw', [2;3]);
1298 fcn = @(x)my_legacy_cost_fcn(x, cp, om, {'Va', 'Pg'});
1299 om.add_nln_cost('ucost', 1, fcn, {'Va', 'Pg'});
1300 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1301 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1302 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1303 
1304 t = 'om.add_nln_cost(''vcost'', cp)';
1305 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
1306             'Cw', [2;3]);
1307 fcn = @(x)my_legacy_cost_fcn(x, cp, om);
1308 om.add_nln_cost('vcost', 1, fcn);
1309 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1310 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1311 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1312 
1313 t = 'om.init_indexed_name(''nlc'', ''wc'', {2,2})';
1314 om.init_indexed_name('nlc', 'wc', {2,2});
1315 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1316 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1317 
1318 for i = 1:2
1319     for j = 1:2
1320         t = 'om.add_nln_cost(''wc'', {i, j}, cp, vs)';
1321         cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
1322             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
1323         cp.Cw = (i+j:-1:1)';
1324         if i == 2
1325             cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
1326         end
1327         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
1328         fcn = @(x)my_legacy_cost_fcn(x, cp, om, vs);
1329         om.add_nln_cost('wc', {i, j}, 1, fcn, vs);
1330         nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1331         t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N  = %d', t, nlcN));
1332         t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1333     end
1334 end
1335 
1336 %%-----  params_nln_cost  -----
1337 t = 'om.params_nln_cost(''ucost'')';
1338 [N, fcn] = om.params_nln_cost('ucost');
1339 t_is(N, 1, 14, [t, ' : N']);
1340 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1341 
1342 t = 'om.params_nln_cost(''vcost'')';
1343 [N, fcn] = om.params_nln_cost('vcost');
1344 t_is(N, 1, 14, [t, ' : N']);
1345 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1346 
1347 t = 'om.params_nln_cost(''wc'') : error';
1348 try
1349     [N, fcn] = om.params_nln_cost('wc')
1350     t_ok(0, t);
1351 catch
1352     t_ok(strfind(lasterr, '@opt_model/params_nln_cost: general nonlinear cost set ''wc'' requires an IDX_LIST arg'), t);
1353 end
1354 
1355 t = 'om.params_nln_cost(''wc'', {1,2})';
1356 [N, fcn] = om.params_nln_cost('wc', {1,2});
1357 t_is(N, 1, 14, [t, ' : N']);
1358 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1359 
1360 t = 'om.params_nln_cost(''wc'', {2,1})';
1361 [N, fcn] = om.params_nln_cost('wc', {2,1});
1362 t_is(N, 1, 14, [t, ' : N']);
1363 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1364 
1365 %%-----  eval_nln_cost  -----
1366 t = 'om.eval_nln_cost(x) : ';
1367 x = [1:7 rand(1,10) 8:(vN-10)]';
1368 f = om.eval_nln_cost(x);
1369 ef = 343;
1370 ii = [1 2 3 4 5 6 7 19 21 23 25 26];
1371 jj = [1 1 1 1 1 1 1 1 1 1 1 1];
1372 ss = [4 6 -4 -6 1 -26 -29 -3 -6 34 -3 52];
1373 edf = full(sparse(ii,jj,ss,vN,1));
1374 ii = [5 23 26 6 23 26 7 23 26 5 6 7 23 25 26 5 6 7 25 26];
1375 jj = [5 5 5 6 6 6 7 7 7 23 23 23 23 25 25 26 26 26 26 26];
1376 ss = [2 -1 -1 4 -2 -2 6 -3 -3 -1 -2 -3 6 4 -4 -1 -2 -3 -4 10];
1377 ed2f = full(sparse(ii,jj,ss,vN,vN));
1378 t_is(f, ef, 14, [t 'f']);
1379 [f, df] = om.eval_nln_cost(x);
1380 t_is(f, ef, 14, [t 'f']);
1381 t_is(df, edf, 14, [t 'df']);
1382 [f, df, d2f] = om.eval_nln_cost(x);
1383 t_is(f, ef, 14, [t 'f']);
1384 t_is(df, edf, 14, [t 'df']);
1385 t_is(d2f, ed2f, 14, [t 'd2f']);
1386 
1387 t = 'om.eval_nln_cost(''ucost'') : ';
1388 f = om.eval_nln_cost(x, 'ucost');
1389 ef = 52;
1390 edf = [2; 3; -2; -3; 4; 0; 6];
1391 t_is(f, ef, 14, [t 'f']);
1392 [f, df] = om.eval_nln_cost(x, 'ucost');
1393 t_is(f, ef, 14, [t 'f']);
1394 t_is(df, edf, 14, [t 'df']);
1395 [f, df, d2f] = om.eval_nln_cost(x, 'ucost');
1396 t_is(f, ef, 14, [t 'f']);
1397 t_is(df, edf, 14, [t 'df']);
1398 t_is(full(d2f), zeros(7,7), 14, [t 'd2f']);
1399 
1400 t = 'om.eval_nln_cost(''wc'', {2,1}) : ';
1401 f = om.eval_nln_cost(x, 'wc', {2,1});
1402 ef = 91;
1403 edf = [-5; -12; -17; 0; 34; 0];
1404 ii = [1 5 2 5 3 5 1 2 3 5];
1405 jj = [1 1 2 2 3 3 5 5 5 5];
1406 ss = [1 -1 2 -2 3 -3 -1 -2 -3 6];
1407 ed2f = full(sparse(ii,jj,ss,6,6));
1408 t_is(f, ef, 14, [t 'f']);
1409 [f, df] = om.eval_nln_cost(x, 'wc', {2,1});
1410 t_is(f, ef, 14, [t 'f']);
1411 t_is(df, edf, 14, [t 'df']);
1412 [f, df, d2f] = om.eval_nln_cost(x, 'wc', {2,1});
1413 t_is(f, ef, 14, [t 'f']);
1414 t_is(df, edf, 14, [t 'df']);
1415 t_is(d2f, ed2f, 14, [t 'd2f']);
1416 
1417 t = 'om.eval_nln_cost(''wc'') : ';
1418 f = om.eval_nln_cost(x, 'wc');
1419 t_is(f, 239, 14, [t 'f']);
1420 
1421 %%-----  copy  -----
1422 t = 'copy constructor';
1423 if have_feature('octave') && have_feature('octave', 'vnum') < 5.003
1424     t_skip(1, [t ' - https://savannah.gnu.org/bugs/?52614']);
1425 else
1426     om1 = opf_model(om);
1427     om1.add_var('test', 10);
1428     t_is(om1.var.N, om.var.N+10, 12, t);
1429 end
1430 
1431 t = 'copy';
1432 om2 = om.copy();
1433 om2.add_var('test', 10);
1434 t_is(om2.var.N, om.var.N+10, 12, t);
1435 
1436 % om
1437 % om = struct(om);
1438 
1439 t_end
1440 
1441 function [g, dg] = my_fcn(x, p1, p2)
1442 if iscell(x)
1443     xx = [];
1444     for k = 1:length(x)
1445         xx = [xx; x{k}];
1446     end
1447 else
1448     xx = x;
1449 end
1450 M = p1;
1451 N = length(xx);
1452 if M > N
1453     error('M <= length(x)');
1454 end
1455 g = xx(1:M) + p2;
1456 dg = sparse(1:M, 1:M, p2, M, N) + sparse(1, 1:N, xx, M, N);
1457 
1458 function d2G = my_hess(x, lam, p3)
1459 if iscell(x)
1460     xx = [];
1461     for k = 1:length(x)
1462         xx = [xx; x{k}];
1463     end
1464 else
1465     xx = x;
1466 end
1467 N = length(xx);
1468 M = length(lam);
1469 MM = min(M, N);
1470 d2G = sparse(1:MM, 1:MM, xx(1:MM) + lam(1:MM) + p3, N, N);
1471 %full(d2G(1:MM,1:MM))
1472 
1473 function [varargout] = my_legacy_cost_fcn(x, cp, om, vs)
1474 [nw, nx] = size(cp.N);
1475 if ~isfield(cp, 'H') || isempty(cp.H)
1476     cp.H = sparse(nw, nw);
1477 end
1478 if ~isfield(cp, 'dd') || isempty(cp.dd)
1479     cp.dd = ones(nw, 1);
1480 end
1481 if ~isfield(cp, 'rh') || isempty(cp.rh)
1482     cp.rh = zeros(nw, 1);
1483 end
1484 if ~isfield(cp, 'kk') || isempty(cp.kk)
1485     cp.kk = zeros(nw, 1);
1486 end
1487 if ~isfield(cp, 'mm') || isempty(cp.mm)
1488     cp.mm = ones(nw, 1);
1489 end
1490 if iscell(x)
1491     xx = vertcat(x{:});
1492 else
1493     xx = x;
1494 end
1495 [varargout{1:nargout}] = opf_legacy_user_cost_fcn(xx, cp);

Generated on Fri 09-Oct-2020 11:21:31 by m2html © 2005