4.5. Call MindOpt with JAVA

This section uses a simple example to show how to use the Java to call MindOpt to read and solve the optimization model.

4.5.1. Edit .java File

Below we will show how to call MindOpt JAVA API in the .java file to read the optimization problem model file and solve it.

First import the Java mindopt package:

1import com.alibaba.damo.mindopt.*;

Then create the environment and optimize the model:

 9        MDOEnv env = new MDOEnv();
10        MDOModel model = null;

Then call the constructor of MDOModel.MDOModel to read the optimization problem in MPS/LP format:

21            model = new MDOModel(env, args[0]);

Finally, use MDOModel.optimize to solve the problem, and call MDOModel.get to get the objective function value of the optimal solution.

22            model.optimize();
23            System.out.println("Obj value: " + model.get(MDO.DoubleAttr.ObjVal));

Below is the complete source code file ReadMps.java.

 1import com.alibaba.damo.mindopt.*;
 2
 3public class ReadMps {
 4    public static void main(String[] args) throws MDOException {
 5        if (args.length != 1) {
 6            return;
 7        }
 8
 9        /*------------------------------------------------------------------*/
10        /* Step 1. Create a model and change the parameters.                */
11        /*------------------------------------------------------------------*/
12        /* Create an empty model. */
13        MDOEnv env = new MDOEnv();
14        MDOModel model = null;
15
16        try {
17            /*--------------------------------------------------------------*/
18            /* Step 2. Input model.                                         */
19            /*--------------------------------------------------------------*/
20            /* Read model from file. */
21            model = new MDOModel(env, args[0]);
22
23            /*--------------------------------------------------------------*/
24            /* Step 3. Solve the problem and print the result.              */
25            /*--------------------------------------------------------------*/
26            /* Solve the problem. */
27            model.optimize();
28            /* Print the result. */
29            System.out.println("Obj value: " + model.get(MDO.DoubleAttr.ObjVal));
30         } catch (MDOException e) {
31            System.out.println(e.getMessage());
32         } finally {
33            /* Dispose of model and environment */
34            if (model != null) {
35                model.dispose();
36            }
37            env.dispose();
38         }
39     }
40 }

You can find more Java language related example files under the installation path <MDOHOME>/<VERSION>/examples/java.

4.5.2. Compile on Linux and macOS Platforms

We provide example files under the installation path <MDOHOME>/<VERSION>/examples/java. Taking linux x86 as an example, execute the following instructions to compile the sample code and perform optimization solution:

javac -cp .:<MDOHOME>/linux64-x86/lib/mindoptj.jar ReadMps.java
java  -cp .:<MDOHOME>/linux64-x86/lib/mindoptj.jar -Djava.library.path=$MINDOPT_HOME/linux64-x86/lib/ ReadMps ../data/afiro.mps

At the same time, we also provide the run.bat script, which can compile and execute the sample code through the following instructions:

bash run.sh

4.5.3. Compile on Windows Platform

We provide example files under the installation path <MDOHOME>\<VERSION>\examples\java. Execute the following instructions to compile the sample code and perform an optimized solution:

javac -cp .;<MDOHOME>\win64-x86\lib\mindoptj.jar ReadMps.java
java  -cp .;<MDOHOME>\win64-x86\lib\mindoptj.jar -Djava.library.path=%MINDOPT_HOME%\win64-x86\bin ReadMps ..\data\afiro.mps

At the same time, we also provide the run.bat script, which can compile and execute the sample code through the following instructions:

run.bat

Note

  • In order for the application to correctly locate the dynamic library, the user needs to specify the path of the dynamic library in the environment variable. If the environment variable is not specified, the user needs to place the dynamic library file in an appropriate location according to the logic of the operating system to find the dynamic library. For environment variable settings, see Installation Instructions.

  • See Supported Platform for supported Java language compiler versions.