Linear Programming in AnyLogic with the ojAlgo Solver

Why AnyLogic and the Petroleum Refining Library Need an Optimization Solver

       AnyLogic and the Petroleum Refining Library (PRL) are designed to model complex oil and gas processing systems as dynamic simulation models. However, many real-world operational problems cannot be solved by simulation alone. They require an analytical optimization model to determine the best decision under a given set of constraints. Typical examples include splitting incoming flows between process lines, blending product streams, allocating available resources, planning production, managing storage, and solving many other operational optimization problems. This makes it useful to combine simulation with mathematical optimization in a hybrid simulation model. AnyLogic can represent the dynamic behavior of the physical system, while an optimization solver can solve specific analytical problems arising during the simulation. For Petroleum Refining Library, linear programming (LP) and mixed-integer linear programming (MILP) are particularly relevant because many flow allocation, blending, planning, and scheduling problems can be formulated using linear constraints and objective functions. To support these applications, Petroleum Refining Library needs an optimization solver that can be integrated directly into its Java-based simulation environment.

Choosing an Optimization Solver

       A wide range of optimization solvers can be integrated with Java-based applications, including CPLEX, Gurobi, SCIP, GLPK, lp_solve, and ojAlgo. They differ in supported optimization methods, licensing models, performance, and integration requirements.
       For Petroleum Refining Library, the main requirements were support for linear programming (LP) and mixed-integer linear programming (MILP), including integer and binary variables, together with Java integration and a practical licensing model suitable for a simulation library. Based on these requirements, the initial choice for Petroleum Refining Library was lp_solve. It is a free and well-established solver that supports linear, integer, and mixed-integer programming. Its Java integration also made it possible to formulate and solve optimization problems directly from Java code. At the time, lp_solve provided a practical solution for integrating optimization into Petroleum Refining Library and AnyLogic simulation models. However, its native-code architecture introduced additional deployment requirements for server-side and cloud environments.

Why Petroleum Refining Library Selected ojAlgo

       After evaluating the available alternatives, Petroleum Refining Library selected ojAlgo as the new optimization engine.
The main reason was its pure-Java implementation. Unlike lp_solve, ojAlgo does not require native libraries or JNI, which makes it much easier to distribute and run in Java-based environments such as AnyLogic and AnyLogic Cloud. ojAlgo also provides the optimization capabilities required by Petroleum Refining Library, including linear programming (LP) and mixed-integer linear programming (MILP) with integer and binary variables. It is open source and can be included in a Java project as a regular dependency.

The Petroleum Refining Library Wrapper for ojAlgo

       Although ojAlgo provides a powerful optimization API, its API exposes many implementation details that are unnecessary for the typical optimization tasks solved in Petroleum Refining Library. To simplify solver integration, we developed a lightweight wrapper that provides a PRL-oriented API for defining optimization problems without requiring direct interaction with the ojAlgo API. The wrapper provides a small set of classes covering the main elements of an optimization problem:
It creates a simple abstraction layer:
Petroleum Refining Library model → Petroleum Refining Library ojAlgo wrapper → ojAlgo → optimization solution
This approach keeps the Petroleum Refining Library API simple while allowing ojAlgo to handle the underlying LP and MILP calculations.

A Simple Feedstock Allocation Example

       The following example shows a simple feedstock allocation problem. An incoming feedstock flow of 1,000 t/h must be distributed between two process lines. Each line has its own operating limits. If the available processing capacity is insufficient, the remaining volume is assigned to a residual variable. The objective is to minimize this residual.
import com.prl.ilp.*;

Problem problem = new Problem("Feedstock Allocation");

Linear flow = new Linear()
        .add(1, "line1")
        .add(1, "line2")
        .add(1, "residual");

problem.add("Feedstock balance", flow, "=", 1000);
problem.add("Line 1 limit", new Linear().add(1, "line1"), "<=", 500);
problem.add("Line 2 limit", new Linear().add(1, "line2"), "<=", 300);

problem.setObjective(new Linear().add(1, "residual"), OptType.MIN);

Result result = problem.solve();

double line1Flow = result.get("line1");
double line2Flow = result.get("line2");
double residual = result.get("residual");
       For the given constraints, the optimal solution assigns 500 t/h to Line 1, 300 t/h to Line 2, and leaves 200 t/h as residual. The optimizer therefore uses the maximum available processing capacity while minimizing the unprocessed flow.

Using the Petroleum Refining Library ojAlgo Wrapper in AnyLogic

       The Petroleum Refining Library ojAlgo wrapper is distributed as a Java library and can be added to an AnyLogic project as an external JAR dependency. After adding the library to the model, the optimization classes can be imported and used directly from AnyLogic Java code. The simulation model can then create and solve optimization problems whenever an analytical decision is required during simulation. This allows optimization to become an integral part of the simulation logic. For example, an AnyLogic model can collect the current process state, formulate an LP or MILP problem using the Petroleum Refining Library wrapper, solve it with ojAlgo, and use the resulting decision variables to control material flows or other model parameters. The Petroleum Refining Library wrapper provides the optimization interface, while ojAlgo performs the underlying mathematical optimization.

Download the Petroleum Refining Library ojAlgo Wrapper

Current version: PRL ILP 1.1
download

The wrapper requires ojAlgo as its underlying optimization engine. You can download ojAlgo from this page or get the original ojAlgo library from the official project.

Conclusion

The ojAlgo integration brings LP and MILP optimization directly into AnyLogic while keeping the PRL API simple and deployment fully Java-based. The PRL ojAlgo wrapper allows models to formulate and solve optimization problems during simulation and apply the resulting decisions to the modeled system. With no native libraries or JNI dependencies, this approach also simplifies deployment to Java environments, including AnyLogic Cloud.

FAQ

1. What is ojAlgo?
ojAlgo is an open-source optimization library implemented entirely in Java. It provides mathematical optimization capabilities including linear programming (LP) and mixed-integer linear programming (MILP).

2. Can ojAlgo solve MILP problems?
Yes. ojAlgo supports mixed-integer optimization, including integer and binary decision variables, making it suitable for many operational optimization problems in Petroleum Refining Library.

3. Can ojAlgo be used directly in AnyLogic?
Yes. ojAlgo runs on the Java Virtual Machine and can be used from Java code in AnyLogic models. Petroleum Refining Libraryadditionally provides a lightweight wrapper that simplifies its integration.

4. Why does Petroleum Refining Library provide its own ojAlgo wrapper?
The wrapper provides a simpler, Petroleum Refining Library-oriented API for defining optimization variables, linear expressions, constraints, objective functions, and retrieving results. It also keeps the Petroleum Refining Library-model code independent from the underlying solver API.

5. What optimization problems can be solved with the Petroleum Refining Library wrapper?
The wrapper is designed primarily for LP and MILP problems, including feedstock allocation, flow distribution, product blending, production planning, resource allocation, storage management, and scheduling.

6. Does the Petroleum Refining Library wrapper require native libraries?
No. The wrapper uses ojAlgo as its optimization engine, and ojAlgo is implemented in pure Java. No native solver libraries or JNI components are required.

7. Does the wrapper expose the complete ojAlgo API?
No. The Petroleum Refining Library wrapper is intentionally lightweight. It provides the functionality required for common LP and MILP optimization problems rather than exposing every feature of ojAlgo.

8. Where can I download the Petroleum Refining Library ojAlgo wrapper?
The current Petroleum Refining Library ILP library is available for download from the link provided in the Download the Petroleum Refining Library ojAlgo Wrapper section above.