 # Solving Optimization Problem

You Don’t Write a Code for Solving Your Problem

Instead you may simply call JavaSolver’s methods minimize(), maximize(), or solve(). The introductory example shows how its main method calls define() the ProblemZoo and then calls minimize() to find an optimal solution:

Thus, with Java Solver you only concentrate on problem definition and rely on the standard problem resolution.

Minimize/Maximize Optimization Objective

If you specified an optimization objection using the method setObjective(var),  you may simply call the standard JavaSolver’s method minimize() to find a solution that minimizes this objective. Similarly you may call maximize() to maximize it.

Finding One Solution

If you simply want to find a feasible solution that satisfies all posted constraints without optimization, you may call the standard JavaSolver’s method solve().

Finding Many Solutions

If you simply want to find a feasible solution that satisfies all posted constraints without optimization, you may call the standard JavaSolver’s method solveAll().

Limiting Time for Solution Search

If you want to limit time available to your optimization model to find optimal or feasible solutions, you may call the standard JavaSolver’s method setTimeLimit(seconds). Let’s say you define a time limit as 60. In this case Java Solver will try to find within 60 seconds the most optimal solution (for minimize/maximize) or at least one feasible solution and will stop its execution when the time expires. It may find no solution if there is not enough time (or if your problem is over-constrained).

Limiting Maximal Number of Solutions

You may direct Java Solver to consider no more than a certain number of solutions using the standard method setMaxNumberOfSolutions(int maxSolutions).

Saving Found Solutions

By default, Java Solver simply displays a found solution (s) or informs you that “No Solutions Found”. It’s done in the standard method saveSolution(Solution solution). You may overload this method to save the found solution(s) into your own Java objects.

The methods minimize(), maximize(), and solve() return one object of the type Solution which is described in the JSR-331 User Manual. The Solution’s method getValue(name) returns a found value of the variable “name”. Here is an example of an overloaded saveSolution(): This function receives a solution found by the above JavaSolver’s methods such as  solve() or minimize().  First the solution logs (displays) itself. Then it iterates through all defined integer constrained variables and logs their names and found values of constrained variables saved in the solution. Instead of logging you may save the found values to your business objects.

When the method solveAll() finds many solutions it automatically calls the method saveSolutions(Solution[] solutions): 