How to solve optimization problems when the objective function includes a symbolic function

4 ビュー (過去 30 日間)
NJ2Cali
NJ2Cali 2023 年 1 月 26 日
編集済み: Torsten 2023 年 1 月 31 日
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.

回答 (2 件)

Torsten
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
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:
NJ2Cali
NJ2Cali 2023 年 1 月 31 日
Pass the necessary parameters to nonlcon as you pass them to Func1 to calculate Func2in.
This could also be of help:
This did help, thanks!
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.
The issue is that Func2 is called 5 times in Func1. More than that, the outputs to some of the first calls to Func2 are used in the inputs to the later calls. So even if I keep Func2 I will still need to define 14 optimization variables and a pair of constraints corresponding to the set of equations that Func2 is trying to solve for each of the 10 new optimization variables. I need to define additional constraints so the inputs to the later calls of Func2 are properly constrained. For example:
c(1)=C1*Func2Var1_1^Func2Var1_2/(Func2in_1-Func2Var1_2) - Func2Var1_1*C1;
c(2)=C2*Func2Var1_2/C1 - Func2in_1
c(3)=Func2in_2-Func2out_1/input1
c(4)=C1*Func2Var2_1^Func2Var2_2/(Func2in_2-Func2Var2_2) - Func2Var2_1*C1;
c(5)=C2*Func2Var2_2/C1 - Func2in_2
c(6)=Func2in_3-Func2out_1/input1*Func2out2
c(7)=C1*Func2Var3_1^Func2Var3_2/(Func2in_3-Func2Var3_2) - Func2Var3_1*C1;
c(8)=C2*Func2Var3_2/C1 - Func2in_3
c(9)=Func2in_4-Func2out_3/input1*Func2out2
%etc...
By doing this I wind up with 14 optimization variables and dozens of nonlinear constraints, making the program very hard to get to converge.
Is there another way to go about this or am I stuck with a 14 variable nonlinear optimization problem?

サインインしてコメントする。


Paul
Paul 2023 年 1 月 27 日
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))
Warning: Matrix is singular to working precision.
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
x = 1×7
1.0076 2.0076 2.9924 4.1147 5.0918 6.5000 6.5000
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
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
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 ExchangeGet Started with Optimization Toolbox についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by