0001 function t_opf_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 = 305;
0017
0018 t_begin(num_tests, quiet);
0019
0020
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
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
0037 t = 'add_vars';
0038 vN = 0;
0039 vNS = 0;
0040 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0041 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0042
0043 t = 'add_vars(om, ''Va'', 4)';
0044 om = add_vars(om, 'Va', 4);
0045 vNS = vNS + 1; vN = vN + 4;
0046 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0047 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0048
0049 t = 'add_vars(om, ''Pg'', 3, Pg0, Pgmin, Pgmax)';
0050 om = add_vars(om, 'Pg', 3, [2;4;6], [1;2;3], [10;20;30]);
0051 vNS = vNS + 1; vN = vN + 3;
0052 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0053 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0054
0055 t = 'add_vars(om, ''Vm1'', 5, V0, Vmin, Vmax, ''I'')';
0056 V0 = [1;1;1;1;1];
0057 Vmin = zeros(5, 1);
0058 Vmax = 1 + 0.01*(1:5)';
0059 vt = 'I';
0060 om = add_vars(om, 'Vm1', 5, V0, Vmin, Vmax, vt);
0061 vNS = vNS + 1; vN = vN + 5;
0062 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0063 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0064
0065 t = 'add_vars(om, ''Vm2'', 5, V0, Vmin, Vmax, ''CIBIC'')';
0066 vt = 'CIBIC';
0067 om = add_vars(om, 'Vm2', 5, V0, Vmin, Vmax, vt);
0068 vNS = vNS + 1; vN = vN + 5;
0069 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0070 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0071
0072 t = 'add_vars(om, ''x'', dims)';
0073 om = add_vars(om, 'x', {2,2});
0074 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0075 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0076
0077 t = 'add_vars(om, ''x'', {1,1}, 2)';
0078 om = add_vars(om, 'x', {1,1}, 2);
0079 vNS = vNS + 1; vN = vN + 2;
0080 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0081 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0082
0083 t = 'add_vars(om, ''x'', {1,2}, 2, x0(1,2))';
0084 om = add_vars(om, 'x', {1,2}, 2, [-1;-2]);
0085 vNS = vNS + 1; vN = vN + 2;
0086 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0087 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0088
0089 t = 'add_vars(om, ''x'', {2,1}, 3)';
0090 om = add_vars(om, 'x', {2,1}, 3);
0091 vNS = vNS + 1; vN = vN + 3;
0092 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0093 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0094
0095 t = 'add_vars(om, ''x'', {2,2}, 2, x0(2,2), xmin(2,2), xmax(2,2))';
0096 om = add_vars(om, 'x', {2,2}, 2, [1;0],[0;-1],[2;1]);
0097 vNS = vNS + 1; vN = vN + 2;
0098 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0099 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0100
0101 t = 'add_vars(om, ''y'', {2,3,4})';
0102 om = add_vars(om, 'y', {2,3,4});
0103 vt0 = {'C', 'I', 'B'};
0104 for i = 1:2
0105 for j = 1:3
0106 for k = 1:4
0107 n = i+j+k;
0108 if i == 1
0109 vt = vt0{j};
0110 else
0111 vt = char(vt0{j} * ones(1, n));
0112 vt(j+1) = vt0{1+rem(j,3)};
0113 end
0114
0115 t = sprintf('add_vars(om, ''y'', {%d,%d,%d}, y0, ymin, ymax, vt)', i,j,k);
0116 om = add_vars(om, 'y', {i,j,k}, n, 10*(n:-1:1)', -1*(n:-1:1)', 100+(n:-1:1)', vt);
0117 vNS = vNS + 1; vN = vN + n;
0118 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N = %d', t, vN));
0119 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0120 end
0121 end
0122 end
0123
0124
0125 t = 'getN(om, ''var'', ''Pg'') == 3';
0126 t_ok(getN(om, 'var', 'Pg') == 3, t);
0127
0128 t = 'size(getN(om, ''var'', ''x'')) == [2 2]';
0129 t_is(size(getN(om, 'var', 'x')), [2,2], 1e-14, t);
0130
0131 t = 'getN(om, ''var'', ''x'')(1,2) == 2';
0132 N = getN(om, 'var', 'x');
0133 t_is(N(1,2), 2, 1e-14, t);
0134
0135 t = 'getN(om, ''var'', ''x'', {2,1}) == 3';
0136 t_is(getN(om, 'var', 'x', {2,1}), 3, 1e-14, t);
0137
0138 t = 'getN(om, ''var'', ''y'', {2,1,3}) == 6';
0139 t_is(getN(om, 'var', 'y', {2,1,3}), 6, 1e-14, t);
0140
0141 t = 'getN(om, ''var'')';
0142 t_is(getN(om, 'var'), vN, 1e-14, t);
0143
0144
0145 t = 'get_idx : var';
0146 vv = get_idx(om);
0147 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 1e-14, [t ' : Pg']);
0148 t_is(size(vv.i1.x), [2, 2], 1e-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], 1e-14, [t ' : x(2,1)']);
0150 t_is(size(vv.i1.y), [2, 3, 4], 1e-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], 1e-14, [t ' : y(2,2,4)']);
0152
0153
0154 t = 'getv(om, ''Va'')';
0155 [v0, vl, vu] = getv(om, '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 = 'getv(om, ''Pg'')';
0161 [v0, vl, vu] = getv(om, 'Pg');
0162 t_is(v0, [2;4;6], 1e-14, [t ' : v0']);
0163 t_is(vl, [1;2;3], 1e-14, [t ' : vl']);
0164 t_is(vu, [10;20;30], 1e-14, [t ' : vu']);
0165
0166 t = 'getv(om, ''Vm1'')';
0167 [v0, vl, vu, vt] = getv(om, 'Vm1');
0168 t_is(double(vt), double('I'), 1e-14, [t ' : vt']);
0169
0170 t = 'getv(om, ''Vm2'')';
0171 [v0, vl, vu, vt] = getv(om, 'Vm2');
0172 t_is(double(vt), double('CIBIC'), 1e-14, [t ' : vt']);
0173
0174 t = 'getv(om, ''x'')';
0175 [v0, vl, vu, vt] = getv(om, 'x');
0176 t_is(size(v0), [2,2], 1e-14, [t ' : size(v0)']);
0177 t_is(v0{2,2}, [1;0], 1e-14, [t ' : v0{2,2}']);
0178 t_is(vl{2,2}, [0;-1], 1e-14, [t ' : vl{2,2}']);
0179 t_is(vu{2,2}, [2;1], 1e-14, [t ' : vu{2,2}']);
0180 t_is(double(vt{2,2}), double('C'), 1e-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('getv(om, ''y'', {%d,%d,%d})', i, j, k);
0193 [v0, vl, vu, gvt] = getv(om, 'y', {i,j,k});
0194 t_is(v0, 10*(n:-1:1)', 1e-14, [t ' : v0']);
0195 t_is(vl, -1*(n:-1:1)', 1e-14, [t ' : vl']);
0196 t_is(vu, 100+(n:-1:1)', 1e-14, [t ' : vu']);
0197 t_is(gvt, vt, 1e-14, [t ' : vt']);
0198 end
0199 end
0200 end
0201
0202 t = 'getv(om)';
0203 [v0, vl, vu, vt] = getv(om);
0204 t_ok(length(v0) == getN(om, 'var'), [t ' : length(v0)']);
0205 t_ok(length(vl) == getN(om, 'var'), [t ' : length(vl)']);
0206 t_ok(length(vu) == getN(om, 'var'), [t ' : length(vu)']);
0207 t_is(v0(vv.i1.x(2,2):vv.iN.x(2,2)), [1;0], 1e-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], 1e-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], 1e-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', 1e-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)', 1e-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)', 1e-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)', 1e-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', 1e-14, [t ' : vt(vv.i1.y(2,2,4):vv.iN.y(2,2,4))']);
0216 vt0 = 'CCCCCCCIIIIICIBICCCCCCCCCCCCCCCCCCCCCCCCCCCCIIIIIIIIIIIIIIIIIIIIIIBBBBBBBBBBBBBBBBBBBBBBBBBBCICCCICCCCICCCCCICCCCCIIBIIIIBIIIIIBIIIIIIBIIIIIBBBCBBBBBCBBBBBBCBBBBBBBCBBBBB';
0217 t_is(vt, vt0, 1e-14, [t ' : vt']);
0218
0219
0220 t = 'add_constraints';
0221 lN = 0;
0222 lNS = 0;
0223 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0224 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0225
0226 t = 'add_constraints(om, ''Pmis'', A, l, u, {''Va'', ''Pg''})';
0227 A = sparse([1:3 1:3 1:3]', [1:3 4:6 7 7 7]', [1 1 1 -1 -1 -1 2 3 4]', 3, 7);
0228 l = -(1:3)'; u = (1:3)';
0229 om = add_constraints(om, 'Pmis', A, l, u, {'Va', 'Pg'});
0230 lNS = lNS + 1; lN = lN + 3;
0231 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0232 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0233
0234 t = 'add_constraints(om, ''Qmis'', A, l, u)';
0235 A = sparse([1:3 1:3 1:3]', [1:3 4:6 7 7 7]', [1 1 1 -1 -1 -1 2 3 4]', 3, vN);
0236 om = add_constraints(om, 'Qmis', A, l, u);
0237 lNS = lNS + 1; lN = lN + 3;
0238 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0239 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0240
0241 t = 'add_constraints(om, ''mylin'', {2, 2})';
0242 om = add_constraints(om, 'mylin', {2, 2});
0243 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0244 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0245
0246 for i = 1:2
0247 for j = 1:2
0248 t = sprintf('add_constraints(om, ''mylin'', {%d,%d}, A, l, u, vs)', i,j);
0249 A = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0250 [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0251 l = -ones(i+j, 1); u = [];
0252 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0253 om = add_constraints(om, 'mylin', {i, j}, A, l, u, vs);
0254 lNS = lNS + 1; lN = lN + i+j;
0255 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N = %d', t, lN));
0256 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0257 end
0258 end
0259
0260
0261 t = 'get_idx : lin';
0262 [vv, ll] = get_idx(om);
0263 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 1e-14, [t ' : Qmis']);
0264 t_is(size(ll.i1.mylin), [2, 2], 1e-14, [t ' : size(ll.i1.mylin)']);
0265 t_is([ll.i1.mylin(2,1) ll.iN.mylin(2,1) ll.N.mylin(2,1)], [12 14 3], 1e-14, [t ' : mylin(2,1)']);
0266
0267
0268 t = 'linear_constraints';
0269 [A, l, u] = linear_constraints(om);
0270 t_ok(issparse(A), [t ' : issparse(A)']);
0271 t_is(size(A), [lN, vN], 1e-14, [t ' : size(A)']);
0272 t_is(length(l), lN, 1e-14, [t ' : length(l)']);
0273 t_is(length(u), lN, 1e-14, [t ' : length(u)']);
0274 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);
0275 t_is(full(A(ll.i1.Qmis:ll.iN.Qmis, :)), full(AA), 1e-14, [t ' : A(<Qmis>,:)']);
0276 t_is(full(A(ll.i1.mylin(2,1):ll.iN.mylin(2,1), vv.i1.Pg:vv.iN.Pg)), eye(3,3), 1e-14, [t ' : A(<mylin(2,1)>,<Pg>)']);
0277 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], 1e-14, [t ' : A(<mylin(2,1)>,<x(2,1)>)']);
0278
0279
0280 t = 'add_costs';
0281 cN = 0;
0282 cNS = 0;
0283 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N = %d', t, cN));
0284 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0285
0286 t = 'add_costs(om, ''ucost'', cp, {''Va'', ''Pg''})';
0287 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
0288 'Cw', [2;3]);
0289 om = add_costs(om, 'ucost', cp, {'Va', 'Pg'});
0290 cNS = cNS + 1; cN = cN + 2;
0291 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N = %d', t, cN));
0292 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0293
0294 t = 'add_costs(om, ''vcost'', cp)';
0295 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
0296 'Cw', [2;3]);
0297 om = add_costs(om, 'vcost', cp);
0298 cNS = cNS + 1; cN = cN + 2;
0299 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N = %d', t, cN));
0300 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0301
0302 t = 'add_costs(om, ''wc'', {2,2})';
0303 om = add_costs(om, 'wc', {2,2});
0304 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N = %d', t, cN));
0305 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0306
0307 for i = 1:2
0308 for j = 1:2
0309 t = 'add_costs(om, ''wc'', {i, j}, cp, vs)';
0310 cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0311 [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0312 cp.Cw = (i+j:-1:1)';
0313 if i == 2
0314 cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
0315 end
0316 vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0317 om = add_costs(om, 'wc', {i, j}, cp, vs);
0318 cNS = cNS + 1; cN = cN + i+j;
0319 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N = %d', t, cN));
0320 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0321 end
0322 end
0323
0324
0325 t = 'get_cost_params : error';
0326 try
0327 cp = get_cost_params(om);
0328 t_ok(0, t);
0329 catch
0330 t_ok(strfind(lasterr, '@opt_model/get_cost_params: must call build_cost_params first'), t);
0331 end
0332
0333 t = 'build_cost_params';
0334 om = build_cost_params(om);
0335 cp = get_cost_params(om);
0336 t_ok(isfield(cp, 'N'), t);
0337
0338
0339 t = 'get_idx : cost';
0340 [vv, ll, nn, cc] = get_idx(om);
0341 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 1e-14, [t ' : vcost']);
0342 t_is(size(cc.i1.wc), [2, 2], 1e-14, [t ' : size(cc.i1.wc)']);
0343 t_is([cc.i1.wc(2,1) cc.iN.wc(2,1) cc.N.wc(2,1)], [10 12 3], 1e-14, [t ' : wc(2,1)']);
0344
0345
0346 t = 'get_cost_params(om, ''ucost'')';
0347 cp = get_cost_params(om, 'ucost');
0348 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0349 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0350 t_is(cp.Cw, [2;3], 1e-14, [t, ' : Cw']);
0351 t_is(full(cp.H), zeros(2,2), 1e-14, [t, ' : H']);
0352 t_is(cp.dd, ones(2,1), 1e-14, [t, ' : dd']);
0353 t_is(cp.rh, zeros(2,1), 1e-14, [t, ' : rh']);
0354 t_is(cp.kk, zeros(2,1), 1e-14, [t, ' : kk']);
0355 t_is(cp.mm, ones(2,1), 1e-14, [t, ' : mm']);
0356
0357 t = 'get_cost_params(om, ''vcost'')';
0358 cp = get_cost_params(om, 'vcost');
0359 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0360 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0361 t_is(cp.Cw, [2;3], 1e-14, [t, ' : Cw']);
0362 t_is(full(cp.H), zeros(2,2), 1e-14, [t, ' : H']);
0363 t_is(cp.dd, ones(2,1), 1e-14, [t, ' : dd']);
0364 t_is(cp.rh, zeros(2,1), 1e-14, [t, ' : rh']);
0365 t_is(cp.kk, zeros(2,1), 1e-14, [t, ' : kk']);
0366 t_is(cp.mm, ones(2,1), 1e-14, [t, ' : mm']);
0367
0368 t = 'get_cost_params(om, ''wc'') : error';
0369 try
0370 cp = get_cost_params(om, 'wc')
0371 t_ok(0, t);
0372 catch
0373 t_ok(strfind(lasterr, '@opt_model/get_cost_params: cost set ''wc'' requires an idx arg'), t);
0374 end
0375
0376 t = 'get_cost_params(om, ''wc'', {1,2})';
0377 cp = get_cost_params(om, 'wc', {1,2});
0378 N = sparse([1:3 1:3]', [vv.i1.Pg-1+(1:3) vv.i1.x(1,2)+ones(1,3)]', [ones(3,1);-ones(3,1)], 3, vN);
0379 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0380 t_is(cp.Cw, [3;2;1], 1e-14, [t, ' : Cw']);
0381 t_is(full(cp.H), zeros(3,3), 1e-14, [t, ' : H']);
0382 t_is(cp.dd, ones(3,1), 1e-14, [t, ' : dd']);
0383 t_is(cp.rh, zeros(3,1), 1e-14, [t, ' : rh']);
0384 t_is(cp.kk, zeros(3,1), 1e-14, [t, ' : kk']);
0385 t_is(cp.mm, ones(3,1), 1e-14, [t, ' : mm']);
0386
0387 t = 'get_cost_params(om, ''wc'', {2,1})';
0388 cp = get_cost_params(om, 'wc', {2,1});
0389 N = sparse([1:3 1:3]', [vv.i1.Pg-1+(1:3) vv.i1.x(2,1)+ones(1,3)]', [ones(3,1);-ones(3,1)], 3, vN);
0390 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0391 t_is(cp.Cw, [3;2;1], 1e-14, [t, ' : Cw']);
0392 H = sparse(1:3, 1:3, 1:3, 3, 3);
0393 t_is(full(cp.H), full(H), 1e-14, [t, ' : H']);
0394 t_is(cp.dd, ones(3,1), 1e-14, [t, ' : dd']);
0395 t_is(cp.rh, zeros(3,1), 1e-14, [t, ' : rh']);
0396 t_is(cp.kk, zeros(3,1), 1e-14, [t, ' : kk']);
0397 t_is(cp.mm, ones(3,1), 1e-14, [t, ' : mm']);
0398
0399 t = 'get_cost_params(om)';
0400 cp = get_cost_params(om);
0401 t_ok(issparse(cp.N), [t ' : issparse(cp.N)']);
0402 t_is(size(cp.N), [cN, vN], 1e-14, [t ' : size(cp.N)']);
0403 t_is(size(cp.H), [cN, cN], 1e-14, [t ' : size(cp.H)']);
0404 t_is(length(cp.Cw), cN, 1e-14, [t ' : length(cp.Cw)']);
0405 t_is(length(cp.dd), cN, 1e-14, [t ' : length(cp.dd)']);
0406 t_is(length(cp.rh), cN, 1e-14, [t ' : length(cp.rh)']);
0407 t_is(length(cp.kk), cN, 1e-14, [t ' : length(cp.kk)']);
0408 t_is(length(cp.mm), cN, 1e-14, [t ' : length(cp.mm)']);
0409 N = sparse([1:2 1:2]', [1:4]', [1 1 -1 -1]', 2, 4);
0410 Cw = [2;3];
0411 H = zeros(2,2);
0412 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Va:vv.iN.Va)), full(N), 1e-14, [t ' : N(<vcost>,<Va>)']);
0413 N = sparse([1:2]', [1 3]', [2 2]', 2, 3);
0414 t_is(full(cp.N(cc.i1.vcost:cc.iN.vcost, vv.i1.Pg:vv.iN.Pg)), full(N), 1e-14, [t ' : N(<vcost>,<Pg>)']);
0415 t_is(full(cp.Cw(cc.i1.vcost:cc.iN.vcost)), Cw, 1e-14, [t ' : Cw(<vcost>)']);
0416 t_is(full(cp.H(cc.i1.vcost:cc.iN.vcost, cc.i1.vcost:cc.iN.vcost)), full(H), 1e-14, [t ' : H(<vcost>,<vcost>)']);
0417 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
0418 t_is(full(cp.N(cc.i1.wc(1,2):cc.iN.wc(1,2), vv.i1.Pg:vv.iN.Pg)), full(N), 1e-14, [t ' : N(<wc(1,2)>,<Pg>)']);
0419 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 2);
0420 Cw = [3;2;1];
0421 H = zeros(3,3);
0422 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), 1e-14, [t ' : N(<wc(1,2)>,<x(1,2)>)']);
0423 t_is(full(cp.Cw(cc.i1.wc(1,2):cc.iN.wc(1,2))), Cw, 1e-14, [t ' : Cw(<wc(1,2)>)']);
0424 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), 1e-14, [t ' : H(<wc(1,2)>,<wc(1,2)>)']);
0425
0426 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
0427 t_is(full(cp.N(cc.i1.wc(2,1):cc.iN.wc(2,1), vv.i1.Pg:vv.iN.Pg)), full(N), 1e-14, [t ' : N(<wc(2,1)>,<Pg>)']);
0428 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 3);
0429 Cw = [3;2;1];
0430 H = full(sparse((1:3)', (1:3)', (1:3)', 3, 3));
0431 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), 1e-14, [t ' : N(<wc(2,1)>,<x(2,1)>)']);
0432 t_is(full(cp.Cw(cc.i1.wc(2,1):cc.iN.wc(2,1))), Cw, 1e-14, [t ' : Cw(<wc(2,1)>)']);
0433 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), 1e-14, [t ' : H(<wc(2,1)>,<wc(2,1)>)']);
0434
0435 t_is(cp.dd, ones(cN,1), 1e-14, [t, ' : dd']);
0436 t_is(cp.rh, zeros(cN,1), 1e-14, [t, ' : rh']);
0437 t_is(cp.kk, zeros(cN,1), 1e-14, [t, ' : kk']);
0438 t_is(cp.mm, ones(cN,1), 1e-14, [t, ' : mm']);
0439
0440
0441
0442 t = 'compute_cost(om, x)';
0443 x = [1:7 rand(1,10) 8:(vN-10)]';
0444 f = compute_cost(om, x);
0445 t_is(f, 343, 1e-14, t);
0446
0447 t = 'compute_cost(om, ''ucost'')';
0448 f = compute_cost(om, x, 'ucost');
0449 t_is(f, 52, 1e-14, t);
0450
0451 t = 'compute_cost(om, ''wc'', {2,1})';
0452 f = compute_cost(om, x, 'wc', {2,1});
0453 t_is(f, 91, 1e-14, t);
0454
0455
0456
0457
0458 t_end;