How can I illustrate the objective function and their constraints in function file?

Constraint:
How can I write the objective function and their constraints in the "function file" in MATLAB?
In this case: C=5, S=3 and N=6
and Xcsn, Pcsn, Rcsn are the binary decision variables while initialYardcs is the parameters that have value in their matrix for example InitialYardcs=[1,0,0;0,0,0 ... ]
Thanks you for your help and your attention!!!

6 Comments

Don't you have constraints on P and R ?
Are P and R free variables to be determined by the optimizer ?
Yes, I have constraint for P and R. But in my exercise, we ignore the P and R, and only focus on 3 constraints to analyze the results.
Choose P_csn = 0 for all c,s and n and R_csn = X_csn. Then you get the minimum value for the objective function, namely 0.
Thank you for your answer. But I don't know how to write it in function file
It sounds like you are looking for a complete tutorial on how to use MATLAB, how to build matrices and work with them, and how to write functions. For us to write that would be a waste of time, since there are tutorials existing on how to use MATLAB, and we would want to spend a lot of time teaching you things, some of which you may already know. Essentially, you need to learn MATLAB.
Start with the MATLAB Onramp tutorials.
Thank you for your advice, I'm still newbie in this.

Sign in to comment.

Answers (1)

I'm sure you can use a loop to define the constraints, and I'm sure you will find out how.
C = 5;
S = 3;
N = 6;
prob = optimproblem;
P = optimvar('P',[C,S,N],'Type','integer','LowerBound',0,'UpperBound',1);
R = optimvar('R',[C,S,N],'Type','integer','LowerBound',0,'UpperBound',1);
X = optimvar('X',[C,S,N],'Type','integer','LowerBound',0,'UpperBound',1);
prob.Objective = sum(P,'All');
Xstart = randi(2,C,S,1)-1
Xstart = 5×3
1 1 1 0 0 0 0 0 1 0 0 0 0 1 0
prob.Constraints.c1 = X(:,:,1)-Xstart-P(:,:,1)+R(:,:,1)==0 ;
prob.Constraints.c2 = X(:,:,2)-X(:,:,1)-P(:,:,2)+R(:,:,2)==0 ;
prob.Constraints.c3 = X(:,:,3)-X(:,:,2)-P(:,:,3)+R(:,:,3)==0 ;
prob.Constraints.c4 = X(:,:,4)-X(:,:,3)-P(:,:,4)+R(:,:,4)==0 ;
prob.Constraints.c5 = X(:,:,5)-X(:,:,4)-P(:,:,5)+R(:,:,5)==0 ;
prob.Constraints.c6 = X(:,:,6)-X(:,:,5)-P(:,:,6)+R(:,:,6)==0 ;
sol = solve(prob)
Solving problem using intlinprog. LP: Optimal objective value is 0.000000. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0. The intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05.
sol = struct with fields:
P: [5×3×6 double] R: [5×3×6 double] X: [5×3×6 double]
sol.P
ans =
ans(:,:,1) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,3) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,5) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,6) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sol.R
ans =
ans(:,:,1) = 1 1 1 0 0 0 0 0 1 0 0 0 0 1 0 ans(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,3) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,5) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,6) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sol.X
ans =
ans(:,:,1) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,2) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,3) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,4) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,5) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ans(:,:,6) = 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Categories

Find more on Model Predictive Control Toolbox in Help Center and File Exchange

Tags

Asked:

on 14 Dec 2023

Edited:

on 14 Dec 2023

Community Treasure Hunt

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

Start Hunting!