1

I'm trying to solve a NP-Hard problem using docplex and cplex and I need that docplex return the first factible solution founded and stop the search. In general, stop when find the nth best solution. Something like that:

set limits solutions n

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
Sebastian Jose
  • 301
  • 2
  • 9

1 Answers1

1

You can use parameters.mip.limits.solutions with docplex python api

A small change to this code gives

from docplex.mp.model import Model

mdl = Model(name='buses')

mdl.parameters.mip.limits.solutions=1


nbbus40 = mdl.integer_var(name='nbBus40')
nbbus30 = mdl.integer_var(name='nbBus30')
mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve()



print("int sol limit = ",mdl.parameters.mip.limits.solutions.get())


for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

which gives

int sol limit =  1
nbBus40  =  8.0
nbBus30  =  0

With all other apis you can do the same.

With pyomo for instance:

import pyomo.environ as pyo
from pyomo.opt import SolverFactory

opt = pyo.SolverFactory("cplex")

opt.options['mip limits solutions'] = 1

model = pyo.ConcreteModel()

model.nbBus = pyo.Var([40,30], domain=pyo.PositiveIntegers)

model.OBJ = pyo.Objective(expr = 500*model.nbBus[40] + 400*model.nbBus[30])

model.Constraint1 = pyo.Constraint(expr = 40*model.nbBus[40] + 30*model.nbBus[30] >= 300)




opt.solve(model)

print("nbBus40=",model.nbBus[40].value)
print("nbBus30=",model.nbBus[30].value)
McKinley
  • 1,123
  • 1
  • 8
  • 18
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15