When I tried to use piecewise linear (PWL) function in Python, after I set the problem type to cplex.Cplex.problem_type.LP, the PWL cpnstraint (or function) disappear when I write out the model to the file. However if I specified the problem type is cplex.Cplex.problem_type.MILP, it said that no solution, although there exist solution in LP. So I want to ask if someone face this problem before and how to solve it
The behavior you describe is expected. PWL constraints are considered MILP modeling objects. When you change the problem type to an LP, the MILP modeling objects are removed. Given that the LP is feasible, it sounds like your PWL constraint has introduced a conflict or there is something wrong with the definition of the PWL.
What exactly is the solution status code that you are getting when solving the MILP? That is, what is the value of Cplex.solution.get_status()? What does the engine log contain? These will likely give you some useful information.
In case you haven't found it, the CPLEX User's Manual contains a section on piecewise linear constraints here. See also the transport.py example that is included when you install CPLEX.
Related
I need to run a model, where I optimise a diet within a set of constraints and call all integer solutions in the end. I have found a diet example matching almost what I need here: hakank.org. However, in my case, my variables take continuous values, so in the examples this would be all the nutritional values and the cost, while only x take integer. However, it seems like I can only define either 'intvar' or 'boolvar' when defining by variables with this model. Is there a way to overcome this? Other would there be other more suitable models with examples that I can read online?
I'm new to constraint programming, so any help would be appreaciated!
Thanks.
Most Constraint Programming tools and solvers only work with integers. That is where their strength is. If you have a mixture of continuous and discrete variables, it is a good idea to have a look at Mixed Integer Programming. MIP tools and solvers are widely available.
The diet model is a classic example of an LP (Linear Programming) Model. When adding integer restrictions, you end up with a MIP model.
To answer your question: CPMpy does not support float variables (and I'm not sure that it's in the pipeline for future extensions).
Another take - than using MIP solvers as Erwin suggest - would be to write a MiniZinc (https://www.minizinc.org/) model of the problem and use some of its solvers. See my MiniZinc version of the diet problem: http://hakank.org/minizinc/diet1.mzn. And see the MiniZinc version of Stigler's Diet problem though it's float vars only: http://hakank.org/minizinc/stigler.mzn.
There are some MiniZinc CP solvers that also supports float variables, e.g. Gecode, JaCoP, and OptimathSAT. However, depending on the exact constraints - such as the relation with the float vars and the integer vars - they might struggle to find solutions fast. In contrast to some MIP solvers, generating all solutions is one of the general features of CP solvers.
Perhaps all these diverse suggestions more confuse than help you. Sorry about that. It might help if you give some more details about your problem.
I am actually running a MILP on Python using DOcplex and it is my very first time using it. It is saying that my model which I ran on Lindo is infeasible. Therefore, I am guessing that I erroneously entered my constraints, but I cannot view them. I used "mdl.print_information()", but it is not helping much. Does anyone know how I can solve my problem?
Thanks in advance.
You can export the model to LP format (a human readable LP format), by Model.export_as_lp(). You can also export the model to a string in LP format with Model.lp_string, to get a complete view of the model.
In addition, this notebook: https://github.com/IBMDecisionOptimization/docplex-examples/blob/master/examples/mp/jupyter/infeasible.ipynb
gives hints on how to investigate infeasible models, in particular with the Relaxer class.
Knowing which constraint is infeasible (and by how much slack) may lead you to the root cause of the infeasibility.
I've a MIP to solve with Pyomo and I want to set an initial solution for cplex.
So googling I find that I can set some variable of instance to some value and then execute this:
solver.solve(instance,warmstart=True,tee=True)
But when I run cplex it seem that it doesn't use the warm start, because for example i pass a solution with value 16, but in 5 seconds it return a solution with value 60.
So I don't know there is some error or other stuff that doesn't work.
P.S.
I don't know if is a problem but my warm start solution set only some variale to a value, but not all. Could be a problem?
Make sure that the solution you give to CPLEX is feasible. Otherwise, CPLEX will reject it and start from scratch.
If your solution is feasible, it is possible that CPLEX simply found a better solution than yours, since, after all, it is CPLEX's job, and in my own experience, CPLEX is very good at it. Is this a maximization problem? If so, in your example, CPLEX found a better solution (objective=60) than yours (objective=16), which is the expected behavior.
Sadly, CPLEX is often greedy in term of verbose, so it is hard to know from the solver log if warmstart was used or not (unlike its competitor GUROBI where it is clearly written in the log). However, it seems like you started the warmstart correctly, using the warmstart=True parameter.
If, however, your problem isn't a maximization problem, it is possible that CPLEX will not make a differenciation between the variables that you gave a value and the variable that still holds a solution from last solve. Plus, giving values to only a fraction of your variables might make the problem infeasible, considering that all values not manually specified are the values previously found by CPLEX. ex: contraint x<=2y. The solver found x=2, y=1 as a feasible solution. You define x:=3, then your constraint is not respected (y is still =1 for CPLEX, so the constraint x<=2y is 3<=2, which is false). CPLEX will see it as infeasible and will reject your solution.
One alternative that I can give you, if you absolutely want to use your own values in the final solution, is instead of defining values for your variables, create a constraint that explicitly defines your variable value. This constraint can afterward be "deactivated" if needed. But be careful, as this does not necessarily yield the optimal solution, but the "optimal solution when some variables have the specific value".
I have a small size linear program with restrictions Ax<=b and x>=0. To solve it I've been using the Python library scipy.optimize.linprog.
From my n variables, I need to know which ones form the basis.
I searched a lot and couldn't find a way for this. I also tried the Pulp library without success.
I'm interested in degenerate instances, so looking for positive variables is not enough; it is likely that some zero variable is basic.
Do you know a way to get this information? I can use any library, but it has to be on Python.
Thanks!
There is almost no LP solver that does not return arrays/vectors containing the basis status of each variable and each row (or slack). Unfortunately scipy.optimize.linprog is the exception. I am sure if you look at the source you can locate basis status arrays. Pulp is not a solver but a modeling tool. I am not sure what solution info Pulp provides. But you can always generate an MPS file with PULP and feed that in any LP solver.
If the solution is non-degenerate you can do something like
x(j)>0 => basic
x(j)=0 => non-basic
s(i)>0 => slack is basic
s(i)=0 => slack is non-basic
However if the solution is degenerate we have some basic variables/slacks with value zero.
I am solving a series of LP problems using the CPLEX Python API.
Since many of the problems are essentially the same, save a hand full of parameters. I want to use a warm start with the solution of the previous problem for most of them, by calling the function cpx.start.set_start(col_status, row_status, col_primal, row_primal, col_dual, row_dual) where cpx = cplex.Cplex().
This function is documented here. Two of the arguments, col_status and row_status, are obtained by calling cpx.solution.basis.get_col_basis() and cpx.solution.basis.get_row_basis().
However, despite cpx.solution.status[cpx.solution.get_status()] returning optimal and being able to obtain both cpx.solution.get_values() and cpx.solution.get_dual_values() ...
Calling cpx.solution.basis.get_basis() returns CPLEX Error 1262: No basis exists.
Now, according to this post one can call the warm start function with empty lists for the column and row basis statuses, as follows.
lastsolution = cpx.solution.get_values()
cpx.start.set_start(col_status=[], row_status=[],
col_primal=lastsolution, row_primal=[],
col_dual=[], row_dual=[])
However, this actually results in making a few more CPLEX iterations. Why more is unclear, but the overall goal is to have significantly less, obviously.
Version Info
Python 2.7.12
CPLEX 12.6.3
I'm not sure why you're getting the CPXERR_NO_BASIS. See my comment.
You may have better luck if you provide the values for row_primal, col_dual, and row_dual too. For example:
cpx2.start.set_start(col_status=[],
row_status=[],
col_primal=cpx.solution.get_values(),
row_primal=cpx.solution.get_linear_slacks(),
col_dual=cpx.solution.get_reduced_costs(),
row_dual=cpx.solution.get_dual_values())
I was able to reproduce the behavior you describe using the afiro.mps model that comes with the CPLEX examples (number of deterministic ticks actually increased when specifying col_primal alone). However, when doing the above, it did help (number of det ticks improved and iterations went to 0).
Finally, I don't believe that there is any guarantee that using set_start will always help (it may even be a bad idea in some cases). I don't have a reference for this.