How to solve optimization problems when the objective function includes a symbolic function
1 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to use fmincon to optimize a certain function that I will call Func1, subject to several inequality constraints and one nonlinear constraint Func3. The Func1 file calls another function Func2 several times. Func2 solves a system of nonlinear equations using vpasolve. When I try to run it I receive the following error:
"Unable to convert 'optim.problemdef.OptimizationExpression' to 'sym'"
Apparently fmincon is unable to optimize a function that uses sym. What options are available to optimize such nonlinear functions?
The functions are detailed below:
function [var1Opt, var2Opt, var3Opt] = OptimizeFunc1(input1, input2, input3, input 4)
var1=optimvar('var1')
var2=optimvar('var2')
var3=optimvar('var3')
fun=@Func1
x0 = [1 1 1]
lb = [0 0 0]
ub = [3 3 9]
A=[];
b=[];
Aeq = [];
Beq = [];
nonlcon = @Func3;
x = fmincon(fun(var1, var2, var3, input1, input2, input3, input4), x0, A, b, Aeq, Beq, lb, ub, nonlcon)
The function that I am trying to optimize looks like this:
function X = Func1(var1, var2, var3, input1, input2, input3, input4)
Func2in = (var1+var2)/(var3)-(input1*input2)/(input3-input4)
Func2out = Func2(Func2in)
X = Func2in+Func2out
Now for Func2, which is giving me the headache:
function Func2out = Func2(Func2in)
C1=20
C2=30
syms Func2Var1 Func2Var2
eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1;
eqn2 = C2*Func2Var2/C1 == Func2in
sol = vpasolve([eqn1 eqn2], [FuncVar1, FuncVar2];
Func2out=double(Func2Var1)*double(Func2Var2)
These aren't the actual functions I've used but they are representative.
I've seen solutions to problems like this that used matlabFunction but I'm not entirely sure where I would call it from. Any help is very much appreciated.
0 个评论
回答(2 个)
Torsten
2023-1-26
移动:Torsten
2023-1-26
The usual way to deal with this problem is to define Func2Var1 and Func2Var2 as additional optimization variables and to specify the two equations
eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1;
eqn2 = C2*Func2Var2/C1 == Func2in
as nonlinear constraints in nonlcon.
6 个评论
Torsten
2023-1-27
编辑:Torsten
2023-1-27
The issue with this approach is that Func2 is called several times, replacing it with optimization variables will create a LOT of new variables. I'm concerned that fmincon will not converge on a solution if that happens, or will wind up on a local minimum more easily.
I don't understand this. The purpose of Func2 is to supply the product of var5 and var6 (the new solution variables). You can keep Func2 if you like and call it with var5 and var6 as input and var5*var6 as output.
Another question: How do I pass Func2in, which is calculated in Func1, to the constraint function nonlcon? Note that Func2in is much more complicated than I've specified here.
Pass the necessary parameters to nonlcon as you pass them to Func1 to calculate Func2in.
This could also be of help:
Paul
2023-1-27
Hi @NJ2Cali
I was able to get fmincon to run to completion using an objective function that uses syms. I changed Func2 to something I could understand better, but just trying to show it's feasible to use syms.
x = fmincon(@(x) Func1(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),1:7,eye(7),1e6*ones(7,1))
function X = Func1(var1, var2, var3, input1, input2, input3, input4)
Func2in = (var1+var2)/(var3)-(input1*input2)/(input3-input4);
Func2out = Func2(Func2in);
X = Func2in+Func2out;
end
function Func2out = Func2(Func2in)
C1 = 20;
C2 = 30;
syms Func2Var1 Func2Var2
%eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1
%eqn2 = C2*Func2Var2/C1 == Func2in
eqn1 = Func2Var1 + Func2Var2 == C1 + Func2in;
eqn2 = Func2Var1 - Func2Var2 == C2 + Func2in;
sol = vpasolve([eqn1 eqn2], [Func2Var1, Func2Var2]);
Func2out=double(sol.Func2Var1)*double(sol.Func2Var2);
end
3 个评论
Paul
2023-1-31
When running code here on the Answers forum, there is an indicatore on the upper right of the post, which in this case: "Ran in: R2022b"
Torsten
2023-1-31
编辑:Torsten
2023-1-31
The point is that you seem to use the problem-based optimization approach. I don't have another explanation for your error message
"Unable to convert 'optim.problemdef.OptimizationExpression' to 'sym'"
If you used the solver-based approach (as Paul does in his example), your function "Func2" would work without problems.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!