PuLP—线性规划高级例子

文章来自Optimization Modeling in Python: PuLP, Gurobi, and CPLEX,做了简化。

问题:
m i n ∑ i = 1 n ∑ j = 1 m c i j x i j s . t . ∑ i = 1 n a i j x i j ≤ b j ∀ j x i j ≥ l i j ∀ i , j x i j ≤ u i j ∀ i , j \begin{aligned} & min & \quad \sum_{i=1}^{n} \sum_{j=1}^{m} c_{ij}x_{ij} \\ & \\ & s.t. \quad & \sum_{i=1}^{n}a_{ij}x_{ij} \le b_j \quad \forall j \\ & \quad & x_{ij} \ge l_{ij} \quad \forall i,j \\ & \quad & x_{ij} \le u_{ij} \quad \forall i,j \\ \end{aligned} mins.t.i=1nj=1mcijxiji=1naijxijbjjxijliji,jxijuiji,j
其中, a , b , c a,b,c a,b,c l , u l,u l,u都是已知的参数,要求 x x x。明显这是一个线性规划问题, l l l是下界约束, u u u是上界约束。

Python代码:

# coding =utf-8
import random
from pulp import LpProblem, LpVariable, LpConstraint, LpConstraintLE, lpSum, LpMinimize, LpContinuous, LpStatus

random.seed(615)

# Params
n = 10
m = 5
I = range(1, n + 1)
J = range(1, m + 1)
c = {(i, j): random.normalvariate(0, 1) for i in I for j in J}
a = {(i, j): random.normalvariate(0, 5) for i in I for j in J}
b = {j: random.randint(0, 30) for j in J}
l = {(i, j): random.randint(0, 10) for i in I for j in J}
u = {(i, j): random.randint(10, 20) for i in I for j in J}

# Create a new model
model = LpProblem(name="MyModel", sense=LpMinimize)

# Create variables
x_vars = {(i, j): LpVariable(cat=LpContinuous, lowBound=l[i, j], upBound=u[i, j], name="x_{0}_{1}".format(i, j))
          for i in I for j in J}

# Add constraints
constraints = {j: model.addConstraint(LpConstraint(e=lpSum(a[i, j] * x_vars[i, j] for i in I),
                                                   sense=LpConstraintLE, rhs=b[j],
                                                   name="constraint_{0}".format(j))) for j in J}

# Set objective
objective = lpSum(x_vars[i, j] * c[i, j] for i in I for j in J)
model.setObjective(objective)

# Calculate with the default CBC optimizer
status = model.solve()

if LpStatus[status] == 'Optimal':
    for v in model.variables():
        print(v.name, v.varValue)
    print(f'### Optimal value:{model.objective.value()}')

参数:

I:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
J:[1, 2, 3, 4, 5]
c:{(1, 1): -0.4997407219933318, (1, 2): 0.3134848143780606, (1, 3): -0.5484484918143573, (1, 4): -1.516567863158416, (1, 5): 0.7950242867610001, (2, 1): -2.6643505331434927, (2, 2): -1.025647992319976, (2, 3): -0.00542041111555558, (2, 4): -0.03371645436756252, (2, 5): 0.8315981133252557, (3, 1): -0.634385093896054, (3, 2): -0.4259518347028522, (3, 3): 0.029622245326267158, (3, 4): -1.032013259912289, (3, 5): 0.9379890186101643, (4, 1): 0.7473619984894426, (4, 2): 0.9698110192882912, (4, 3): -0.7346583063279307, (4, 4): 0.5009470344528884, (4, 5): 0.9105210771521489, (5, 1): 0.26700505930797397, (5, 2): 1.4978001885243544, (5, 3): -0.29232432628381677, (5, 4): -0.20954834960836538, (5, 5): 0.862944980092729, (6, 1): 0.677968441675048, (6, 2): 0.8460784144389238, (6, 3): 1.3285958025240843, (6, 4): -1.793841952960535, (6, 5): -0.2544111715028798, (7, 1): -0.4944639407935773, (7, 2): -0.0035563712327750835, (7, 3): 1.720813002141594, (7, 4): 1.4569497961294893, (7, 5): 1.1267565374420456, (8, 1): -1.0182927087552192, (8, 2): -0.6865168667950509, (8, 3): -0.31966071237097476, (8, 4): -1.3308655970606895, (8, 5): 0.19401936862980598, (9, 1): -0.12484726497916372, (9, 2): 0.8700171952330454, (9, 3): 0.8571039464326584, (9, 4): 2.1468072152393223, (9, 5): -0.2870739420917296, (10, 1): -2.0179176225277304, (10, 2): -0.4336884097845431, (10, 3): -0.1388989279961204, (10, 4): 0.7985707764672813, (10, 5): 0.7266430263212601}
a:{(1, 1): -0.6013870345388351, (1, 2): -1.374695926969495, (1, 3): 5.045353636458976, (1, 4): 5.31051522688759, (1, 5): 4.868853064245902, (2, 1): 0.8578835574964747, (2, 2): 1.9237306560631084, (2, 3): -4.238633087723224, (2, 4): 1.1581401698845264, (2, 5): -1.2505489219728203, (3, 1): -2.0146213538523705, (3, 2): 4.178831107014905, (3, 3): -0.9760470110963132, (3, 4): 4.281221304822003, (3, 5): 4.31062514863164, (4, 1): -3.508116194771368, (4, 2): 1.0208245957835453, (4, 3): -2.0811821008170646, (4, 4): -6.101883416244098, (4, 5): -3.88091862260002, (5, 1): -2.330559474104569, (5, 2): -3.903920775965308, (5, 3): 4.657308806034402, (5, 4): 2.3945817711437773, (5, 5): 6.91659107808716, (6, 1): 2.5893671430370597, (6, 2): 3.4509735274406723, (6, 3): 3.1557756937114014, (6, 4): -7.538637401214347, (6, 5): -10.936422476739622, (7, 1): 3.3610233230283177, (7, 2): 11.868177096084738, (7, 3): -5.23637802791627, (7, 4): 12.842709234868028, (7, 5): -1.4057145922395722, (8, 1): -3.620129516475024, (8, 2): 1.3455598700177336, (8, 3): -1.7020320128298991, (8, 4): -4.021583223493273, (8, 5): 0.8450636511162299, (9, 1): 2.3449036877174785, (9, 2): 2.0965306644455257, (9, 3): -8.986751349050394, (9, 4): 5.799136321962887, (9, 5): 0.31204144784861404, (10, 1): -1.7524360743912122, (10, 2): -5.493899227416156, (10, 3): -5.2984485752370265, (10, 4): -4.753107972640907, (10, 5): 8.121695410340237}
b:{1: 19, 2: 13, 3: 30, 4: 20, 5: 18}
l:{(1, 1): 3, (1, 2): 10, (1, 3): 6, (1, 4): 10, (1, 5): 7, (2, 1): 6, (2, 2): 4, (2, 3): 5, (2, 4): 3, (2, 5): 5, (3, 1): 4, (3, 2): 4, (3, 3): 9, (3, 4): 3, (3, 5): 6, (4, 1): 5, (4, 2): 3, (4, 3): 0, (4, 4): 3, (4, 5): 1, (5, 1): 8, (5, 2): 2, (5, 3): 9, (5, 4): 9, (5, 5): 4, (6, 1): 2, (6, 2): 0, (6, 3): 2, (6, 4): 0, (6, 5): 7, (7, 1): 0, (7, 2): 8, (7, 3): 4, (7, 4): 3, (7, 5): 7, (8, 1): 5, (8, 2): 2, (8, 3): 3, (8, 4): 7, (8, 5): 2, (9, 1): 1, (9, 2): 5, (9, 3): 8, (9, 4): 4, (9, 5): 4, (10, 1): 5, (10, 2): 9, (10, 3): 1, (10, 4): 1, (10, 5): 0}
u:{(1, 1): 18, (1, 2): 15, (1, 3): 12, (1, 4): 11, (1, 5): 17, (2, 1): 11, (2, 2): 18, (2, 3): 11, (2, 4): 13, (2, 5): 16, (3, 1): 13, (3, 2): 16, (3, 3): 16, (3, 4): 20, (3, 5): 18, (4, 1): 16, (4, 2): 11, (4, 3): 20, (4, 4): 14, (4, 5): 16, (5, 1): 12, (5, 2): 20, (5, 3): 15, (5, 4): 20, (5, 5): 19, (6, 1): 15, (6, 2): 11, (6, 3): 16, (6, 4): 10, (6, 5): 12, (7, 1): 13, (7, 2): 18, (7, 3): 15, (7, 4): 10, (7, 5): 15, (8, 1): 11, (8, 2): 19, (8, 3): 12, (8, 4): 11, (8, 5): 18, (9, 1): 12, (9, 2): 19, (9, 3): 12, (9, 4): 11, (9, 5): 11, (10, 1): 19, (10, 2): 12, (10, 3): 10, (10, 4): 16, (10, 5): 18}

输出:

x_10_1 19.0
x_10_2 12.0
x_10_3 10.0
x_10_4 1.2076868
x_10_5 0.0
x_1_1 18.0
x_1_2 15.0
x_1_3 12.0
x_1_4 11.0
x_1_5 7.0
x_2_1 11.0
x_2_2 18.0
x_2_3 11.0
x_2_4 3.0
x_2_5 5.0
x_3_1 13.0
x_3_2 4.0
x_3_3 9.0
x_3_4 20.0
x_3_5 6.0
x_4_1 5.0
x_4_2 3.0
x_4_3 20.0
x_4_4 14.0
x_4_5 1.0
x_5_1 8.0
x_5_2 20.0
x_5_3 15.0
x_5_4 9.0
x_5_5 4.0
x_6_1 2.0
x_6_2 0.0
x_6_3 2.0
x_6_4 10.0
x_6_5 12.0
x_7_1 13.0
x_7_2 8.0
x_7_3 4.0
x_7_4 3.0
x_7_5 7.0
x_8_1 11.0
x_8_2 13.223213
x_8_3 12.0
x_8_4 11.0
x_8_5 2.0
x_9_1 12.0
x_9_2 5.0
x_9_3 8.0
x_9_4 4.0
x_9_5 11.0
### Optimal value:-132.80148189199
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页