【问题标题】:How to formulate and solve optimization problem with pulp in python如何在python中用纸浆制定和解决优化问题
【发布时间】:2022-11-11 00:34:48
【问题描述】:

输入:

  • 2 个变量列表,list_1 和 list_2。
  • 从每个列表中获取一个元素并返回某个值 >=0 的函数: 富(el_list1,el_list2) 我们可以假设我们有一个形状为 (len(list_1), len(list_2)) 的矩阵,其值 >=0。

任务:假设每个列表的每个元素只能使用一次,查找 list_1、list_2 的元素对,它们给出 foo(el_list1, el_list2) 的最大总和。

例子:

  • list_1 = [l1_0,l1_1,l1_2]

  • list_2 = [l2_0,l2_1,l2_2,l2_3]

A = 
[[0.0, 1.5, 2.4, 0.8],
 [3.1, 0.5, 0.0, 0.0],
 [0.0, 1.3, 2.5, 1.0]
],
where A[i, j] = foo(list_1[i],list_2[j])

对于此示例,结果可能类似于:

  • 最大总和 = 3.1 + 1.5 + 2.5 + 0 = 7.1
  • 给出这个总和的对:
    [(l1_1;l2_0),(l1_0;l2_1),(l1_2;l2_2),(无,l2_3)]

【问题讨论】:

    标签: python pulp


    【解决方案1】:
    from pulp import *
    
    model = pulp.LpProblem("_problem", LpMaximize)
    
    A = [[0.0, 1.5, 2.4, 0.8],
     [3.1, 0.5, 0.0, 0.0],
     [0.0, 1.3, 2.5, 1.0]
    ]
            
    # Creates a list of all the supply nodes
    list1 = ["I0", "I1", "I2"]
    
    # Creates a list of all demand nodes
    list2 = ["J0","J1", "J2", "J3"]
    
    # The cost data is made into a dictionary
    Acosts = makeDict([list1,list2],A,0)
    
    Routes = [(w,b) for w in list1 for b in list2]
    
    pv = LpVariable.dicts("Route",(list1,list2),0)
    
    # Objective
    model += lpSum([pv[w][b]*Acosts[w][b] for (w,b) in Routes])
    
    # Because you don't have any constraints, I made it up. 
    for w in list1:
        model += lpSum([pv[w][b] for b in list2])==1, "Sum_of_Products_out_of_plants_%s"%w
    
    for b in list2:
        model += lpSum([pv[w][b] for w in list1])==1, "Sum_of_Products_into_centers%s"%b
        
    model.solve()
    print("Status:", LpStatus[model.status])
    for v in model.variables():
        print(v.name, "=", v.varValue)
    print("Maximize Cost = ", pulp.value(model.objective))
    

    输出:

    Status: Infeasible
    Route_I0_J0 = 0.0
    Route_I0_J1 = 1.0
    Route_I0_J2 = 0.0
    Route_I0_J3 = 0.0
    Route_I1_J0 = 1.0
    Route_I1_J1 = 0.0
    Route_I1_J2 = 0.0
    Route_I1_J3 = 0.0
    Route_I2_J0 = 0.0
    Route_I2_J1 = 0.0
    Route_I2_J2 = 1.0
    Route_I2_J3 = 1.0
    Maximize Cost =  8.1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多