CPLEX12.9: Strong branching is not available for mixed-integer problems? - python

I want to get strong branching scores by cplex and python, and for the first step I just tried to use "cplex.advanced.strong_branching" to solve a very simple MILP problem (my code followed the example usage of this function exactly). However it told me that "CPLEX Error 1017: Not available for mixed-integer problems", which made me quite confused because SB should be a traditional branch-and-bound algorithm. But when I used it to solve a LP problem it worked well.
The error seemed to be raised from "CPXXstrongbranch", a base C/C++ API, which also made me question that how cplex could make SB decisions when I set the branching strategy parameter to SB. A similar question is that I know Python API doesn't have the important "CPXgetcallbacknodelp" function, so how could "cplex.advanced.strong_branching" work? Could it be the reason of this error?
I don't totally understand how "CPXstrongbranch" works in C, so the following information may be incorrect: I tried to use "CPXstrongbranch" in the user-set branch callback of the example "adlpex1.c", and the same error was raised; it stopped me to use "ctypes" to get the "CPXgetcallbacknodelp" function.
Could it be a version problem? Does Cplex block the access of SB? Because I have read a paper which relied on the SB scores in Cplex 12.6.1 and C API. Or I just made some mistakes.
My question is whether Cplex can do SB and deliver its results to users in the MILP problem.

cplex.advanced.strong_branching does not carry out any branching. The doc is a bit confusing here. What this function does is computing the strong branching scores for the variables you pass (or all variables if you don't pass a list).
This requires an LP because usually in a MIP search tree you call this function with the current LP relaxation.
If you want to use CPLEX with strong branching then set the variable selection parameter to "strong branching":
with cplex.Cplex() as cpx:
cpx.parameters.mip.strategy.variableselect.set(cpx.parameters.mip.strategy.variableselect.values.strong_branching)
cpx.solve()
The strong_branching function is only needed if you want to implement your own branching algorithm.

Related

Where in Pyomo/PyROS do I find the interface (APPSI) for ‘baron’ solver?

I followed this example in the PyROS docs: https://pyomo.readthedocs.io/en/stable/contributed_packages/pyros.html?highlight=pyros#step-3-solve-with-pyros
And I used the following solvers:
import pyomo.environ as pe
local_solver = pe.SolverFactory('appsi_ipopt')
global_solver= pe.SolverFactory('appsi_ipopt')
(which, by the way, is not exactly what’s in the example – see related bug here https://github.com/Pyomo/pyomo/issues/2428). However, my problem is non-convex and mixed integer non-linear. I saw in the source code that you need a solver of the class APPSIBaseClass: https://pyomo.readthedocs.io/en/stable/library_reference/appsi/appsi.base.html But I can’t find “baron” in the solvers under pyomo/contrib/appsi/, which is where I would expect to find it.
How do I find the APPSI for the ‘baron’ solver? pe.SolverFactory('baron') does not work (again - see related bug https://github.com/Pyomo/pyomo/issues/2428). Or is there another solver that I can use to solve an MINLP. PyROS touts that it can solve MINLP’s so I would imagine there is a way to do this.

How to use the LazyConstraintCallback in the CPLEX Python API to solve a MILP problem through Benders' decomposition

I have a MILP problem with binary and continuous variables. I want to use the Benders' decomposition algorithm to solve it. I am following this presentation: http://www.iems.ucf.edu/qzheng/grpmbr/seminar/Yuping_Intro_to_BendersDecomp.pdf
I separate my problem into a master problem composed of the discrete variables and a slave problem that has the continuous variables.
I am using the CPLEX Python API to solve it, based on the ATSP example: https://github.com/cswaroop/cplex-samples/blob/master/bendersatsp.py
I create the master problem and the dual of the slave as in this example.
I use the BendersLazyConsCallback, but I am not sure I understand it entirely. Please help me through.
The function separate is called when the current master solution is obtained, then the dual objective function is updated, and the dual problem is re-solved.
If the dual is unbounded then it adds the ray to the master problem constraints, e.g., self.add(constraint = workerLP.cutLhs, sense = "L", rhs = workerLP.cutRhs), which happens in the BendersLazyConsCallback class.
But the example does not include the code when the dual is optimal.
So, when the dual is optimal, I add a similar call and add the constraint to the master problem based on the dual solution.
However, if I try to print the master problem constraints, e.g., problem.linear_constraints.get_rows(), I do not see the newly included constraints. It seems that the self.add(constraint = workerLP.cutLhs, sense = "L", rhs = workerLP.cutRhs) command does not push this to the master constraints but keeps it as a member of the LazyConstraintCallback class. Is this correct? How can I see that these new constraints are actually added?
Also, how does the algorithm stop? In the traditional Benders' algorithm the lower and upper bounds of the problem are updated based the the dual and master solution and when they are equal we have convergence.
In the ATSP example, I don't see where this is happening. When exactly is the BendersLazyConsCallback triggered and how does it know when to stop?
The example that you linked to above is from CPLEX 12.6. This is quite old, by now (currently, the latest version is 12.9). If you haven't updated to a more recent version yet, it would probably be a good idea to do so. One reason to update is that with CPLEX 12.7, there is support for automatic Benders' decomposition. With CPLEX 12.8, there is a new generic callback (and a new bendersatsp2.py example to demonstrate it).
With that said, I'll try to answer some of your other questions.
First, as described here, if you write out the model, it will not include the lazy constraints that you have added dynamically in the callback. You can print them out in the callback yourself easily enough (e.g., print("LC:", workerLP.cutLhs, "G", workerLP.cutRhs). You can determine if your constraints have been applied, by the presence of a message at the end of the engine log, like:
User cuts applied: 3
As to your final questions about how lazy constraints work, please see the section on legacy callbacks in the CPLEX User's Manual. Also, there is the section on termination conditions of the MIP optimizer. The lazy constraint callback is invoked whenever CPLEX finds an integer feasible solution (see this thread on the IBM developerWorks forum for more on that).

Genetic Algorithm for Hybrid Flowshops Scheduling using Python

I apply optimization tool to solve pratical production planning problem.
My current problem is doing planning for a factory with various items in a unique production flow stage. In each stage, there are few parallel machines as graph below.
I have done maths MILP model, and try to solve by CPLEX but it too hard to handle the big scale model by itself.
Currently, I prepare to use Genetic Algorithm to solve it, but don't know where to start.
I have some knowdlege in Python Language. My friends, please advise how I start to deal with this problem?
Do someone have a similar solved problem with code, that I can have a reference?
Before you start writing code(or using someone else's) you must understand the theory behind the scene.
What are the main entities of your Production System?
You need to formulate optimization objectives,optimal schedule but defined in terms of optimization problem.
One example that comes to my mind
https://github.com/jpuigcerver/jsp-ga
Take a look at this thesis
http://lancet.mit.edu/~mbwall/phd/thesis/thesis.pdf

Saving the "state" of a Z3 solver in SMT2 format

is it possible, using the Z3 API (e.g. the Python API), to save the current state of a solver, including what the solver has learned (in SAT solving we would say the "learned clauses") in a file in SMT2 format?
Because I would like to be able to save the state of the solver in a temporary file in order to resume solving later, in order to have some time to understand what further queries I should make to it.
Many thanks in advance...
SMT2 has no provisions of saving a given solvers state, which will no doubt differ widely from solver to solver. Each solver might have different mechanisms of doing so, however, but it will definitely not be in SMTLib2 format.
Since your question is entirely Z3 specific, I recommend asking it on https://github.com/Z3Prover/z3/issues to see if they might have anything interesting. So far as I know, however, this isn't possible currently.
At the end Levent was right :)
Below are some observations by Nikolaj Bjorner, from the Z3 github website.
"The state of the solver isn't fully serializable to SMT2 format.
You can print the solver to smt2 format based on the current assertions,
but not learned clauses/units using the sexpr() method on the Solver object."
...
"We don't expose ways to print internal state. You could perhaps interrupt the solver, then clone it using the "translate" methods and access the translated solver state using internal print utilities. You would have to change the code a bit to get to this state.
The print features on solvers don't access the internal state of any of the solvers, instead they look at the asserted formulas and print them.
I don't translate learned lemmas. For example, the code in smt_context.cpp line 176 is disabled because it didn't help with any performance enhancements. Similarly, the copy code in sat_solver does not copy learned clauses even though it retains unit literals and binary clauses that are learned."
You can see the above comments by Nicolaj at this link.

Nash equilibrium in Python

Is there a Python library out there that solves for the Nash equilibrium of two-person zero-games? I know the solution can be written down in terms of linear constraints and, in theory, scipy should be able to optimize it. However, for two-person zero-games the solution is exact and unique, but some of the solvers fail to converge for certain problems.
Rather than listing any of the libraries on Linear programing on the Python website, I would like to know what library would be most effective in terms of ease of use and speed.
Raymond Hettinger wrote a recipe for solving zero-sum payoff matrices. It should serve your purposes alright.
As for a more general library for solving game theory, there's nothing specifically designed for that. But, like you said, scipy can tackle optimization problems like this. You might be able to do something with GarlicSim, which claims to be for "any kind of simulation: Physics, game theory..." but I've never used it before so I can't recommend it.
There is Gambit, which is a little difficult to set up, but has a python API.
I've just started putting together some game theory python code: http://drvinceknight.github.com/Gamepy/
There's code which:
solves matching games,
calculates shapley values in cooperative games,
runs agent based simulations to identify emergent behaviour in normal form games,
(clumsily - my python foo is still growing) uses the lrs library (written in C: http://cgm.cs.mcgill.ca/~avis/C/lrs.html) to calculate the solutions to normal form games (this is I believe what you want).
The code is all available on github and that site (the first link at the beginning of this answer) explains how the code works and gives user examples.
You might also want to check out 'Gambit' which I've never used.

Categories