ADD_QUAD_COST Adds a set of user costs to the model. OM.ADD_QUAD_COST(NAME, Q, C); OM.ADD_QUAD_COST(NAME, Q, C, K); OM.ADD_QUAD_COST(NAME, Q, C, K, VARSETS); OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C); OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K); OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K, VARSETS); Adds a named block of quadratic costs to the model. Costs are of the form F(X) = 1/2 * X'*Q*X + C'*X + K where Q is an NX x NX matrix (possibly sparse), C is an NX x 1 vector, K is a scalar and NX is the number of elements in X. Here X is the vector formed by combining the specified VARSETS (the full optimization vector by default). Alternatively, if Q is an NX x 1 vector, then K and F(X) are also NX x 1 vectors F(X) = 1/2 * Q .* X.^2 + C .* X + K If Q is empty, then F(X) is also an NX x 1 vector, unless K is scalar and non-zero. Indexed Named Sets A cost set can be identified by a single NAME, as described above, such as 'PgCost', or by a name that is indexed by one or more indices, such as 'PgCost(3,4)'. For an indexed named set, before adding the cost sets themselves, the dimensions of the indexed set must be set by calling INIT_INDEXED_NAME. The constraints are then added using the following, where all arguments are as described above, except IDX_LIST is a cell array of the indices for the particular cost set being added. OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K); OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K, VARSETS); Examples: om.add_quad_cost('quad_cost1', Q1, c1, 0); om.add_quad_cost('lin_cost2', [], c2, k2, {'Vm', 'Pg', 'z'}); om.init_indexed_name('c', {2, 3}); for i = 1:2 for j = 1:3 om.add_quad_cost('c', {i, j}, Q{i,j}, ...); end end See also OPT_MODEL, PARAMS_QUAD_COST, EVAL_QUAD_COST.
0001 function om = add_quad_cost(om, name, idx, Q, c, k, varsets) 0002 %ADD_QUAD_COST Adds a set of user costs to the model. 0003 % OM.ADD_QUAD_COST(NAME, Q, C); 0004 % OM.ADD_QUAD_COST(NAME, Q, C, K); 0005 % OM.ADD_QUAD_COST(NAME, Q, C, K, VARSETS); 0006 % OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C); 0007 % OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K); 0008 % OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K, VARSETS); 0009 % 0010 % Adds a named block of quadratic costs to the model. Costs are of the 0011 % form 0012 % F(X) = 1/2 * X'*Q*X + C'*X + K 0013 % where Q is an NX x NX matrix (possibly sparse), C is an NX x 1 vector, 0014 % K is a scalar and NX is the number of elements in X. Here X is the vector 0015 % formed by combining the specified VARSETS (the full optimization vector 0016 % by default). Alternatively, if Q is an NX x 1 vector, then K and F(X) 0017 % are also NX x 1 vectors 0018 % F(X) = 1/2 * Q .* X.^2 + C .* X + K 0019 % 0020 % If Q is empty, then F(X) is also an NX x 1 vector, unless K is scalar 0021 % and non-zero. 0022 % 0023 % Indexed Named Sets 0024 % A cost set can be identified by a single NAME, as described 0025 % above, such as 'PgCost', or by a name that is indexed by one 0026 % or more indices, such as 'PgCost(3,4)'. For an indexed named 0027 % set, before adding the cost sets themselves, the dimensions 0028 % of the indexed set must be set by calling INIT_INDEXED_NAME. 0029 % 0030 % The constraints are then added using the following, where 0031 % all arguments are as described above, except IDX_LIST is a cell 0032 % array of the indices for the particular cost set being added. 0033 % 0034 % OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K); 0035 % OM.ADD_QUAD_COST(NAME, IDX_LIST, Q, C, K, VARSETS); 0036 % 0037 % Examples: 0038 % om.add_quad_cost('quad_cost1', Q1, c1, 0); 0039 % om.add_quad_cost('lin_cost2', [], c2, k2, {'Vm', 'Pg', 'z'}); 0040 % 0041 % om.init_indexed_name('c', {2, 3}); 0042 % for i = 1:2 0043 % for j = 1:3 0044 % om.add_quad_cost('c', {i, j}, Q{i,j}, ...); 0045 % end 0046 % end 0047 % 0048 % See also OPT_MODEL, PARAMS_QUAD_COST, EVAL_QUAD_COST. 0049 0050 % MATPOWER 0051 % Copyright (c) 2008-2017, Power Systems Engineering Research Center (PSERC) 0052 % by Ray Zimmerman, PSERC Cornell 0053 % 0054 % This file is part of MATPOWER. 0055 % Covered by the 3-clause BSD License (see LICENSE file for details). 0056 % See https://matpower.org for more info. 0057 0058 %% initialize input arguments 0059 if iscell(idx) %% indexed named set 0060 if nargin < 7 0061 varsets = {}; 0062 end 0063 else %% simple named set 0064 if nargin < 6 0065 varsets = {}; 0066 else 0067 varsets = k; 0068 end 0069 if nargin < 5 0070 k = 0; 0071 else 0072 k = c; 0073 end 0074 c = Q; 0075 Q = idx; 0076 idx = {}; 0077 end 0078 0079 %% convert varsets from cell to struct array if necessary 0080 varsets = om.varsets_cell2struct(varsets); 0081 nv = om.varsets_len(varsets); %% number of variables 0082 0083 %% check sizes 0084 [MQ, NQ] = size(Q); 0085 [Mc, Nc] = size(c); 0086 if MQ 0087 if NQ ~= MQ && NQ ~= 1 0088 error('@opt_model/add_quad_cost: Q (%d x %d) must be square or a column vector (or empty)', MQ, NQ); 0089 end 0090 end 0091 if Mc && Nc ~= 1 0092 error('@opt_model/add_quad_cost: c (%d x %d) must be a column vector (or empty)', Mc, Nc); 0093 end 0094 if MQ 0095 if Mc && Mc ~= MQ 0096 error('@opt_model/add_quad_cost: dimensions of Q (%d x %d) and c (%d x %d) are not compatible', MQ, NQ, Mc, Nc); 0097 end 0098 nx = MQ; 0099 else 0100 if ~Mc 0101 error('@opt_model/add_quad_cost: Q and c cannot both be empty'); 0102 end 0103 nx = Mc; 0104 end 0105 if nx ~= nv 0106 error('@opt_model/add_quad_cost: dimensions of Q (%d x %d) and c (%d x %d) do not match\nnumber of variables (%d)\n', MQ, NQ, Mc, Nc, nv); 0107 end 0108 0109 %% size of named cost set 0110 if NQ == 1 || (isempty(Q) && (length(k) > 1 || k == 0)) 0111 %% Q is a column vector (cost is element-wise, i.e. a vector) 0112 %% OR Q is empty and k is either a vector or zero 0113 N = nx; 0114 else %% Q is a square matrix (cost is a scalar) 0115 N = 1; 0116 end 0117 0118 %% add the named quadratic cost set 0119 om.add_named_set('qdc', name, idx, N, Q, c, k, varsets);