Mathematica从5.0开始就可以解复杂的规划模型,主要用Maximize和Minimize这两个函数。下面给出几个例子,说明如何用这两个函数来求解规划问题。
(
*
linear programming
*
)
Maximize[{x1
+
x2
+
x3
+
x4, (
*
objective function
*
)
x5
+
x6
+
x7
+
x8
>=
250000
, (
*
conditions
*
)
x1
+
x5
<=
380000
,
x2
+
x6
<=
265200
,
x3
+
x7
<=
408100
,
x4
+
x8
<=
130100
,
2.85
*
x1
-
1.42
*
x2
+
4.27
*
x3
-
18.49
*
x4
>=
0
,
2.85
*
x5
-
1.42
*
x6
+
4.27
*
x7
-
18.49
*
x8
>=
0
,
16.5
*
x1
+
2
*
x2
-
4
*
x3
+
17
*
x4
>=
0
,
7.5
*
x5
-
7
*
x6
-
13.0
*
x7
+
8
*
x8
>=
0
,
x1
>=
0
, x2
>=
0
, x3
>=
0
, x4
>=
0
, x5
>=
0
, x6
>=
0
, x7
>=
0
, x8
>=
0
},
{x1, x2, x3, x4, x5, x6, x7, x8} (
*
vars
*
)
]
计算结果:
{
933400
., {x1
->
264938
., x2
->
135702
., x3
->
408100
., x4
->
124660
., x5
->
115062
., x6
->
129498
., x7
->
0
., x8
->
5440.01
}}
其中,933400为目标函数的最优值,紧接的表为目标函数达到最优值时的结果。
(
*
0
-
1
Programming
*
)
Maximize[{
3
x1
-
2
x2
+
5
x3,
x1
+
2
x2
-
x3 ≤
2
,
x1
+
4
x2
+
x3 ≤
4
,
x1
+
x2 ≤
3
,
4
x2
+
x3 ≤
6
,
x1
==
1
||
x1
==
0
,
x2
==
1
||
x2
==
0
,
x3
==
1
||
x3
==
0
},
{x1, x2, x3}
]
(
*
Integer Programming
*
)
Maximize[{
40
x1
-
90
x2,
9
x1
+
7
x2 ≤
56
,
7
x1
+
20
x2 ≤
70
,
x1 ≥
0
,
x2 ≥
0
,
x1 ∈ Integers,
x2 ∈ Integers
}, {x1, x2}
]
- 求近似解(用NMaximize/NMinimize)
NMinimize[{
8
x1
+
10
x2
+
7
x3
+
6
x4
+
11
x5
+
9
x6,
12
x1
+
9
x2
+
25
x3
+
20
x4
+
17
x5
+
13
x6 ≥
60
,
35
x1
+
42
x2
+
18
x3
+
31
x4
+
56
x5
+
49
x6 ≥
150
,
37
x1
+
53
x2
+
28
x3
+
24
x4
+
29
x5
+
20
x6 ≥
125
,
0
≤ x1 ≤
1
,
0
≤ x2 ≤
1
,
0
≤ x3 ≤
1
,
0
≤ x4 ≤
1
,
0
≤ x5 ≤
1
,
0
≤ x6 ≤
1
}, {x1, x2, x3, x4, x5, x6}]
Maximize[{
120
x1
+
10
x2
+
100
x3
-
5000
y1
-
2000
y2
-
1000
y3,
5
x1
+
x2
+
4x3 ≤
2000
,
3
x1 ≤
300
y1,
0.5
x2 ≤
300
y2,
2
x3 ≤
300
y3,
x1 ≥
0
, x2 ≥
0
, x3 ≥
0
,
x1 ∈ Integers, x2 ∈ Integers, x3 ∈ Integers,
y1
==
0
||
y1
==
1
,
y2
==
0
||
y2
==
1
,
y3
==
0
||
y3
==
1
}, {x1, x2, x3, y1, y2, y3}
]