solve optimization with constraints
1 view (last 30 days)
Show older comments
I want to maximize an equation with 12 variables (The comment in code described the problem). I solved this by excel solver, but in matlab I dont know how to write the constraints. Below is my try:
% Maximize B = 3.9 X1 + 4.2 X2 + 4.2 X3 + 4.5 X4 + 4.1 X5 + 3.6 X6 + 3.1 X7
% + 2.7 X8 + 2.5 X9 + 2.6 X10 + 2.9 X11 +3.6 X12
%Subject to:
% S0 = 5;
% S(t-1) + I(t) - X(t) = S(t) (t =1,...,12) --> how to wirte this constraint?
% 1<= X(t) <= 7 (t = 1,...,12 )
% S(t) <= 10 (t = 1,...,12 )
f = [-3.9 -4.2 -4.2 -4.5 -4.1 -3.6 -3.1 -2.7 -2.5 -2.6 -2.9 -3.6];
lb=[1;1;1;1;1;1;1;1;1;1;1;1];
ub=[7;7;7;7;7;7;7;7;7;7;7;7];
I = [4 3 2 2 1 2 3 3 2 2 2 3];
0 Comments
Accepted Answer
Alan Weiss
on 28 Feb 2021
This problem is similar to Create Multiperiod Inventory Model in Problem-Based Framework. I think that you will find the problem-based approach easy to use.
Alan Weiss
MATLAB mathematical toolbox documentation
3 Comments
Alan Weiss
on 1 Mar 2021
Well, it is more awkward, but fairly straightforward. You simply have to keep careful track of variable indices.
Let S(1) through S(12) represent the S variables, and X(1) through X(12) the X variables. You have to put all of the variables into one, typically called x (lower case). Say the mapping is x = [X,S], where all variables are row vectors. Then you can write your constraints all in terms of x in matrices A and Aeq to represent the dynamics. For example, the first equation
S(t-1) + I(t) - X(t) = S(t)
becomes, for t = 1,
S0 + I(1) = S(1) + X(1) = x(1) + x(13)
You can represent this as row 1 in matrix Aeq with beq(1) = S0 + I(1) = 5 + 4. The Aeq row is
[1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0] % Aeq(1,1) = Aeq(1,13) = 1
Similarly, for t = 2 the equation is
I(2) = S(2) + X(2) - S(1)
In terms of Aeq and beq you get
Aeq(2,2) = Aeq(2,14) = 1
Aeq(2,13) = -1
beq(2) = I(2) = 3
The matrix Aeq has a simple banded structure. You have to be careful about the bounds on S and X; for example, does S have a lower bound?
You have to represent the f coefficients in terms of x, which means adjoin 12 zeros to the end of your current f vector. OK?
Alan Weiss
MATLAB mathematical toolbox documentation
More Answers (0)
See Also
Categories
Find more on Nonlinear Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!