Variable in such-that part of set specification

Dear ,

I hope this email finds you well. I am currently working on building an optimization model in AMPL and have encountered a persistent issue that has been perplexing me for quite some time. I am reaching out to seek your guidance and expertise in resolving this matter.

To provide some context, I am dealing with binary variables, Phi[k, c], where k belongs to the set {1, 2, 3, …, 9}. As an illustrative example, the values of Phi[k, c] might be {0, 0, 0, 1, 0, 0, 1, 0, 1}. My objective is to establish constraints that would allow me to select the minimum value of k when Phi[k, c] equals 1. In the provided example, I would like to determine the minimum k corresponding to the first occurrence of 1, which is k=3.

In attempting to express this in AMPL, I entered the following constraint:

subject to k_min_constraint{c in car_ID}:
k_min[c] = min{k in KK: Phi[k, c] = 1}*k;

However, I encountered an error message stating: “Variable in such-that part of set specification.”

I would greatly appreciate it if you could provide insights into how I can properly formulate this constraint or suggest an alternative approach to achieve the desired result. Your assistance in resolving this matter would be invaluable to me.

Thank you very much for your time and consideration. I look forward to hearing from you soon.

Best regards,
Liufan

You can try this approach:

param K integer > 0;

var phi {1..K} binary;
var kmin in 1..K;

subj to kminDefn:
   kmin = min {k in 1..K} (k*phi[k] + K*(1-phi[k]));

To keep the example simple, I have left out the index c , but you could introduce it by adding another index set to the definitions of phi and kmin.

Applying the “min” operator to an expression involving variables will work with any MIP solver that has AMPL’s new “MP” interface, including CBC, COPT, Gurobi, HiGHS, Mosek, SCIP, and Xpress. (Also there is a beta MP interface for CPLEX, called cplexmp; access it with option solver cplexmp;.)