## JOM (Java Optimization Modeler)Getting started
## Getting started
- First download and install JOM.
- Then read the web page you are seeing now: you will go through the steps to solve an optimization problem with JOM.
- Then read the JOM Expression syntax page (click here).
- Use one of the examples (or other program) as a template to write your program, then
- Use the JAVADOC information for knowing the details of the (reduced) amount of calls needed to solve optimization problems with JOM. Javadocs of the releases contain the most up-to-date information and documentation.
- Use the JOM Expression syntax to solve the doubts about the syntax to write expressions in JOM.
As a tutorial, below we use the example in the home page to illustrate the main steps in solving an optimization problem with JOM.
## 1. Importing the librariesThe `com.jom`: This is the main package, and the only one you have to import in your Java program.`com.jom.javaluator`: JOM uses a slightly modified version of the Javaluator package to be able to parse the JOM expressions using a MATLAB-like syntax. You do NOT have to import this package, since your code does not access to it. In summary, you do not have to know anything about it, it is for JOM internals.`by.bsu.JVmipcl`: This package is includes the JNI interface for MIPCL solver. Copy-paste from the MIPCL package (LGPL license).
import you need in your Java program to use JOM functionalities is:
import com.jom.*; ## 2. Creating the OptimizationProblem ObjectTo be able to define and finally solve an optimization problem, you first have to create an OptimizationProblem op = new OptimizationProblem();
## 3. Adding decision variables to the problemThe decision variables in the problem can be added by calling the In our example, we create a decision variable with name "x", constrained to be integer, of size op.addDecisionVariable("x", true, new int[] { N , N , N }, 0, 1);
## 4. Setting the values to the input parametersIt is possible to assign arrayed constant values of any dimension to input parameters, to be used later in expressions, calling the op.setInputParameter("c", new DoubleMatrixND(new int [] { N , N , N} , "random")); Input parameters can also be useful for setting the values of notable constants like i.e. number \( \pi \), as shown in next line of code: op.setInputParameter("pi", 3.141592653589793); ## 5. Adding the objective functionThe method In our example, the objective is to maximimize the sum of all the values in the "x" decision variable element-by-element multiplied by their associated benefit given by the "c" array. op.setObjectiveFunction("maximize", "sum(x .* c)");
## 6. Adding the constraintsThe method `==`: for equality constraints`>=`or`=>`: for greater-or-equal constraints`<=`or`=<`: for less-or-equal constraints
Note that strict inequalities are not allowed. In general, solvers consider strict inequalities as non-strict inequalities, so we preferred to avoid any confussion prohibiting the In general, the size of the lhs and rhs expressions in a constraint must be the same. Then, an array of constraints is defined with one line of code, element by element. If one expression (lhs or rhs) is an array, and the other is an scalar expression, again multiple constraints are added, one for each element in the array taking the same scalar expression in the other side of the constraint. For instance, in the following constraint: op.addConstraint(" sum(sum(x,2),1) <= 1"); - The lhs expression is
`sum(sum(x,2),1)`. The inner expression`sum(x,2)`, means that array`x`is summed in its dimension 2 (the second dimension, recall that first dimension is dimension 1), producing an array of size NxN. Then, this NxN array is summed in its dimension 1 (the first dimension for this 2D array, the rows), producing a row vector of size 1xN. The k-th element of this vector contains the sum \( \sum_{ij} x_{ijk} \). - The
`<=`symbol means that we have a less-or-equal inequality. - The rhs expression is
`1`, an scalar. In this case, JOM adds as many constraints as the number of cells in the lhs array, repeating for all of them the same rhs expression.
## 7. Solving the problemThe method It is possible to pass several pairs of parameter-value parameters to the function. See the section "About solvers' parameters" for more details. An important parameter is "solverLibraryName", which is followed by a String indicating the relative of full path to the .DLL or .SO file in the file system. If this parameter is not included, some default names are used. In our example, the GLPK solver is used, calling to the binary file op.solve("glpk" , "solverLibraryName" , "glpk_4_47");
## 8. Retrieving the solution optimality propertiesAfter the solver has been called, several methods in the `solutionIsOptimal ()`indicates if the solution found is optimal, or optimal within the tolerances set for the solver`solutionIsFeasible ()`indicates if the solution found is feasible (optimal or not)`feasibleSolutionDoesNotExist ()`to check if the solver proved that the problem has no feasible solutions`foundUnboundedSolution ()`to check if the solver proved that the problem is unbounded, finding a solution with \( - \infty\) cost of \( + \infty \) benefit.
if (!op.solutionIsOptimal ()) throw new RuntimeException ("An optimal solution was not found");
## 9. Retrieveing the problem solution and Lagrange multipliersSeveral methods exist to access the solution to the problem: `getPrimalSolution`to retrieve the problem solution.`getMultipliersOfConstraint`to access the Lagrange multipliers of the constraints. This is not available for integer problems`getSlackOfConstraint`to access the slack of the constraints.`getMultiplierOfLowerBoundConstraintToPrimalVariables`and`getMultiplierOfUpperBoundConstraintToPrimalVariables`to access the Lagrange multipliers of the implicit constraints related to the upper and lower bounds to the decision variables set.
See the javadoc for further details. In the example, we just retrieve the primal solution for the decision variable DoubleMatrixND sol = op.getPrimalSolution("x"); |