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

5 views (last 30 days)
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)
x0 = [1 1 1]
lb = [0 0 0]
ub = [3 3 9]
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)
syms Func2Var1 Func2Var2
eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1;
eqn2 = C2*Func2Var2/C1 == Func2in
sol = vpasolve([eqn1 eqn2], [FuncVar1, FuncVar2];
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.

Answers (2)

Torsten on 26 Jan 2023
Moved: Torsten on 26 Jan 2023
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.
NJ2Cali on 31 Jan 2023
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(4)=C1*Func2Var2_1^Func2Var2_2/(Func2in_2-Func2Var2_2) - Func2Var2_1*C1;
c(5)=C2*Func2Var2_2/C1 - Func2in_2
c(7)=C1*Func2Var3_1^Func2Var3_2/(Func2in_3-Func2Var3_2) - Func2Var3_1*C1;
c(8)=C2*Func2Var3_2/C1 - Func2in_3
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?

Sign in to comment.

Paul on 27 Jan 2023
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;
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]);
Torsten on 31 Jan 2023
Edited: Torsten on 31 Jan 2023
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!