Safekipedia

OptimJ

Adapted from Wikipedia · Discoverer experience

OptimJ is a special tool made to work with a computer language called Java. It helps people write plans for solving problems where they need to find the best way to use resources, like making the most of limited supplies. This tool was created by a company named Ateji, but the company stopped operating in September 2011.

OptimJ lets users write these problem-solving plans in a clear and easy way, mixing them with regular Java programs. This means it can work together with many existing Java tools, such as those for connecting to databases, using Excel files, or creating graphs.

The tool works well with popular development programs like Eclipse and supports testing tools such as JUnit. OptimJ can be used for free with several problem-solving engines and also works with advanced engines like MOSEK and IBM ILOG CPLEX Optimization Studio.

Language concepts

OptimJ mixes ideas from languages that focus on objects and steps you can take with ideas from languages that help you create math models for solving problems. This helps make it easier to write and understand these kinds of models while using Java. We’ll look at the new ideas OptimJ added to Java, starting with a real example.

The example of map coloring

The goal of a map coloring problem is to color a map so that regions sharing a border have different colors. This can be done using OptimJ, a special version of Java.

In OptimJ, you can write a program that decides the colors for different countries. For example, you can set a maximum number of colors and make sure that neighboring countries, like Belgium and Germany, have different colors. The program then finds a solution and shows the color for each country.

Readers who know Java will see that OptimJ looks very similar. OptimJ is built on Java, meaning any normal Java program will also work in OptimJ. The map coloring example also shows special features in OptimJ, like the words model, var, and constraints, which help solve optimization problems.

OR-specific concepts

Models

A model is like a special type of Java class. It can hold not just normal data and actions, but also rules and a goal. You start a model with the word model. Every model needs to be linked to a solver, which is a tool that finds answers to the model’s rules. The solver decides what kinds of rules the model can use. For example, a simple solver might only work with straight-line rules.

public model SimpleColoring solver lpsolve

Decision variables

In regular Java, variables are like boxes where you can store and change values. OptimJ adds something new called decision variables. These are like mystery boxes whose values we need to find. The solution to an optimization problem is a set of values for all its decision variables that fits the rules of the problem. Decision variables use the word var and can be of any 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 show the range of values they can take.

var int germany in 1 .. nbColors;

Constraints

Constraints are rules that must be followed in any solution. They can be any Java expression that gives a true or false result and involve decision variables.

In the map coloring example, these constraints make sure that Belgium’s color is different from Germany’s, and Germany’s color is different from Denmark’s.

constraints {
  belgium != germany;
  germany != denmark;
}

The != operator is the standard Java not-equal sign.

Constraints often come in groups and can use the forall operator. For example, instead of listing all countries and their neighbors, you can use arrays and a rule to check neighbors.

constraints {
  forall(Country c1 : countries, Country c2 : countries, :isNeighbor(c1,c2)) {
    color[c1] != color[c2];
  }
}

Country c1 : countries goes through all countries. :isNeighbor(c1,c2) keeps only the pairs that are neighbors.

Objectives

Sometimes, a model can have a goal to either make something as small as possible or as big as possible. This is called an objective function and it is optional.

Generalist concepts

Generalist concepts are programming ideas that are not just for solving math problems, but can be used in any kind of app. OptimJ added these concepts to Java to make it easier to write math models. These ideas are often found in older math modeling languages, so they feel familiar to experts.

Associative arrays

In Java, arrays can only use numbers as indexes, like the position in a list. But OptimJ arrays can use any type as an index, like names or other values. These are called associative arrays or maps. For example, you can create an array called age that stores a person's age using their name as the index:

int[String] age;

You can use these arrays just like normal Java arrays:

age["Stephan"] = 37; x = age["Lynda"];

Associative arrays are often used in math problems. OptimJ makes it easy to set up these arrays with values. You can set values directly, like:

int[String] age = { "Stephan" -> 37, "Lynda" -> 29 };

Or you can set values based on other data, like:

int[String] length[String name : names] = name.length();

Here, each entry length[i] gets the length of the name at position i.

Tuples

Tuples are common in computing but not found in most main languages like Java. OptimJ adds tuples as a basic part of the language. Tuples can be useful as indexes when used with associative arrays.

You can create a tuple like this:

(: int, String :) myTuple = new (: 3, "Three" :);

And get values from a tuple like this:

String s = myTuple#1;

Tuple types and values are written between (: and :).

Ranges

Comprehensions

Comprehensions are expressions in OptimJ that apply an operation to a group of values. They are like a sum in math. For example, to add all numbers from 1 to 10, you can write:

// the sum of all integers from 1 to 10 int k = sum { i | int i in 1 .. 10};

This looks similar to the big-sigma notation used in math, but it works with Java's syntax.

Comprehensions can also build collections like lists, sets, or maps. For example, to create a set of numbers from 1 to 10:

// the set of all integers from 1 to 10 HashSet s = \hashSet(){ i | int i in 1 .. 10};`

Comprehension expressions can use any calculation as their target. For example, to find the sum of the squares of numbers from 1 to 10:

// the sum of all squares of integers from 1 to 10 int k = sum { i\*i | int i in 1 .. 10};

They can also use many different inputs and filters. For example:

// the sum of all f(i,j), for 0 18 }

In math models, comprehensions are a clear and powerful way to prepare and clean input data, and to format output data.

Development environment

OptimJ works as an add-on for Eclipse, a tool used for building software. It changes OptimJ code into regular Java code, so it can work with many Java tools easily. This makes it simpler to use with other programs and tools that support Java.

OptimJ GUI and rapid prototyping

The OptimJ compiler understands the structure of data used in models, so it can create a clear graphical view of this data when it compiles. This is helpful for showing associative arrays, where the compiler knows how data is organized.

The basic view looks like an OLAP cube and can be customized with colors and new tools for displaying data. This helps experts avoid writing extra code to connect graphics with data. It also allows quick testing by showing visual hints about data structure right away.

The OptimJ GUI also shows performance statistics from the solver in real time. This helps understand and improve solving time, but it is currently only available for lp_solve.

Supported solvers

OptimJ can be used for free with solvers like lp_solve, glpk, and LP or MPS file formats. It also works with commercial solvers such as Mosek and IBM ILOG CPLEX Optimization Studio.

Related articles

This article is a child-friendly adaptation of the Wikipedia article on OptimJ, available under CC BY-SA 4.0.