[AMPL 24902] optimizing index of summation in AMPL

Dear group,

I have an optimization problem where, I need maximize the summation index, as follows:

var x integer;
var y {1…x} integer;

maximize x;

subject to:

sum{i in 1…x}y[i] = a;

y[i] + y[j] >= b;

and other constraints.

How could I write that model in AMPL.

I appreciate your help.



AMPL does not accept indexing over {1…x} where x is a variable. This is because when AMPL sends the problem to the solver, it needs to tell the solver many variables are in the problem, and what coefficient each variable has in each constraint. If you have var y {1…x}, the number of y variables is not known until the solver determines the optimal value of x.

You’ll need to reformulate your problem so that AMPL and the solver can handle it. One way is to set a maximum possible value for x – call it xlim – then index y over 1…xlim, but add constraints that if i > x, then y[i] must equal 0:

param a;
param xlim;

var x integer >= 0, <= xlim;
var y {1..xlim} integer >= 0;

maximize xval: x;

subject to ysum: sum {i in 1..xlim} y[i] = a;
subject to ysum_to_x {i in 1..xlim}: i > x ==> y[i] = 0;

The ==> (“implies”) operator, used in constraint ysum_to_x, is recognized by CBC, COPT, GCG, Gurobi, HiGHS, Mosek, SCIP, and Xpress. (CPLEX will be added soon.) If you have any problem getting it to work, please reply with your model and data attached.

Dear Robert, thanks for the suggestion, this is very helpful.