How do I check if a solution is feasible for a model in Gurobi? - gurobi

I have built my model, and I have a specific point which I would like to know if the point is a feasible solution to the constraints. Is there a command to do this?

Related

Best way to implement a primal heuristic that fixes certain Variables

I am using PySCIPOpt and have a MIP with some quadratic constraints (works). Now, I want to implement a Primal Heuristic (should run once before presolving), that fixes certain Variables and optimizes afterwards.
Ìn pseudo-code something like:
For x in ToFIX:
model.fixVar(x, my_guess(x))
model.optimize()
*Any found solution is used as solution of the original problem*
For x in ToFIX:
model.unFixVar(x)
I worked around that problem by creating a second model, solving that, identifying the variables by their name and using model.trySol().
This mostly works but is slow and certainly not the way it is meant to be implemented.
Any hint, which functionalities to use is appreciated.
sorry this took a while to answer.
What you want to implement is a sub-scip heuristic. This is certainly possible, but if you want to do it in PySCIPOpt you might have to wrap some missing methods from the C-API.
I suggest you take a look at heur_rens.c in the SCIP code. The methods you would need to wrap are probably SCIPcopyLargeNeighborhoodSearch and SCIPtranslateSubSol which should save you a lot of trouble. Please refer to the section extending the interface in the PySCIPopt Readme.

OptaPlanner - Create a good, initial solution by hand

Is there a way to create an initial solution for OptaPlanner by hand, in Java code?
I know that I can write the constructionHeuristic in the config XML to create a good initial solution.
But if I can write a better initial solution than OptaPlanner founds, is there a way to start the planning from my solution?
Thanks,
Vilmos
There are several ways of giving OptaPlanner a custom solution:
You can generate any Solution you like ahead of time. If the solution you give to solver.solve(solution) is already initialized at the time, you do not need to call construction heuristics at all.
If you instead wish to take an uninitialized solution and initialize it yourself in OptaPlanner, look into CustomPhaseCommand. Several OptaPlanner examples use that technique, see Machine Reassignment as an example. (However, beware that this is not a public API and we therefore do not guarantee its long-term stability.)

SCIP using old code

I am kind of new to the SCIP. I want to use SCIP as a branch and price framework. I have coded the problem in C++ already and also have implemented the pricer or column generation as a function. In fact I have implemented the BP algorithm for the root node by linking Cplex.dll to the project and now need to code the branching tree and decided to use SCIP for this purpose.
I want to know what is the fastest way I can solve my problem using SCIP and the old codes which I have? Or maybe using GCG is a better and faster way?
I have read the GCG documentation but doesn't understand if I should implement the pricer myself again or not? In fact I don't understand the difference between these two (SCIP and GCG).
Thanks.
In GCG, you do not need to implement anything yourself. It is a generic solver for branch-and-price. You have to provide the compact formulation, that is, a model which after applying a Dantzig-Wolfe reformulation leads to the master problem you are solving. The reformulation also provides a MIP-formulation of the pricing problem, so GCG can solve this as a sub-MIP for pricing. There is the possibility, however, to plug-in a pricing solver in GCG, to which the pricing MIP to be solved will be passed (with objective function corresponding to the current pricing round). The pricing solver can then solve this problem with any problem-specific algorithm and pass solutions back to GCG.
In SCIP, on the other hand, you create the master problem you want to solve and implement a pricer which gets dual values from the LP and solves the corresponding pricing problem. This is probably very similar to what you have already.
Additionally, if you want to do branch-and-price, you need a branching rule. GCG comes with some generic ones, in SCIP you would have to implement one yourself (since the branching decisions must be regarded within your pricing procedure).
Overall, SCIP is a framework for branch-and-price, i.e., it provides the tree management, LP solving and updates, etc., but you need to implement some things yourself like a reader, the pricer, and the branching rules. GCG is a generic solver, so you can just plug in a compact model, which is reformulated and solved in a generic way. The reformulation is either provided by you via an input file or you can try to let GCG detect an appropriate structure. You do not need to implement anything. It already provides some nice features like primal heuristics that make use of the reformulation, an automatic management of which pricing problem is solved when, and more. On the other hand, the possibilities to extend it further, e.g., by a pricing solver and branching rules are restricted compared to SCIP, since you have to stick to the structure defined by GCG.
I would say that using SCIP and adding your pricer is probably the easier way and more similar to what you already have (you do not need to formulate the compact model). If you already have an idea on how your branching should work, it should also not be too hard to implement within SCIP.

Dynamic constraint satisfaction in OptaPlanner

I am evaluating OptaPlanner for a planning problem I have. I have seen several responses to this topic, but nothing quite like I am looking for.
I am looking for the capability to extend the problem on the fly; that is, as the planner is solving a problem.
For example, in the CloudComputing example, I would like to be able to add computers on the fly (to a point) while the problem is being solved. The easiest case is that the problem is initially over-constrained and to resolve this I would like to be able to add computers, and then replan.
Or, I would like to be able to add a lecture, or a lecturer in one of the scheduling problems, etc.
It seems like the OptaPlanner requires a static number of entities / variables at solve time.
Any pointers would be appreciated.
Take a look at the Real-time planning section of the OptaPlanner User Guide.
You could also look at the Travelling Salesman Problem example in optaplanner-examples. Specifically, look at the org.optaplanner.examples.tsp.swingui.TspPanel class and traverse down from there. It's a pretty standard implementation of real-time planning AFAIK. I can also recommend to run the TSP example first to "see" how it works.

How can I decide when to use linear programming?

When I look at optimization problems I see a lot of options. One is linear programming. I understand in abstract terms how LP works, but I find it difficult to see whether a particular problem is suitable for LP or not. Are there any heuristics that can help guide this decision?
For example, the work described in Is there a good way to do this type of mining? took weeks before I saw how to structure the problem correctly. Is it possible to know "in advance" that problem could be solved by LP, without first seeing "how to phrase it"?
Is there a checklist I can use to decide whether a problem is suitable for LP? Is there a standard (readable) reference for this topic?
Heuristics (and/or checklists) to decide if the problem at hand is really a Linear Program.
Here's my attempt at answering, and I have also tried to outline how I'd approach this problem.
Questions that indicate that a given problem is suitable to be formulated as an LP/IP:
Are there decisions that need to be taken regularly, at different time intervals?
Are there a number of resources (workers, machines, vehicles) that need to be assigned tasks? (hours, jobs, destinations)
Is this a routing problem, where different "points" have to be visited?
Is this a location or a "layout" problem? (Whole class of Stock-cutting problems fall into this group)
Answering yes to these questions means that an LP formulation might work.
Commonly encountered LP's include: Resource allocation.: (Assignment, Transportation, Trans-shipment, knapsack) ,Portfolio Allocation, Job Scheduling, and network flow problems.
Here's a good list of LP Applications for anyone new to LPs or IPs.
That said, there are literally 1000s of different types of problems that can be formulated as LP/IP. The people I've worked with (researchers, colleagues) develop an intuition. They are good at recognizing that a problem is a certain type of an Integer Program, even if they don't remember the details, which they can then look up.
Why this question is tricky to answer:
There are many reasons why it is not always straightforward to know if an LP formulation will cut it.
There is a lot of "art" (subjectivity) in the approach to modeling/formulation.
Experience helps a lot. People get good at recognizing that this problem can be "likened" to another known formulation
Even if a problem is not a straight LP, there are many clever master-slave techniques (sub-problems), or nesting techniques that make the overall formulation work.
What looks like multiple objectives can be combined into one objective function, with an appropriate set of weights attached.
Experienced modelers employ decomposition and constraint-relaxation techniques and later compensate for it.
How to Proceed to get the basic formulation done?
The following has always steered me in the right direction. I typically start by listing the Decision Variables, Constraints, and the Objective Function. I then usually iterate among these three to make sure that everything "fits."
So, if you have a problem at hand, ask yourself:
What are the Decision Variables (DV)? I find that this is always a good place to start the process of formulation. How many types of DV's are there? (Which resource gets which task, and when should it start?)
What are the Constraints?
Some constraints are very readily visible. Others take a little bit of teasing out. The constraints have to be written in terms of your decision variables, and any constants/limits that are imposed.
What is the Objective Function?
What are the quantities that need to be maximized or minimized? Note: Sometimes, it is not clear what the objective function is. That is okay, because it could well be a constraint-satisfaction problem.
A couple of quick Sanity Checks once you think your LP formulation is done:
I always try to see if a trivial solution (all 0s or all big
numbers) is not part of the solution set. If yes, then the
formulation is most probably not correct. Some constraint is
missing.
Make sure that each and every constraint is "related"' to
the Decision Variables. (I occasionally find constraints that are
just "hanging out there." This means that a "bookkeeping constraint"
has been missed.)
In my experience, people who keep at it almost always develop the needed intuition. Hope this helps.