Dear Dr Zimmerman, 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_costfcnToo 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 gradientError 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); %% costError 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 %% note: assumes all necessary data included in 1st arg (mpc, om, results) %% so, no additional explicit args are needed 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; endelse 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 modelfcn1 = @(x)branch_loss_costfcn(x);om.add_nln_cost('mycost1', 1, fcn1);endend* 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 datampc = om.get_mpc();[baseMVA, gen, gencost, bus, branch] = deal(mpc.baseMVA, mpc.gen, mpc.gencost, mpc.bus, ... mpc.branch);%% problem dimensionsnxyz = length(x); %% total number of control vars of all typesnn=length(branch(:,1)); %% total number of branchesnft=zeros(nn,2);nft(:,1)=mpc.branch(:,1);nft(:,2)=mpc.branch(:,2);%% grab Va & VmVa = x(vv.i1.Va:vv.iN.Va <http://v.iN.Va>); %% voltage anglesVm = 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 variablef=0;for i=1:nnf=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)))));endend* 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 of you regarding this ! чт, 28 янв. 2021 г. в 19:00, Ray Daniel Zimmerman <r...@cornell.edu>: > Hi Veniamin, > > The legacy cost function only supports costs of a particular form, which I > don’t believe yours can be made to fit. So the only option is to add a > non-linear cost. Unfortunately, I never got around to implementing the > direct specification of non-linear costs (like we have for non-linear > constraints), so you’ll have to do it via a callback function and the use > of add_nln_cost(). > > If you look in opf_setup.m, you can see examples of two functions that > are used with add_nln_cost(), namely, opf_gen_cost_fcn() for handling > higher order polynomial generator costs, and opf_legacy_user_cost_fcn() > for the general legacy user cost. In both cases, a function handle that > only takes *x* as an input is created before passing it to add_nln_cost(). > > You’ll want to have a look at some of the toggle_*.m functions, like > toggle_reserves.m to see how to create the callback functions (see > Sections 7.3-7.6 in the manual). > > I hope this helps, but don’t hesitate to ask if you have more specific > questions in the process. I am working to make these sorts of > customizations simpler in the future. > > Ray > > > On Jan 27, 2021, at 4:31 AM, Вениамин Сукровицин <venya.s...@gmail.com> > wrote: > > Dear Dr Zimmerman and Matpower Community, > > Despite the ways to add user defined variables, cost and constraints have > been already discussed, I haven't found a solution for my case. > I'm working on multi-objective reactive optimal power flow (ROPF) and want > to expand a Matpower cost function which minimizes a cost of reactive power > generation with a standard cost function for ROPF (attached picture). > As you can see, the additional cost function depends on voltages and > branch angles. > The question is what is the best way to implement this cost function ? > I tried to use add_nln_cost but failed because i wasnt able to determine > an appropriate handle function. > I also tried this approach ( > https://www.mail-archive.com/matpower-l@cornell.edu/msg05517.html), > which describes (as i understand) a legacy cost specification for Vm. > > But I want to implement multi-variable cost as I described above. > > I will be very grateful for your advice and examples. > > Big thanks in advance! > > -- > > Veniamin S. > <F.JPG> > > > -- Veniamin S.