7.1. MindOpt APL

MindOpt Algebraic Programming Language (MindOpt Algebraic Programming Language, MindOpt APL, referred to as MAPL) is a general-purpose algebraic modeling language developed by the MindOpt team, which can help users efficiently build a connection between mathematical models and solver invocation. Its features include:

  • The grammar is concise and natural, close to the language of mathematics. Using MAPL to describe a mathematical programming model is very similar to using a mathematical formula to describe it.

  • It supports modeling and preprocessing of linear programming, integer programming, 0-1 programming, quadratic programming, nonlinear programming, multi-objective programming, constraint programming and other types of problems.

  • It supports switching between various optimization solvers such as MindOpt Solver and complete the optimization solution, which is convenient for comparison of results.

7.1.1. Try MAPL on the Cloud

MindOpt Cloud Modeling Solution Platform (MindOpt Studio) is a cloud-based development platform for optimization technology learners and developers. On this platform, you can view a wealth of mathematical modeling cases and develop your own optimization model in the notebook on the web.

The modeling and solving platform on the cloud provides the latest version of the MindOpt APL modeling language and the MindOpt solver that have been deployed, which can be used immediately without installation. The platform has multiple functions such as complete project management, machine resource management, file upload and download, etc., which provides good support for the learning of modeling optimization technology and the collaborative development of operational optimization projects.

You can use the following links to get more help on using the modeling platform on the cloud and view MindOpt APL modeling language cases:

7.1.2. Try MAPL Locally

You can Contact Us to request a local download of MindOpt APL. We currently provide installation packages for Linux, macOS and other platforms. For details, please refer to Local download and installation. The latest version of the MindOpt solver is already built into the local installation package of MindOpt APL, so there is no need to download and install it separately.

7.1.3. Set MindOpt Parameters with MAPL

MindOpt optimization solver provides a large number of parameters for users to set, such as selecting different solving algorithms, setting the number of threads, etc. In MindOpt APL these solver parameters are passed in as strings, a basic example of which follows:

option filepath path/to/your/model/folder;     # specify the path to the model file (use `./` as default)
option solverpath path/to/your/solver/folder;  # specify the path to the solver (use `./` and the installed solvers' folder as default)
option solver mindopt;                         # specify the solver to use
option mindopt_options 'print=1 method=-1 num_threads=8'; # specify the settings for the chosen solver

For all configurable parameters of MindOpt, please refer to the Parameters section.

7.1.4. An Example of MAPL

In this section, we take the classic Diet problem in mathematical programming as an example to introduce the use of MindOpt APL in MindOpt Studio to model and call the MindOpt solver to solve the overall process of the problem. For a detailed introduction to MindOpt APL syntax, please refer to MindOpt APL v2.0 Syntax Manual.

7.1.4.1. Problem Description

There are eight kinds of food in the recipe, namely beef (BEEF), chicken (CHK), fish (FISH), ham (HAM), macaroni and cheese (MCH), meatloaf (MTL), pasta (SPG), turkey ( TUR). The four vitamins that people need to take in every day are vitamins A, C, B1, and B. The known vitamin content per gram of food is shown in the figure:

Nutrition of foods

Food

A

C

B1

B2

BEEF

60

20

10

15

CHK

8

0

20

20

FISH

8

10

15

10

HAM

40

40

35

10

MCH

15

35

15

15

MTL

70

30

15

15

SPG

25

50

25

15

TUR

60

20

15

10

And the amount spent per gram of food is known to be purchased as follows:

Prices of foods

Food

Price

BEEF

3.19

CHK

2.59

FISH

2.29

HAM

2.89

MCH

1.89

MTL

1.99

SPG

1.99

TUR

2.49

Assuming that each person’s daily intake of each food should not exceed 100 grams, and the intake of each vitamin should not be less than 700 but not more than 10,000. How can we make decisions about food intake so that we can minimize the cost of buying food while meeting the above requirements?

The algebraic mathematical model of the problem is as follows:

\[\begin{split}\begin{eqnarray} &\min & \sum_{j \in J} \mbox{cost}_j \times \mbox{buy}_j \\ &\mbox{s.t.} & \mbox{n_min}_i \leq \sum_{j \in J} \mbox{amt}_{i,j} \times \mbox{buy}_j \leq \mbox{n_max}_i, \forall i \in I, \\ & & \mbox{f_min}_j \leq \mbox{buy}_j \leq \mbox{f_max}_j, \forall j \in J. \end{eqnarray}\end{split}\]

Here,

  • \(\mbox{buy}\) is the decision variable,

  • \(\mbox{f_min}\) and \(\mbox{f_max}\) are the lower and upper bounds of \(\mbox{buy}\) respectively,

  • \(\mbox{cost}\) is the coefficient in the objective function,

  • \(\mbox{amt}\) is the constraint matrix,

  • \(\mbox{n_min}\) and \(\mbox{n_max}\) are the lower and upper bounds of the constraints, respectively.

7.1.4.2. MindOpt APL Modeling

Next, we model the above problem using MindOpt APL on MindOpt Studio. First, enter our Cloud Modeling Platform, and then click the “Projects” in the top menu. Register or log in with an Alibaba Cloud account.

../_images/cloudopt-overview.png

Click “Create New Project” in the upper right corner, and then create a project named Diet.

../_images/cloudopt-projects.png

On the new project interface, click the Notebook icon to enter the IDE development platform as shown below.

../_images/cloudopt-diet.png

After creating a new Notebook with MindOpt APL as the core (pointed by the arrow in the figure), a .ipynb file will be automatically generated. Notebook’s Kernel can be switched in the upper right corner of the editor.

../_images/cloudopt-ide.png

Next, you can model the above problem in two ways:

  • Method 1: You can directly type the following code in the Notebook input block. Note that each MindOpt APL command ends with a semicolon. To run the code, click the triangle button above the Notebook window, or press the Ctrl+Enter or Shift+Enter key combination.

clear model;    # clear previous model

# diet.mapl
# modified based on the 'diet example' in Chapter 2 of the book "AMPL: A Modeling Language for Mathematical
# Programming"

set NUTR := { "A", "B1", "B2", "C" };
set FOOD := {"BEEF", "CHK", "FISH", "HAM", "MCH", "MTL", "SPG", "TUR"} ;
set F:= {"cost", "f_min", "f_max"};
set N:= {"n_min", "n_max"};

param data1[FOOD * F] :=
        | "cost"  , "f_min" , "f_max" |
|"BEEF" |  3.19   ,  0      ,  100    |
|"CHK"  |  2.59   ,  0      ,  100    |
|"FISH" |  2.29   ,  0      ,  100    |
|"HAM"  |  2.89   ,  0      ,  100    |
|"MCH"  |  1.89   ,  0      ,  100    |
|"MTL"  |  1.99   ,  0      ,  100    |
|"SPG"  |  1.99   ,  0      ,  100    |
|"TUR"  |  2.49   ,  0      ,  100    |;

param data2[NUTR * N] :=
      | "n_min", "n_max"|
|"A"  |  700,     10000 |
|"C"  |  700,     10000 |
|"B1" |  700,     10000 |
|"B2" |  700,     10000 |;

param amt[FOOD * NUTR] :=
        | "A",  "C",  "B1",  "B2"|
|"BEEF" |  60,   20,   10,    15 |
|"CHK"  |  8,    0,    20,    20 |
|"FISH" |  8,    10,   15,    10 |
|"HAM"  |  0,    40,   35,    10 |
|"MCH"  |  15,   35,   0,     15 |
|"MTL"  |  70,   30,   15,    0  |
|"SPG"  |  0,    50,   25,    15 |
|"TUR"  |  60,   0,    15,    0  |;

var x[j in FOOD] >= data1[j, "f_min"] <= data1[j, "f_max"];

minimize Total_Cost:  sum {j in FOOD}  data1[j, "cost"] * x[j];

subto Diet: forall {i in NUTR }
        data2[i, "n_min"] <= sum {j in FOOD} amt[j, i] * x[j] <= data2[i, "n_max"];

print NUTR;
print N;

After running the above code, the following output is obtained, indicating that the modeling of the problem has been completed.

List: |1|{<"A">,<"B1">,<"B2">,<"C">}
List: |1|{<"n_min">,<"n_max">}
  • Method 2: In addition to the above methods, you can also save the modeling code separately as a file diet.mapl, and then enter the following command in Notebook

model <file_path>/diet.mapl;

After running it will get the same result. When dealing with complex models, it is often more convenient to model in this way.

7.1.4.3. Invoking MindOpt

Next, we solve this mathematical model using MindOpt. Enter the following command in the code block of Notebook

option solver mindopt;   # specify the solver (with MindOpt set as default)
solve;                   # solve the problem

The above command invokes the MindOpt solver pre-installed on the platform. For more details about interactive commands between MindOpt APL modeling language and MindOpt solver, please refer to the chapter “APIs and error codes -> Command line interface (CLI)” in this documentation.

The solution results are displayed as follows:

Running mindoptampl
wantsol=1
MindOpt Version 0.24.1 (Build date: 20230423)
Copyright (c) 2020-2023 Alibaba Cloud.

Start license validation (current time : 07-JUL-2023 11:20:32).
License validation terminated. Time : 0.007s

Only one thread allowed -- optimize with Simplex method.
Model summary.
 - Num. variables     : 8
 - Num. constraints   : 4
 - Num. nonzeros      : 25
 - Bound range        : [1.0e+02,1.0e+04]
 - Objective range    : [1.9e+00,3.2e+00]
 - Matrix range       : [8.0e+00,7.0e+01]

Presolver started.
Presolver terminated. Time : 0.002s

Simplex method started.
Model fingerprint: iVWY5d2diV2dvd3Y

    Iteration       Objective       Dual Inf.     Primal Inf.     Time
            0     0.00000e+00      0.0000e+00      2.6250e+02     0.02s
            4     1.01011e+02      0.0000e+00      0.0000e+00     0.02s
Postsolver started.
Simplex method terminated. Time : 0.009s


OPTIMAL; objective 101.01
4 simplex iterations

Completed.

At the same time, two files .nl and .sol will be generated in the same directory as the modeling file. The .nl file is the problem model file for modeling, and the .sol file stores the solution results.

../_images/cloudopt-result2.png

Finally, we execute the display; command

print "-----------------Display---------------";
display;        # show the optimization result

print "Minimal Cost: ";
print  sum {<j> in FOOD} data1[j, "cost"] * x[j];

The following output results are obtained:

-----------------Display---------------
Primal Solution:
x@BEEF   = 0.000000000000000E+00
x@CHK    = 2.497123130034522E+01
x@FISH   = 0.000000000000000E+00
x@HAM    = 0.000000000000000E+00
x@MCH    = 8.538550057537401E+00
x@MTL    = 5.316455696202531E+00
x@SPG    = 4.833141542002300E+00
x@TUR    = 0.000000000000000E+00

Dual Solution:
Diet_1   = 2.438506904487917E-02
Diet_2   = 1.852876869965478E-02
Diet_3   = 1.012172036823936E-01
Diet_4   = 1.704545454545448E-04

Minimal Cost:
101.0110471806674

Here, Primal Solution is the value of the decision variable, and the following Dual Solution is the value of the dual solution. For this problem, the above solution results show that the recommended foods are chicken (CHK), macaroni and cheese (MCH), meatloaf (MTL), pasta (SPG).