如何使用Mathematica解规划模型?
如何使用Mathematica解规划模型?
Mathematica从5.0开始就可以解复杂的规划模型,主要用Maximize和Minimize这两个函数。下面给出几个例子,说明如何用这两个函数来求解规划问题。- 线性规划
(*linearprogramming*)
Maximize[{x1+x2+x3+x4,(*objectivefunction*)
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*)
]
Maximize[{x1+x2+x3+x4,(*objectivefunction*)
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}}
124660.,x5->115062.,x6->129498.,x7->0.,x8->5440.01}}
其中,933400为目标函数的最优值,紧接的表为目标函数达到最优值时的结果。
- 二次规划
- 0-1规划
(*0-1Programming*)
Maximize[{3x1-2x2+5x3,
x1+2x2-x3≤2,
x1+4x2+x3≤4,
x1+x2≤3,
4x2+x3≤6,
x1==1||x1==0,
x2==1||x2==0,
x3==1||x3==0
},
{x1,x2,x3}
]
Maximize[{3x1-2x2+5x3,
x1+2x2-x3≤2,
x1+4x2+x3≤4,
x1+x2≤3,
4x2+x3≤6,
x1==1||x1==0,
x2==1||x2==0,
x3==1||x3==0
},
{x1,x2,x3}
]
- 整数规划
(*IntegerProgramming*)
Maximize[{40x1-90x2,
9x1+7x2≤56,
7x1+20x2≤70,
x1≥0,
x2≥0,
x1∈Integers,
x2∈Integers
},{x1,x2}
]
Maximize[{40x1-90x2,
9x1+7x2≤56,
7x1+20x2≤70,
x1≥0,
x2≥0,
x1∈Integers,
x2∈Integers
},{x1,x2}
]
- 求近似解(用NMaximize/NMinimize)
NMinimize[{8x1+10x2+7x3+6x4+11x5+9x6,
12x1+9x2+25x3+20x4+17x5+13x6≥60,
35x1+42x2+18x3+31x4+56x5+49x6≥150,
37x1+53x2+28x3+24x4+29x5+20x6≥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}]
12x1+9x2+25x3+20x4+17x5+13x6≥60,
35x1+42x2+18x3+31x4+56x5+49x6≥150,
37x1+53x2+28x3+24x4+29x5+20x6≥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[{120x1+10x2+100x3-5000y1-2000y2-1000y3,
5x1+x2+4x3≤2000,
3x1≤300y1,
0.5x2≤300y2,
2x3≤300y3,
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}
]
5x1+x2+4x3≤2000,
3x1≤300y1,
0.5x2≤300y2,
2x3≤300y3,
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}
]
- 一个特殊的例子(帮助中说的原本的用法)