OptimJ code samples |
|
Concepts found in modeling languages (models, decision variables, constraints...) are directly expressible in OptimJ™. Here we will develop two well-known examples: the diet problem and the portfolio problem. Diet: a linear programming problemThe goal of the diet problem is to find the cheapest combination of foods that will satisfy all the daily nutritional requirements of a person. The problem is formulated as a linear program. Following is the entire code for the model. Comments are given below. Source code If you're familiar with Java, you will notice a few additional constructions : model cplex, var, constraints... If you're familiar with modeling languages, you will notice a few additional programming artefacts such as package, public, extends... Comments A model is a Java class extended with optimization specific concepts. Like any other Java class, it has modifiers for controlling access and visibility, it can extend classes and implement interfaces. DietModelParams is a superclass used to define parameters as fields of the model. A solver context is introduced by the model declaration with the keyword solver. Reusing the same model for a different solver is simply a matter of changing the solver declaration. This is the model constructor. As OptimJ is an extension of Java, this is nothing else than a standard class constructor. In particular, model parameters can be retrieved from any data sources using standard Java APIs, such as:
This is a superclass constructor invocation that initializes the fields with the values of the parameters and checks consistency of the input data using the standard Java assertion mechanism. var double is the type of decision variables whose solutions are of type double. Every Java data type, including user-defined types, has a var equivalent. This code declares an associative array of var double, ie. an array of decision variables. The j-th variable is bound by f_min[j] and f_max[j]. The major difference between an imperative language such as Java and an optimization modelling language is the notion of what is a variable:
If you're familiar with other algebraic modelling languages, you may find these type declarations unnecessarily verbose. This may be true for simple textbook examples, but decades of experience in software engineering prove that they provide invaluable help in guaranteeing correctness of code. They also promote code readability and reusability. The same applies to optimization models. Additionally, OptimJ provides familiar and concise notation for
This introduces the objective function. It is followed by a standard Java expression.
Aggregate operators such as sum, product , etc., allow expressions to range over arbitrary collections of data.
A constraint is any Java boolean expression involving decision variables. They have the same syntax as ordinary Java expressions. Constraints can be written within a constraints block.
Collections of constraints can be expressed with the forall construct, based on the comprehension notation. Download more examples and an evaluation licence. Portfolio optimization: a quadratic programming problemIn this example, we show how the portfolio selection problem is formulated as a convex quadratic programming problem. The Portfolio model calculates the optimal capital weightings for a basket of investments that gives the highest return for the least risk.
Source code Investment.optimj
Example code comments: This generator introduces a new local variable i that iterates over all values of investments Any Java expression can be used as a constraint or an objective function. OptimJ type-checking will complain if this is not a valid expression according to the solver. In this sample code the objective function is quadratic. Download more examples and an evaluation licence. |
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.



