How to deal with discrete constraint fmincon

8 visualizzazioni (ultimi 30 giorni)
Saverio Borreli
Saverio Borreli il 9 Feb 2021
Risposto: Aurele Turnes il 10 Feb 2021
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

Risposte (1)

Aurele Turnes
Aurele Turnes il 10 Feb 2021
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.

Categorie

Scopri di più su Problem-Based Optimization Setup in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by