1. 程式人生 > >使用 線性規劃 解決 數字 排序問題, +Leapms模型

使用 線性規劃 解決 數字 排序問題, +Leapms模型

問題

將如下一組數字從大到小排序。

{10, 20, -32, 177, 0, -11.5, 19, 7, 6.2, -6.28, -2.71, 44}

解決辦法

建立象形規劃模型,給出各個數字的次序值。

模型

設x[i]為第i個數的次序值。根據排序規則有如下約束:

x[i] <= x[j] -1 | i=1,...,n; j=1,...,n; i<>j; d[i] >d[j] 

希望次序值從1開始,最大不超過數字的總數:

x[i] >= 1 | i=1,...,n

x[i] <= n | i=1,...,n

不需要目標:

max 0

最終模型為

max 0
subject to
	x[i] <= x[j] -1 | i=1,...,n; j=1,...,n; i<>j; d[i] >d[j]
	x[i] >= 1 | i=1,...,n
	x[i] <= n | i=1,...,n
where 
	n is a number
	d is a set
	x[i] is a variable of nonnegative integer| i=1,...,n
data_relation
	n=_$(d)
data
	d={10, 20, -32, 177, 0, -11.5, 19, 7, 6.2, -6.28, -2.71, 44}

求解:

+Leapms>load
 Current directory is "ROOT".
 .........
        sort.leap
 .........
please input the filename:sort
================================================================
1:  max 0
2:  subject to
3:      x[i] <= x[j] -1 | i=1,...,n; j=1,...,n; i<>j; d[i] >d[j]
4:      x[i] >= 1 | i=1,...,n
5:      x[i] <= n | i=1,...,n
6:  where
7:      n is a number
8:      d is a set
9:      x[i] is a variable of nonnegative integer| i=1,...,n
10:  data_relation
11:     n=_$(d)
12:  data
13:     d={10, 20, -32, 177, 0, -11.5, 19, 7, 6.2, -6.28, -2.71, 44}
================================================================
>>end of the file.
Parsing model:
1D
2R
3V
4O
5C
6S
7End.
..................................
number of variables=12
number of constraints=90
..................................
+Leapms>mip
relexed_solution=0; number_of_nodes_branched=0; memindex=(2,2)
The Problem is solved to optimal as an MIP.
找到整數規劃的最優解.非零變數值和最優目標值如下:
  .........
    x1* =5
    x2* =3
    x3* =12
    x4* =1
    x5* =8
    x6* =11
    x7* =4
    x8* =6
    x9* =7
    x10* =10
    x11* =9
    x12* =2
  .........
    Objective*=0
  .........
+Leapms>