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
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.
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 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!