linear programming in Lingo

833 views Asked by At

I want to do a linear programming with Lingo, I have the solution but I want to improve the code.

Here is what I want to do:

SETS:
 SEMANA/ 1..12/: D, X, I, Y, Z, R, n;
ENDSETS

X(1)>=D(1); 

X(2)+I(1)>=D(2);

X(3)+I(2)>=D(3);

X(4)+I(3)>=D(4);

X(5)+I(4)>=D(5);

X(6)+I(5)>=D(6);

X(7)+I(6)>=D(7);

X(8)+I(7)>=D(8);

X(9)+I(8)>=D(9);

X(10)+I(9)>=D(10);

X(11)+I(10)>=D(11);

X(12)+I(11)>=D(12);

I have tried this option but there is a mistake that says: Subscript out of range on attribute I.

@FOR (SEMANA(j): 

X(j)+ I(j-1)>= D(j)) ; 

I(j-1) is out of range so I canĀ“t solve the problem.

Thank you

2

There are 2 answers

0
mka On

You need a index filter

@FOR (SEMANA(j) | j#GT#1: 
  X(j)+ I(j-1)>= D(j)) ;

This would be equivalent to having I(0)=0, but only implicitly. If you have I(0)>0, e.g. you have positive initial inventory, then you will need to extend your index set to include '0' and put an additional constraint I(0)=INITIAL_VALUE

Consequently, your @for loop will have to be

@FOR (SEMANA(j) | j#GT#0: 
  X(j)+ I(j-1)>= D(j)) ;
0
Tidhar On

Notice that your code will try to get into I(0) when j=1. I(0) does not defined and that's why it's out of range problem.