OptimJ is an extension for Java with language support for writing optimization models and abstractions for bulk data processing. The extensions and the proprietary product implementing the extensions were developed by Ateji which went out of business in September 2011. OptimJ aims at providing a clear and concise algebraic notation for optimization modeling, removing compatibility barriers between optimization modeling and application programming tools, and bringing software engineering techniques such as object-orientation and modern IDE support to optimization experts.
OptimJ models are directly compatible with Java source code, existing Java libraries such as database access, Excel connection or graphical interfaces. OptimJ is compatible with development tools such as Eclipse, CVS, JUnit or JavaDoc. OptimJ is available free with the following solvers: lp_solve, glpk, LP or MPS file formats and also supports the following commercial solvers: MOSEK, IBM ILOG CPLEX Optimization Studio.
Language concepts
OptimJ combines concepts from object-oriented imperative languages with concepts from algebraic modeling languages for optimization problems. Here we will review the optimization concepts added to Java, starting with a concrete example.
The example of map coloring
The goal of a map coloring problem is to color a map so that regions sharing a common border have different colors. It can be expressed in OptimJ as follows.
package examples;
// a simple model for the map-coloring problem
public model SimpleColoring solver lpsolve
{
// maximum number of colors
int nbColors = 4;
// decision variables hold the color of each country
var int belgium in 1 .. nbColors; var int denmark in 1 .. nbColors; var int germany in 1 .. nbColors;
// neighbouring countries must have a different color
constraints {
belgium != germany; germany != denmark; }
// a main entry point to test our model
public static void main(String[] args) { // instantiate the model SimpleColoring m = new SimpleColoring();
// solve it
m.extract(); m.solve();
// print solutions
System.out.println("Belgium: " + m.value(m.belgium)); System.out.println("Denmark: " + m.value(m.denmark)); System.out.println("Germany: " + m.value(m.germany)); } }
Readers familiar with Java will notice a strong similarity with this language. Indeed, OptimJ is a conservative extension of Java: every valid Java program is also a valid OptimJ program and has the same behavior.
This map coloring example also shows features specific to optimization that have no direct equivalent in Java, introduced by the keywords model, var, constraints.
OR-specific concepts
Models
A model is an extension of a Java class that can contain not only fields and methods but also constraints and an objective function. It is introduced by the model keyword and follows the same rules as class declarations. A non-abstract model must be linked to a solver, introduced by the keyword solver. The capabilities of the solver will determine what kind of constraints can be expressed in the model, for instance a linear solver such as lp solve will only allow linear constraints.
public model SimpleColoring solver lpsolve
Decision variables
Imperative languages such as Java provide a notion of imperative variables, which basically represent memory locations that can be written to and read from.
OptimJ also introduces the notion of a decision variable, which basically represents an unknown quantity whose value one is searching. A solution to an optimization problem is a set of values for all its decision variables that respects the constraints of the problem—without decision variables, it would not possible to express optimization problems. The term "decision variable" comes from the optimization community, but decision variables in OptimJ are the same concept as logical variables in logical languages such as Prolog.
Decision variables have special types introduced by the keyword var. There is a var type for each possible Java type.
// a var type for a Java primitive type
var int x;
// a var type for a user-defined class
var MyClass y;
In the map coloring example, decision variables were introduced together with the range of values they may take.
var int germany in 1 .. nbColors;
This is just a shorthand equivalent to putting a constraint on the variable.
Constraints
Constraints express conditions that must be true in any solution of the problem. A constraint can be any Java boolean expression involving decision variables.