How to deal with discrete constraint fmincon

조회 수: 8 (최근 30일)
Saverio Borreli
Saverio Borreli 2021년 2월 9일
답변: Aurele Turnes 2021년 2월 10일
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

답변 (1개)

Aurele Turnes
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.

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by