statBeginner
statBeginner

Reputation: 849

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

Upvotes: 2

Views: 658

Answers (2)

statBeginner
statBeginner

Reputation: 849

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.

Upvotes: 1

vitaut
vitaut

Reputation: 55574

Since AMPL is an algebraic modeling language rather than a general-purpose programming language, variables in it denote optimization variables which are determined during the solution process. So each time you call solve, optimization variable total is reset. What you need here is a parameter which, unlike variable, is not changed during the optimization:

param total;

Upvotes: 1

Related Questions