Home > matpower5.1 > t > t_opf_model.m

t_opf_model

PURPOSE ^

T_OPF_MODEL Tests for OPF_MODEL.

SYNOPSIS ^

function t_opf_model(quiet)

DESCRIPTION ^

T_OPF_MODEL Tests for OPF_MODEL.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function t_opf_model(quiet)
0002 %T_OPF_MODEL Tests for OPF_MODEL.
0003 
0004 %   MATPOWER
0005 %   Copyright (c) 2012-2015 by Power System Engineering Research Center (PSERC)
0006 %   by Ray Zimmerman, PSERC Cornell
0007 %
0008 %   $Id: t_opf_model.m 2644 2015-03-11 19:34:22Z ray $
0009 %
0010 %   This file is part of MATPOWER.
0011 %   Covered by the 3-clause BSD License (see LICENSE file for details).
0012 %   See http://www.pserc.cornell.edu/matpower/ for more info.
0013 
0014 if nargin < 1
0015     quiet = 0;
0016 end
0017 
0018 num_tests = 305;
0019 
0020 t_begin(num_tests, quiet);
0021 
0022 %% define named indices into data matrices
0023 [PQ, PV, REF, NONE, BUS_I, BUS_TYPE, PD, QD, GS, BS, BUS_AREA, VM, ...
0024     VA, BASE_KV, ZONE, VMAX, VMIN, LAM_P, LAM_Q, MU_VMAX, MU_VMIN] = idx_bus;
0025 [GEN_BUS, PG, QG, QMAX, QMIN, VG, MBASE, GEN_STATUS, PMAX, PMIN, ...
0026     MU_PMAX, MU_PMIN, MU_QMAX, MU_QMIN, PC1, PC2, QC1MIN, QC1MAX, ...
0027     QC2MIN, QC2MAX, RAMP_AGC, RAMP_10, RAMP_30, RAMP_Q, APF] = idx_gen;
0028 [F_BUS, T_BUS, BR_R, BR_X, BR_B, RATE_A, RATE_B, RATE_C, ...
0029     TAP, SHIFT, BR_STATUS, PF, QF, PT, QT, MU_SF, MU_ST, ...
0030     ANGMIN, ANGMAX, MU_ANGMIN, MU_ANGMAX] = idx_brch;
0031 
0032 %%-----  opt_model  -----
0033 t = 'constructor';
0034 om = opf_model;
0035 t_ok(isa(om, 'opf_model'), t);
0036 t_ok(isa(om, 'opt_model'), t);
0037 
0038 %%-----  add_vars  -----
0039 t = 'add_vars';
0040 vN = 0;
0041 vNS = 0;
0042 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0043 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0044 
0045 t = 'add_vars(om, ''Va'', 4)';
0046 om = add_vars(om, 'Va', 4);
0047 vNS = vNS + 1; vN = vN + 4;
0048 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0049 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0050 
0051 t = 'add_vars(om, ''Pg'', 3, Pg0, Pgmin, Pgmax)';
0052 om = add_vars(om, 'Pg', 3, [2;4;6], [1;2;3], [10;20;30]);
0053 vNS = vNS + 1; vN = vN + 3;
0054 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0055 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0056 
0057 t = 'add_vars(om, ''Vm1'', 5, V0, Vmin, Vmax, ''I'')';
0058 V0 = [1;1;1;1;1];
0059 Vmin = zeros(5, 1);
0060 Vmax = 1 + 0.01*(1:5)';
0061 vt = 'I';
0062 om = add_vars(om, 'Vm1', 5, V0, Vmin, Vmax, vt);
0063 vNS = vNS + 1; vN = vN + 5;
0064 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0065 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0066 
0067 t = 'add_vars(om, ''Vm2'', 5, V0, Vmin, Vmax, ''CIBIC'')';
0068 vt = 'CIBIC';
0069 om = add_vars(om, 'Vm2', 5, V0, Vmin, Vmax, vt);
0070 vNS = vNS + 1; vN = vN + 5;
0071 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0072 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0073 
0074 t = 'add_vars(om, ''x'', dims)';
0075 om = add_vars(om, 'x', {2,2});
0076 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0077 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0078 
0079 t = 'add_vars(om, ''x'', {1,1}, 2)';
0080 om = add_vars(om, 'x', {1,1}, 2);
0081 vNS = vNS + 1; vN = vN + 2;
0082 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0083 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0084 
0085 t = 'add_vars(om, ''x'', {1,2}, 2, x0(1,2))';
0086 om = add_vars(om, 'x', {1,2}, 2, [-1;-2]);
0087 vNS = vNS + 1; vN = vN + 2;
0088 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0089 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0090 
0091 t = 'add_vars(om, ''x'', {2,1}, 3)';
0092 om = add_vars(om, 'x', {2,1}, 3);
0093 vNS = vNS + 1; vN = vN + 3;
0094 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0095 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0096 
0097 t = 'add_vars(om, ''x'', {2,2}, 2, x0(2,2), xmin(2,2), xmax(2,2))';
0098 om = add_vars(om, 'x', {2,2}, 2, [1;0],[0;-1],[2;1]);
0099 vNS = vNS + 1; vN = vN + 2;
0100 t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0101 t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0102 
0103 t = 'add_vars(om, ''y'', {2,3,4})';
0104 om = add_vars(om, 'y', {2,3,4});
0105 vt0 = {'C', 'I', 'B'};
0106 for i = 1:2
0107     for j = 1:3
0108         for k = 1:4
0109             n = i+j+k;
0110             if i == 1
0111                 vt = vt0{j};
0112             else
0113                 vt = char(vt0{j} * ones(1, n));
0114                 vt(j+1) = vt0{1+rem(j,3)};
0115             end
0116 %             fprintf('%d %d %d : %s\n', i, j, k, vt);
0117             t = sprintf('add_vars(om, ''y'', {%d,%d,%d}, y0, ymin, ymax, vt)', i,j,k);
0118             om = add_vars(om, 'y', {i,j,k}, n, 10*(n:-1:1)', -1*(n:-1:1)', 100+(n:-1:1)', vt);
0119             vNS = vNS + 1; vN = vN + n;
0120             t_ok(getN(om, 'var') == vN, sprintf('%s : var.N  = %d', t, vN));
0121             t_ok(get(om, 'var', 'NS') == vNS, sprintf('%s : var.NS = %d', t, vNS));
0122         end
0123     end
0124 end
0125 
0126 %%-----  getN  -----
0127 t = 'getN(om, ''var'', ''Pg'') == 3';
0128 t_ok(getN(om, 'var', 'Pg') == 3, t);
0129 
0130 t = 'size(getN(om, ''var'', ''x'')) == [2 2]';
0131 t_is(size(getN(om, 'var', 'x')), [2,2], 1e-14, t);
0132 
0133 t = 'getN(om, ''var'', ''x'')(1,2) == 2';
0134 N = getN(om, 'var', 'x');
0135 t_is(N(1,2), 2, 1e-14, t);
0136 
0137 t = 'getN(om, ''var'', ''x'', {2,1}) == 3';
0138 t_is(getN(om, 'var', 'x', {2,1}), 3, 1e-14, t);
0139 
0140 t = 'getN(om, ''var'', ''y'', {2,1,3}) == 6';
0141 t_is(getN(om, 'var', 'y', {2,1,3}), 6, 1e-14, t);
0142 
0143 t = 'getN(om, ''var'')';
0144 t_is(getN(om, 'var'), vN, 1e-14, t);
0145 
0146 %%-----  get_idx  -----
0147 t = 'get_idx : var';
0148 vv = get_idx(om);
0149 t_is([vv.i1.Pg vv.iN.Pg vv.N.Pg], [5 7 3], 1e-14, [t ' : Pg']);
0150 t_is(size(vv.i1.x), [2, 2], 1e-14, [t ' : size(vv.i1.x)']);
0151 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)']);
0152 t_is(size(vv.i1.y), [2, 3, 4], 1e-14, [t ' : size(vv.i1.y)']);
0153 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)']);
0154 
0155 %%-----  getv  -----
0156 t = 'getv(om, ''Va'')';
0157 [v0, vl, vu] = getv(om, 'Va');
0158 t_ok(~any(v0), [t ' : v0']);
0159 t_ok(all(isinf(vl) & vl < 0), [t ' : vl']);
0160 t_ok(all(isinf(vu) & vu > 0), [t ' : vu']);
0161 
0162 t = 'getv(om, ''Pg'')';
0163 [v0, vl, vu] = getv(om, 'Pg');
0164 t_is(v0, [2;4;6], 1e-14, [t ' : v0']);
0165 t_is(vl, [1;2;3], 1e-14, [t ' : vl']);
0166 t_is(vu, [10;20;30], 1e-14, [t ' : vu']);
0167 
0168 t = 'getv(om, ''Vm1'')';
0169 [v0, vl, vu, vt] = getv(om, 'Vm1');
0170 t_is(double(vt), double('I'), 1e-14, [t ' : vt']);
0171 
0172 t = 'getv(om, ''Vm2'')';
0173 [v0, vl, vu, vt] = getv(om, 'Vm2');
0174 t_is(double(vt), double('CIBIC'), 1e-14, [t ' : vt']);
0175 
0176 t = 'getv(om, ''x'')';
0177 [v0, vl, vu, vt] = getv(om, 'x');
0178 t_is(size(v0), [2,2], 1e-14, [t ' : size(v0)']);
0179 t_is(v0{2,2}, [1;0], 1e-14, [t ' : v0{2,2}']);
0180 t_is(vl{2,2}, [0;-1], 1e-14, [t ' : vl{2,2}']);
0181 t_is(vu{2,2}, [2;1], 1e-14, [t ' : vu{2,2}']);
0182 t_is(double(vt{2,2}), double('C'), 1e-14, [t ' : vt{2,2}']);
0183 
0184 for i = 1:2
0185     for j = 1:3
0186         for k = 1:4
0187             n = i+j+k;
0188             if i == 1
0189                 vt = vt0{j};
0190             else
0191                 vt = char(vt0{j} * ones(1, n));
0192                 vt(j+1) = vt0{1+rem(j,3)};
0193             end
0194             t = sprintf('getv(om, ''y'', {%d,%d,%d})', i, j, k);
0195             [v0, vl, vu, gvt] = getv(om, 'y', {i,j,k});
0196             t_is(v0, 10*(n:-1:1)', 1e-14, [t ' : v0']);
0197             t_is(vl, -1*(n:-1:1)', 1e-14, [t ' : vl']);
0198             t_is(vu, 100+(n:-1:1)', 1e-14, [t ' : vu']);
0199             t_is(gvt, vt, 1e-14, [t ' : vt']);
0200         end
0201     end
0202 end
0203 
0204 t = 'getv(om)';
0205 [v0, vl, vu, vt] = getv(om);
0206 t_ok(length(v0) == getN(om, 'var'), [t ' : length(v0)']);
0207 t_ok(length(vl) == getN(om, 'var'), [t ' : length(vl)']);
0208 t_ok(length(vu) == getN(om, 'var'), [t ' : length(vu)']);
0209 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))']);
0210 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))']);
0211 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))']);
0212 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))']);
0213 n = 8;
0214 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))']);
0215 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))']);
0216 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))']);
0217 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))']);
0218 vt0 = 'CCCCCCCIIIIICIBICCCCCCCCCCCCCCCCCCCCCCCCCCCCIIIIIIIIIIIIIIIIIIIIIIBBBBBBBBBBBBBBBBBBBBBBBBBBCICCCICCCCICCCCCICCCCCIIBIIIIBIIIIIBIIIIIIBIIIIIBBBCBBBBBCBBBBBBCBBBBBBBCBBBBB';
0219 t_is(vt, vt0, 1e-14, [t ' : vt']);
0220 
0221 %%-----  add_constraints  -----
0222 t = 'add_constraints';
0223 lN = 0;
0224 lNS = 0;
0225 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0226 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0227 
0228 t = 'add_constraints(om, ''Pmis'', A, l, u, {''Va'', ''Pg''})';
0229 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);
0230 l = -(1:3)'; u = (1:3)';
0231 om = add_constraints(om, 'Pmis', A, l, u, {'Va', 'Pg'});
0232 lNS = lNS + 1; lN = lN + 3;
0233 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0234 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0235 
0236 t = 'add_constraints(om, ''Qmis'', A, l, u)';
0237 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);
0238 om = add_constraints(om, 'Qmis', A, l, u);
0239 lNS = lNS + 1; lN = lN + 3;
0240 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0241 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0242 
0243 t = 'add_constraints(om, ''mylin'', {2, 2})';
0244 om = add_constraints(om, 'mylin', {2, 2});
0245 t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0246 t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0247 
0248 for i = 1:2
0249     for j = 1:2
0250         t = sprintf('add_constraints(om, ''mylin'', {%d,%d}, A, l, u, vs)', i,j);
0251         A = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0252             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0253         l = -ones(i+j, 1); u = [];
0254         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0255         om = add_constraints(om, 'mylin', {i, j}, A, l, u, vs);
0256         lNS = lNS + 1; lN = lN + i+j;
0257         t_ok(getN(om, 'lin') == lN, sprintf('%s : lin.N  = %d', t, lN));
0258         t_ok(get(om, 'lin', 'NS') == lNS, sprintf('%s : lin.NS = %d', t, lNS));
0259     end
0260 end
0261 
0262 %%-----  get_idx  -----
0263 t = 'get_idx : lin';
0264 [vv, ll] = get_idx(om);
0265 t_is([ll.i1.Qmis ll.iN.Qmis ll.N.Qmis], [4 6 3], 1e-14, [t ' : Qmis']);
0266 t_is(size(ll.i1.mylin), [2, 2], 1e-14, [t ' : size(ll.i1.mylin)']);
0267 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)']);
0268 
0269 %%-----  linear_constraints  -----
0270 t = 'linear_constraints';
0271 [A, l, u] = linear_constraints(om);
0272 t_ok(issparse(A), [t ' : issparse(A)']);
0273 t_is(size(A), [lN, vN], 1e-14, [t ' : size(A)']);
0274 t_is(length(l), lN, 1e-14, [t ' : length(l)']);
0275 t_is(length(u), lN, 1e-14, [t ' : length(u)']);
0276 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);
0277 t_is(full(A(ll.i1.Qmis:ll.iN.Qmis, :)), full(AA), 1e-14, [t ' : A(<Qmis>,:)']);
0278 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>)']);
0279 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)>)']);
0280 
0281 %%-----  add_costs  -----
0282 t = 'add_costs';
0283 cN = 0;
0284 cNS = 0;
0285 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0286 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0287 
0288 t = 'add_costs(om, ''ucost'', cp, {''Va'', ''Pg''})';
0289 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2,7), ...
0290             'Cw', [2;3]);
0291 om = add_costs(om, 'ucost', cp, {'Va', 'Pg'});
0292 cNS = cNS + 1; cN = cN + 2;
0293 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0294 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0295 
0296 t = 'add_costs(om, ''vcost'', cp)';
0297 cp = struct('N', sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN), ...
0298             'Cw', [2;3]);
0299 om = add_costs(om, 'vcost', cp);
0300 cNS = cNS + 1; cN = cN + 2;
0301 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0302 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0303 
0304 t = 'add_costs(om, ''wc'', {2,2})';
0305 om = add_costs(om, 'wc', {2,2});
0306 t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0307 t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0308 
0309 for i = 1:2
0310     for j = 1:2
0311         t = 'add_costs(om, ''wc'', {i, j}, cp, vs)';
0312         cp.N = sparse([1:(i+j) 1:(i+j)]', [1:(i+j) 5*ones(1,i+j)]', ...
0313             [ones(i+j,1);-ones(i+j,1)], i+j, 3+2+(i==2 && j==1));
0314         cp.Cw = (i+j:-1:1)';
0315         if i == 2
0316             cp.H = sparse((1:i+j)', (1:i+j)', (1:i+j)', i+j, i+j);
0317         end
0318         vs = struct('name', {'Pg', 'x'}, 'idx', {{}, {i,j}});
0319         om = add_costs(om, 'wc', {i, j}, cp, vs);
0320         cNS = cNS + 1; cN = cN + i+j;
0321         t_ok(getN(om, 'cost') == cN, sprintf('%s : cost.N  = %d', t, cN));
0322         t_ok(get(om, 'cost', 'NS') == cNS, sprintf('%s : cost.NS = %d', t, cNS));
0323     end
0324 end
0325 
0326 %%-----  get_cost_params/build_cost_params  -----
0327 t = 'get_cost_params : error';
0328 try
0329     cp = get_cost_params(om);
0330     t_ok(0, t);
0331 catch
0332     t_ok(strfind(lasterr, '@opt_model/get_cost_params: must call build_cost_params first'), t);
0333 end
0334 
0335 t = 'build_cost_params';
0336 om = build_cost_params(om);
0337 cp = get_cost_params(om);
0338 t_ok(isfield(cp, 'N'), t);
0339 
0340 %%-----  get_idx  -----
0341 t = 'get_idx : cost';
0342 [vv, ll, nn, cc] = get_idx(om);
0343 t_is([cc.i1.vcost cc.iN.vcost cc.N.vcost], [3 4 2], 1e-14, [t ' : vcost']);
0344 t_is(size(cc.i1.wc), [2, 2], 1e-14, [t ' : size(cc.i1.wc)']);
0345 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)']);
0346 
0347 %%-----  get_cost_params  -----
0348 t = 'get_cost_params(om, ''ucost'')';
0349 cp = get_cost_params(om, 'ucost');
0350 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0351 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0352 t_is(cp.Cw, [2;3], 1e-14, [t, ' : Cw']);
0353 t_is(full(cp.H), zeros(2,2), 1e-14, [t, ' : H']);
0354 t_is(cp.dd, ones(2,1),  1e-14, [t, ' : dd']);
0355 t_is(cp.rh, zeros(2,1), 1e-14, [t, ' : rh']);
0356 t_is(cp.kk, zeros(2,1), 1e-14, [t, ' : kk']);
0357 t_is(cp.mm, ones(2,1),  1e-14, [t, ' : mm']);
0358 
0359 t = 'get_cost_params(om, ''vcost'')';
0360 cp = get_cost_params(om, 'vcost');
0361 N = sparse([1:2 1:2 1:2]', [1:4 5 7]', [1 1 -1 -1 2 2]', 2, vN);
0362 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0363 t_is(cp.Cw, [2;3], 1e-14, [t, ' : Cw']);
0364 t_is(full(cp.H), zeros(2,2), 1e-14, [t, ' : H']);
0365 t_is(cp.dd, ones(2,1),  1e-14, [t, ' : dd']);
0366 t_is(cp.rh, zeros(2,1), 1e-14, [t, ' : rh']);
0367 t_is(cp.kk, zeros(2,1), 1e-14, [t, ' : kk']);
0368 t_is(cp.mm, ones(2,1),  1e-14, [t, ' : mm']);
0369 
0370 t = 'get_cost_params(om, ''wc'') : error';
0371 try
0372     cp = get_cost_params(om, 'wc')
0373     t_ok(0, t);
0374 catch
0375     t_ok(strfind(lasterr, '@opt_model/get_cost_params: cost set ''wc'' requires an idx arg'), t);
0376 end
0377 
0378 t = 'get_cost_params(om, ''wc'', {1,2})';
0379 cp = get_cost_params(om, 'wc', {1,2});
0380 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);
0381 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0382 t_is(cp.Cw, [3;2;1], 1e-14, [t, ' : Cw']);
0383 t_is(full(cp.H), zeros(3,3), 1e-14, [t, ' : H']);
0384 t_is(cp.dd, ones(3,1),  1e-14, [t, ' : dd']);
0385 t_is(cp.rh, zeros(3,1), 1e-14, [t, ' : rh']);
0386 t_is(cp.kk, zeros(3,1), 1e-14, [t, ' : kk']);
0387 t_is(cp.mm, ones(3,1),  1e-14, [t, ' : mm']);
0388 
0389 t = 'get_cost_params(om, ''wc'', {2,1})';
0390 cp = get_cost_params(om, 'wc', {2,1});
0391 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);
0392 t_is(full(cp.N), full(N), 1e-14, [t, ' : N']);
0393 t_is(cp.Cw, [3;2;1], 1e-14, [t, ' : Cw']);
0394 H = sparse(1:3, 1:3, 1:3, 3, 3);
0395 t_is(full(cp.H), full(H), 1e-14, [t, ' : H']);
0396 t_is(cp.dd, ones(3,1),  1e-14, [t, ' : dd']);
0397 t_is(cp.rh, zeros(3,1), 1e-14, [t, ' : rh']);
0398 t_is(cp.kk, zeros(3,1), 1e-14, [t, ' : kk']);
0399 t_is(cp.mm, ones(3,1),  1e-14, [t, ' : mm']);
0400 
0401 t = 'get_cost_params(om)';
0402 cp = get_cost_params(om);
0403 t_ok(issparse(cp.N), [t ' : issparse(cp.N)']);
0404 t_is(size(cp.N), [cN, vN], 1e-14, [t ' : size(cp.N)']);
0405 t_is(size(cp.H), [cN, cN], 1e-14, [t ' : size(cp.H)']);
0406 t_is(length(cp.Cw), cN, 1e-14, [t ' : length(cp.Cw)']);
0407 t_is(length(cp.dd), cN, 1e-14, [t ' : length(cp.dd)']);
0408 t_is(length(cp.rh), cN, 1e-14, [t ' : length(cp.rh)']);
0409 t_is(length(cp.kk), cN, 1e-14, [t ' : length(cp.kk)']);
0410 t_is(length(cp.mm), cN, 1e-14, [t ' : length(cp.mm)']);
0411 N = sparse([1:2 1:2]', [1:4]', [1 1 -1 -1]', 2, 4);
0412 Cw = [2;3];
0413 H = zeros(2,2);
0414 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>)']);
0415 N = sparse([1:2]', [1 3]', [2 2]', 2, 3);
0416 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>)']);
0417 t_is(full(cp.Cw(cc.i1.vcost:cc.iN.vcost)), Cw, 1e-14, [t ' : Cw(<vcost>)']);
0418 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>)']);
0419 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
0420 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>)']);
0421 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 2);
0422 Cw = [3;2;1];
0423 H = zeros(3,3);
0424 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)>)']);
0425 t_is(full(cp.Cw(cc.i1.wc(1,2):cc.iN.wc(1,2))), Cw, 1e-14, [t ' : Cw(<wc(1,2)>)']);
0426 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)>)']);
0427 
0428 N = sparse([1:3]', [1:3]', [1 1 1]', 3, 3);
0429 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>)']);
0430 N = sparse([1:3]', [2 2 2]', [-1 -1 -1]', 3, 3);
0431 Cw = [3;2;1];
0432 H = full(sparse((1:3)', (1:3)', (1:3)', 3, 3));
0433 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)>)']);
0434 t_is(full(cp.Cw(cc.i1.wc(2,1):cc.iN.wc(2,1))), Cw, 1e-14, [t ' : Cw(<wc(2,1)>)']);
0435 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)>)']);
0436 
0437 t_is(cp.dd, ones(cN,1),  1e-14, [t, ' : dd']);
0438 t_is(cp.rh, zeros(cN,1), 1e-14, [t, ' : rh']);
0439 t_is(cp.kk, zeros(cN,1), 1e-14, [t, ' : kk']);
0440 t_is(cp.mm, ones(cN,1),  1e-14, [t, ' : mm']);
0441 
0442 %%-----  compute_cost  -----
0443 % f = compute_cost(om, x, name, idx)
0444 t = 'compute_cost(om, x)';
0445 x = [1:7 rand(1,10) 8:(vN-10)]';
0446 f = compute_cost(om, x);
0447 t_is(f, 343, 1e-14, t);
0448 
0449 t = 'compute_cost(om, ''ucost'')';
0450 f = compute_cost(om, x, 'ucost');
0451 t_is(f, 52, 1e-14, t);
0452 
0453 t = 'compute_cost(om, ''wc'', {2,1})';
0454 f = compute_cost(om, x, 'wc', {2,1});
0455 t_is(f, 91, 1e-14, t);
0456 
0457 % om
0458 % om = struct(om);
0459 
0460 t_end;

Generated on Fri 20-Mar-2015 18:23:34 by m2html © 2005