[AMPL 24613] Declaring constraint without defining it


Is there a way to declare a collection of constraints in the model file but not necessarily define it until after the data has been loaded?

This is mostly relevant for nonlinear programs where I would need to change the functions in the constraints depending on which instance I am solving. And perhaps also the number of constraints. For example, something like this

s.t. mycons {1…m};

Some code in between

let m := 3;
let mycons[1] := x[1]x[2] - 2x[2]x[3]^2 + 3x[1]*x[4] <= 2;
let mycons[2] := # something
let mycons[3] := # something

AMPL assignment (“let”) statements only work for assigning expressions that evaluate to numbers, character strings, or sets. It is possible in AMPL to write

let mycons[1] := ;

but only if evaluates to a number; in that case, the number is assigned as the dual value associated with constraint mycons[1]. AMPL does not recognize this kind of assignment where is a constraint-expression using <= or another relational operator.

There are other ways to selectively include constraints depending on the data, such as by using “drop” and “restore” commands. But it depends on the specifics of what you are trying to do. For example, do you have M different constraints whose expressions are known, but you only want to use the first m of them, where m is given in the data?