6.1. Modeling and optimization in AMPL

A Mathematical Programming Language (AMPL) is an algebraic modeling language that aims to simplify complex problems into abstract algebraic expression forms. In this way, you only need to focus on building an algebraic model and input data into the model. This accelerates the development process and reduces the possibility of model input errors.

At present, MindOpt allows you to build a linear programming model by using AMPL on Windows, Linux, or OSX and call MindOpt to solve problems. For more information about AMPL, see the official document.

This topic describes how to use the mindoptampl application of MindOpt to solve an AMPL model.

6.1.1. Verify mindoptampl

You need to install MindOpt before you call mindoptampl. For more information about how to install and configure MindOpt, see Installation.

You can vefify the mindoptampl as follows:

You can find mindoptampl in the following location of the installation package:

<MDOHOME>\<VERSION>\<PLATFORM>\bin\mindoptampl

You can execute the following command to verify mindoptampl.

mindoptampl --version

If the version information about MindOpt and mindoptampl is returned like follows, the installation is successful.

0.19.0 (Darwin x86_64), driver(20220127), ASL(20201107)

6.1.2. Install AMPL

You can download the installation package from the official website (TRY AMPL) of AMPL.

6.1.3. Parameters and return values of AMPL API

mindoptampl provides some configurable parameters. You can use the AMPL option command to set the mindoptampl_options parameter. For example:

ampl: option mindoptampl_options 'num_threads=4 max_time=3600';

You can execute the following command to obtain all parameters supported by mindoptampl:

mindoptampl -=

For more information about MindOpt parameters, see Optional input parameters.

Parameters of MindOpt AMPL API

Parameter

Description

dualization

Indicates whether to perform dualization for the model.

enable_network_flow

Indicates whether to enable the network simplex method.

enable_stochastic_lp

Indicates whether to enable stochastic for LP, it will bring acceleration for some problems.

ipm_dual_tolerance

The dual feasible (relative) tolerance in the interior point method (IPM).

ipm_gap_tolerance

The dual gap (relative) tolerance in the IPM.

ipm_max_iterations

The maximum number of iterations in the IPM.

ipm_primal_tolerance

The primal feasible (relative) tolerance in the IPM.

max_time

The maximum solving time of the solver, in seconds.

method

The method or algorithm used by the solver.

mip_gap_abs

The allowable gap (absolute) for MIP.

mip_gap_rel

The allowable gap (relative) for MIP.

mip_integer_tolerance

The tolerance of the value of an integer for MIP.

mip_max_nodes

The maximum number of nodes in MIP.

mip_objective_tolerance

The tolerance of the objective for MIP.

mip_root_parallelism

The maximum number of concurrent threads allowed in the root node.

num_threads

The maximum number of threads used for optimization solving.

presolve

The presolver level.

print

Set the level of print.

spx_crash_start

Indicates whether to use initial basic solution generation in the simplex method.

spx_column_generation

Indicates whether to use column generation in the simplex method.

spx_dual_pricing

The dual pricing strategy in the simplex method.

spx_dual_tolerance

The dual feasible (relative) tolerance in the simplex method.

spx_max_iterations

The maximum number of iterations in the simplex method.

spx_primal_pricing

The primal pricing strategy in the simplex method.

spx_primal_tolerance

The primal feasible tolerance in the simplex method.

wantsol

Indicates whether to return results. 1 generate an .sol file, 2 print the solution, 8 do not print the solution.

After MindOpt finishes calculation, the status information is returned to AMPL in the form of exit code. You can execute the following command to obtain the status information:

ampl: display solve_result_num;

For more information about exit code and status information, see Result Code.

6.1.4. Example of calling MindOpt by AMPL

The following section takes the diet problem as an example to describe how to build an AMPL model and call mindoptampl for solving.

The diet problem uses data of the following two tables: Prices of foods and Nutrition of foods.

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

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

The objective of the diet problem is to configure a combination of foods that meets nutrition requirements with the lowest prices. The following part shows the algebraic model of this problem.

\[\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}\]

In the model:

  • \(\mbox{buy}\) is a decision variable.

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

  • \(\mbox{cost}\) is a coefficient in the target function.

  • \(\mbox{amt}\) is a constraint matrix.

  • \(\mbox{n_min}\) and \(\mbox{n_max}\) are the lower bound and upper bound of a constraint, respectively.

Before you use AMPL, convert the diet problem into the following AMPL model.

  1. Abstract algebraic model: diet.mod

    set NUTR;
    set FOOD;
    
    param cost {FOOD} > 0;
    param f_min {FOOD} >= 0;
    param f_max {j in FOOD} >= f_min[j];
    
    param n_min {NUTR} >= 0;
    param n_max {i in NUTR} >= n_min[i];
    
    param amt {NUTR,FOOD} >= 0;
    
    var Buy {j in FOOD} >= f_min[j], <= f_max[j];
    
    minimize Total_Cost:  sum {j in FOOD} cost[j] * Buy[j];
    
    subject to Diet {i in NUTR}:
       n_min[i] <= sum {j in FOOD} amt[i,j] * Buy[j] <= n_max[i];
    
  2. Data file: diet.dat

    data;
    
    set NUTR := A B1 B2 C ;
    set FOOD := BEEF CHK FISH HAM MCH MTL SPG TUR ;
    
    param:   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:   n_min  n_max :=
       A      700   10000
       C      700   10000
       B1     700   10000
       B2     700   10000 ;
    
    param amt (tr):
               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 ;
    

Load the preceding file into AMPL model and call mindoptampl to solve this problem.

ampl: model diet.mod;
ampl: data diet.dat;
ampl: option solver mindoptampl;
ampl: option mindoptampl_options 'numthreads=4 maxtime=1e+4';
ampl: solve;

After the solving procedure is completed, you can use the AMPL command display to view the result.

ampl: display Buy;
Buy [*] :=
BEEF   0
CHK    0
FISH   0
HAM    0
MCH    46.6667
MTL    0
SPG    0
TUR    0
;