First, if all you want to do minimize active power losses, see FAQ #1<https://matpower.org/doc/faq/#lossminimization> or the “loss as objective function”<https://www.mail-archive.com/[email protected]/msg07661.html> thread from today.
However, the error you are getting appears to be due to the fact that your branch_loss_costfcn() does not handle the case with two or three output arguments, used to compute the gradient and Hessian of the cost function. See Section 5.3.2 in the MP-Opt-Model User’s Manual<https://matpower.org/docs/MP-Opt-Model-manual-3.0.pdf> for details on the expected form of a general cost function to be added by add_nln_cost(). Hope this helps, Ray On Feb 14, 2021, at 9:46 AM, Veniamin S. <[email protected]<mailto:[email protected]>> wrote: 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); " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "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);" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "f," 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) " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "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" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "add"----- formulation -------------------------------------------------- function om = userfcn_branch_loss_formulation(om, mpopt, args) %om = branch_loss_formulation(om, mpopt, args) " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "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)" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "add" get some data mpc = om.get_mpc(); [baseMVA, gen, gencost, bus, branch] = deal(mpc.baseMVA, mpc.gen, mpc.gencost, mpc.bus, mpc.branch); " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "problem dimensions nxyz = length(x);" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "problem" total number of control vars of all types nn=length(branch(:,1)); " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "total number of branches nft=zeros(nn,2); nft(:,1)=mpc.branch(:,1); nft(:,2)=mpc.branch(:,2);" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "total" grab Va & Vm Va = x(vv.i1.Va:vv.iN.Va<http://v.in.va/>); " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "voltage angles Vm = x(vv.i1.Vm:vv.iN.Vm);" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "voltage" voltage magnitudes " OutmailID: 125428138, List: 'matpower-l', MemberID: 82861091 SCRIPT: "build admittance matrices [Ybus, Yf, Yt] = makeYbus(mpc.baseMVA, mpc.bus, mpc.branch);" TCL MERGE ERROR ( 03/01/2021 15:35:36 ): "invalid command name "Ybus," 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.
