All Products
Search
Document Center

Optimization Solver:Logistics: network flow problem ‒ minimum-cost flow problem

Last Updated:Oct 13, 2025

Industrial background

Logistics is the key to supply chain management, and it often involves multiple activities such as transportation, warehousing, loading and unloading, and distribution. How can transportation plans be reasonably arranged to improve the efficiency and reliability of cargo transportation and reduce logistics costs? This optimization problem can be modeled and solved by using mathematical programming methods.

For example, Enterprise A needs to transport products from their factory to the distribution center and then to each warehouse. The cost of delivering each product to different warehouses is different, and each transportation line has an upper limit on delivery capacity. How can transportation be properly arranged to minimize the shipping and distribution cost?

image.png

Business research, data quantification, and mathematical modeling

When using optimization techniques, you need to investigate the needs of business in more detail, organize the relevant business logic and data, and quantify them. Then, you can use a mathematical programming method for modeling.

For more details, see . This section lists only the related mathematical formula.

image.svg

In the preceding formula, s represents the goods that are available at each site, d represents the goods that are needed at each site, i and j represent the start and end sites of a transportation line, and k represents the intermediate site.

Constraints on the preceding formula:

  1. The sum of the goods available at a site plus the goods shipped into that site is equal to the sum of the goods needed at that site plus the goods shipped out of that site.

  2. Each transportation line has an upper limit on delivery capacity.

Source code

Optimization Solver supports multiple programming languages or modeling languages. This section provides only one of the languages for reference.

MindOpt APL

Source code for MindOpt APL (you can access to perform a dry run):

##====Source code for MindOpt APL====

clear model;

# Modeling -------
# net2.mapl
# Data
set CITIES := {"HN", "NE", "SE", "LN", "JL", "HLJ", "JS", "ZJ"} ;

set LINKS := {<"HN", "NE">, <"HN", "SE">, <"NE", "LN">, <"NE","JL">, <"NE","HLJ">, <"SE","LN">, <"SE","JL">, <"SE", "JS">, <"SE", "ZJ">};

param supply[CITIES] := <"HN"> 450 default 0;

param demand[CITIES] := <"JS"> 90,  <"ZJ"> 70, <"JL"> 120,  <"LN"> 120, <"HLJ"> 50 default 0;

set C := {"cost", "capacity"};

param data[LINKS * C] :=
            | "cost",  "capacity"|
|"HN", "NE" |    3.5,    250    |
|"HN", "SE" |    2.5,    250    |
|"NE", "LN" |    1.5,    100    |
|"NE", "JL" |    1.7,    100    |
|"NE", "HLJ"|    2.0,    100    |
|"SE", "LN" |    2.6,    100    |
|"SE", "JL" |    2.7,    100    |
|"SE", "JS" |    1.3,    100    |
|"SE", "ZJ" |    1.5,    100    |;

# Check whether the data is correct.
forall {<i> in CITIES } check supply[i] >= 0;
forall {<i> in CITIES } check demand[i] >= 0;
check sum {<i> in CITIES } supply[i] >= sum {<j> in CITIES} demand[j]; # The supply exceeds the demand.

# Model
var Ship[<i, j> in LINKS] >= 0 <= data[i, j, "capacity"];

minimize Total_Cost: sum {<i, j> in LINKS } data[i, j, "cost"] * Ship[i, j];

subto Balance: 
   forall {<k> in CITIES }
       supply[k] + sum {<i, k> in LINKS} Ship[i, k] >= demand[k] + sum {<k, j> in LINKS} Ship[k,j]; # The supply at each site meets the demand.


print "------------- Solving by using Optimization Solver---------------";
option solver mindopt; # (Optional) Specifies the solver used for solving the problem. Optimization Solver is used by default.
#option mindopt_options 'print=0'; # Sets output levels for the solver to simplify outputs.
solve; # Solves the problem.

print "-----------------Display---------------";
display; # Displays the result.
print "The lowest shipping cost after optimization = " , sum {<i, j> in LINKS } data[i, j, "cost"] * Ship[i, j];

Result and result usage

The logs vary based on code. The following section provides a part of the logs.

...
Model summary.
 - Num. variables     : 9
 - Num. constraints   : 5
 - Num. nonzeros      : 15
 - Bound range        : [5.0e+01,4.5e+02]
 - Objective range    : [1.3e+00,3.5e+00]
 - Matrix range       : [1.0e+00,1.0e+00]
...
Simplex method terminated. Time : 0.008s


OPTIMAL; objective 2123.00
...
----------------- Result ---------------
The lowest shipping cost after optimization = 2123

The optimal solution is 2123. If you want to learn more details of the solution, such as the values of the decision variables and whether the constraints are correct, you can run the print command or obtain the values of the decision variables from the .sol file for programming. You can also run the display command to obtain the values of all variables.

For example, run the following command to verify the constraint that the sum of the goods available at a site plus the goods shipped into that site is equal to the sum of the goods needed at that site plus the goods shipped out of that site.

forall {<k> in CITIES }
    print 'The sum of the goods available at the {} site plus the goods shipped into this site: {}'%k , 
    supply[k] + sum {<i, k> in LINKS} Ship[i, k];
print "-------------------";    
forall {<k> in CITIES }
    print 'The sum of the goods needed at the {} site plus the goods shipped out of this site: {}'%k , 
    demand[k] + sum {<k, j> in LINKS} Ship[k,j];

Results:

The sum of the goods available at the HN site plus the goods shipped into the HN site: 450
The sum of the goods available at the NE site plus the goods shipped into the NE site: 250
The sum of the goods available at the SE site plus the goods shipped into the SE site: 200
The sum of the goods available at the LN site plus the goods shipped into the LN site: 120
The sum of the goods available at the JL site plus the goods shipped into the JL site: 120
The sum of the goods available at the HLJ site plus the goods shipped into the HLJ site: 50
The sum of the goods available at the JS site plus the goods shipped into the JS site: 90
The sum of the goods available at the ZJ site plus the goods shipped into the ZJ site: 70
-------------------
The sum of the goods needed at the HN site plus the goods shipped out of the HN site: 450
The sum of the goods needed at the NE site plus the goods shipped out of the NE site: 250
The sum of the goods needed at the SE site plus the goods shipped out of the SE site: 200
The sum of the goods needed at the LN site plus the goods shipped out of the LN site: 120
The sum of the goods needed at the JL site plus the goods shipped out of the JL site: 120
The sum of the goods needed at the HLJ site plus the goods shipped out of the HLJ site: 50
The sum of the goods needed at the JS site plus the goods shipped out of the JS site: 90
The sum of the goods needed at the ZJ site plus the goods shipped out of the ZJ site: 70

Run the following code to display the results in CSV format as a solution to the transportation problem in logistics services:

print "{},{},{} "% "Start site","Intermediate site","Quantity of goods" : "Results.csv";
close "Results.csv";

forall {<i, j> in LINKS}
    print "{},{},{}" % i,j,Ship[i,j]  >> "Results.csv";

close "Results.csv";

The results shown in the following table are returned:

image.png

The results indicate that the lowest cost for shipping goods from the factory to each warehouse is 2123. The following shipping scheme is recommended:

  • The amount of goods shipped from the factory HN to the distribution center NE is 250 and to the distribution center SE is 200.

    • The amount of goods shipped from the distribution center NE to the warehouse LN is 100, to the warehouse JL is 100, and to the warehouse HLJ is 110.

    • The amount of goods shipped from the distribution center SE to the warehouse LN is 20, to the warehouse JL is 20, to the warehouse JS is 90, and to the warehouse ZJ is 70.

      • The amount of goods in the warehouse LN is 120, in the warehouse JN is 120, in the warehouse HLJ is 110, in the warehouse JS is 90, and in the warehouse ZJ is 70.

        • It can be calculated that the minimum cost of shipping goods from the two distribution centers to each warehouse is 2123.

image.png