裝箱問題的CPLEX求解


裝箱問題(Bin Packing Problem)

裝箱問題即搬家公司問題。一個搬家公司有無限多的箱子,每個箱子的承重上限為W,當搬家公司進入一個房間時,所有物品都必須被裝入箱子,每個物品的重量為wi (i=1,...,m),規划裝箱方式,使得使用的箱子最少。此文及所有本博客中的博文均為原創,本博客不轉發他人博文,特此聲明。

 

實例

一個海運公司有若干貨輪, 一個貨輪的最大載重量4000噸, 客戶貨物的重量是 1020T, 1930T, 3575T, 2861T, 4221T, 1541T, 2348T, and 1170T, 問如何分配貨物可以使總計需求的貨輪數量(航次) 最小。

 

建模 

 

假設搬家公司帶來n個箱子,且n個箱子足夠裝入所有物品。設0-1變量x[i][j]表示第j個物品是否被安排裝入第i個箱子,0表示不裝入,1表示裝入。根據題意,任何物品必須被裝入某個箱子中,於是有約束:

 

        sum{i=1,...,n} x[i][j] = 1 | j=1,...,m            // (1) 

 

如果箱子i有任何物品被裝入,則說該箱子被打開,並設0-1變量y[i]表示箱子i是否被打開(0-表示不打開,1-表示打開)。顯然目標是極小化打開箱子的數目,即:

 

       min sum{i=1,...,n} y[i]                              //(2)

 

裝入箱子的物品重量和不能超過該箱子的承重,即:

    

     sum{j=1,...,m} x[i][j] <= W*y[i] | i=1,...,n   //(3)

 

上式表示當聚焦第i個箱子時,如果y[i]=0則任何x[i][j]都必須為0,亦即如果第i個箱子沒有被打開,則沒有物品可以裝入該箱子。反之,如果y[i]=1,則裝入該箱子的物品的重量和必須小於箱子的最大承重W。

 

綜合(1)-(3), 裝箱問題模型的核心部分如下: 

//-------------------------------------------------------

 min sum{i=1,...,n} y[i]                                               //(2)

subject to 

       sum{i=1,...,n} x[i][j] = 1 | j=1,...,m                     // (1) 

       sum{j=1,...,m} x[i][j]w[i] <= W*y[i] | i=1,...,n    //(3)

//-------------------------------------------------------

 

添加where段對模型中常量符號和變量符號的說明

//-------------------------------------------------------

where

       m,n are integers

       W is a number

       w is a set

       x[i][j] is a variable of binary|i=1,...,n;j=1,...,m

       y[i] is a variable of binary|i=1,...,n

//-------------------------------------------------------

 

E、添加數據段

 //-------------------------------------------------------

data

       W=4000

       w={1020, 1930,3575,2861,4221,1541,2348, 1170}

data_relation

       m=_$(w)        // <--  _\$(w) 函數給出集合w中的元素數。

       n=m/2           // <-- 預備箱子數取為物體數的一半。

//-------------------------------------------------------

 上面模型中,物品個數由求w中的元素數給出。預備箱子數給為物體數的一半。預備箱子數必須大於實際最優箱子數,否則問題無解。

 

CPLEX求解 

求解模型,在Leapms環境中, 首先使用load命令調入並解析模型,  而后使用"cplex" 命令調用IBMC PLEX求解器完成求解.

如果你的leapms版本不支持cplex命令,可用savemps或者savelp保存成mps或lp模型,然后再用cplex求解(https://www.cnblogs.com/leapms/p/11846039.html)。

 

Leapms>load
Current directory is "ROOT".
.........
       binpacking.leap
.........
lease input the filename:binpacking
===============================================================
:  //-------------------------------------------------------
:
:  min sum{i=1,...,n} y[i]                                    //(2)
:  subject to
:         sum{i=1,...,n} x[i][j] = 1 | j=1,...,m              // (1)
:         sum{j=1,...,m} x[i][j]w[i] <= W*y[i] | i=1,...,n    //(3)
:  where
:         m,n are integers
:         W is a number
0:         w is a set
1:         x[i][j] is a variable of binary|i=1,...,n;j=1,...,m
2:         y[i] is a variable of binary|i=1,...,n
3:  data
4:         W=4000
5:         w={1020M, 1930M, 3575M, 2861M, 4221M, 1541M, 2348M, 1170}
6:         //w={1020, 1930,3575,2861,4221,1541,2348, 1170}
7:  data_relation
8:         m=_$(w)
9:         n=m
0:  //-------------------------------------------------------
===============================================================
>end of the file.
arsing model:
D
R
V
O
C
S
End.
.................................
umber of variables=72
umber of constraints=16
.................................
Leapms>cplex
 You must have licience for Ilo Cplex, otherwise you will violate
 corresponding copyrights, continue(Y/N)?
 你必須有Ilo Cplex軟件的授權才能使用此功能,否則會侵犯相應版權,
 是否繼續(Y/N)?y

Tried aggregator 1 time.
MIP Presolve eliminated 1 rows and 9 columns.
MIP Presolve modified 61 coefficients.
Reduced MIP has 15 rows, 63 columns, and 119 nonzeros.
Reduced MIP has 63 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.02 sec. (0.14 ticks)
Found incumbent of value 7.000000 after 0.08 sec. (0.32 ticks)
Probing time = 0.00 sec. (0.06 ticks)
Tried aggregator 1 time.
Reduced MIP has 15 rows, 63 columns, and 119 nonzeros.
Reduced MIP has 63 binaries, 0 generals, 0 SOSs, and 0 indicators.
Presolve time = 0.02 sec. (0.08 ticks)
Probing time = 0.00 sec. (0.06 ticks)
Clique table members: 43.
MIP emphasis: balance optimality and feasibility.
MIP search method: dynamic search.
Parallel mode: deterministic, using up to 4 threads.
Root relaxation solution time = 0.00 sec. (0.05 ticks)

        Nodes                                         Cuts/
   Node  Left     Objective  IInf  Best Integer    Best Bound    ItCnt     Gap

*     0+    0                            7.0000        0.0000           100.00%
*     0+    0                            4.0000        0.0000           100.00%
      0     0        2.2824     5        4.0000        2.2824        8   42.94%
*     0+    0                            3.0000        2.2824            23.92%
      0     0        cutoff              3.0000        2.2824        8   23.92%
Elapsed time = 0.20 sec. (0.72 ticks, tree = 0.00 MB, solutions = 3)

Root node processing (before b&c):
  Real time             =    0.22 sec. (0.72 ticks)
Parallel b&c, 4 threads:
  Real time             =    0.00 sec. (0.00 ticks)
  Sync time (average)   =    0.00 sec.
  Wait time (average)   =    0.00 sec.
                          ------------
Total (root+branch&cut) =    0.22 sec. (0.72 ticks)
Solution status = Optimal
Solution value  = 3
        x1_1=1
        x1_5=1
        x1_8=1
        x6_2=1
        x6_7=1
        x8_3=1
        x8_4=1
        x8_6=1
        y1=1
        y6=1
        y8=1

求解結果為:文件分配方案是:第一航次運送1、5、8貨物;第二航次2、7貨物;第三航次3、4、6貨物。總計使用三個航次或三艘貨輪。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM