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