使用 Cplex 计算唯一索引的数量

Count number of unique indices using Cplex

对于我的合并问题,我想计算我的决策变量 dvar int+ y[terminal, consolidated_truck, time, order] in 0..1; //y is 1 if order n is consolidated at terminal i in consolidated truck c at time t 对合并卡车采用的唯一值的数量。如果我简单地计算 forall(i in terminal, t in time, n in order) sum(c in consolidated_trucks) y[i,c,t,n] == trucks_used; 我得到每个值,而我只想获得 c 的所有唯一值。 Cplex 中是否有我可以计算这个的函数或正确制定它的方法?

非常感谢您的帮助! 亲切的问候

起点在Easy optimization : count true constraints

int nbKids=300;

// a tuple is like a struct in C, a class in C++ or a record in Pascal
tuple bus
{
  key int nbSeats;
  float cost;
}

// This is a tuple set
{bus} buses={<40,500>,
    <30,400>,
    <35,450>,
    <20,300>};

// asserts help make sure data is fine
assert forall(b in buses) b.nbSeats>0;
assert forall(b in buses) b.cost>0;

// decision variable array
dvar int+ nbBus[buses];

// objective
minimize
     sum(b in buses) b.cost*nbBus[b];
     
// constraints
subject to
{
   sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
   
   // Number of sizes where we have 1 or 2 buses should be at least 3
   
   sum(b in buses) (1<=nbBus[b]<=2) >=3;
}

甚至

int nbKids=300;

// a tuple is like a struct in C, a class in C++ or a record in Pascal
tuple bus
{
  key int nbSeats;
  float cost;
}

// This is a tuple set
{bus} buses={<40,500>,
    <30,400>,
    <35,450>,
    <20,300>};

// asserts help make sure data is fine
assert forall(b in buses) b.nbSeats>0;
assert forall(b in buses) b.cost>0;

range quantity = 0..10;

// decision variable array
dvar int+ nbBus[buses] in quantity;

// objective
minimize
     sum(b in buses) b.cost*nbBus[b];
     
// constraints
subject to
{
   sum(b in buses) b.nbSeats*nbBus[b]>=nbKids;
   
   // Quantities should have less than 1 value
   


     sum(q in quantity)
        (1<=sum(b in buses) (q==nbBus[b])) <=1;
   }

来自 count values