Detecting termination in CP-SAT - java

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.

Related

Can I use Optaplanner to locally optimize the solution I input without changing most of the sequence?

At present, I have established my business model by referring to the tsp problem. The part I input is an ordered data set (an initial solution can be regarded as a route of tsp). This initial solution is compiled by the production scheduling system, so most of the routes are reasonable, and there may be a few unreasonable places. According to the configured constraint rules, I can find the violation of the solution constraint (that is, unreasonable place) through ScoreManager.
My question is, can I continue to use solver for local optimization? The ideal situation is to keep most of the sequence of the original plan, and only fine-tune the unreasonable part of the plan to satisfy all constraints.
ps:My hard constraints are some production process constraints, for example, the width difference between adjacent slabs does not exceed xxx
The soft constraint is that the smaller the sum of the difference between adjacent slabs, the better.
There is no guarantee that, in order to satisfy all constraints, only the "unreasonable" part of the plan will have to change. The solver may or may not still need to make significant changes.
That said, consider implementing non-disruptive replanning. If you penalize the number of changes between the original plan and the new one, you should be able to find a reasonable balance.

Objective bound when time limit is reached (GUROBI)

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()

Genetic Algorithm - convergence

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.

JSprit Algorithm soft and hard time windows

I have list of services with time windows. Is it possible to configure JSprit in way that some time windows must be met (hard time windows) while other are configured to be soft time windows?
Thanks in advance for help
Hard time windows can be implemented by assigning a time window to a service via Service.Builder. By default, services do not have any time windows. Soft time windows can be considered by implementing core.problem.constraint.SoftActivityConstraint
https://github.com/jsprit/jsprit/blob/master/jsprit-core/src/main/java/jsprit/core/problem/constraint/SoftActivityConstraint.java
Here, you can penalize "late" arrivals. Keep in mind that the insertion of a new activity does not only have a local impact, i.e. on the two neighboring activities, but it can have an impact on the whole route since it shifts all subsequent activities. This, in turn, can yield additional penalties that need to be considered as well. To evaluate this in constant time, you require some sort of approximation of the global impact. Once you add the soft constraint, you need to account for it in your objective function as well (see for example and its respective code examples to see how this works).

Implementing threads over an existing code

I am not a thorough JAVA professional but have experience in Programming, none with threads though. I have an application code which currently does the following.
Make connection with a DB
Pull records from DB into a collection (Each record has an 'action code' 1-5 besides other things
Each record is picked one by one and Based on each action code some particular method (One each for each action code ) is called from a class EVENTHANDLER.class
These individual methods also use/share some other methodsin EVENTHANDLER.class and some other classes for some common functionality that there is
Finally the db_sequence is updated
All records processed so finish
Now , I have a requirement, which is little vague right now, but it wants the introduction of threads into above for primarily a performance enhancement. Along with prioritizing the process of some records with some specific action code above the others. for example- A record with Action code -2 should be on high prority over 1 and then 3 and then 4.
My question is to how to go about first with the approach to implement this. Secondly this is to be done in JAVA 1.6 so what classes should I use. Also any direction codewise (example code) or based on functional flow above would be greatly helpful.
A very direct question is- for the above action code (1-5) should I have five threads running concurrently in whole or should I have one thread for each record (there can be hundreds), irrespective of Action Code?
Thanks Already
I'd be concerned if I were you or the person who asked you to do this.
Do you have numbers to show what the performance for the existing app is? If yes, do they exceed the target for the expected performance? I wouldn't make a judgment regarding threads until I had both.
Threading is an advanced topic that's easy to get wrong, even if you're experienced.
It sounds to me like the database portion can be a single thread. The handlers might be long-running, so I'd run those using Executor and the new constructs in the Java concurrency package. Under no circumstances should you do this with raw Threads.
It sounds to me like you'll need help. I'd find someone that knows Java better than you do to consult.

Categories

Resources