Dear Dr Zimmerman,

This is a duplicate of my previous email. I tried to remove the extra
spaces, because the email was displayed incorrectly in the mail-archive and
it isn't possible to read my comments and code (however, everything is
displayed fine in my email box).
I hope this time everything will be displayed correctly.

Big thanks for your instructions.
I tried this way to add my cost function, but I got an error. Could you
please take a look ?
Here is the error description:
Error using branch_loss_costfcn
Too many output arguments.
Error in toggle_branch_loss>@(x)branch_loss_costfcn(x) (line 33)
fcn1 = @(x)branch_loss_costfcn(x);
Error in opt_model/eval_nln_cost (line 48)
[fk, dfk] = fcn(xx);    %% evaluate kth cost and gradient
Error in opf_costfcn (line 45)
[f, df]   = om.eval_nln_cost(x);
Error in mipsopf_solver>@(x)opf_costfcn(x,om) (line 125)
f_fcn = @(x)opf_costfcn(x, om);
Error in mips (line 349)
[f, df] = f_fcn(x);             %% cost
Error in mipsopf_solver (line 129)
mips(f_fcn, x0, A, l, u, xmin, xmax, gh_fcn, hess_fcn, opt);
Error in opf_execute (line 86)
[results, success, raw] = mipsopf_solver(om, mpopt);
Error in opf (line 232)
[results, success, raw] = opf_execute(om, mpopt);
Error in runopf (line 75)
[r, success] = opf(casedata, mpopt);

I used the following script to run OPF:
mpc = loadcase('case9Q.m');
mpc = toggle_branch_loss(mpc, 'on');
results = runopf(mpc);

with the following functions:
function mpc = toggle_branch_loss(mpc, on_off)
%fcn toggle_loss_costfcn
%mpc = toggle_branch_loss(mpc, on_off)
%% add callback functions
if strcmp(upper(on_off), 'ON')
mpc = add_userfcn(mpc, 'formulation', @userfcn_branch_loss_formulation);
mpc.userfcn.status.branch_loss = 1;
elseif strcmp(upper(on_off), 'OFF')
mpc = remove_userfcn(mpc, 'formulation', @userfcn_branch_loss_formulation);
mpc.userfcn.status.branch_loss = 0;
elseif strcmp(upper(on_off), 'STATUS')
if isfield(mpc, 'userfcn') && isfield(mpc.userfcn, 'status') && ...
isfield(mpc.userfcn.status, 'branch_loss')
mpc = mpc.userfcn.status.branch_loss;
else
mpc = 0;
end
else
error('toggle_reserves: 2nd argument must be ''on'', ''off'' or
''status''');
end

%%-----  formulation  --------------------------------------------------
function om = userfcn_branch_loss_formulation(om, mpopt, args)
%om = branch_loss_formulation(om, mpopt, args)
%% add cost to the model
fcn1 = @(x)branch_loss_costfcn(x);
om.add_nln_cost('mycost1', 1, fcn1);
end
end

As you can see toggle_branch_loss.m consist of only one callback function
- userfcn_branch_loss_formulation.m because in my case I dont need to
define other callback funtions (I guess).

Here is the my additional cost function  branch_loss_costfcn.m which is
used as a hande function in a solver stage (as I understand). So I'm trying
to add a cost function to reduce transmission system losses
min Ploss=Gij*(Vi^2+Vj^2-2*Vi*Vj*cosθij):
function f = branch_loss_costfcn(x)
%fcn branch_loss_costfcn determines an additional multi-variable cost fcn
%f = branch_loss_costfcn(x)
%% get some data
mpc = om.get_mpc();
[baseMVA, gen, gencost, bus, branch] = deal(mpc.baseMVA, mpc.gen,
mpc.gencost, mpc.bus, mpc.branch);
%% problem dimensions
nxyz = length(x);       %% total number of control vars of all types
nn=length(branch(:,1)); %% total number of branches
nft=zeros(nn,2);
nft(:,1)=mpc.branch(:,1);
nft(:,2)=mpc.branch(:,2);
%% grab Va & Vm
Va = x(vv.i1.Va:vv.iN.Va <http://v.in.va/>);  %% voltage angles
Vm = x(vv.i1.Vm:vv.iN.Vm);  %% voltage magnitudes
%% build admittance matrices
[Ybus, Yf, Yt] = makeYbus(mpc.baseMVA, mpc.bus, mpc.branch);
%% add cost for my variable
f=0;
for i=1:nn
f=f+real(Ybus(nft(i,1),nft(i,2)))*(Vm(nft(i,1))+Vm(nft(i,2))-2*Vm(nft(i,1))*Vm(nft(i,2))*cos(abs(Va(nft(i,1))-Va(nft(i,2)))));
end
end

I still do not understant how is that possible
that  branch_loss_costfcn.m causes the error 'Too many output arguments'
because f (output) is a scalar.
Also according to the descriptoin of add_nln_cost.m, additional cost
function  should take the following form: f=fcn(x) or [f, df]=fcn(x),
or [f, df, d2f]=fcn(x). So X - the optimization vector - is only one one
(allowed) input vector. Is that possible to use  om.get_mpc() and define
Ybus, ..., etc inside  branch_loss_costfcn.m as I did ?
Big thanks in advance for your time !    This case in highly important for
my research and I appriciate for any information from you regarding this !
-- 
With best regards,
Veniamin S.

Reply via email to