1. 程式人生 > >Matpower 物件操作,修改變數,去除線性約束

Matpower 物件操作,修改變數,去除線性約束

1. 修改變數上下限

npg = vv.N.Pg;
idpg =[vv.i1.Pg: vv.iN.Pg];

om = change_vars(om, 'Pg',npg, x0(idpg), xmin(idpg)+2, xmax(idpg));

[x0, xmin, xmax] = getv(om);

新增change_vars()函式

function om = change_vars(om, name, N, v0, vl, vu)
%ADD_VARS  Adds a set of variables to the model.
%   OM = ADD_VARS(OM, NAME, N, V0, VL, VU)
%   OM = ADD_VARS(OM, NAME, N, V0, VL)
%   OM = ADD_VARS(OM, NAME, N, V0)
%   OM = ADD_VARS(OM, NAME, N)
%   
%   Adds a set of variables to the model, where N is the number of
%   variables in the set, V0 is the initial value of those variables,
%   and VL and VU are the lower and upper bounds on the variables.
%   The defaults for the last three arguments, which are optional,
%   are for all values to be initialized to zero (V0 = 0) and unbounded
%   (VL = -Inf, VU = Inf).
%
%   See also OPF_MODEL, GETV.

%   MATPOWER
%   $Id: add_vars.m,v 1.7 2010/04/26 19:45:25 ray Exp $
%   by Ray Zimmerman, PSERC Cornell
%   Copyright (c) 2008-2010 by Power System Engineering Research Center (PSERC)
%
%   This file is part of MATPOWER.
%   See http://www.pserc.cornell.edu/matpower/ for more info.
%
%   MATPOWER is free software: you can redistribute it and/or modify
%   it under the terms of the GNU General Public License as published
%   by the Free Software Foundation, either version 3 of the License,
%   or (at your option) any later version.
%
%   MATPOWER is distributed in the hope that it will be useful,
%   but WITHOUT ANY WARRANTY; without even the implied warranty of
%   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%   GNU General Public License for more details.
%
%   You should have received a copy of the GNU General Public License
%   along with MATPOWER. If not, see <http://www.gnu.org/licenses/>.
%
%   Additional permission under GNU GPL version 3 section 7
%
%   If you modify MATPOWER, or any covered work, to interface with
%   other modules (such as MATLAB code and MEX-files) available in a
%   MATLAB(R) or comparable environment containing parts covered
%   under other licensing terms, the licensors of MATPOWER grant
%   you additional permission to convey the resulting work.

% %% prevent duplicate named var sets
% if isfield(om.var.idx.N, name)
%     error('@opf_model/add_vars: variable set named ''%s'' already exists', name);
% end
% 
% %% initialize args and assign defaults
% if nargin < 6
%     vu = [];
%     if nargin < 5
%         vl = [];
%         if nargin < 4
%             v0 = [];
%         end
%     end
% end
% if isempty(v0)
%     v0 = zeros(N, 1);           %% init to zero by default
% end
% if isempty(vl)
%     vl = -Inf * ones(N, 1);     %% unbounded below by default
% end
% if isempty(vu)
%     vu = Inf * ones(N, 1);      %% unbounded above by default
% end

%% add info about this var set
% om.var.idx.i1.(name)  = om.var.N + 1;   %% starting index
% om.var.idx.iN.(name)  = om.var.N + N;   %% ending index
% om.var.idx.N.(name)   = N;              %% number of vars
om.var.data.v0.(name) = v0;             %% initial value
om.var.data.vl.(name) = vl;             %% lower bound
om.var.data.vu.(name) = vu;             %% upper bound

% %% update number of vars and var sets
% om.var.N  = om.var.idx.iN.(name);
% om.var.NS = om.var.NS + 1;
% 
% %% put name in ordered list of var sets
% om.var.order{om.var.NS} = name;

去除線性約束

function om = remove_constraints(om, name, idx, varargin)
%ADD_CONSTRAINTS  Adds a set of constraints to the model.
%   OM = ADD_CONSTRAINTS(OM, NAME, A, L, U);
%   OM = ADD_CONSTRAINTS(OM, NAME, A, L, U, VARSETS);
%   OM = ADD_CONSTRAINTS(OM, NAME, DIM_LIST);
%   OM = ADD_CONSTRAINTS(OM, NAME, IDX_LIST, A, L, U);
%   OM = ADD_CONSTRAINTS(OM, NAME, IDX_LIST, A, L, U, VARSETS);
%   OM = ADD_CONSTRAINTS(OM, NAME, N, 'NON-LINEAR');
%
%   Linear constraints are of the form L <= A * x <= U, where
%   x is a vector made of of the vars specified in VARSETS (in
%   the order given). This allows the A matrix to be defined only
%   in terms of the relevant variables without the need to manually
%   create a lot of zero columns. If VARSETS is empty, x is taken
%   to be the full vector of all optimization variables. If L or 
%   U are empty, they are assumed to be appropriately sized vectors
%   of -Inf and Inf, respectively.
%
%   For nonlinear constraints, the 3rd argument, N, is the number
%   of constraints in the set. Currently, this is used internally
%   by MATPOWER, but there is no way for the user to specify
%   additional nonlinear constraints.
%
%   Examples:
%       om = add_constraints(om, 'vl', Avl, lvl, uvl, {'Pg', 'Qg'});
%       om = add_constraints(om, 'Pmis', nb, 'nonlinear');
%
%       om = add_constraints(om, 'R', {2, 3});
%       for i = 1:2
%         for j = 1:3
%           om = add_constraints(om, 'R', {i, j}, A{i,j}, ...);
%         end
%       end
%
%   See also OPT_MODEL, LINEAR_CONSTRAINTS.

%   MATPOWER
%   Copyright (c) 2008-2016 by Power System Engineering Research Center (PSERC)
%   by Ray Zimmerman, PSERC Cornell
%
%   This file is part of MATPOWER.
%   Covered by the 3-clause BSD License (see LICENSE file for details).
%   See http://www.pserc.cornell.edu/matpower/ for more info.

nonlin = 0;

        %% prevent duplicate named constraint sets
if isfield(om.lin.idx.N, name)
    fprintf('%s'' already exists,you can remove it\n', name);
else
    error('%s does not exists, you can not remove it\n',name);
    return;
end


%% add info about this linear constraint set
%     om.lin.idx.i1 = es;
%     om.lin.idx.iN = es;
%     om.lin.idx.N = es;
%     om.lin.N = 0;
%     om.lin.NS = 0;
%     om.lin.order = {};
%     om.lin.data.A = es;
%     om.lin.data.l = es;
%     om.lin.data.u = es;
%     om.lin.data.vs = es;


% !!!! must follow the order to remove elements
om.lin.idx.i1 = rmfield(om.lin.idx.i1,name);
om.lin.idx.iN = rmfield(om.lin.idx.iN,name);
om.lin.idx.N = rmfield(om.lin.idx.N,name);
om.lin.order(om.lin.NS)  = [];
om.lin.NS = om.lin.NS - 1;

endname = om.lin.order(om.lin.NS);
om.lin.N  = om.lin.idx.iN.(char(endname));

om.lin.data.A = rmfield(om.lin.data.A,name);
om.lin.data.l = rmfield(om.lin.data.l,name);
om.lin.data.u = rmfield(om.lin.data.u,name);
om.lin.data.vs = rmfield(om.lin.data.vs,name);

%         om.lin.idx.i1.(name)  = om.lin.N + 1;   %% starting index
%         om.lin.idx.iN.(name)  = om.lin.N + N;   %% ending index
%         om.lin.idx.N.(name)   = N;              %% number of constraints
%         om.lin.data.A.(name)  = A;
%         om.lin.data.l.(name)  = l;
%         om.lin.data.u.(name)  = u;
%         om.lin.data.vs.(name) = varsets;

%% update number of linear constraints and constraint sets
%         om.lin.N  = om.lin.idx.iN.(name);
%         om.lin.NS = om.lin.NS - 1;

%         om.lin.N  = om.lin.idx.iN.(name);
%         om.lin.NS = om.lin.NS + 1;

%% add to ordered list of linear constraint sets
% %         om.lin.order(om.lin.NS).name = name;
%         om.lin.order(om.lin.NS).idx  = {};