linear-programmingglpkmathprog

Issue on GMPL code


I was trying to solve the following problem, using the GLPSOL solver:

Fred has $5000 to invest over the next five years. At the beginning of each year he can invest money in one- or two-year time deposits. The bank pays 4% interest on one-year time deposits and 9 percent (total) on two-year time deposits. In addition, West World Limited will offer three-year certificates starting at the beginning of the second year. These certificates will return 15% (total). If Fred reinvest his money that is available every year, formulate a linear program to show him how to maximize his total cash on hand at the end of the fifth year.

I came up with the following LP model:

Being xij the amount invested in option i at year j, we look to
maximize z = 1,04x15 + 1,09x24 + 1,15x33,
subject to:

  • x11 + x12 <= 5000
  • x31 = x34 = x35 = 0
  • x12 + x22 + x32 <= 1,04 x11
  • x13 + x23 + x33 <= 1,04 x12 + 1,09 x21
  • x14 + x24 <= 1,04 x13 + 1,09 x22
  • x15 <= 1,04 x14 + 1,09 x23 + 1,15 x32
  • xij >= 0

And tried to write it in GMPL:

/* Variables */
var x{i in 1..3, j in 1..5} >= 0;

/* Objective */
maximize money: 1.04*x[1,5] + 1.09*x[2,4] + 1.15*x[3,3];

/* Constraints */
s.t. x[1,1] + x[2,1] <= 5000;
s.t. x[3,1] = x[3,4] = x[3,5] = 0;
s.t. x[1,2] + x[2,2] + x[3,2] <= 1.04 * x[1,1];
s.t. x[1,3] + x[2,3] + x[3,3] <= 1.04 * x[1,2] + 1.09 * x[2,1];
s.t. x[1,4] + x[2,4] <= 1.04 * x[1,3] + 1.09 * x[2,2];
s.t. x[1,5] <= 1.04 * x[1,4] + 1.09 * x[2,3] + 1.15 * x[3,2];

/* Resolve */
solve;

/* Results */
printf{j in 1..5}:"\n* %.2f %.2f %2.f \n", x[1,j], x[2,j], x[3,j];

end;

However, I'm getting the following error:

inv.mod:14: x multiply declared
Context: ...[ 1 , 5 ] + 1.09 * x [ 2 , 4 ] + 1.15 * x [ 3 , 3 ] ; s.t. x
MathProg model processing error

Does anyone have any thoughts about this?


Solution

  • You have to give a unique name to each constraint. Multiple assignments are not allowed.

    This works on my machine:

    /* Variables */
    var x{i in 1..3, j in 1..5} >= 0;
    
    /* Objective */
    maximize money: 1.04*x[1,5] + 1.09*x[2,4] + 1.15*x[3,3];
    
    /* Restrições */
    s.t. c1: x[1,1] + x[2,1] <= 5000;
    s.t. c2: x[3,1] = 0;
    s.t. c3: x[3,4] = 0;
    s.t. c4: x[3,5] = 0;
    s.t. c5: x[1,2] + x[2,2] + x[3,2] <= 1.04 * x[1,1];
    s.t. c6: x[1,3] + x[2,3] + x[3,3] <= 1.04 * x[1,2] + 1.09 * x[2,1];
    s.t. c7: x[1,4] + x[2,4] <= 1.04 * x[1,3] + 1.09 * x[2,2];
    s.t. c8: x[1,5] <= 1.04 * x[1,4] + 1.09 * x[2,3] + 1.15 * x[3,2];
    
    /* Resolve */
    solve;
    
    /* Results */
    printf{j in 1..5}:"\n* %.2f %.2f %2.f \n", x[1,j], x[2,j], x[3,j];
    
    end;
    

    It prints:

    * 0.00 5000.00  0 
    
    * 0.00 0.00  0 
    
    * 0.00 0.00 5450 
    
    * 0.00 0.00  0 
    
    * 0.00 0.00  0 
    

    Good luck!