Optimization models integrated with the Java™ ecosystem

An OptimJ model and a Java application work together with zero integration effort

The OptimJ compiler is implemented as a source-to-source translator outputting pure Java source code. This approach has been chosen because it allows a smooth and easy integration with most Java-based programming tools, at the expense of a slightly longer compilation time :

  • OptimJ and Java source files can coexist within the same project.
  • All tools and plugins reading Java source code or byte code are directly usable with OptimJ programs.
  • Tools that modify Java source code (code generators) are usually not compatible with OptimJ source files.

The modelization code operates directly over your application data

Following code shows a typical application entry point that use the diet model

public static void main(String[] args) {

// define some sample parameters for the model
final String[] VTMN = {"A", "B1",};
final String[] FOOD = {"BEEF", "CHK",};
final double[String] cost = {"BEEF" -> 3.19, "CHK" -> 2.59,};
final float[String] f_min[FOOD] = 0;
final float[String] f_max[FOOD] = 100;
final float[String] n_min[VTMN] = 700;
final float[String] n_max[VTMN] = 1000;
final float[String][String] amt =
{
"A" -> {"BEEF" -> 60, "CHK" -> 8},
"B1" -> {"BEEF" -> 20, "CHK" -> 8},
}

// Instanciate the model with these parameters
DietModel m = new DietModel(VTMN, FOOD, cost, f_min, f_max, n_min, n_max, amt);

try{
// Give the model to the solver
m.extract();

// Ask cplex to solve the model
if (m.solve()) {
// Print the objective value
System.out.println("objective value " + m.objValue());

// Print value of each variable
for(String food : FOOD) {
System.out.println(
food +
" " +
m.value(m.Buy[food])
);
}
} else {
System.out.println("no solution");
}
}
finally
{
m.dispose();
}
}

cost, f_min, f_max... are parameters of diet model. They can come from any sources:

  • Diet's parameters are stored in an Excel sheet.
  • POI-HSSF is a Java API to Access Microsoft Excel Format Files.

    // Here we retrieve the model parameters from an Excel sheet

    // Get a workbook from the given file name
    HSSFWorkbook wb;
    String filename = getFileName() ;
    POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(filename));
    wb = new HSSFWorkbook(fs);

    // Transform data into arrays
    final String[] VTMN = HSSFArea.make(wb, "Vitamins").toStringArray();
    final String[] FOOD = HSSFArea.make(wb, "Foods").toStringArray();
    final double[] cost = HSSFArea.make(wb, "Cost").toDoubleArray();
    ....
  • Connect your model to a data base (JDBC API)
  • The Java Database Connectivity (JDBC) API is a standard for database-independent connectivity between the Java programming language and a wide range of databases. The JDBC API provides a call-level API for SQL-based database access.

    // Here we retrieve the model parameters from a data base

    // Connect to a database.
    Connection c = DriverManager.getConnection(...);

    // Send queries to the database.
    Statement statement = c.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT * FROM Foods");

    // Process the results received from the database in answer to the query.
    while (resultSet.next())
    {
    FOOD[i] = resultSet.getString("food");
    cost[i] = resultSet.getDouble("cost");
    ...
    }

     

  • Read the model's parameters in an XML document.
  • Package javax.xml provides classes allowing the processing of XML documents.

     

    // Here we retrieve the model parameters from an XML document

    // Create the document from the given file name
    DocumentBuilderFactory DOMFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder DOMBuilder = DOMFactory.newDocumentBuilder();

    // Parse the input file to get a Document object.
    File file = new File (getFileName());
    Document document = DOMBuilder.parse(file);

    // Locate food nodes
    NodeList nodes = document.getElementsByTagName("foods");
    for (Node food : nodes)
    {

    // Get the attributes of one node 'food'.
    NamedNodeMap attributes = food.getAttributes();

    FOOD[foodPos] = food.getNodeName();
    cost[foodPos] = new Double(attributes.getNamedItem("cost").getNodeValue());
    ...
    }
  • From our own data structure.

Previous examples are completely implemented in our samples packages.

All constructs of the Java language are available inside models

The following are examples of valid constraints:

// A domain for String variables
String[] strings = { "abc", "def" };
var String S in strings;

// Method invocation
constraints {
S.charAt(0) <= 'a';
S.length() >= 4;
}

The whole Java library is available inside models

We have already shown how to read diet's parameters with some java apis. Let us show you how to define domain of a decision variable as a set of values:

// Set is java interface for collections that contain no duplicate elements.
import java.util.Set;

// HashSet implements the Set interface, backed by a hash table.
import java.util.HashSet;


public model M solver mosek {

Set<String> domain = new HashSet<String>();
{domain.add("ab"); domain.add("bc");}

var String s in domain;
}

Any boolean Java expression involving decision variables is a constraint

OptimJ allows you to write the following, although not many solvers on the market may be able to solve it directly :

abstract model BallisticBody
{
double g = 9.81;
var double v0, alpha,range;
constraints {
// The ballistic body equation
range == v0*v0 * Math.sin(2 * alpha) / g ;
}
maximize range;
}
 

SEO by AceSEF

Customer Quotes

 

We're going to deploy OptimJ capabilities for our ongoing Java-based projects to close a gap between optimization engines and Java applications.

Andrey Torzhkov,
Research Scientist,
Siemens Corporate Research.
Siemens

 

With OptimJ you get the expressiveness of OPL™ with the integrability and flexibility of Ilog Concert™ -- the best of both worlds.

Luc Mercier,
Phd student,
Brown University.
Brown

 

Integrating optimization projects in a Java environment becomes a breeze using the Eclipse IDE, shortening project development times up to 50%.

David Gravot,
Consulting expert in optimization,
Rostudel.

 

OptimJ made it easy to use results from different solvers and combine exact methods with metaheuristics coded in Java, for solving complex industrial problems.

Médéric Suon,
Industrial Engineer,

PSA

 

I used OptimJ to implement a model for production planning in a polystyrene factory.

Luc Mercier,
Phd student,
Brown University.
Brown

 

We've succesfully applied OptimJ to improve an existing software application developed in one of our past numerical optimization projects.

Andrey Torzhkov,
Research Scientist,
Siemens Corporate Research.
Siemens

 

Using OptimJ enabled a rapid development and integration of optimization models in Java-based applications.

Médéric Suon,
Industrial Engineer,

PSA

Newsletter

To request a free subscription to our bi-monthly newsletter, enter your e-mail address below:
To prevent spam, please answer this little quiz: