How to deal with discrete constraint fmincon
3 次查看(过去 30 天)
显示 更早的评论
Hello! I'm writing the code for an optimization problem, and I'm having problem writing the equality constraints.
I have to write these constraints (discrete) and I don't know how to do it. I'm trying to write them into "Aeq", but I don't know how to handle this "sk+1" and "vk+1". Any ideas? Thank you
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/514047/image.png)
0 个评论
回答(1 个)
Aurele Turnes
2021-2-10
You can write the equality constraint matrix by keeping track of the indices, or you can use the new problem-based workflow to define these constraints more easily.
Start by defining your variables for say N = 10
N = 10;
% Some made-up data
tau = 0.1;
vlower = 0;
vupper = 1;
s0 = 1;
sF = 2;
v0 = 0.1;
vF = 0.8;
% Define variables
a = optimvar('a', N);
s = optimvar('s', N);
v = optimvar('v', N, 'LowerBound', vlower, 'UpperBound', vupper);
Then your constraints are simply:
conS = s(2:end) == s(1:end-1) + tau*v(1:end-1);
conV = v(2:end) == v(1:end-1) + tau*a(1:end-1);
conInitS = s(1) == s0;
conFinalS = s(end) == sF;
conInitV = v(1) == v0;
conFinalV = v(end) == vF;
Note that you could write the same constraints with a for-loop instead, but it is always more efficient to vectorize your code as above.
% conS = optimconstr(N-1,1);
% for i = 1:N-1;
% conS(i) = s(i+1) == s(i) + tau*v(i);
% end
Then, just create your optimproblem and populate Objective and Constraints.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Direct Search 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!