Problem-based optimization, Conversion to double from optim.prob​lemdef.Opt​imizationE​xpression is not possible.

Hi,
I have a optimazition problem on DG allocation using mat-power case30, and when I run the code I face the below error:
"The following error occurred converting from optim.problemdef.OptimizationExpression to
double:
Conversion to double from optim.problemdef.OptimizationExpression is not possible.
Error in lossobj (line 8)
mpc.bus(5,3)=mpc.bus(5,3)-PDg
Error in dg_allocation_problemBased (line 6)
prob = optimproblem('Objective',lossobj(PDg));"
clc;
clear all;
PDg = optimvar('PDg','LowerBound',0,'UpperBound',1);
prob = optimproblem('Objective',lossobj(PDg));z
show(prob)
x0.PDg=0;
[sol,fval,exitflag,output] = solve(prob,x0)
function obj=lossobj(PDg)
mpc=case30;
mpc.bus(5,3)=mpc.bus(5,3)-PDg;
mpopt=mpoption('out.all',0,'verbose',0);
results=runpf(mpc,mpopt);
loss=sum(get_losses(results));
obj=real(loss);
the codes files are attached in zip files.
thanks for your help and support.

 Accepted Answer

To use a general nonlinear function with an optimization variable, you must first convert the function to an optimization expression using fcn2optimexpr. For your problem, after defining PDg, enter
expr = fcn2optimexpr(@lossobj,PDg);
prob = optimproblem('Objective',expr);
Then run the optimization.
Alan Weiss
MATLAB mathematical toolbox documentation

3 Comments

Thanks alot Alan it did work well.
but I have another question
I add another integer variable which is "the Location of Distributed Generation" in this problem but I face the below Error:
"Nonlinear problem with integer variables not supported"
Is this the weakness of problem based solving or my code is wrong?
clc;
clear all;
PDg = optimvar('PDg','LowerBound',0,'UpperBound',1);
locPDg = optimvar('locPDg','type','integer','lowerBound',0,'UpperBound',30);
expr = fcn2optimexpr(@lossobj,PDg,locPDg);
prob = optimproblem('Objective',expr);
show(prob)
x0.PDg=0;
x0.locPDg=0;
[sol,fval,exitflag,output] = solve(prob,x0)
function obj=lossobj(PDg,locPDg)
mpc=case30;
mpc.bus(locPDg,3)=mpc.bus(locPDg,3)-PDg;
mpopt=mpoption('out.all',0,'verbose',0);
results=runpf(mpc,mpopt);
loss=sum(get_losses(results));
obj=real(loss);
thanks for your help and support.
Best regards
That depends on your MATLAB version. Nonlinear programming with integer constraints was added to solve in R2021b (the latest release). If you don't want to upgrade, then I think that you will need to formulate your problem in the solver-based approach and use ga as your solver, or possibly surrogateopt.
Or, because you have just one scalar integer variable, you could just search through all of the possible values, solving for the continuous variables as in your current solution.
Alan Weiss
MATLAB mathematical toolbox documentation
Thank you Alan for your help and support.
Best regurds
JJ

Sign in to comment.

More Answers (0)

Products

Asked:

on 18 Dec 2021

Commented:

on 26 Dec 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!