I am currently working on modelling a certain MIP in gurobi (working in java).
This problem requires a lot of computational time and I don't want to waste more than 100s on each problem. However, when this time limit is reached, I want gurobi to print the best feasable solution it has found within these 100s. I tried using the attribute ObjBound, but this always gives me a lower objective than when I let gurobi find the optimale solution (my problem is a minimization problem). So I'm guessing here that this ObjBound gives a lower bound, while I'm trying to find an upper bound.
Is there any way I can accomplish this?
If you are simply looking to get any feasible solution (not the optimal, just feasbile), set the MIPFocus parameter to 1. This tells gurobi to concentrate on finding a feasible solution rather than an optimal solution.
model.setParam('MIPFocus', 1)
model.optimize()
Related
I was exploring the CP-SAT APIs for fetching all solution for given set of constraints.
As per the API documentation, onSolutionCallback() function is called for every solution found. However if I need to find all solutions for a given model, is there a way to detect the last solution or the feasibility of no more solutions through onSolutionCallback() function or other means?
I found that searchAllSolutions() API can be used and we can set termination conditions based on time or number of solutions. Assuming I can wait unlimited amount of time, how do I detect that there are no more solutions feasible?
https://developers.google.com/optimization/cp/cp_tasks
Another related question:
Is there any remote chance for a CP SAT solver to run into a non-deterministic state or run into infinite loops (or such) even when there is a feasible solution possible for given set of constraints?
I plan to use the CPSAT for a production usecase and hence would like to know its determinism and upper bounds for execution.
Edit: Added the second question.
I have a few questions about my genetic algorithm and GAs overall.
I have created a GA that when given points to a curve it tries to figure out what function produced this curve.
An example is the following
Points
{{-2, 4},{-1, 1},{0, 0},{1, 1},{2, 4}}
Function
x^2
Sometimes I will give it points that will never produce a function, or will sometimes produce a function. It can even depend on how deep the initial trees are.
Some questions:
Why does the tree depth matter in trying to evaluate the points and
produce a satisfactory function?
Why do I sometimes get a premature convergence and the GA never
breaks out if the cycle?
What can I do to prevent a premature convergence?
What about annealing? How can I use it?
Can you take a quick look at my code and tell me if anything is obviously wrong with it? (This is test code, I need to do some code clean up.)
https://github.com/kevkid/GeneticAlgorithmTest
Source: http://www.gp-field-guide.org.uk/
EDIT:
Looks like Thomas's suggestions worked well I get very fast results, and less premature convergence. I feel like increasing the Gene pool gives better results, but i am not exactly sure if it is actually getting better over every generation or if the fact that it is random allows it to find a correct solution.
EDIT 2:
Following Thomas's suggestions I was able to get it work properly, seems like I had an issue with getting survivors, and expanding my gene pool. Also I recently added constants to my GA test if anyone else wants to look at it.
In order to avoid premature convergence you can also use multiple-subpopulations. Each sub-population will evolve independently. At the end of each generation you can exchange some individuals between subpopulations.
I did an implementation with multiple-subpopulations for a Genetic Programming variant: http://www.mepx.org/source_code.html
I don't have the time to dig into your code but I'll try to answer from what I remember on GAs:
Sometimes I will give it points that will never produce a function, or will sometimes produce a function. It can even depend on how deep the initial trees are.
I'm not sure what's the question here but if you need a result you could try and select the function that provides the least distance to the given points (could be sum, mean, number of points etc. depending on your needs).
Why does the tree depth matter in trying to evaluate the points and produce a satisfactory function?
I'm not sure what tree depth you mean but it could affect two things:
accuracy: i.e. the higher the depth the more accurate the solution might be or the more possibilities for mutations are given
performance: depending on what tree you mean a higher depth might increase performance (allowing for more educated guesses on the function) or decrease it (requiring more solutions to be generated and compared).
Why do I sometimes get a premature convergence and the GA never breaks out if the cycle?
That might be due to too little mutations. If you have a set of solutions that all converge around a local optimimum only slight mutations might not move the resulting solutions far enough from that local optimum in order to break out.
What can I do to prevent a premature convergence?
You could allow for bigger mutations, e.g. when solutions start to converge. Alternatively you could throw entirely new solutions into the mix (think of is as "immigration").
What about annealing? How can I use it?
Annealing could be used to gradually improve your solutions once they start to converge on a certain point/optimum, i.e. you'd improve the solutions in a more controlled way than "random" mutations.
You can also use it to break out of a local optimum depending on how those are distributed. As an example, you could use your GA until solutions start to converge, then use annealing and/or larger mutations and/or completely new solutions (you could generate several sets of solutions with different approaches and compare them at the end), create your new population and if the convergence is broken, start a new iteration with the GA. If the solutions still converge at the same optimum then you could stop since no bigger improvement is to be expected.
Besides all that, heuristic algorithms may still hit a local optimum but that's the tradeoff they provide: performance vs. accuracy.
I am still using the OptaPlanner to optimize a chained planning problem which is similar to the VehicleRoutingExample. My planning entities have a planning variable which is another planning entity. The scores are HardSoftScores.
I have initialized an IncrementalScore to solve my runtime-problem, but something isn’t working right and I wasn’t able to find the reason why. (with debugging, system.out… ect. ...)
The Problem is that the Solver doesn’t create a feasible solution of my testing dataset by using the IncrementalScore. I know that it is possible to find a feasible solution because the solver created one with my EasyScore.
The optimal solution created by the EasyScore gives me the score: (0hard/-151763soft) when the IncrementalScore is solving I get some strange values.
The score given by solver.getBestsolution().getScore() is (-25hard/-207111soft), when I instantiate an extra IncrementalScoreCalculator and let it calculate a Score of the bestSolution, I get the values (-34hard/-207111soft). I checked the solution and found out, that the first values are just wrong, but the second ones are “correct” (but not feasible).
Does someone has an idea where the mistake is? Or are there some other ways to find the reason?
In the manual see chapter "5.3.6. Invalid score detection" to find out when the easy and incremental score calculation go out of sync.
<environmentMode>FULL_ASSERT</environmentMode>
...
<scoreDirectorFactory>
<scoreDefinitionType>...</scoreDefinitionType>
<incrementalScoreCalculatorClass>...IncrementalScoreCalculator</incrementalScoreCalculatorClass>
<assertionScoreDirectorFactory>
<easyScoreCalculatorClass>...EasyScoreCalculator</easyScoreCalculatorClass>
</assertionScoreDirectorFactory>
</scoreDirectorFactory>
Then comment out constraint type's in both implementation to zero in on the constraint that's implemented differently. It looks like it's one of your hard constraints that's calculating invalidly.
Let's say I want to build a function that would properly schedule three bus drivers to drive in a week with the following constraints:
Each driver must not drive more than five times per week
There must be two drivers driving everyday
They will rest one day each week (will not clash with other drivers' rest day)
What kind of algorithm would be used to solve a problem like this?
I looked through several sites and I found these:
1) Backtracking algorithm (brute force)
2) Genetic algorithm
3) Constraint programming
Frankly, these are all "culture shock" for me as I have never learnt any kind of linear programming in the past. There are two things I want to know:
1) Which algorithm will best suit the case scenario above?
2) What would be the simplest algorithm to solve this problem?
3) Please suggest any other algorithms I can look into to solve the above problem.
1) I agree brute force is bad.
2) Your Problem is an Integer Problem. They can be solved with Linear Programming though.
3) You can distinquish 2 different approaches: heuristics and exact approaches.
Heuristics provide good solutions in reasonable computation time. They are used when there are strict requirements on the computation time or if the problem is too hard to calculate an optimal solution. Genetic Algorithms is a heuristic.
As your Problem is comparably simple, you would probably go with an exact approach.
4) The standard way to solve this exacly, is to embed a Linear Program in a Branch & Bound search tree. There is lots of literature on it. The procedure can be outlined as follows:
Solve the Linear Program with the Simplex-Algorithm
Find a fractional variable for branching. I.e. x=1.5
Create two new nodes and add the constraints x<=1 and x>=2 respectively
Go into one node (selected by some strategy)
Go to point 1
Additionally, at every node in the tree, after point 1, the algorithms checks, if a node can be pruned. That means to stop searching 'deeper' from this node on, because
a) the problem has become infeasible,
b) a better solution already exists,
c) an integer solution is found. This objective value of this solution is used to determine point b.
The procedure finishes when all nodes are pruned.
Luckily, as Nicolas stated, there are free implementations that do just this. All you have to do is to create your model. Code its objective and constraints in some tool and let it solve.
First of all this is a discrete optimization problem, so linear programming is probably not a good idea (since it is meant for continuous optimization). You can still solve this using linear programming (it will become an integer or mixed-integer program) but that is exponentially heard (if your input size is small then it is ok).
Now back to the comparison:
Brute force : worst.
Genetic: Can not guarantee optimality. The algorithm may not be able to solve the problem.
Constraint programming: definitely the best in this case (and in many discrete optimization problems). There is a super efficient implementation of it in IBM ILOG CPLEX solver (but is is not free, it is free for academia or for testing though).
I can't find a way to efficiently use MIP starts in CPLEX java API.
I have a linear problem that I need to solve many times by changing just one constraint or changing the objective so I thought that starting from a solution (with MIP starts) could be a good way to speed up the calculations.
So in order to do that, after the first time I solve the problem, I save all variables in an array of IloNumVar and double that I pass to my other cplex objects with cplex.addMIPStart.
The problem is it doesn't speed up anything it makes it slower and gives me this message :
Warning: No solution found from 1 MIP starts.
So maybe I shouldn't give the MIP start all the variables but how do I know what variables to give it ?
I also tried to change the MIP start effort but it does not seem to make any difference ...
Why doesn't it make calculations faster ? Is there a better way to solve many problems that have just a few differences ?
This message usually means that you either haven't specified values for enough decision variables in your model, or the values you have given to cplex aren't feasible. You can check feasibility by using IloNumVar.setBounds on the variables then trying to solve the model. If that comes up infeasible, then you can write an iis file.
CPLEX tries, but isn't able to make use of your mipstart, so it runs slower. A good MIP start can improve the solution time dramatically, especially if cplex has a hard time finding a first-feasible solution and your MIP start has an objective function value that is close to optimal, but for many instances, it doesn't make any difference. Warm starting MIPs is much harder than warm-starting LPs.