0001 function t_opt_model(quiet)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 if nargin < 1
0013 quiet = 0;
0014 end
0015
0016 num_tests = 686;
0017
0018 t_begin(num_tests, quiet);
0019
0020
0021 t = 'constructor';
0022 om = opt_model;
0023 t_ok(isa(om, 'opt_model'), t);
0024
0025
0026 t = 'add_var';
0027 vN = 0;
0028 vNS = 0;
0029 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0030 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0031
0032 t = 'om.add_var(''Va'', 4)';
0033 nVa = 4;
0034 om.add_var('Va', nVa);
0035 vNS = vNS + 1; vN = vN + nVa;
0036 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0037 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0038
0039 t = 'om.add_var(''Pg'', 3, Pg0, Pgmin, Pgmax)';
0040 nPg = 3;
0041 om.add_var('Pg', nPg, [2;4;6], [1;2;3], [10;20;30]);
0042 vNS = vNS + 1; vN = vN + nPg;
0043 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0044 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0045
0046 t = 'om.add_var(''Vm1'', 5, V0, Vmin, Vmax, ''I'')';
0047 V0 = 1;
0048 Vmin = 0;
0049 Vmax = 1 + 0.01*(1:5)';
0050 vt = 'I';
0051 om.add_var('Vm1', 5, V0, Vmin, Vmax, vt);
0052 vNS = vNS + 1; vN = vN + 5;
0053 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0054 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0055
0056 t = 'om.add_var(''Vm2'', 5, V0, Vmin, Vmax, ''CIBIC'')';
0057 nVm2 = 5;
0058 vt = 'CIBIC';
0059 om.add_var('Vm2', nVm2, V0, Vmin, Vmax, vt);
0060 vNS = vNS + 1; vN = vN + nVm2;
0061 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0062 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0063
0064 t = 'om.init_indexed_name(''var'', ''x'', dims)';
0065 om.init_indexed_name('var', 'x', {2,2});
0066 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0067 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0068
0069 t = 'om.add_var(''x'', {1,1}, 2)';
0070 om.add_var('x', {1,1}, 2);
0071 vNS = vNS + 1; vN = vN + 2;
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.add_var(''x'', {1,2}, 2, x0(1,2))';
0076 om.add_var('x', {1,2}, 2, [-1;-2]);
0077 vNS = vNS + 1; vN = vN + 2;
0078 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0079 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0080
0081 t = 'om.add_var(''x'', {2,1}, 3)';
0082 om.add_var('x', {2,1}, 3);
0083 vNS = vNS + 1; vN = vN + 3;
0084 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0085 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0086
0087 t = 'om.add_var(''x'', {2,2}, 2, x0(2,2), xmin(2,2), xmax(2,2))';
0088 om.add_var('x', {2,2}, 2, [1;0],[0;-1],[2;1]);
0089 vNS = vNS + 1; vN = vN + 2;
0090 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0091 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0092
0093 t = 'om.init_indexed_name(''var'', ''y'', {2,3,4})';
0094 om.init_indexed_name('var', 'y', {2,3,4});
0095 vt0 = {'C', 'I', 'B'};
0096 for i = 1:2
0097 for j = 1:3
0098 for k = 1:4
0099 n = i+j+k;
0100 if i == 1
0101 vt = vt0{j};
0102 else
0103 vt = char(vt0{j} * ones(1, n));
0104 vt(j+1) = vt0{1+rem(j,3)};
0105 end
0106
0107 t = sprintf('om.add_var(''y'', {%d,%d,%d}, y0, ymin, ymax, vt)', i,j,k);
0108 om.add_var('y', {i,j,k}, n, 10*(n:-1:1)', -1*(n:-1:1)', 100+(n:-1:1)', vt);
0109 vNS = vNS + 1; vN = vN + n;
0110 t_ok(om.getN('var') == vN, sprintf('%s : var.N = %d', t, vN));
0111 t_ok(om.get('var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0112 end
0113 end
0114 end
0115
0116
0117 t = 'om.getN(''var'', ''Pg'') == 3';
0118 t_ok(om.getN('var', 'Pg') == 3, t);
0119
0120 t = 'size(om.getN(''var'', ''x'')) == [2 2]';
0121 t_is(size(om.getN('var', 'x')), [2,2], 14, t);
0122
0123 t = 'om.getN(''var'', ''x'')(1,2) == 2';
0124 N = om.getN('var', 'x');
0125 t_is(N(1,2), 2, 14, t);
0126
0127 t = 'om.getN(''var'', ''x'', {2,1}) == 3';
0128 t_is(om.getN('var', 'x', {2,1}), 3, 14, t);
0129
0130 t = 'om.getN(''var'', ''y'', {2,1,3}) == 6';
0131 t_is(om.getN('var', 'y', {2,1,3}), 6, 14, t);
0132
0133 t = 'om.getN(''var'')';
0134 t_is(om.getN('var'), vN, 14, t);
0135
0136
0137 t = 'get_idx : var';
0138 vv = om.get_idx();
0139 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
0140 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
0141 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)']);
0142 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
0143 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)']);
0144
0145 t = 'get_idx(''var'')';
0146 vv = om.get_idx('var');
0147 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
0148 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
0149 t_is([vv.i1.x(2,1) vv.iN.x(2,1) vv.N.x(2,1)], [22 24 3], 14, [t ' : x(2,1)']);
0150 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
0151 t_is([vv.i1.y(2,2,4) vv.iN.y(2,2,4) vv.N.y(2,2,4)], [133 140 8], 14, [t ' : y(2,2,4)']);
0152
0153
0154 t = 'om.params_var(''Va'')';
0155 [v0, vl, vu] = om.params_var('Va');
0156 t_ok(~any(v0), [t ' : v0']);
0157 t_ok(all(isinf(vl) & vl < 0), [t ' : vl']);
0158 t_ok(all(isinf(vu) & vu > 0), [t ' : vu']);
0159
0160 t = 'om.params_var(''Pg'')';
0161 [v0, vl, vu] = om.params_var('Pg');
0162 t_is(v0, [2;4;6], 14, [t ' : v0']);
0163 t_is(vl, [1;2;3], 14, [t ' : vl']);
0164 t_is(vu, [10;20;30], 14, [t ' : vu']);
0165
0166 t = 'om.params_var(''Vm1'')';
0167 [v0, vl, vu, vt] = om.params_var('Vm1');
0168 t_is(double(vt), double('I'), 14, [t ' : vt']);
0169
0170 t = 'om.params_var(''Vm2'')';
0171 [v0, vl, vu, vt] = om.params_var('Vm2');
0172 t_is(double(vt), double('CIBIC'), 14, [t ' : vt']);
0173
0174 t = 'om.params_var(''x'')';
0175 [v0, vl, vu, vt] = om.params_var('x');
0176 t_is(size(v0), [2,2], 14, [t ' : size(v0)']);
0177 t_is(v0{2,2}, [1;0], 14, [t ' : v0{2,2}']);
0178 t_is(vl{2,2}, [0;-1], 14, [t ' : vl{2,2}']);
0179 t_is(vu{2,2}, [2;1], 14, [t ' : vu{2,2}']);
0180 t_is(double(vt{2,2}), double('C'), 14, [t ' : vt{2,2}']);
0181
0182 for i = 1:2
0183 for j = 1:3
0184 for k = 1:4
0185 n = i+j+k;
0186 if i == 1
0187 vt = vt0{j};
0188 else
0189 vt = char(vt0{j} * ones(1, n));
0190 vt(j+1) = vt0{1+rem(j,3)};
0191 end
0192 t = sprintf('om.params_var(''y'', {%d,%d,%d})', i, j, k);
0193 [v0, vl, vu, gvt] = om.params_var('y', {i,j,k});
0194 t_is(v0, 10*(n:-1:1)', 14, [t ' : v0']);
0195 t_is(vl, -1*(n:-1:1)', 14, [t ' : vl']);
0196 t_is(vu, 100+(n:-1:1)', 14, [t ' : vu']);
0197 t_is(gvt, vt, 14, [t ' : vt']);
0198 end
0199 end
0200 end
0201
0202 t = 'om.params_var()';
0203 [v0, vl, vu, vt] = om.params_var();
0204 t_ok(length(v0) == om.getN('var'), [t ' : length(v0)']);
0205 t_ok(length(vl) == om.getN('var'), [t ' : length(vl)']);
0206 t_ok(length(vu) == om.getN('var'), [t ' : length(vu)']);
0207 t_is(v0(vv.i1.x(2,2):vv.iN.x(2,2)), [1;0], 14, [t ' : v0(vv.i1.x(2,2):vv.iN.x(2,2))']);
0208 t_is(vl(vv.i1.x(2,2):vv.iN.x(2,2)), [0;-1], 14, [t ' : vl(vv.i1.x(2,2):vv.iN.x(2,2))']);
0209 t_is(vu(vv.i1.x(2,2):vv.iN.x(2,2)), [2;1], 14, [t ' : vu(vv.i1.x(2,2):vv.iN.x(2,2))']);
0210 t_is(vt(vv.i1.x(2,2):vv.iN.x(2,2)), 'C', 14, [t ' : vt(vv.i1.x(2,2):vv.iN.x(2,2))']);
0211 n = 8;
0212 t_is(v0(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), 10*(n:-1:1)', 14, [t ' : v0(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0213 t_is(vl(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), -1*(n:-1:1)', 14, [t ' : vl(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0214 t_is(vu(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), 100+(n:-1:1)', 14, [t ' : vu(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0215 t_is(vt(vv.i1.y(2,2,4):vv.iN.y(2,2,4)), 'IIBIIIII', 14, [t ' : vt(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0216 vt0 = 'CCCCCCCIIIIICIBICCCCCCCCCCCCCCCCCCCCCCCCCCCCIIIIIIIIIIIIIIIIIIIIIIBBBBBBBBBBBBBBBBBBBBBBBBBBCICCCICCCCICCCCCICCCCCIIBIIIIBIIIIIBIIIIIIBIIIIIBBBCBBBBBCBBBBBBCBBBBBBBCBBBBB';
0217 t_is(vt, vt0, 14, [t ' : vt']);
0218
0219
0220 t = 'om.varsets_len(vs) : ';
0221 vs = om.varsets_cell2struct({'Pg'});
0222 t_is(om.varsets_len(vs), 3, 14, [t '{''Pg''}']);
0223
0224 vs = om.varsets_cell2struct({'Pg', 'Va'});
0225 t_is(om.varsets_len(vs), 7, 14, [t '{''Pg'', ''Va''}']);
0226
0227 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0228 t_is(om.varsets_len(vs), 5, 14, [t '''x'', {{1,1},{2,1}}']);
0229
0230 vs = om.varsets_cell2struct({'x'});
0231 t_is(om.varsets_len(vs), 9, 14, [t '{''x''}']);
0232
0233 vs = om.varsets_cell2struct({'x', 'y', 'Pg'});
0234 t_is(om.varsets_len(vs), 156, 14, [t '{''x'', ''y'', ''Pg''}']);
0235
0236 vs = om.varsets_cell2struct({});
0237 t_is(om.varsets_len(vs), om.var.N, 14, [t '<all>']);
0238
0239
0240 t = 'om.varsets_idx(vs) : ';
0241 vv = om.get_idx('var');
0242 vs = om.varsets_cell2struct({'Pg'});
0243 t_is(om.varsets_idx(vs), [vv.i1.Pg:vv.iN.Pg], 14, [t '{''Pg''}']);
0244
0245 vs = om.varsets_cell2struct({'Pg', 'Va'});
0246 t_is(om.varsets_idx(vs), [vv.i1.Pg:vv.iN.Pg vv.i1.Va:vv.iN.Va], 14, [t '{''Pg'', ''Va''}']);
0247
0248 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0249 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}}']);
0250
0251 vs = om.varsets_cell2struct({'x'});
0252 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''}']);
0253
0254 vs = om.varsets_cell2struct({});
0255 t_is(om.varsets_idx(vs), 1:om.var.N, 14, [t '<all>']);
0256
0257
0258 t = 'varsets_x(x, vs) : ';
0259 x = (1:om.var.N)';
0260 vs = om.varsets_cell2struct({'Pg'});
0261 xx = om.varsets_x(x, vs);
0262 t_is(length(xx), 1, 14, [t '{''Pg''} : length']);
0263 t_is(xx{1}, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''Pg''} : 1']);
0264
0265 vs = om.varsets_cell2struct({'Pg', 'Va'});
0266 xx = om.varsets_x(x, vs);
0267 t_is(length(xx), 2, 14, [t '{''Pg'', ''Va''} : length']);
0268 t_is(xx{1}, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''Pg'', ''Va''} : 1']);
0269 t_is(xx{2}, [vv.i1.Va:vv.iN.Va]', 14, [t '{''Pg'', ''Va''} : 2']);
0270
0271 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0272 xx = om.varsets_x(x, vs);
0273 t_is(length(xx), 2, 14, [t '''x'', {{1,1},{2,1}} : length']);
0274 t_is(xx{1}, [vv.i1.x(1,1):vv.iN.x(1,1)]', 14, [t '''x'', {{1,1},{2,1}} : 1']);
0275 t_is(xx{2}, [vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '''x'', {{1,1},{2,1}} : 2']);
0276
0277 vs = om.varsets_cell2struct({'x'});
0278 xx = om.varsets_x(x, vs);
0279 t_is(length(xx), 4, 14, [t '{''x''} : length']);
0280 t_is(xx{1}, [vv.i1.x(1,1):vv.iN.x(1,1)]', 14, [t '{''x''} : 1']);
0281 t_is(xx{2}, [vv.i1.x(1,2):vv.iN.x(1,2)]', 14, [t '{''x''} : 2']);
0282 t_is(xx{3}, [vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '{''x''} : 3']);
0283 t_is(xx{4}, [vv.i1.x(2,2):vv.iN.x(2,2)]', 14, [t '{''x''} : 4']);
0284
0285 vs = om.varsets_cell2struct({'x', 'y', 'Pg'});
0286 xx = om.varsets_x(x, vs);
0287 t_is(length(xx), 29, 14, [t '{''x'', ''y'', ''Pg''} : length']);
0288 t_is(xx{ 1}, [vv.i1.x(1,1):vv.iN.x(1,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 1']);
0289 t_is(xx{ 2}, [vv.i1.x(1,2):vv.iN.x(1,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 2']);
0290 t_is(xx{ 3}, [vv.i1.x(2,1):vv.iN.x(2,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 3']);
0291 t_is(xx{ 4}, [vv.i1.x(2,2):vv.iN.x(2,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 4']);
0292 t_is(xx{ 5}, [vv.i1.y(1,1,1):vv.iN.y(1,1,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 5']);
0293 t_is(xx{ 6}, [vv.i1.y(1,1,2):vv.iN.y(1,1,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 6']);
0294 t_is(xx{ 7}, [vv.i1.y(1,1,3):vv.iN.y(1,1,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 7']);
0295 t_is(xx{ 8}, [vv.i1.y(1,1,4):vv.iN.y(1,1,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 8']);
0296 t_is(xx{ 9}, [vv.i1.y(1,2,1):vv.iN.y(1,2,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 9']);
0297 t_is(xx{10}, [vv.i1.y(1,2,2):vv.iN.y(1,2,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 10']);
0298 t_is(xx{11}, [vv.i1.y(1,2,3):vv.iN.y(1,2,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 11']);
0299 t_is(xx{12}, [vv.i1.y(1,2,4):vv.iN.y(1,2,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 12']);
0300 t_is(xx{13}, [vv.i1.y(1,3,1):vv.iN.y(1,3,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 13']);
0301 t_is(xx{14}, [vv.i1.y(1,3,2):vv.iN.y(1,3,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 14']);
0302 t_is(xx{15}, [vv.i1.y(1,3,3):vv.iN.y(1,3,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 15']);
0303 t_is(xx{16}, [vv.i1.y(1,3,4):vv.iN.y(1,3,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 16']);
0304 t_is(xx{17}, [vv.i1.y(2,1,1):vv.iN.y(2,1,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 17']);
0305 t_is(xx{18}, [vv.i1.y(2,1,2):vv.iN.y(2,1,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 18']);
0306 t_is(xx{19}, [vv.i1.y(2,1,3):vv.iN.y(2,1,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 19']);
0307 t_is(xx{20}, [vv.i1.y(2,1,4):vv.iN.y(2,1,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 20']);
0308 t_is(xx{21}, [vv.i1.y(2,2,1):vv.iN.y(2,2,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 21']);
0309 t_is(xx{22}, [vv.i1.y(2,2,2):vv.iN.y(2,2,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 22']);
0310 t_is(xx{23}, [vv.i1.y(2,2,3):vv.iN.y(2,2,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 23']);
0311 t_is(xx{24}, [vv.i1.y(2,2,4):vv.iN.y(2,2,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 24']);
0312 t_is(xx{25}, [vv.i1.y(2,3,1):vv.iN.y(2,3,1)]', 14, [t '{''x'', ''y'', ''Pg''} : 25']);
0313 t_is(xx{26}, [vv.i1.y(2,3,2):vv.iN.y(2,3,2)]', 14, [t '{''x'', ''y'', ''Pg''} : 26']);
0314 t_is(xx{27}, [vv.i1.y(2,3,3):vv.iN.y(2,3,3)]', 14, [t '{''x'', ''y'', ''Pg''} : 27']);
0315 t_is(xx{28}, [vv.i1.y(2,3,4):vv.iN.y(2,3,4)]', 14, [t '{''x'', ''y'', ''Pg''} : 28']);
0316 t_is(xx{29}, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''x'', ''y'', ''Pg''} : 29']);
0317
0318 vs = om.varsets_cell2struct({});
0319 xx = om.varsets_x(x, vs);
0320 t_is(length(xx), om.var.N, 14, [t '<all> : length']);
0321 t_is(xx, [1:om.var.N]', 14, [t '<all>']);
0322
0323 t = 'varsets_x(x, vs, ''vector'') : ';
0324 vs = om.varsets_cell2struct({'Pg'});
0325 xx = om.varsets_x(x, vs, 'vector');
0326 t_is(length(xx), vv.N.Pg, 14, [t '{''Pg''} : length']);
0327 t_is(xx, [vv.i1.Pg:vv.iN.Pg]', 14, [t '{''Pg''}']);
0328
0329 vs = om.varsets_cell2struct({'Pg', 'Va'});
0330 xx = om.varsets_x(x, vs, 'vector');
0331 t_is(length(xx), vv.N.Va + vv.N.Pg, 14, [t '{''Pg'', ''Va''} : length']);
0332 t_is(xx, [vv.i1.Pg:vv.iN.Pg vv.i1.Va:vv.iN.Va]', 14, [t '{''Pg'', ''Va''}']);
0333
0334 vs = struct('name', 'x', 'idx', {{1,1},{2,1}});
0335 xx = om.varsets_x(x, vs, 'vector');
0336 t_is(length(xx), vv.N.x(1,1) + vv.N.x(2,1), 14, [t '''x'', {{1,1},{2,1}} : length']);
0337 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}}']);
0338
0339 vs = om.varsets_cell2struct({'x'});
0340 xx = om.varsets_x(x, vs, 'vector');
0341 t_is(length(xx), sum(vv.N.x(:)), 14, [t '{''x''} : length']);
0342 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''}']);
0343
0344 vs = om.varsets_cell2struct({'x', 'y', 'Pg'});
0345 xx = om.varsets_x(x, vs, 'vector');
0346 t_is(length(xx), sum(vv.N.x(:))+sum(vv.N.y(:))+vv.N.Pg, 14, [t '{''x'', ''y'', ''Pg''} : length']);
0347 t_is(xx, [ vv.i1.x(1,1):vv.iN.x(1,1) vv.i1.x(1,2):vv.iN.x(1,2) ...
0348 vv.i1.x(2,1):vv.iN.x(2,1) vv.i1.x(2,2):vv.iN.x(2,2) ...
0349 vv.i1.y(1,1,1):vv.iN.y(1,1,1) vv.i1.y(1,1,2):vv.iN.y(1,1,2) ...
0350 vv.i1.y(1,1,3):vv.iN.y(1,1,3) vv.i1.y(1,1,4):vv.iN.y(1,1,4) ...
0351 vv.i1.y(1,2,1):vv.iN.y(1,2,1) vv.i1.y(1,2,2):vv.iN.y(1,2,2) ...
0352 vv.i1.y(1,2,3):vv.iN.y(1,2,3) vv.i1.y(1,2,4):vv.iN.y(1,2,4) ...
0353 vv.i1.y(1,3,1):vv.iN.y(1,3,1) vv.i1.y(1,3,2):vv.iN.y(1,3,2) ...
0354 vv.i1.y(1,3,3):vv.iN.y(1,3,3) vv.i1.y(1,3,4):vv.iN.y(1,3,4) ...
0355 vv.i1.y(2,1,1):vv.iN.y(2,1,1) vv.i1.y(2,1,2):vv.iN.y(2,1,2) ...
0356 vv.i1.y(2,1,3):vv.iN.y(2,1,3) vv.i1.y(2,1,4):vv.iN.y(2,1,4) ...
0357 vv.i1.y(2,2,1):vv.iN.y(2,2,1) vv.i1.y(2,2,2):vv.iN.y(2,2,2) ...
0358 vv.i1.y(2,2,3):vv.iN.y(2,2,3) vv.i1.y(2,2,4):vv.iN.y(2,2,4) ...
0359 vv.i1.y(2,3,1):vv.iN.y(2,3,1) vv.i1.y(2,3,2):vv.iN.y(2,3,2) ...
0360 vv.i1.y(2,3,3):vv.iN.y(2,3,3) vv.i1.y(2,3,4):vv.iN.y(2,3,4) ...
0361 vv.i1.Pg:vv.iN.Pg]', 14, [t '{''x'', ''y'', ''Pg''}']);
0362
0363 vs = om.varsets_cell2struct({});
0364 xx = om.varsets_x(x, vs, 'vector');
0365 t_is(length(xx), om.var.N, 14, [t '<all> : length']);
0366 t_is(xx, [1:om.var.N]', 14, [t '<all>']);
0367
0368
0369 t = 'add_lin_constraint';
0370 lN = 0;
0371 lNS = 0;
0372 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0373 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0374
0375 t = 'om.add_lin_constraint(''Pmis'', A, l, u, {''Va'', ''Pg''})';
0376 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);
0377 l1 = -(1:3)'; u1 = (1:3)';
0378 om.add_lin_constraint('Pmis', A1, l1, u1, {'Va', 'Pg'});
0379 lNS = lNS + 1; lN = lN + 3;
0380 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0381 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0382
0383 t = 'om.add_lin_constraint(''Qmis'', A, l, u)';
0384 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);
0385 om.add_lin_constraint('Qmis', A2, l1, u1);
0386 lNS = lNS + 1; lN = lN + 3;
0387 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0388 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0389
0390 t = 'om.init_indexed_name(''lin'', ''mylin'', {2, 2})';
0391 om.init_indexed_name('lin', 'mylin', {2, 2});
0392 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0393 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0394
0395 for i = 1:2
0396 for j = 1:2
0397 t = sprintf('om.add_lin_constraint(''mylin'', {%d,%d}, A, l, u, vs)', i,j);
0398 A = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0399 [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0400 l = -1; u = [];
0401 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0402 om.add_lin_constraint('mylin', {i, j}, A, l, u, vs);
0403 lNS = lNS + 1; lN = lN + i+j;
0404 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0405 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0406 end
0407 end
0408
0409 t = 'om.add_lin_constraint(''onerow'', A, l, u)';
0410 A4 = sparse([1 1 1]', [1:3]', [-1 -2 -3]', 1, vN);
0411 om.add_lin_constraint('onerow', A4, 0, Inf);
0412 lNS = lNS + 1; lN = lN + 1;
0413 t_ok(om.getN('lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0414 t_ok(om.get('lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0415
0416
0417 t = 'add_nln_constraint (equality)';
0418 neN = 0;
0419 neNS = 0;
0420 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N = %d', t, neN));
0421 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0422
0423 t = 'om.add_nln_constraint(''Pmise'', N, 1, fcn, hess, {''Pg'', ''Va''})';
0424 N = 4;
0425 fcn = @(x)my_fcn(x, N, 2);
0426 hess = @(x, lam)my_hess(x, lam, 10);
0427 om.add_nln_constraint('Pmise', N, 1, fcn, hess, {'Pg', 'Va'});
0428 neNS = neNS + 1; neN = neN + N;
0429 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N = %d', t, neN));
0430 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0431
0432 t = 'om.add_nln_constraint(''Qmise'', N, 1, fcn, hess)';
0433 N = 3;
0434 fcn = @(x)my_fcn(x, N, 2);
0435 hess = @(x, lam)my_hess(x, lam, 10);
0436 om.add_nln_constraint('Qmise', N, 1, fcn, hess);
0437 neNS = neNS + 1; neN = neN + N;
0438 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N = %d', t, neN));
0439 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0440
0441 t = 'om.add_nln_constraint({''P'',''Q'',''R''}, [3;2;1], 1, fcn, hess, {''Pg'', ''Va''})';
0442 N = [3;2;1];
0443 fcn = @(x)my_fcn(x, sum(N), 2);
0444 hess = @(x, lam)my_hess(x, lam, 10);
0445 om.add_nln_constraint({'P', 'Q', 'R'}, N, 1, fcn, hess, {'Pg', 'Va'});
0446 neNS = neNS + length(N); neN = neN + sum(N);
0447 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N = %d', t, neN));
0448 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0449
0450 t = 'om.init_indexed_name(''nle'', ''mynle'', {2, 2})';
0451 om.init_indexed_name('nle', 'mynle', {2, 2});
0452 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N = %d', t, neN));
0453 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0454
0455 for i = 1:2
0456 for j = 1:2
0457 t = sprintf('om.add_nln_constraint(''mynle'', {%d,%d}, N, 1, fcn, hess, vs)', i,j);
0458 N = i+j;
0459 fcn = @(x)my_fcn(x, N, i);
0460 hess = @(x, lam)my_hess(x, lam, j);
0461 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0462 om.add_nln_constraint('mynle', {i, j}, N, 1, fcn, hess, vs);
0463 neNS = neNS + 1; neN = neN + N;
0464 t_ok(om.getN('nle') == neN, sprintf('%s : nle.N = %d', t, neN));
0465 t_ok(om.get('nle', 'NS') == neNS, sprintf('%s : nle.NS = %d', t, neNS));
0466 end
0467 end
0468
0469
0470 t = 'add_nln_constraint (inequality)';
0471 niN = 0;
0472 niNS = 0;
0473 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N = %d', t, niN));
0474 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0475
0476 t = 'om.add_nln_constraint(''Pmisi'', N, 0, fcn, hess, {''Pg'', ''Va''})';
0477 N = 3;
0478 fcn = @(x)my_fcn(x, N, -2);
0479 hess = @(x, lam)my_hess(x, lam, -10);
0480 om.add_nln_constraint('Pmisi', N, 0, fcn, hess, {'Pg', 'Va'});
0481 niNS = niNS + 1; niN = niN + N;
0482 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N = %d', t, niN));
0483 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0484
0485 t = 'om.add_nln_constraint(''Qmisi'', N, 0, fcn, hess)';
0486 N = 2;
0487 fcn = @(x)my_fcn(x, N, -2);
0488 hess = @(x, lam)my_hess(x, lam, -10);
0489 om.add_nln_constraint('Qmisi', N, 0, fcn, hess);
0490 niNS = niNS + 1; niN = niN + N;
0491 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N = %d', t, niN));
0492 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0493
0494 t = 'om.init_indexed_name(''nli'', ''mynli'', {2, 2})';
0495 om.init_indexed_name('nli', 'mynli', {2, 2});
0496 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N = %d', t, niN));
0497 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0498
0499 for i = 1:2
0500 for j = 1:2
0501 t = sprintf('om.add_nln_constraint(''mynli'', {%d,%d}, N, 0, fcn, hess, vs)', i,j);
0502 N = i+j-1;
0503 fcn = @(x)my_fcn(x, N, i);
0504 hess = @(x, lam)my_hess(x, lam, j);
0505 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0506 om.add_nln_constraint('mynli', {i, j}, N, 0, fcn, hess, vs);
0507 niNS = niNS + 1; niN = niN + N;
0508 t_ok(om.getN('nli') == niN, sprintf('%s : nli.N = %d', t, niN));
0509 t_ok(om.get('nli', 'NS') == niNS, sprintf('%s : nli.NS = %d', t, niNS));
0510 end
0511 end
0512
0513
0514 t = 'get_idx : lin';
0515 [vv, ll] = om.get_idx();
0516 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0517 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0518 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)']);
0519
0520 t = 'get_idx(''lin'')';
0521 ll = om.get_idx('lin');
0522 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
0523 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
0524 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)']);
0525
0526
0527 t = 'om.params_lin_constraint(''Pmis'')';
0528 [A, l, u, vs] = om.params_lin_constraint('Pmis');
0529 t_is(A, A1, 14, [t, ' : A']);
0530 t_is(l, l1, 14, [t, ' : l']);
0531 t_is(u, u1, 14, [t, ' : u']);
0532 vs1 = struct('name', {'Va', 'Pg'}, 'idx', {{}, {}});
0533 t_ok(isequal(vs, vs1), [t, ' : vs']);
0534
0535 t = 'om.params_lin_constraint(''Qmis'')';
0536 [A, l, u, vs] = om.params_lin_constraint('Qmis');
0537 t_is(A, A2, 14, [t, ' : A']);
0538 t_is(l, l1, 14, [t, ' : l']);
0539 t_is(u, u1, 14, [t, ' : u']);
0540 t_ok(isequal(vs, {}), [t, ' : vs']);
0541
0542 for i = 1:2
0543 for j = 1:2
0544 t = sprintf('om.params_lin_constraint(''mylin'', {%d,%d})', i,j);
0545 A3 = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0546 [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0547 l3 = -ones(i+j, 1); u = [];
0548 vvs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0549 [A, l, u, vs] = om.params_lin_constraint('mylin', {i,j});
0550 t_is(A, A3, 14, [t, ' : A']);
0551 t_is(l, l3, 14, [t, ' : l']);
0552 t_ok(all(isinf(u)) & all(u > 0), [t, ' : u']);
0553 t_ok(isequal(vs, vvs), [t, ' : vs']);
0554 end
0555 end
0556
0557 t = 'om.params_lin_constraint(''onerow'')';
0558 [A, l, u, vs] = om.params_lin_constraint('onerow');
0559 t_is(A, A4, 14, [t, ' : A']);
0560 t_is(l, 0, 14, [t, ' : l']);
0561 t_ok(all(isinf(u)) & all(u > 0), [t, ' : u']);
0562 t_ok(isequal(vs, {}), [t, ' : vs']);
0563
0564 t = 'om.params_lin_constraint()';
0565 [A, l, u] = om.params_lin_constraint();
0566 t_ok(issparse(A), [t ' : issparse(A)']);
0567 t_is(size(A), [lN, vN], 14, [t ' : size(A)']);
0568 t_is(length(l), lN, 14, [t ' : length(l)']);
0569 t_is(length(u), lN, 14, [t ' : length(u)']);
0570 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);
0571 t_is(full(A(ll.i1.Qmis:ll.iN.Qmis, :)), full(AA), 14, [t ' : A(<Qmis>,:)']);
0572 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>)']);
0573 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)>)']);
0574
0575
0576 t = '[Ax_u, l_Ax, A] = om.eval_lin_constraint(x)';
0577 x = (1:om.var.N)';
0578 [Ax_u, l_Ax, AA] = om.eval_lin_constraint(x);
0579 t_is(Ax_u, A*x-u, 14, [t ' : Ax_u']);
0580 t_is(l_Ax, l-A*x, 14, [t ' : l_Ax']);
0581 t_is(AA, A, 14, [t ' : A']);
0582
0583 t = 'Ax_u = om.eval_lin_constraint(x, ''Pmis'')';
0584 vs = om.varsets_cell2struct({'Va', 'Pg'});
0585 xx = om.varsets_x(x, vs, 'vector');
0586 Ax_u = om.eval_lin_constraint(x, 'Pmis');
0587 t_is(Ax_u, A1*xx-u1, 14, [t ' : Ax_u']);
0588
0589 t = '[Ax_u, l_Ax] = om.eval_lin_constraint(x, ''Pmis'')';
0590 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'Pmis');
0591 t_is(Ax_u, A1*xx-u1, 14, [t ' : Ax_u']);
0592 t_is(l_Ax, l1-A1*xx, 14, [t ' : l_Ax']);
0593
0594 t = '[Ax_u, l_Ax, A] = om.eval_lin_constraint(x, ''Pmis'')';
0595 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'Pmis');
0596 t_is(Ax_u, A1*xx-u1, 14, [t ' : Ax_u']);
0597 t_is(l_Ax, l1-A1*xx, 14, [t ' : l_Ax']);
0598 t_is(A, A1, 14, [t ' : A']);
0599
0600 t = '[Ax_u, l_Ax, A] = om.eval_lin_constraint(x, ''Qmis'')';
0601 vs = om.varsets_cell2struct({'Va', 'Pg'});
0602 xx = om.varsets_x(x, vs, 'vector');
0603 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'Qmis');
0604 t_is(Ax_u, A2*x-u1, 14, [t ' : Ax_u']);
0605 t_is(l_Ax, l1-A2*x, 14, [t ' : l_Ax']);
0606 t_is(A, A2, 14, [t ' : A']);
0607
0608 for i = 1:2
0609 for j = 1:2
0610 t = sprintf('om.params_lin_constraint(''mylin'', {%d,%d})', i,j);
0611 A3 = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0612 [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0613 l3 = -ones(i+j, 1); u = [];
0614 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0615 xx = om.varsets_x(x, vs, 'vector');
0616 [Ax_u, l_Ax, A] = om.eval_lin_constraint(x, 'mylin', {i,j});
0617 t_ok(all(isinf(Ax_u)) & all(Ax_u < 0), [t ' : Ax_u']);
0618 t_is(l_Ax, l3-A3*xx, 14, [t ' : l_Ax']);
0619 t_is(A, A3, 14, [t ' : A']);
0620 end
0621 end
0622
0623
0624 t = 'om.params_nln_constraint(1, ''Pmise'')';
0625 N = om.params_nln_constraint(1, 'Pmise');
0626 t_is(N, 4, 14, [t, ' : N']);
0627 [N, fcn] = om.params_nln_constraint(1, 'Pmise');
0628 t_is(N, 4, 14, [t, ' : N']);
0629 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0630 [N, fcn, hess] = om.params_nln_constraint(1, 'Pmise');
0631 t_is(N, 4, 14, [t, ' : N']);
0632 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0633 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0634 [N, fcn, hess, vs] = om.params_nln_constraint(1, 'Pmise');
0635 t_is(N, 4, 14, [t, ' : N']);
0636 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0637 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0638 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0639 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0640 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0641 t_ok(strcmp(vs(2).name, 'Va'), [t, ' : vs(2).name']);
0642 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0643 t_ok(isempty(vs(2).idx), [t, ' : vs(2).idx']);
0644 [N, fcn, hess, vs, include] = om.params_nln_constraint(1, 'Pmise');
0645 t_ok(strcmp(include, ''), [t, ' : include']);
0646
0647 t = 'om.params_nln_constraint(1, ''P'')';
0648 [N, fcn, hess, vs, include] = om.params_nln_constraint(1, 'P');
0649 t_is(N, 3, 14, [t, ' : N']);
0650 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0651 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0652 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0653 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0654 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0655 t_ok(strcmp(vs(2).name, 'Va'), [t, ' : vs(2).name']);
0656 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0657 t_ok(isempty(vs(2).idx), [t, ' : vs(2).idx']);
0658 t_ok(isstruct(include), [t, ' : istruct(include)']);
0659 t_is(length(include.name), 2, 14, [t, ' : length(include.name)']);
0660 t_is(length(include.N), 2, 14, [t, ' : length(include.N)']);
0661 t_ok(strcmp(include.name{1}, 'Q'), [t, ' : include.name{1}']);
0662 t_ok(strcmp(include.name{2}, 'R'), [t, ' : include.name{2}']);
0663 t_is(include.N, [2 1], 14, [t, ' : include.N']);
0664
0665 t = 'om.params_nln_constraint(1, ''mynle'') : error';
0666 try
0667 [N, fcn] = om.params_nln_constraint(1, 'mynle')
0668 t_ok(0, t);
0669 catch
0670 t_ok(strfind(lasterr, '@opt_model/params_nln_constraint: nonlinear constraint set ''mynle'' requires an IDX_LIST arg'), t);
0671 end
0672
0673 t = 'om.params_nln_constraint(0, ''mynli'', {1,2})';
0674 [N, fcn, hess, vs] = om.params_nln_constraint(0, 'mynli', {1,2});
0675 t_is(N, 2, 14, [t, ' : N']);
0676 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0677 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0678 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0679 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0680 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0681 t_ok(strcmp(vs(2).name, 'x'), [t, ' : vs(2).name']);
0682 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0683 t_is(length(vs(2).idx), 2, 14, [t, ' : length(vs(2).idx)']);
0684 t_is(vs(2).idx{1}, 1, 14, [t, ' : vs(2).idx{1}']);
0685 t_is(vs(2).idx{2}, 2, 14, [t, ' : vs(2).idx{2}']);
0686
0687 t = 'om.params_nln_constraint(0, ''mynli'', {2,2})';
0688 [N, fcn, hess, vs] = om.params_nln_constraint(0, 'mynli', {2,2});
0689 t_is(N, 3, 14, [t, ' : N']);
0690 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
0691 t_ok(isa(hess, 'function_handle'), [t, ' : hess']);
0692 t_ok(isstruct(vs), [t, ' : isstruct(vs)']);
0693 t_is(length(vs), 2, 14, [t, ' : length(vs)']);
0694 t_ok(strcmp(vs(1).name, 'Pg'), [t, ' : vs(1).name']);
0695 t_ok(strcmp(vs(2).name, 'x'), [t, ' : vs(2).name']);
0696 t_ok(isempty(vs(1).idx), [t, ' : vs(1).idx']);
0697 t_is(length(vs(2).idx), 2, 14, [t, ' : length(vs(2).idx)']);
0698 t_is(vs(2).idx{1}, 2, 14, [t, ' : vs(2).idx{1}']);
0699 t_is(vs(2).idx{2}, 2, 14, [t, ' : vs(2).idx{2}']);
0700
0701
0702 t = 'g = om.eval_nln_constraint';
0703 x = (1:om.var.N)';
0704 [g, dg] = om.eval_nln_constraint(x, 1);
0705 t_is(length(g), neN, 14, [t ' : length(g)']);
0706 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]';
0707 t_is(g, eg, 14, [t ' : g']);
0708
0709 t = 'g = om.eval_nln_constraint(x, 1, ''Qmise'')';
0710 x = (1:om.var.N)';
0711 [g, dg] = om.eval_nln_constraint(x, 1, 'Qmise');
0712 t_is(length(g), 3, 14, [t ' : length(g)']);
0713 t_is(g, eg([5:7]), 14, [t ' : g']);
0714
0715 t = 'g = om.eval_nln_constraint(x, 1, ''mynle'', {1,2})';
0716 x = (1:om.var.N)';
0717 [g, dg] = om.eval_nln_constraint(x, 1, 'mynle', {1,2});
0718 t_is(length(g), 3, 14, [t ' : length(g)']);
0719 t_is(g, eg(16:18), 14, [t ' : g']);
0720
0721 t = '[g, dg] = om.eval_nln_constraint';
0722 x = (1:om.var.N)';
0723 [g, dg] = om.eval_nln_constraint(x, 1);
0724 t_is(length(g), neN, 14, [t ' : length(g)']);
0725 t_ok(issparse(dg), [t ' : issparse(dg)']);
0726 t_is(size(dg), [neN, vN], 14, [t ' : size(dg)']);
0727 t_is(g, eg, 14, [t ' : g']);
0728 ePmise = [[ 1 2 3 4 7 6 7;
0729 0 0 0 0 0 2 0;
0730 0 0 0 0 0 0 2;
0731 2 0 0 0 0 0 0 ] zeros(4, vN-7) ];
0732 t_is(full(dg(1:4, :)), ePmise, 14, [t ' : dg(1:4, :) [Pmise]']);
0733 eQmise = [[3 2:vN]; [0 2 0 zeros(1, vN-3)]; [0 0 2 zeros(1, vN-3)]];
0734 t_is(full(dg(5:7, :)), eQmise, 14, [t ' : dg(5:7, :) [Qmise]']);
0735 e = [[ 1 2 3 4 7 6 7;
0736 0 0 0 0 0 2 0;
0737 0 0 0 0 0 0 2;
0738 2 0 0 0 0 0 0;
0739 0 2 0 0 0 0 0;
0740 0 0 2 0 0 0 0 ] zeros(6, vN-7) ];
0741 t_is(full(dg(8:13, :)), e, 14, [t ' : dg(8:13, :) [mynle(1,1)]']);
0742 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)];
0743 t_is(full(dg(14:15, :)), e, 14, [t ' : dg(14:15, :) [mynle(1,1)]']);
0744 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)];
0745 t_is(full(dg(16:18, :)), emynle12, 14, [t ' : dg(16:18, :) [mynle(1,2)]']);
0746 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)];
0747 t_is(full(dg(19:21, :)), e, 14, [t ' : dg(19:21, :) [mynle(2,1)]']);
0748 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)];
0749 t_is(full(dg(22:25, :)), e, 14, [t ' : dg(22:25, :) [mynle(2,2)]']);
0750
0751 t = '[g, dg] = om.eval_nln_constraint(x, 1, ''Pmise'')';
0752 x = (1:om.var.N)';
0753 [g, dg] = om.eval_nln_constraint(x, 1, 'Pmise');
0754 t_is(length(g), 4, 14, [t ' : length(g)']);
0755 t_is(g, eg([1:4]), 14, [t ' : g']);
0756 t_is(full(dg), ePmise(:, [5:7 1:4]), 14, [t ' : dg']);
0757
0758 t = '[g, dg] = om.eval_nln_constraint(x, 1, ''Qmise'')';
0759 x = (1:om.var.N)';
0760 [g, dg] = om.eval_nln_constraint(x, 1, 'Qmise');
0761 t_is(length(g), 3, 14, [t ' : length(g)']);
0762 t_is(g, eg([5:7]), 14, [t ' : g']);
0763 t_is(full(dg), eQmise, 14, [t ' : dg']);
0764
0765 t = '[g, dg] = om.eval_nln_constraint(x, 1, ''mynle'', {1,2})';
0766 x = (1:om.var.N)';
0767 [g, dg] = om.eval_nln_constraint(x, 1, 'mynle', {1,2});
0768 t_is(length(g), 3, 14, [t ' : length(g)']);
0769 t_is(g, eg(16:18), 14, [t ' : g']);
0770 t_is(full(dg), emynle12(:, [5:7 20:21]), 14, [t ' : dg']);
0771
0772
0773
0774
0775
0776 t = 'h = om.eval_nln_constraint';
0777 h = om.eval_nln_constraint(x, 0);
0778 t_is(length(h), niN, 14, [t ' : length(h)']);
0779 t_is(h, [3 4 5 -1 0 6 6 7 7 8 7 8 9]', 14, [t ' : h']);
0780
0781 t = '[h, dh] = om.eval_nln_constraint';
0782 [h, dh] = om.eval_nln_constraint(x, 0);
0783 t_is(length(h), niN, 14, [t ' : length(h)']);
0784 t_ok(issparse(dh), [t ' : issparse(dh)']);
0785 t_is(size(dh), [niN, vN], 14, [t ' : size(dh)']);
0786 eh = [3 4 5 -1 0 6 6 7 7 8 7 8 9]';
0787 t_is(h, eh, 14, [t ' : h']);
0788 ePmisi = [[ 1 2 3 4 3 6 7;
0789 0 0 0 0 0 -2 0;
0790 0 0 0 0 0 0 -2 ] zeros(3, vN-7) ];
0791 t_is(full(dh(1:3, :)), ePmisi, 14, [t ' : dh(1:3, :) [Pmisi]']);
0792 eQmisi = [[-1 2:vN]; [0 -2 zeros(1, vN-2)]];
0793 t_is(full(dh(4:5, :)), eQmisi, 14, [t ' : dh(5:7, :) [Qmisi]']);
0794 e = [[0 0 0 0 6 6 7] zeros(1, 10) [18 19] zeros(1, vN-19)];
0795 t_is(full(dh(6, :)), e, 14, [t ' : dh(6, :) [mynli(1,1)]']);
0796 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)];
0797 t_is(full(dh(7:8, :)), e, 14, [t ' : dh(7:8, :) [mynli(1,2)]']);
0798 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)];
0799 t_is(full(dh(9:10, :)), e, 14, [t ' : dh(9:10, :) [mynli(2,1)]']);
0800 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)];
0801 t_is(full(dh(11:13, :)), emynli22, 14, [t ' : dh(11:13, :) [mynli(2,2)]']);
0802
0803 t = '[h, dh] = om.eval_nln_constraint(x, 0, ''Pmisi'')';
0804 x = (1:om.var.N)';
0805 [h, dh] = om.eval_nln_constraint(x, 0, 'Pmisi');
0806 t_is(length(h), 3, 14, [t ' : length(h)']);
0807 t_is(h, eh([1:3]), 14, [t ' : h']);
0808 t_is(full(dh), ePmisi(:, [5:7 1:4]), 14, [t ' : dh']);
0809
0810 t = '[h, dh] = om.eval_nln_constraint(x, 0, ''Qmisi'')';
0811 x = (1:om.var.N)';
0812 [h, dh] = om.eval_nln_constraint(x, 0, 'Qmisi');
0813 t_is(length(h), 2, 14, [t ' : length(h)']);
0814 t_is(h, eh([4:5]), 14, [t ' : h']);
0815 t_is(full(dh), eQmisi, 14, [t ' : dh']);
0816
0817 t = '[h, dh] = om.eval_nln_constraint(x, 0, ''mynli'', {2,2})';
0818 x = (1:om.var.N)';
0819 [h, dh] = om.eval_nln_constraint(x, 0, 'mynli', {2,2});
0820 t_is(length(h), 3, 14, [t ' : length(h)']);
0821 t_is(h, eh(11:13), 14, [t ' : h']);
0822 t_is(full(dh), emynli22(:, [5:7 25:26]), 14, [t ' : dh']);
0823
0824
0825
0826
0827 t = 'eval_nln_constraint_hess';
0828 lam = (1:neN)'/100;
0829 d2G = om.eval_nln_constraint_hess(x, lam, 1);
0830 t_ok(issparse(d2G), [t ' : issparse(d2G)']);
0831 t_is(size(d2G), [vN, vN], 14, [t ' : size(d2G)']);
0832
0833
0834 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);
0835 t_is(d2G, e, 13, [t ' : d2G']);
0836
0837
0838
0839 lam = -(1:niN)'/100;
0840 d2H = om.eval_nln_constraint_hess(x, lam, 0);
0841 t_ok(issparse(d2H), [t ' : issparse(d2H)']);
0842 t_is(size(d2H), [vN, vN], 14, [t ' : size(d2H)']);
0843
0844
0845 e = sparse([1:2 5:7], [1:2 5:7], [-9.04 -8.05 20.66 18.68 5.84], vN, vN);
0846 t_is(d2H, e, 13, [t ' : d2H']);
0847
0848
0849
0850
0851 t = 'add_quad_cost';
0852 qcN = 0;
0853 qcNS = 0;
0854 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0855 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0856
0857 t = 'om.add_quad_cost(''qc1'', <mat>Q, c, k, {''Pg'', ''Va''})';
0858 n = nVa + nPg;
0859 Q1 = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n:-1:1, 1:n, n, n);
0860 c1 = 10*(1:n)';
0861 k1 = n;
0862 om.add_quad_cost('qc1', Q1, c1, k1, {'Pg', 'Va'});
0863 qcNS = qcNS + 1; qcN = qcN + 1;
0864 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0865 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0866
0867 t = 'om.add_quad_cost(''qc2'', <mat>Q, c)';
0868 n = om.getN('var');
0869 Q2 = sparse(1, 1:n, 1:n, n, n) + sparse(1:n, 1, n:-1:1, n, n);
0870 c2 = 10*(n:-1:1)';
0871 om.add_quad_cost('qc2', Q2, c2);
0872 qcNS = qcNS + 1; qcN = qcN + 1;
0873 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0874 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0875
0876 t = 'om.add_quad_cost(''qc3'', <vec>Q, c, k, {''Vm2'', ''Pg''})';
0877 n = nVm2 + nPg;
0878 Q3 = 2*(1:n)';
0879 c3 = -1*(1:n)';
0880 k3 = -n;
0881 om.add_quad_cost('qc3', Q3, c3, k3, {'Vm2', 'Pg'});
0882 qcNS = qcNS + 1; qcN = qcN + n;
0883 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0884 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0885
0886 t = 'om.add_quad_cost(''qc4'', <vec>Q, [], 0, vs)';
0887 n = om.getN('var', 'x', {2,1}) + om.getN('var', 'y', {1,1,1});
0888 Q4 = 1./(1:n)';
0889 vs = struct('name', {'x', 'y'}, 'idx', {{2,1}, {1,1,1}});
0890 om.add_quad_cost('qc4', Q4, [], 0, vs);
0891 qcNS = qcNS + 1; qcN = qcN + n;
0892 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0893 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0894
0895 t = 'om.add_quad_cost(''qc5'', [], c, k, {''Pg'', ''Va''})';
0896 n = nVa + nPg;
0897 c5 = 100*(1:n)';
0898 k5 = (1:n)';
0899 om.add_quad_cost('qc5', [], c5, k5, {'Pg', 'Va'});
0900 qcNS = qcNS + 1; qcN = qcN + n;
0901 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0902 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0903
0904 t = 'om.add_quad_cost(''qc6'', [], c, <sclr>k)';
0905 n = om.getN('var');
0906 c6 = -(1:n)';
0907 k6 = 3;
0908 om.add_quad_cost('qc6', [], c6, k6);
0909 qcNS = qcNS + 1; qcN = qcN + n;
0910 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0911 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0912
0913 t = 'om.init_indexed_name(''qdc'', ''qc'', {2,2})';
0914 om.init_indexed_name('qdc', 'qc', {2,2});
0915 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0916 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0917
0918 for i = 1:2
0919 for j = 1:2
0920 t = 'om.add_quad_cost(''qc'', {i, j}, cp, vs)';
0921 n = nPg + om.getN('var', 'x', {i,j});
0922 QQ = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n*ones(n,1), 1:n, n, n);
0923 cc = -2*(1:n)';
0924 kk = 1000;
0925 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0926 om.add_quad_cost('qc', {i, j}, QQ, cc, kk, vs);
0927 qcNS = qcNS + 1; qcN = qcN + 1;
0928 t_ok(om.getN('qdc') == qcN, sprintf('%s : qdc.N = %d', t, qcN));
0929 t_ok(om.get('qdc', 'NS') == qcNS, sprintf('%s : qdc.NS = %d', t, qcNS));
0930 end
0931 end
0932
0933
0934 t = 'om.params_quad_cost(''qc1'')';
0935 [Q, c, k, vs] = om.params_quad_cost('qc1');
0936 t_is(Q, Q1, 14, [t, ' : Q']);
0937 t_is(c, c1, 14, [t, ' : c']);
0938 t_is(k, k1, 14, [t, ' : k']);
0939 vs1 = struct('name', {'Pg', 'Va'}, 'idx', {{}, {}});
0940 t_ok(isequal(vs, vs1), [t, ' : vs']);
0941
0942 t = 'om.params_quad_cost(''qc2'')';
0943 [Q, c, k, vs] = om.params_quad_cost('qc2');
0944 t_is(Q, Q2, 14, [t, ' : Q']);
0945 t_is(c, c2, 14, [t, ' : c']);
0946 t_is(k, 0, 14, [t, ' : k']);
0947 t_ok(isequal(vs, {}), [t, ' : vs']);
0948
0949 t = 'om.params_quad_cost(''qc3'')';
0950 [Q, c, k] = om.params_quad_cost('qc3');
0951 t_is(Q, Q3, 14, [t, ' : Q']);
0952 t_is(c, c3, 14, [t, ' : c']);
0953 t_is(k, k3, 14, [t, ' : k']);
0954
0955 t = 'om.params_quad_cost(''qc4'')';
0956 [Q, c] = om.params_quad_cost('qc4');
0957 t_is(Q, Q4, 14, [t, ' : Q']);
0958 t_ok(isempty(c), [t, ' : c']);
0959
0960
0961 t = 'om.params_quad_cost(''qc5'')';
0962 [Q, c, k] = om.params_quad_cost('qc5');
0963 t_ok(isempty(Q), [t, ' : Q']);
0964 t_is(c, c5, 14, [t, ' : c']);
0965 t_is(k, k5, 14, [t, ' : k']);
0966
0967 t = 'om.params_quad_cost(''qc6'')';
0968 [Q, c, k] = om.params_quad_cost('qc6');
0969 t_ok(isempty(Q), [t, ' : Q']);
0970 t_is(c, c6, 14, [t, ' : c']);
0971 t_is(k, k6, 14, [t, ' : k']);
0972
0973 for i = 1:2
0974 for j = 1:2
0975 t = sprintf('om.params_quad_cost(''qc'', {%d, %d})', i, j);
0976 n = nPg + om.getN('var', 'x', {i,j});
0977 QQ = sparse(1:n, 1:n, 1:n, n, n) + sparse(1:n, n*ones(n,1), 1:n, n, n);
0978 cc = -2*(1:n)';
0979 kk = 1000;
0980 [Q, c, k] = om.params_quad_cost('qc', {i,j});
0981 t_is(Q, QQ, 14, [t, ' : Q']);
0982 t_is(c, cc, 14, [t, ' : c']);
0983 t_is(k, kk, 14, [t, ' : k']);
0984 end
0985 end
0986
0987 t = 'om.params_quad_cost()';
0988 [Q, c, k] = om.params_quad_cost();
0989
0990 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]';
0991 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]';
0992 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];
0993 QQ = sparse(ii, jj, ss, om.var.N, om.var.N);
0994 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]';
0995 t_is(Q, QQ, 14, [t, ' : Q']);
0996 t_is(c, cc, 14, [t, ' : c']);
0997 t_is(k, k1+k3*length(Q3)+sum(k5)+k6*length(c6)+4000, 14, [t, ' : k']);
0998
0999
1000 t = 'om.eval_quad_cost(x, ''qc1'')';
1001 x = (1:om.var.N)';
1002 [Q, c, k, vs] = om.params_quad_cost('qc1');
1003 xx = om.varsets_x(x, vs, 'vector');
1004 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1005 edf = Q*xx + c;
1006 f = om.eval_quad_cost(x, 'qc1');
1007 t_is(f, ef, 14, [t, ' : f']);
1008 [f, df] = om.eval_quad_cost(x, 'qc1');
1009 t_is(f, ef, 14, [t, ' : f']);
1010 t_is(df, edf, 14, [t, ' : df']);
1011 [f, df, d2f] = om.eval_quad_cost(x, 'qc1');
1012 t_is(f, ef, 14, [t, ' : f']);
1013 t_is(df, edf, 14, [t, ' : df']);
1014 t_is(d2f, Q, 14, [t, ' : d2f']);
1015
1016 t = 'om.eval_quad_cost(x, ''qc2'')';
1017 [Q, c, k, vs] = om.params_quad_cost('qc2');
1018 xx = om.varsets_x(x, vs, 'vector');
1019 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1020 edf = Q*xx + c;
1021 [f, df, d2f] = om.eval_quad_cost(x, 'qc2');
1022 t_is(f, ef, 14, [t, ' : f']);
1023 t_is(df, edf, 14, [t, ' : df']);
1024 t_is(d2f, Q, 14, [t, ' : d2f']);
1025
1026 t = 'om.eval_quad_cost(x, ''qc3'')';
1027 [Q, c, k, vs] = om.params_quad_cost('qc3');
1028 xx = om.varsets_x(x, vs, 'vector');
1029 ef = 1/2 * Q.*xx.^2 + c.*xx + k;
1030 edf = Q.*xx + c;
1031 [f, df, d2f] = om.eval_quad_cost(x, 'qc3');
1032 t_is(f, ef, 14, [t, ' : f']);
1033 t_is(df, edf, 14, [t, ' : df']);
1034 t_is(d2f, Q, 14, [t, ' : d2f']);
1035
1036 t = 'om.eval_quad_cost(x, ''qc4'')';
1037 [Q, c, k, vs] = om.params_quad_cost('qc4');
1038 xx = om.varsets_x(x, vs, 'vector');
1039 ef = 1/2 * Q.*xx.^2 + k;
1040 edf = Q.*xx;
1041 [f, df, d2f] = om.eval_quad_cost(x, 'qc4');
1042 t_is(f, ef, 14, [t, ' : f']);
1043 t_is(df, edf, 14, [t, ' : df']);
1044 t_is(d2f, Q, 14, [t, ' : d2f']);
1045
1046 t = 'om.eval_quad_cost(x, ''qc5'')';
1047 [Q, c, k, vs] = om.params_quad_cost('qc5');
1048 xx = om.varsets_x(x, vs, 'vector');
1049 ef = c.*xx + k;
1050 edf = c;
1051 [f, df, d2f] = om.eval_quad_cost(x, 'qc5');
1052 t_is(f, ef, 14, [t, ' : f']);
1053 t_is(df, edf, 14, [t, ' : df']);
1054 t_is(d2f, sparse(length(xx), 1), 14, [t, ' : d2f']);
1055
1056 t = 'om.eval_quad_cost(x, ''qc6'')';
1057 [Q, c, k, vs] = om.params_quad_cost('qc6');
1058 xx = x;
1059 ef = c.*xx + k;
1060 edf = c;
1061 [f, df, d2f] = om.eval_quad_cost(x, 'qc6');
1062 t_is(f, ef, 14, [t, ' : f']);
1063 t_is(df, edf, 14, [t, ' : df']);
1064 t_is(d2f, sparse(length(x), 1), 14, [t, ' : d2f']);
1065
1066 for i = 1:2
1067 for j = 1:2
1068 t = sprintf('om.eval_quad_cost(x, ''qc'', {%d, %d})', i, j);
1069 [Q, c, k, vs] = om.params_quad_cost('qc', {i,j});
1070 xx = om.varsets_x(x, vs, 'vector');
1071 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1072 edf = Q*xx + c;
1073 [f, df, d2f] = om.eval_quad_cost(x, 'qc', {i,j});
1074 t_is(f, ef, 14, [t, ' : f']);
1075 t_is(df, edf, 14, [t, ' : df']);
1076 t_is(d2f, Q, 14, [t, ' : d2f']);
1077 end
1078 end
1079
1080 t = 'om.eval_quad_cost(x)';
1081 [Q, c, k] = om.params_quad_cost();
1082 xx = x;
1083 ef = 1/2 * xx'*Q*xx + c'*xx + k;
1084 edf = Q*xx + c;
1085 [f, df, d2f] = om.eval_quad_cost(x);
1086 t_is(f, ef, 14, [t, ' : f']);
1087 t_is(df, edf, 14, [t, ' : df']);
1088 t_is(d2f, Q, 14, [t, ' : d2f']);
1089
1090
1091 t = 'get_idx(''var'', ''lin'')';
1092 [ll, vv] = om.get_idx('lin', 'var');
1093 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 14, [t ' : Pg']);
1094 t_is(size(vv.i1.x), [2, 2], 14, [t ' : size(vv.i1.x)']);
1095 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)']);
1096 t_is(size(vv.i1.y), [2, 3, 4], 14, [t ' : size(vv.i1.y)']);
1097 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)']);
1098 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 14, [t ' : Qmis']);
1099 t_is(size(ll.i1.mylin), [2, 2], 14, [t ' : size(ll.i1.mylin)']);
1100 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)']);
1101
1102
1103 t = 'add_nln_cost';
1104 nlcN = 0;
1105 nlcNS = 0;
1106 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N = %d', t, nlcN));
1107 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1108
1109 t = 'om.add_nln_cost(''ucost'', 1, fcn, {''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 fcn = @(x)my_legacy_cost_fcn(x, cp, om, {'Va', 'Pg'});
1113 om.add_nln_cost('ucost', 1, fcn, {'Va', 'Pg'});
1114 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1115 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N = %d', t, nlcN));
1116 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1117
1118 t = 'om.add_nln_cost(''vcost'', cp)';
1119 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
1120 'Cw', [2;3]);
1121 fcn = @(x)my_legacy_cost_fcn(x, cp, om);
1122 om.add_nln_cost('vcost', 1, fcn);
1123 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1124 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N = %d', t, nlcN));
1125 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1126
1127 t = 'om.init_indexed_name(''nlc'', ''wc'', {2,2})';
1128 om.init_indexed_name('nlc', 'wc', {2,2});
1129 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N = %d', t, nlcN));
1130 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1131
1132 for i = 1:2
1133 for j = 1:2
1134 t = 'om.add_nln_cost(''wc'', {i, j}, cp, vs)';
1135 cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
1136 [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
1137 cp.Cw = (i+j:-1:1)';
1138 if i == 2
1139 cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
1140 end
1141 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
1142 fcn = @(x)my_legacy_cost_fcn(x, cp, om, vs);
1143 om.add_nln_cost('wc', {i, j}, 1, fcn, vs);
1144 nlcNS = nlcNS + 1; nlcN = nlcN + 1;
1145 t_ok(om.getN('nlc') == nlcN, sprintf('%s : nlc.N = %d', t, nlcN));
1146 t_ok(om.get('nlc', 'NS') == nlcNS, sprintf('%s : nlc.NS = %d', t, nlcNS));
1147 end
1148 end
1149
1150
1151 t = 'om.params_nln_cost(''ucost'')';
1152 [N, fcn] = om.params_nln_cost('ucost');
1153 t_is(N, 1, 14, [t, ' : N']);
1154 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1155
1156 t = 'om.params_nln_cost(''vcost'')';
1157 [N, fcn] = om.params_nln_cost('vcost');
1158 t_is(N, 1, 14, [t, ' : N']);
1159 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1160
1161 t = 'om.params_nln_cost(''wc'') : error';
1162 try
1163 [N, fcn] = om.params_nln_cost('wc')
1164 t_ok(0, t);
1165 catch
1166 t_ok(strfind(lasterr, '@opt_model/params_nln_cost: general nonlinear cost set ''wc'' requires an IDX_LIST arg'), t);
1167 end
1168
1169 t = 'om.params_nln_cost(''wc'', {1,2})';
1170 [N, fcn] = om.params_nln_cost('wc', {1,2});
1171 t_is(N, 1, 14, [t, ' : N']);
1172 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1173
1174 t = 'om.params_nln_cost(''wc'', {2,1})';
1175 [N, fcn] = om.params_nln_cost('wc', {2,1});
1176 t_is(N, 1, 14, [t, ' : N']);
1177 t_ok(isa(fcn, 'function_handle'), [t, ' : fcn']);
1178
1179
1180 t = 'om.eval_nln_cost(x) : ';
1181 x = [1:7 rand(1,10) 8:(vN-10)]';
1182 f = om.eval_nln_cost(x);
1183 ef = 343;
1184 ii = [1 2 3 4 5 6 7 19 21 23 25 26];
1185 jj = [1 1 1 1 1 1 1 1 1 1 1 1];
1186 ss = [4 6 -4 -6 1 -26 -29 -3 -6 34 -3 52];
1187 edf = full(sparse(ii,jj,ss,vN,1));
1188 ii = [5 23 26 6 23 26 7 23 26 5 6 7 23 25 26 5 6 7 25 26];
1189 jj = [5 5 5 6 6 6 7 7 7 23 23 23 23 25 25 26 26 26 26 26];
1190 ss = [2 -1 -1 4 -2 -2 6 -3 -3 -1 -2 -3 6 4 -4 -1 -2 -3 -4 10];
1191 ed2f = full(sparse(ii,jj,ss,vN,vN));
1192 t_is(f, ef, 14, [t 'f']);
1193 [f, df] = om.eval_nln_cost(x);
1194 t_is(f, ef, 14, [t 'f']);
1195 t_is(df, edf, 14, [t 'df']);
1196 [f, df, d2f] = om.eval_nln_cost(x);
1197 t_is(f, ef, 14, [t 'f']);
1198 t_is(df, edf, 14, [t 'df']);
1199 t_is(d2f, ed2f, 14, [t 'd2f']);
1200
1201 t = 'om.eval_nln_cost(''ucost'') : ';
1202 f = om.eval_nln_cost(x, 'ucost');
1203 ef = 52;
1204 edf = [2; 3; -2; -3; 4; 0; 6];
1205 t_is(f, ef, 14, [t 'f']);
1206 [f, df] = om.eval_nln_cost(x, 'ucost');
1207 t_is(f, ef, 14, [t 'f']);
1208 t_is(df, edf, 14, [t 'df']);
1209 [f, df, d2f] = om.eval_nln_cost(x, 'ucost');
1210 t_is(f, ef, 14, [t 'f']);
1211 t_is(df, edf, 14, [t 'df']);
1212 t_is(full(d2f), zeros(7,7), 14, [t 'd2f']);
1213
1214 t = 'om.eval_nln_cost(''wc'', {2,1}) : ';
1215 f = om.eval_nln_cost(x, 'wc', {2,1});
1216 ef = 91;
1217 edf = [-5; -12; -17; 0; 34; 0];
1218 ii = [1 5 2 5 3 5 1 2 3 5];
1219 jj = [1 1 2 2 3 3 5 5 5 5];
1220 ss = [1 -1 2 -2 3 -3 -1 -2 -3 6];
1221 ed2f = full(sparse(ii,jj,ss,6,6));
1222 t_is(f, ef, 14, [t 'f']);
1223 [f, df] = om.eval_nln_cost(x, 'wc', {2,1});
1224 t_is(f, ef, 14, [t 'f']);
1225 t_is(df, edf, 14, [t 'df']);
1226 [f, df, d2f] = om.eval_nln_cost(x, 'wc', {2,1});
1227 t_is(f, ef, 14, [t 'f']);
1228 t_is(df, edf, 14, [t 'df']);
1229 t_is(d2f, ed2f, 14, [t 'd2f']);
1230
1231 t = 'om.eval_nln_cost(''wc'') : ';
1232 f = om.eval_nln_cost(x, 'wc');
1233 t_is(f, 239, 14, [t 'f']);
1234
1235 t = 'om.set_params';
1236
1237 if have_feature('octave')
1238 warn_id = 'Octave:classdef-to-struct';
1239 else
1240 warn_id = 'MATLAB:structOnObject';
1241 end
1242 s1 = warning('query', warn_id);
1243 warning('off', warn_id);
1244
1245 if have_feature('isequaln')
1246 s = struct(om);
1247 t_ok(isequaln(struct(om), s), [t 'initial params']);
1248
1249 t = 'om.set_params(''var'' ...) : ';
1250 val = [1:3]';
1251 try
1252 om.set_params('var', 'Va', 'v0', val);
1253 t_ok(0, [t 'Va, v0 (wrong size)']);
1254 catch
1255 me = lasterr;
1256 TorF = strfind(me, 'parameter ''var'' ''Va'' ''v0'' should have length 4 (or 1)');
1257 t_ok(TorF, [t 'Va, v0 (wrong size)']);
1258 if ~TorF
1259 me
1260 end
1261 end
1262
1263 t = 'om.set_params(''var'', name, ...) : ';
1264 val = [1:4]';
1265 s.var.data.v0.Va = val;
1266 om.set_params('var', 'Va', 'v0', val);
1267 t_ok(isequaln(struct(om), s), [t 'Va, v0']);
1268
1269 val = {[3;2;1], [30;20;10]};
1270 s.var.data.vl.Pg = val{1};
1271 s.var.data.vu.Pg = val{2};
1272 om.set_params('var', 'Pg', {'vl', 'vu'}, val);
1273 t_ok(isequaln(struct(om), s), [t 'Pg, {vl,vu}']);
1274
1275 val = {2, [4;2], [2;1], [20;10]};
1276 try
1277 om.set_params('var', 'Pg', 'all', val);
1278 t_ok(0, [t 'Pg, all (wrong size)']);
1279 catch
1280 me = lasterr;
1281 TorF = strfind(me, 'dimension change for ''var'' ''Pg'' not allowed');
1282 t_ok(TorF, [t 'Pg, all (wrong size)']);
1283 if ~TorF
1284 me
1285 end
1286 end
1287
1288 val = {3, [6;4;2], [3;2;1], [30;20;10]};
1289 s.var.data.v0.Pg = val{2};
1290 s.var.data.vl.Pg = val{3};
1291 s.var.data.vu.Pg = val{4};
1292 om.set_params('var', 'Pg', 'all', val);
1293 t_ok(isequaln(struct(om), s), [t 'Pg, all']);
1294
1295 t = 'om.set_params(''var'', name, idx, ...) : ';
1296 val = 'C';
1297 s.var.data.vt.y{1,3,4} = val;
1298 om.set_params('var', 'y', {1,3,4}, 'vt', val);
1299 t_ok(isequaln(struct(om), s), [t 'y{1,3,4}, vt']);
1300
1301 val = {7};
1302 s.var.data.v0.y{1,2,4} = zeros(7,1);
1303 s.var.data.vl.y{1,2,4} = -Inf(7,1);
1304 s.var.data.vu.y{1,2,4} = Inf(7,1);
1305 s.var.data.vt.y{1,2,4} = 'C';
1306 om.set_params('var', 'y', {1,2,4}, 'all', val);
1307 t_ok(isequaln(struct(om), s), [t 'y{1,2,4}, all']);
1308
1309 t = 'om.set_params(''lin'', name, ...) : ';
1310 [m, n] = size(s.lin.data.A.Qmis);
1311 val = sparse(m+1, n);
1312 try
1313 om.set_params('lin', 'Qmis', 'A', val);
1314 t_ok(0, [t 'Qmis, A (wrong size)']);
1315 catch
1316 me = lasterr;
1317 TorF = strfind(me, 'dimension change for ''lin'' ''Qmis'' not allowed except for ''all''');
1318 t_ok(TorF, [t 'Qmis, A (wrong size)']);
1319 if ~TorF
1320 me
1321 end
1322 end
1323
1324 val = sparse(m, n);
1325 s.lin.data.A.Qmis = val;
1326 om.set_params('lin', 'Qmis', 'A', val);
1327 t_ok(isequaln(struct(om), s), [t 'Qmis, A']);
1328
1329 val = {'Pg', 'Va'};
1330 s.lin.data.vs.Pmis = om.varsets_cell2struct(val);
1331 om.set_params('lin', 'Pmis', 'vs', val);
1332 t_ok(isequaln(struct(om), s), [t 'Pmis, vs']);
1333
1334 [A, l, u, vs] = om.params_lin_constraint('Pmis');
1335 val = {A(2:3, :), l(2:3), u(2:3), vs};
1336 s.lin.data.A.Pmis = val{1};
1337 s.lin.data.l.Pmis = val{2};
1338 s.lin.data.u.Pmis = val{3};
1339 dN = -1;
1340 s.lin.idx.N.Pmis = s.lin.idx.N.Pmis + dN;
1341 s.lin.idx.iN.Pmis = s.lin.idx.iN.Pmis + dN;
1342 s.lin.idx.iN.Qmis = s.lin.idx.iN.Qmis + dN;
1343 s.lin.idx.iN.mylin = s.lin.idx.iN.mylin + dN;
1344 s.lin.idx.iN.onerow = s.lin.idx.iN.onerow + dN;
1345 s.lin.idx.i1.Qmis = s.lin.idx.i1.Qmis + dN;
1346 s.lin.idx.i1.mylin = s.lin.idx.i1.mylin + dN;
1347 s.lin.idx.i1.onerow = s.lin.idx.i1.onerow + dN;
1348 s.lin.N = s.lin.N + dN;
1349 om.set_params('lin', 'Pmis', 'all', val);
1350 t_ok(isequaln(struct(om), s), [t 'Pmis, all']);
1351
1352 t = 'om.set_params(''lin'', name, idx, ...) : ';
1353 val = {-Inf(3,1), ones(3,1)};
1354 s.lin.data.u.mylin{2,1} = val{1};
1355 s.lin.data.l.mylin{2,1} = val{2};
1356 om.set_params('lin', 'mylin', {2,1}, {'u', 'l'}, val);
1357 t_ok(isequaln(struct(om), s), [t 'mylin{2,1}, {u,l}']);
1358
1359 [A, l, u, vs] = om.params_lin_constraint('mylin', {2,2});
1360 val = {A(2:3, :), l(2:3), u(2:3)};
1361 try
1362 om.set_params('lin', 'mylin', {2,2}, 'all', val);
1363 t_ok(0, [t 'mylin{2,2}, all (wrong size)']);
1364 catch
1365 me = lasterr;
1366 TorF = strfind(me, 'for ''lin'' ''mylin(2,2)'' number of columns of ''A'' (5) must be consistent with ''vs'' (170)');
1367 t_ok(TorF, [t 'mylin{2,2}, all (wrong size)']);
1368 if ~TorF
1369 me
1370 end
1371 end
1372 val = {A(2:3, :), l(2:3), u(2:3), vs};
1373 s.lin.data.A.mylin{2,2} = val{1};
1374 s.lin.data.l.mylin{2,2} = val{2};
1375 s.lin.data.u.mylin{2,2} = val{3};
1376 dN = -2;
1377 s.lin.idx.N.mylin(2,2) = s.lin.idx.N.mylin(2,2) + dN;
1378 s.lin.idx.iN.mylin(2,2) = s.lin.idx.iN.mylin(2,2) + dN;
1379 s.lin.idx.iN.onerow = s.lin.idx.iN.onerow + dN;
1380 s.lin.idx.i1.onerow = s.lin.idx.i1.onerow + dN;
1381 s.lin.N = s.lin.N + dN;
1382 om.set_params('lin', 'mylin', {2,2}, 'all', val);
1383 t_ok(isequaln(struct(om), s), [t 'mylin{2,2}, all']);
1384
1385 t = 'om.set_params(''nle'', name, ...) : ';
1386 [N, fcn, hess, vs, include] = om.params_nln_constraint(1, 'Pmise');
1387 val = N + 1;
1388 try
1389 om.set_params('nle', 'Qmise', 'N', val);
1390 t_ok(0, [t 'Qmise, N (wrong size)']);
1391 catch
1392 me = lasterr;
1393 TorF = strfind(me, 'dimension change for ''nle'' ''Qmise'' not allowed except for ''all''');
1394 t_ok(TorF, [t 'Qmise, N (wrong size)']);
1395 if ~TorF
1396 me
1397 end
1398 end
1399
1400 val = @(x)my_fcn(x, 4, 1);
1401 s.nle.data.fcn.Qmise = val;
1402 om.set_params('nle', 'Qmise', 'fcn', val);
1403 t_ok(isequaln(struct(om), s), [t 'Qmise, fcn']);
1404
1405 val = {'Va', 'Pg'};
1406 s.nle.data.vs.Pmise = om.varsets_cell2struct(val);
1407 om.set_params('nle', 'Pmise', 'vs', val);
1408 t_ok(isequaln(struct(om), s), [t 'Pmise, vs']);
1409
1410 [N, fcn, hess, vs, include] = om.params_nln_constraint(1, 'Pmise');
1411 fcn = @(x)my_fcn(x, N-1, 2);
1412 hess = @(x, lam)my_hess(x, lam, 12);
1413 val = {N-1, fcn, hess, vs};
1414 s.nle.data.fcn.Pmise = val{2};
1415 s.nle.data.hess.Pmise = val{3};
1416 dN = -1;
1417 s.nle.idx.N.Pmise = s.nle.idx.N.Pmise + dN;
1418 s.nle.idx.iN.Pmise = s.nle.idx.iN.Pmise + dN;
1419 s.nle.idx.iN.Qmise = s.nle.idx.iN.Qmise + dN;
1420 s.nle.idx.iN.P = s.nle.idx.iN.P + dN;
1421 s.nle.idx.iN.Q = s.nle.idx.iN.Q + dN;
1422 s.nle.idx.iN.R = s.nle.idx.iN.R + dN;
1423 s.nle.idx.iN.mynle = s.nle.idx.iN.mynle + dN;
1424 s.nle.idx.i1.Qmise = s.nle.idx.i1.Qmise + dN;
1425 s.nle.idx.i1.P = s.nle.idx.i1.P + dN;
1426 s.nle.idx.i1.Q = s.nle.idx.i1.Q + dN;
1427 s.nle.idx.i1.R = s.nle.idx.i1.R + dN;
1428 s.nle.idx.i1.mynle = s.nle.idx.i1.mynle + dN;
1429 s.nle.N = s.nle.N + dN;
1430 om.set_params('nle', 'Pmise', 'all', val);
1431 t_ok(isequaln(struct(om), s), [t 'Pmise, all']);
1432
1433 t = 'om.set_params(''nle'', name, idx, ...) : ';
1434 fcn = @(x)my_fcn(x, N-1, -7);
1435 hess = @(x, lam)my_hess(x, lam, 11);
1436 val = {hess, fcn};
1437 s.nle.data.hess.mynle{2,1} = val{1};
1438 s.nle.data.fcn.mynle{2,1} = val{2};
1439 om.set_params('nle', 'mynle', {2,1}, {'hess', 'fcn'}, val);
1440 t_ok(isequaln(struct(om), s), [t 'mynle{2,1}, {hess,fcn}']);
1441
1442 [N, fcn, hess, vs] = om.params_nln_constraint(1, 'mynle', {2,2});
1443 fcn = @(x)my_fcn(x, N-2, -7);
1444 hess = @(x, lam)my_hess(x, lam, 6);
1445 val = {N-2, fcn, hess, vs};
1446 s.nle.data.fcn.mynle{2,2} = val{2};
1447 s.nle.data.hess.mynle{2,2} = val{3};
1448 s.nle.data.vs.mynle{2,2} = val{4};
1449 dN = -2;
1450 s.nle.idx.N.mynle(2,2) = s.nle.idx.N.mynle(2,2) + dN;
1451 s.nle.idx.iN.mynle(2,2) = s.nle.idx.iN.mynle(2,2) + dN;
1452 s.nle.N = s.nle.N + dN;
1453 om.set_params('nle', 'mynle', {2,2}, 'all', val);
1454 t_ok(isequaln(struct(om), s), [t 'mynle{2,2}, all']);
1455
1456 t = 'om.set_params(''nli'', name, ...) : ';
1457 [N, fcn, hess, vs, include] = om.params_nln_constraint(0, 'Pmisi');
1458 val = N + 1;
1459 try
1460 om.set_params('nli', 'Qmisi', 'N', val);
1461 t_ok(0, [t 'Qmisi, N (wrong size)']);
1462 catch
1463 me = lasterr;
1464 TorF = strfind(me, 'dimension change for ''nli'' ''Qmisi'' not allowed except for ''all''');
1465 t_ok(TorF, [t 'Qmisi, N (wrong size)']);
1466 if ~TorF
1467 me
1468 end
1469 end
1470
1471 val = @(x)my_fcn(x, 3, 1);
1472 s.nli.data.fcn.Qmisi = val;
1473 om.set_params('nli', 'Qmisi', 'fcn', val);
1474 t_ok(isequaln(struct(om), s), [t 'Qmisi, fcn']);
1475
1476 val = {'Va', 'Pg'};
1477 s.nli.data.vs.Pmisi = om.varsets_cell2struct(val);
1478 om.set_params('nli', 'Pmisi', 'vs', val);
1479 t_ok(isequaln(struct(om), s), [t 'Pmisi, vs']);
1480
1481 [N, fcn, hess, vs, include] = om.params_nln_constraint(0, 'Pmisi');
1482 fcn = @(x)my_fcn(x, N+1, 8);
1483 hess = @(x, lam)my_hess(x, lam, 6);
1484 val = {N+1, fcn, hess, vs};
1485 s.nli.data.fcn.Pmisi = val{2};
1486 s.nli.data.hess.Pmisi = val{3};
1487 dN = 1;
1488 s.nli.idx.N.Pmisi = s.nli.idx.N.Pmisi + dN;
1489 s.nli.idx.iN.Pmisi = s.nli.idx.iN.Pmisi + dN;
1490 s.nli.idx.iN.Qmisi = s.nli.idx.iN.Qmisi + dN;
1491 s.nli.idx.iN.mynli = s.nli.idx.iN.mynli + dN;
1492 s.nli.idx.i1.Qmisi = s.nli.idx.i1.Qmisi + dN;
1493 s.nli.idx.i1.mynli = s.nli.idx.i1.mynli + dN;
1494 s.nli.N = s.nli.N + dN;
1495 om.set_params('nli', 'Pmisi', 'all', val);
1496 t_ok(isequaln(struct(om), s), [t 'Pmisi, all']);
1497
1498 t = 'om.set_params(''nli'', name, idx, ...) : ';
1499 fcn = @(x)my_fcn(x, N+1, -7);
1500 hess = @(x, lam)my_hess(x, lam, 11);
1501 val = {hess, fcn};
1502 s.nli.data.hess.mynli{2,1} = val{1};
1503 s.nli.data.fcn.mynli{2,1} = val{2};
1504 om.set_params('nli', 'mynli', {2,1}, {'hess', 'fcn'}, val);
1505 t_ok(isequaln(struct(om), s), [t 'mynli{2,1}, {hess,fcn}']);
1506
1507 [N, fcn, hess, vs] = om.params_nln_constraint(0, 'mynli', {2,2});
1508 fcn = @(x)my_fcn(x, N+2, -7);
1509 hess = @(x, lam)my_hess(x, lam, 6);
1510 val = {N+2, fcn, hess, vs};
1511 s.nli.data.fcn.mynli{2,2} = val{2};
1512 s.nli.data.hess.mynli{2,2} = val{3};
1513 s.nli.data.vs.mynli{2,2} = val{4};
1514 dN = 2;
1515 s.nli.idx.N.mynli(2,2) = s.nli.idx.N.mynli(2,2) + dN;
1516 s.nli.idx.iN.mynli(2,2) = s.nli.idx.iN.mynli(2,2) + dN;
1517 s.nli.N = s.nli.N + dN;
1518 om.set_params('nli', 'mynli', {2,2}, 'all', val);
1519 t_ok(isequaln(struct(om), s), [t 'mynli{2,2}, all']);
1520
1521 t = 'om.set_params(''qdc'', name, ...) : ';
1522 [m, n] = size(s.qdc.data.Q.qc1);
1523 val = sparse(m+1, n+1);
1524 try
1525 om.set_params('qdc', 'qc1', 'Q', val);
1526 t_ok(0, [t 'qc1, Q (wrong size)']);
1527 catch
1528 me = lasterr;
1529 TorF = strfind(me, 'dimension change for ''qdc'' ''qc1'' not allowed except for ''all''');
1530 t_ok(TorF, [t 'qc1, Q (wrong size)']);
1531 if ~TorF
1532 me
1533 end
1534 end
1535
1536 val = sparse(m, n);
1537 s.qdc.data.Q.qc1 = val;
1538 om.set_params('qdc', 'qc1', 'Q', val);
1539 t_ok(isequaln(struct(om), s), [t 'qc1, Q']);
1540
1541 val = {'Pg', 'Vm1'};
1542 s.qdc.data.vs.qc3 = om.varsets_cell2struct(val);
1543 om.set_params('qdc', 'qc3', 'vs', val);
1544 t_ok(isequaln(struct(om), s), [t 'qc3, vs']);
1545
1546 [Q, c, k, vs] = om.params_quad_cost('qc4');
1547 vs(2) = [];
1548 val = {Q(2:2:6, :), c, k, vs};
1549 s.qdc.data.Q.qc4 = val{1};
1550 s.qdc.data.vs.qc4 = val{4};
1551 dN = -3;
1552 s.qdc.idx.N.qc4 = s.qdc.idx.N.qc4 + dN;
1553 s.qdc.idx.iN.qc4 = s.qdc.idx.iN.qc4 + dN;
1554 s.qdc.idx.iN.qc5 = s.qdc.idx.iN.qc5 + dN;
1555 s.qdc.idx.iN.qc6 = s.qdc.idx.iN.qc6 + dN;
1556 s.qdc.idx.iN.qc = s.qdc.idx.iN.qc + dN;
1557 s.qdc.idx.i1.qc5 = s.qdc.idx.i1.qc5 + dN;
1558 s.qdc.idx.i1.qc6 = s.qdc.idx.i1.qc6 + dN;
1559 s.qdc.idx.i1.qc = s.qdc.idx.i1.qc + dN;
1560 s.qdc.N = s.qdc.N + dN;
1561 om.set_params('qdc', 'qc4', 'all', val);
1562 t_ok(isequaln(struct(om), s), [t 'qc4, all']);
1563
1564 t = 'om.set_params(''qdc'', name, idx, ...) : ';
1565 val = {[-12:2:-2]', 2000};
1566 s.qdc.data.c.qc{2,1} = val{1};
1567 s.qdc.data.k.qc{2,1} = val{2};
1568 om.set_params('qdc', 'qc', {2,1}, {'c', 'k'}, val);
1569 t_ok(isequaln(struct(om), s), [t 'qc{2,1}, {u,l}']);
1570
1571 [Q, c, k, vs] = om.params_quad_cost('qc', {2,2});
1572 val = {Q(1:3, 1:3), c(1:3), k};
1573 try
1574 om.set_params('qdc', 'qc', {2,2}, 'all', val);
1575 t_ok(0, [t 'qc{2,2}, all (wrong size)']);
1576 catch
1577 me = lasterr;
1578 TorF = strfind(me, 'for ''qdc'' ''qc(2,2)'' dimensions of ''Q'', ''c'', ''k'' (3) must be consistent with ''vs'' (170)');
1579 t_ok(TorF, [t 'qc{2,2}, all (wrong size)']);
1580 if ~TorF
1581 me
1582 end
1583 end
1584 vs(2) = [];
1585 val = {Q(1:3, 1:3), c(1:3), k, vs};
1586 s.qdc.data.Q.qc{2,2} = val{1};
1587 s.qdc.data.c.qc{2,2} = val{2};
1588 s.qdc.data.k.qc{2,2} = val{3};
1589 s.qdc.data.vs.qc{2,2} = val{4};
1590 om.set_params('qdc', 'qc', {2,2}, 'all', val);
1591 t_ok(isequaln(struct(om), s), [t 'qc{2,2}, all']);
1592
1593 t = 'om.set_params(''nlc'', name, ...) : ';
1594 val = @my_nln_cost_fcn;
1595 s.nlc.data.fcn.ucost = val;
1596 om.set_params('nlc', 'ucost', 'fcn', val);
1597 t_ok(isequaln(struct(om), s), [t 'ucost, fcn']);
1598
1599 [N, fcn, vs] = om.params_nln_cost('vcost');
1600 val = {N, @my_nln_cost_fcn, vs};
1601 s.nlc.data.fcn.vcost = val{2};
1602 om.set_params('nlc', 'vcost', 'all', val);
1603 t_ok(isequaln(struct(om), s), [t 'vcost, all']);
1604
1605 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
1606 'Cw', [2;3]);
1607 fcn = @(x)my_legacy_cost_fcn(x, cp, om, {'Va', 'Pg'});
1608 val = {fcn, 1};
1609 s.nlc.data.fcn.ucost = val{1};
1610 om.set_params('nlc', 'ucost', {'fcn', 'N'}, val);
1611 t_ok(isequaln(struct(om), s), [t 'ucost, {fcn, N}']);
1612
1613 t = 'om.set_params(''nlc'', name, idx, ...) : ';
1614 val = @my_nln_cost_fcn;
1615 s.nlc.data.fcn.wc{2,1} = val;
1616 om.set_params('nlc', 'wc', {2,1}, 'fcn', val);
1617 t_ok(isequaln(struct(om), s), [t 'wc{2,1}, fcn']);
1618
1619 [N, fcn, vs] = om.params_nln_cost('wc', {2,2});
1620 val = {2, @my_nln_cost_fcn, vs};
1621 try
1622 om.set_params('nlc', 'wc', {2,2}, 'all', val);
1623 t_ok(0, [t 'wc{2,2}, all (vector cost)']);
1624 catch
1625 me = lasterr;
1626 TorF = strfind(me, 'vector value for ''nlc'' ''wc(2,2)'' not yet implemented');
1627 t_ok(TorF, [t 'wc{2,2}, all (vector cost)']);
1628 if ~TorF
1629 me
1630 end
1631 end
1632 val = {N, @my_nln_cost_fcn, vs};
1633 s.nlc.data.fcn.wc{2,2} = val{2};
1634 om.set_params('nlc', 'wc', {2,2}, 'all', val);
1635 t_ok(isequaln(struct(om), s), [t 'wc{2,2}, all']);
1636 else
1637 t_skip(40, 'om.set_params tests require ''isequaln()''');
1638 end
1639
1640
1641 warning(s1.state, warn_id);
1642
1643
1644
1645 t = 'copy constructor';
1646 if have_feature('octave') && have_feature('octave', 'vnum') < 5.003
1647 t_skip(1, [t ' - https://savannah.gnu.org/bugs/?52614']);
1648 else
1649 om1 = opt_model(om);
1650 om1.add_var('test', 10);
1651 t_is(om1.var.N, om.var.N+10, 12, t);
1652 end
1653
1654 t = 'copy';
1655 om2 = om.copy();
1656 om2.add_var('test', 10);
1657 t_is(om2.var.N, om.var.N+10, 12, t);
1658
1659
1660
1661
1662 t_end
1663
1664 function [g, dg] = my_fcn(x, p1, p2)
1665 if iscell(x)
1666 xx = [];
1667 for k = 1:length(x)
1668 xx = [xx; x{k}];
1669 end
1670 else
1671 xx = x;
1672 end
1673 M = p1;
1674 N = length(xx);
1675 if M > N
1676 error('M <= length(x)');
1677 end
1678 g = xx(1:M) + p2;
1679 dg = sparse(1:M, 1:M, p2, M, N) + sparse(1, 1:N, xx, M, N);
1680
1681 function d2G = my_hess(x, lam, p3)
1682 if iscell(x)
1683 xx = [];
1684 for k = 1:length(x)
1685 xx = [xx; x{k}];
1686 end
1687 else
1688 xx = x;
1689 end
1690 N = length(xx);
1691 M = length(lam);
1692 MM = min(M, N);
1693 d2G = sparse(1:MM, 1:MM, xx(1:MM) + lam(1:MM) + p3, N, N);
1694
1695
1696 function [f, df, d2f] = my_legacy_cost_fcn(x, cp, om, vs)
1697 [nw, nx] = size(cp.N);
1698 if ~isfield(cp, 'H') || isempty(cp.H)
1699 cp.H = sparse(nw, nw);
1700 end
1701 if ~isfield(cp, 'dd') || isempty(cp.dd)
1702 cp.dd = ones(nw, 1);
1703 end
1704 if ~isfield(cp, 'rh') || isempty(cp.rh)
1705 cp.rh = zeros(nw, 1);
1706 end
1707 if ~isfield(cp, 'kk') || isempty(cp.kk)
1708 cp.kk = zeros(nw, 1);
1709 end
1710 if ~isfield(cp, 'mm') || isempty(cp.mm)
1711 cp.mm = ones(nw, 1);
1712 end
1713 if iscell(x)
1714 x = vertcat(x{:});
1715 end
1716
1717
1718 [N, Cw, H, dd, rh, kk, mm] = ...
1719 deal(cp.N, cp.Cw, cp.H, cp.dd, cp.rh, cp.kk, cp.mm);
1720 nx = length(x);
1721
1722 if isempty(N)
1723 f = 0;
1724 df = zeros(nx, 1);
1725 d2f = sparse(nx, nx);
1726 else
1727 nw = size(N, 1);
1728 r = N * x - rh;
1729 iLT = find(r < -kk);
1730 iEQ = find(r == 0 & kk == 0);
1731 iGT = find(r > kk);
1732 iND = [iLT; iEQ; iGT];
1733 iL = find(dd == 1);
1734 iQ = find(dd == 2);
1735 LL = sparse(iL, iL, 1, nw, nw);
1736 QQ = sparse(iQ, iQ, 1, nw, nw);
1737 kbar = sparse(iND, iND, [ ones(length(iLT), 1);
1738 zeros(length(iEQ), 1);
1739 -ones(length(iGT), 1)], nw, nw) * kk;
1740 rr = r + kbar;
1741 M = sparse(iND, iND, mm(iND), nw, nw);
1742 diagrr = sparse(1:nw, 1:nw, rr, nw, nw);
1743
1744
1745 w = M * (LL + QQ * diagrr) * rr;
1746
1747 f = (w' * H * w) / 2 + Cw' * w;
1748
1749
1750 if nargout > 1
1751 HwC = H * w + Cw;
1752 AA = N' * M * (LL + 2 * QQ * diagrr);
1753 df = AA * HwC;
1754
1755
1756 if nargout > 2
1757 d2f = AA * H * AA' + 2 * N' * M * QQ * sparse(1:nw, 1:nw, HwC, nw, nw) * N;
1758 end
1759 end
1760 end
1761
1762 function [f, df, d2f] = my_nln_cost_fcn(x)
1763 nx = length(x);
1764 f = sum(2*x.^3 + x.^2 - 4*x -12);
1765 df = 6*x.^2 + 2*x + 4;
1766 d2f = 12*spdiags(x+2, 0, nx, nx);