OptimJ
Adapted from Wikipedia · Adventurer experience
OptimJ is a special tool that works with a computer language called Java. It helps people create plans to solve problems, like finding the best way to use limited supplies.
This tool was made by a company called Ateji, but the company stopped operating in September 2011.
OptimJ lets users write these plans in a clear and easy way, mixing them with regular Java programs. This means it can work with many 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 makes 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 give each area on a map a color so that areas next to each other have different colors. We can use OptimJ, a special version of Java, to solve this.
In OptimJ, you can write a program to pick colors for different countries. For example, you can choose a limit on how many colors to use and make sure that countries that touch, like Belgium and Germany, have different colors. The program will then find a way to color the map and show the color for each country.
People who know Java will notice that OptimJ looks very similar. OptimJ is built using Java, so any normal Java program can also work in OptimJ. This example shows some special words in OptimJ, like model, var, and constraints, which help solve problems with the best possible answer.
OR-specific concepts
Models
A model is a special kind of Java class. It can hold data, actions, rules, and a goal. You start a model with the word model. Every model needs 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.
public model SimpleColoring solver lpsolve
Decision variables
In Java, variables are like boxes where you can store 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 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 you can use in any kind of app, not just for math problems. OptimJ added these concepts to Java to make it easier to write math models. These ideas are often found in older math modeling languages.
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 < i < 10 and 0 < j < 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. This lets it work with many Java tools easily, making it simpler to use with other programs that support Java.
OptimJ GUI and rapid prototyping
The OptimJ compiler knows how data is organized in models. It can make a clear picture of this data when it compiles. This is useful for showing arrays, where the compiler understands data organization.
The basic view looks like an OLAP cube. You can change colors and add new tools to show the data. This helps experts because they do not need to write extra code. It also lets them test quickly by seeing visual hints about the data right away.
The OptimJ GUI also shows performance information from the solver as it works. This helps people understand and improve solving time, but it is only available for lp_solve.
Supported solvers
OptimJ can work for free with solvers such as lp_solve, glpk, and LP or MPS file formats. It also supports commercial solvers like 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.
Safekipedia