2

I have the following restriction implemented on CPLEX OPL.

forall (i in N)
        forall (j in M)
            forall (k in 1..i)
                sum(z in 1 ..i)(p[z]*(x[z][j][k]+y[z][j][k])) + (t[k]*max(z in 1 ..i)(x[z][j][k]+y[z][j][k]))<= d[i];
    

And I already tried to implement it in Docplex (python)but I don't know if it actually works, does anyone know how can I switch the max function from CPLEX OPL to Docplex, or if what I did is ok?

for i in N:
    for j in M:
        for k in range(i):
            mdl.sum((x[(i,j,k)]+y[(i,j,k)])*p[z] for z in range(i))+(t[k]*mdl.max(x[(z,j,k)]+y[(z,j,k)] for z in range(i)) <= d[i]
    

1 Answers1

0

mdl.max is the way.

In OPL https://github.com/AlexFleischerParis/zooopl/blob/master/zoomax.mod

int nbKids=300;

{int} buses={30,40,50};


dvar int+ nbBus[buses];
dvar int maxNbOfBusesGivenSize;

    
minimize maxNbOfBusesGivenSize;
     
subject to
{
 // logical constraint
 // maxNbOfBusesGivenSize is the max of all nbBus
 maxNbOfBusesGivenSize==max(i in buses) nbBus[i];
 sum(i in buses) i*nbBus[i]>=nbKids;
}

execute DISPLAY_After_SOLVE
{
  writeln("The max number of buses is ",maxNbOfBusesGivenSize);
  writeln("nbBus = ",nbBus);
}

whereas in python https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoomax.py

from docplex.mp.model import Model

mdl = Model(name='buses')

nbKids=300;
buses=[30,40,50]

#decision variables
mdl.nbBus = {b: mdl.integer_var(name="nbBus"+str(b)) for b in buses}

# Constraint
mdl.add_constraint(sum(mdl.nbBus[b]*b for b in buses) >= nbKids, 'kids')

# Objective
# logical constraint is the max of all nbBus
mdl.minimize(mdl.max(mdl.nbBus[b] for b in buses)) 

mdl.solve(log_output=True,)

mdl.export("c:\\temp\\buses.lp")

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15