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 :
The modelization code operates directly over your application dataFollowing 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
} else {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]) System.out.println("no solution");
}finally { m.dispose();
}cost, f_min, f_max... are parameters of diet model. They can come from any sources:
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(); .... 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"); ... }
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()); ... } Previous examples are completely implemented in our samples packages. All constructs of the Java language are available inside modelsThe 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 modelsWe 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 constraintOptimJ 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; |
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.
With OptimJ you get the expressiveness of OPL™ with the integrability and flexibility of Ilog Concert™ -- the best of both worlds.
Integrating optimization projects in a Java environment becomes a breeze using the Eclipse IDE, shortening project development times up to 50%.
OptimJ made it easy to use results from different solvers and combine exact methods with metaheuristics coded in Java, for solving complex industrial problems.
I used OptimJ to implement a model for production planning in a polystyrene factory.
We've succesfully applied OptimJ to improve an existing software application developed in one of our past numerical optimization projects.
Using OptimJ enabled a rapid development and integration of optimization models in Java-based applications.



