Search code examples
ampl

AMPL Sum variables operator


I am trying to solve a set of problems using AMPL and add their objective values. However, the sum operator does not seem to work and only keeps getting updated to the most recent value.

set CASES := {1,2,3,4,5,6};
model modelFile.mod;
option solver cplex;
option eexit -123456789;
var total;
let total := 0;

for {j in CASES}
{
    reset data;
    data ("data" & j & ".dat")
    solve;
    display total_Cost; 
    let total := total + total_Cost;
    display total;
}

Sample Output:

CPLEX 12.6.3.0: optimal solution; objective 4.236067977
2 dual simplex iterations (0 in phase I)
total_Cost = 4.23607

total = 4.23607

CPLEX 12.6.3.0: optimal solution; objective 5.656854249
5 dual simplex iterations (0 in phase I)
total_Cost = 5.65685

total = 5.65685

where total_cost is the objective value from the optimization problem


Solution

  • I finally realized that this happened due to the new keyword "reset data" that AMPL has. By changing the keyword to "update", the code works.