symbolic maths and optimization

3 次查看(过去 30 天)
i am trying to maximize a nonlinear objective function which is based on the entropy type..During my formulation of the problem i use some matrices to extract information and by this i create the tables Aeq and beq Aeq=84*126 (so 84 linear equations and 126 X variables) for the representation of my linear constaints.My problem has also some nonlinear equality constraints which i create in a new script (as i should) using symbolic maths..Specifically i create the nonlinear symbolic equations
xR = sym('xR', [1 20]); where xR(1)=X85*X105, xR(2)=X86*X106, xR(3)=X87*X107, xR(4)=X88*X108....xR(20)=X104*X124
avg_xR = sym('avg_xR', [1 2]); where avg_xR(1)=( X85*X105 + X87*X107 + X89*X109 + X91*X111 + X93*X113 + X95*X115 + X97*X117 + X99*X119 + X101*X121 + X103*X123 ) / 10 and avg_xR(2)=( X86*X106 + X88*X108 + X90*X110 + X92*X112 + X94*X114 + X96*X116 + X98*X118 + X100*X120 + X102*X122 + X104*X124 ) / 10
xS = sym('xS%d%d',[2 2]); where xS(rc,rk) = SUM(10, (xR(t,rc)-avg_xR(rc))*(xR(t,rk)-avg_xR(rk))) / (10-1); where for example xs(1,1)=(avg_xR1 - X85*X105)^2 + (avg_xR1 - X87*X107)^2 + (avg_xR1 - X89*X109)^2 + (avg_xR1 - X91*X111)^2 + (avg_xR1 - X93*X113)^2 + (avg_xR1 - X95*X115)^2 + (avg_xR1 - X97*X117)^2 + (avg_xR1 - X99*X119)^2 + (avg_xR1 - X101*X121)^2 + (avg_xR1 - X103*X123)^2
Now we only have equations of X's here but no constraint...well the nonlinear equality constraint will actually be something like xs(1,1) = 14.3 xs(1,2) = 8.7 etc..So we can now have the form ceq(x) = 0.
I am using the fmincon solver to run my optimization because thats the one tha fits to my problem best and then i might run it with the genetic algorithms solver..my question is...How can i use these symbolic nonlinear constraintsin my optimization??
  2 个评论
John D'Errico
John D'Errico 2019-2-4
There is absolutely no need for this to be a symbolic problem at all. NONE. Period. Read the help about fmincon. See how to write a function.
Using symbolic variables gains you nothing at all, except for massively slowing the code down.
Just because you don't know the value of avariable in advance does not mean it needs to be symbolic. That is what an optimization tool (fmincon) does for you.
Takis Tsoukalas
Takis Tsoukalas 2019-2-5
Thanks so much for the quick response John...The thing is i have some data for 48 farms...For each of this farm, based on this data i create a model which i have to optimize and that means that i have to create different equations xR, avg_xR and xS for all of those farms...For example in some of this farms my xR is [1 20] but in some others it is [1 40]...If i dont create these equations symbolically using a routine (in a for loop with an if statement for example) i have to write them down one by one xR(1)=X85*X105, xR(2)=X86*X106, xR(3)=X87*X107 which is impossible because for my 48 farms it might be thousands...thank you so much again..

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2019-2-5
编辑:John D'Errico 2019-2-5
Your first problem is the use of numbered variables. That is the worst mistake that new coders make. INSTEAD, USE VECTORS. USE MATRICES.
So you might have a for loop, with ind as a variable, where we might see lines like this:
for ind = 1:numfarms
xR(ind) = X(84+ind)*X(104 + ind);
...
end
There still is absolutely no need for symbolic here. Just a need to learn to use vectors, and to learn how to use an objective function for fmincon.
Even better than using a loop, you need to start learning how to use MATLAB as it is designed to be used. So in one line, we can do this:
xR = X(85:104).*X(105:124);
Everything I said in my comment is still true. When you know only one way to write code, you use it, for everything. Every problem looks like a nail when you have only a hammer in your toolbox. So you know how to use syms. And since you think you don't know what those variables are, then you think you need to use symbolic variables. Then you learned how to created numbered symbolic variables, like X86, X87, etc.
This path will lead you into programming hell. It is a place you don't want to live in (it gets really hot in programming hell, I think they have poor air conditioning), but you don't need to go there. What you will find is all those numbered variables will create bugs in your code. All you need to do is mistype one line and you will never find the bug. And as you have found, wanting to type all those lines for every variable you need to create is programmatically abhorrent.
So instead, work with vectors. An optimization tool looks at your objective as a black box. It passes in a guess at the unknowns to your objective. Then you can unpack that vector of unknowns, computing the various parts of your objective.
xR = X(85:104).*X(105:124);
avg_xR = [sum(xR(1:19)), sum(xR(2:20))]/10;
...
Still, no need for anything symbolic. And certainly no need for a long list of numbered variables. Learn to use MATLAB as a language of matrices, vectors, etc. Even the name MATLAB invokes the phrase "MATrix LABoratory", as it should.
  2 个评论
Takis Tsoukalas
Takis Tsoukalas 2019-2-5
Thanks again John i am trying to fix the problem but it seems i may have a gap (probably in my brain)...
At my main script i use some data and make Aeq and beq tables which as i said are different for each farm..
Aeq=(84*126) for farm 1 and Aeq=(168*252) for farm 2 for example..I suppose that i have correctly make those tables but i want to check it.. So i then create a symbolic X126 or X252 and execute Aeq*X, with purpose to see the results in my command window and confirm that indeed those are the equations i want to my model..
After that i want to make my script for the non linear constraints..
function[c,ceq]=Maximun_Entropy_NLC_function(LTS_R,Aeq,Num_Eq_Y,Num_Eq_P,Num_Eq_Q,Num_Eq_xY,Num_Eq_xP,Num_Var_Y,Num_Var_P,Num_Var_Q,Num_Var_xY,Num_Var_xP);
I then start writing all those equations xR, avg_xR, xS and then i write xs(1,1) = 14.3 xs(1,2) = 8.7 in the form ceq(x) = 0..
For farm1 xR(1)=X85*X105, xR(2)=X86*X106, xR(3)=X87*X107, xR(4)=X88*X108....xR(20)=X104*X124 but for farm2 xR(1)= X169*X209 xR(2)= X170*X210 xR(3)= X171*X211..... xR(40)=X208*X248. I write this again just to mention that for every farm different X from my Aeq table are associated with the xR, avg_xR and xS equations...But again i want to check if my equations are correct so i create the X symbolic variable again and then i run Maximun_Entropy_NLC_function(X,LTS_R,Aeq,Num_Eq_Y,Num_Eq_P,Num_Eq_Q,Num_Eq_xY,Num_Eq_xP,Num_Var_Y,Num_Var_P,Num_Var_Q,Num_Var_xY,Num_Var_xP) with purpose to see the results in my command window and confirm that indeed those are the equations i want to my model again..
Takis Tsoukalas
Takis Tsoukalas 2019-2-5
PAGE 2
I know that solvers uses the objective function script and the non linear constaints scripts as function handles(that is why we basically call them with @(X) ) and when i will call the fmincon i will delete the X from function [c,ceq]=Maximun_Entropy_NLC_function(X,LTS_R,Aeq,Num_Eq_Y
thats for "And certainly no need for a long list of numbered variables" comment you made..i am trying to explain you why i did it..
Now
xR = sym('xR', [1 Num_Eq_xP]);
for i=(Num_Var_Y+Num_Var_P+Num_Var_Q+1):1:(Num_Var_Y+Num_Var_P+Num_Var_Q+Num_Var_xY) % xR(t,rc)
Num_Eq_xR=Num_Eq_xR+1;
xR(Num_Eq_xR)=X(Num_Var_Y+Num_Var_P+Num_Var_Q+Num_Eq_xR)*X(Num_Var_Y+Num_Var_P+Num_Var_Q+Num_Var_xY+Num_Eq_xR);
end
Thats how i create my symbolic xR and you were reaallly close with your guess..
How will i create 20 or 40 different equations if i delete the first line???will i store each one in a table's line??..thank you so much again john i really appreciate it..i wish i could cook you something for helping me...

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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!

Translated by