coin-or / pulp

A python Linear Programming API
http://coin-or.github.io/pulp/
Other
2.04k stars 381 forks source link

Add setting randomseed support for CPLEX_PY #660

Closed MBradbury closed 11 months ago

MBradbury commented 1 year ago

The random seed of a CPLEX solver can be set by updating the parameters.randomseed variable. This PR adds support for users to set the seed when the CPLEX_PY solver is created.

CLAassistant commented 1 year ago

CLA assistant check
All committers have signed the CLA.

pchtsp commented 1 year ago

Is there a particular reason to do it through the initialization of the solver instead of the current supported way? Ideally, we want to keep an interface as simple as possible and as uniform across solvers as possible. Currently we do not pass seeds for other solvers.

MBradbury commented 1 year ago

What is the existing way? As far as I could see there was no way to pass a seed to CPLEX. If there is an existing API that would be great.

pchtsp commented 11 months ago

Check https://coin-or.github.io/pulp/guides/how_to_configure_solvers.html#using-solver-specific-functionality

import pulp

x = pulp.LpVariable('x', lowBound=0)
prob = pulp.LpProblem('name', pulp.LpMinimize)
prob += x

solver = pulp.CPLEX_PY()
solver.buildSolverModel(prob)
# you can now edit the object or do something with it before solving
# for example, load a MIP_START file for CPLEX_PY:
solver.solverModel.MIP_starts.read(SOME_MST_FILE)
# you can also set parameters arbitrarily:
solver.solverModel.parameters.randomseed.set(42)
# the, you can call the solver to solve the problem
solver.callSolver(prob)
# finally, you fill the PuLP variables with the solution
status = solver.findSolutionValues(prob)