MAKEAY Make the A matrix and RHS for the CCV formulation. [AY, BY] = MAKEAY(BASEMVA, NG, GENCOST, PGBAS, QGBAS, YBAS) Constructs the parameters for linear "basin constraints" on Pg, Qg and Y used by the CCV cost formulation, expressed as AY * X <= BY where X is the vector of optimization variables. The starting index within the X vector for the active, reactive sources and the Y variables should be provided in arguments PGBAS, QGBAS, YBAS. The number of generators is NG. Assumptions: All generators are in-service. Filter any generators that are offline from the GENCOST matrix before calling MAKEAY. Efficiency depends on Qg variables being after Pg variables, and the Y variables must be the last variables within the vector X for the dimensions of the resulting AY to be conformable with X. Example: [Ay, by] = makeAy(baseMVA, ng, gencost, pgbas, qgbas, ybas);
0001 function [Ay, by] = makeAy(baseMVA, ng, gencost, pgbas, qgbas, ybas) 0002 %MAKEAY Make the A matrix and RHS for the CCV formulation. 0003 % [AY, BY] = MAKEAY(BASEMVA, NG, GENCOST, PGBAS, QGBAS, YBAS) 0004 % 0005 % Constructs the parameters for linear "basin constraints" on Pg, Qg 0006 % and Y used by the CCV cost formulation, expressed as 0007 % 0008 % AY * X <= BY 0009 % 0010 % where X is the vector of optimization variables. The starting index 0011 % within the X vector for the active, reactive sources and the Y 0012 % variables should be provided in arguments PGBAS, QGBAS, YBAS. The 0013 % number of generators is NG. 0014 % 0015 % Assumptions: All generators are in-service. Filter any generators 0016 % that are offline from the GENCOST matrix before calling MAKEAY. 0017 % Efficiency depends on Qg variables being after Pg variables, and 0018 % the Y variables must be the last variables within the vector X for 0019 % the dimensions of the resulting AY to be conformable with X. 0020 % 0021 % Example: 0022 % [Ay, by] = makeAy(baseMVA, ng, gencost, pgbas, qgbas, ybas); 0023 0024 % MATPOWER 0025 % Copyright (c) 1996-2016, Power Systems Engineering Research Center (PSERC) 0026 % by Carlos E. Murillo-Sanchez, PSERC Cornell & Universidad Nacional de Colombia 0027 % 0028 % This file is part of MATPOWER. 0029 % Covered by the 3-clause BSD License (see LICENSE file for details). 0030 % See https://matpower.org for more info. 0031 0032 [PW_LINEAR, POLYNOMIAL, MODEL, STARTUP, SHUTDOWN, NCOST, COST] = idx_cost; 0033 0034 % find all pwl cost rows in gencost, either real or reactive 0035 iycost = find(gencost(:, MODEL) == PW_LINEAR); 0036 0037 % this is the number of extra "y" variables needed to model those costs 0038 ny = size(iycost, 1); 0039 0040 if ny == 0 0041 Ay = sparse([], [], [], 0, ybas+ny-1, 0); 0042 by = []; 0043 return 0044 end 0045 0046 % if p(i),p(i+1),c(i),c(i+1) define one of the cost segments, then 0047 % the corresponding constraint on Pg (or Qg) and Y is 0048 % c(i+1) - c(i) 0049 % Y >= c(i) + m * (Pg - p(i)), m = --------------- 0050 % p(i+1) - p(i) 0051 % 0052 % this becomes m * Pg - Y <= m*p(i) - c(i) 0053 0054 % Form A matrix. Use two different loops, one for the Pg/Qg coeffs, 0055 % then another for the y coeffs so that everything is filled in the 0056 % same order as the compressed column sparse format used by MATLAB; 0057 % this should be the quickest. 0058 0059 m = sum(gencost(iycost, NCOST)); % total number of cost points 0060 Ay = sparse([], [], [], m-ny, ybas+ny-1, 2*(m-ny)); 0061 by = []; 0062 % First fill the Pg or Qg coefficients (since their columns come first) 0063 % and the rhs 0064 k = 1; 0065 for i=iycost' 0066 ns = gencost(i, NCOST); % # of cost points; segments = ns-1 0067 p = gencost(i, COST:2:COST+2*ns-1) / baseMVA; 0068 c = gencost(i, COST+1:2:COST+2*ns); 0069 m = diff(c) ./ diff(p); % slopes for Pg (or Qg) 0070 if any(diff(p) == 0) 0071 fprintf('\nmakeAy: bad x axis data in row %i of gencost matrix\n',i); 0072 end 0073 b = m .* p(1:ns-1) - c(1:ns-1); % and rhs 0074 by = [by; b']; 0075 if i > ng 0076 sidx = qgbas + (i-ng) - 1; % this was for a q cost 0077 else 0078 sidx = pgbas + i - 1; % this was for a p cost 0079 end 0080 Ay(k:k+ns-2, sidx) = m'; 0081 k = k + ns - 1; 0082 end 0083 % Now fill the y columns with -1's 0084 k = 1; 0085 j = 1; 0086 for i=iycost' 0087 ns = gencost(i, NCOST); 0088 Ay(k:k+ns-2, ybas+j-1) = -ones(ns-1,1); 0089 k = k + ns - 1; 0090 j = j + 1; 0091 end