Python纸浆数学优化中如何使用索引和导入数据

2024-10-01 15:45:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我过去曾使用过优化建模器,但这已经有一段时间了,我仍在学习如何在python中实现这一点。我已经做了一些在线研究,并且正在使用PuLp的开源版本https://pythonhosted.org/PuLP/

我已经制定了以下三个地点的工作。我的问题是如何用从CSV导入的数据来表示问题,因为我的数据集实际上包含70个位置。输入到这些路径的路径的数量看起来很愚蠢;我想有更好的方法来编程。我知道如何使用csv.reader()但我不太确定如何着手解决这个问题。在

示例模型:

"""
Traveling Salesman Problem (TSP) Simplified Model
Date: 2017-03-30
"""

# Import PuLP modeler functions
from pulp import *

# Create the 'prob' variable to contain the problem data
prob = LpProblem("The TSP Problem1",LpMinimize)

# Formulation summary
#   The decision variable x is equal to 1 or 0, whether the path is chosen
#   Each path has a cost associated with it
#   The objective is to choose the shortest path
#   The constraint, essentially, is that each location is visited

# The LpVariable class has four parameters
    # the first is an arbitrary name
    # the second is the lower bound
    # the third is the upper bound
    # the fourth is the type of data, discrete or continuous
x12=LpVariable("flow12",0,None,LpInteger)
x13=LpVariable("flow13",0,None,LpInteger)
x21=LpVariable("flow21",0,None,LpInteger)
x23=LpVariable("flow23",0,None,LpInteger)
x31=LpVariable("flow31",0,None,LpInteger)
x32=LpVariable("flow32",0,None,LpInteger)

# The objective function is added to 'prob' first
# the objective is to minimize the total distance traveled 
# the numbers represent the distances between two variables

prob += 30*x12 + 5*x13 +25*x21 + 1*x23 + 5*x31 + 8*x32, "Total Distance Traveled"


# The constraints: 

# each location needs to be on the route but only once
prob += x12 + x32 == 1.0, "Location2 is visited"
prob += x13 + x23 == 1.0, "Location3 is visited"
prob += x21 + x31 == 1.0, "Location1 is visited"

# each location is departed exactly once
prob += x31 + x32 == 1.0, "Location3 is departed"
prob += x21 + x23 == 1.0, "Location2 is departed"
prob += x12 + x13 == 1.0, "Location is departed"

# The problem data is written to an .lp file
prob.writeLP("SupplyChainAssignment.lp")

# The problem is solved using PuLP's choice of Solver
prob.solve()

# The status of the solution is printed to the screen
print "Status:", LpStatus[prob.status]

# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
    print v.name, "=", v.varValue

# The optimized objective function value is printed to the screen
print "Total Cost TSP Assignment = ", value(prob.objective)

这就是目前正在运行的示例的结尾。在

我想我可以用下面这样的字典来表示距离:

^{pr2}$

但我不确定以下方法是否适用于变量:

x = {'x12',
     'x13', 
     'x21',
     'x23', 
     'x31',
     'x32'}

x=LpVariable("flow",0,1,LpInteger)

这也不能解决我如何使用索引重写约束的问题,因此我还没有测试我对上述语法的猜测是否有效:

prob += x12 + x32 == 1.0, "Location2 is visited"
prob += x13 + x23 == 1.0, "Location3 is visited"
prob += x21 + x31 == 1.0, "Location1 is visited"

# each location is departed
prob += x31 + x32 == 1.0, "Location3 is departed"
prob += x21 + x23 == 1.0, "Location2 is departed"
prob += x12 + x13 == 1.0, "Location is departed"

关于我应该阅读什么的建议,或者如果有什么你可以给我指出的例子,都会很有帮助。在

谢谢!在


Tags: thetoisx12probx13visitedx31
1条回答
网友
1楼 · 发布于 2024-10-01 15:45:43

阅读更多的文档,您将看到一些很好的示例

https://pythonhosted.org/PuLP/CaseStudies/a_blending_problem.html#solution-to-full-problem

从本文档中,您将看到以下内容


# A dictionary called 'ingredient_vars' is created to contain the referenced Variables
ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)

由于costs和component_vars现在是以引用键作为成分名称的字典,因此可以通过列表理解简单地提取数据,如图所示。函数的作用是:添加结果列表的元素。因此,只需输入目标函数并为其指定一个名称:

^{pr2}$

进一步的列表理解被用来定义另外5个约束,它们也是描述它们的名字。在

# The five constraints are added to 'prob'
prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "PercentagesSum"
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 8.0, "ProteinRequirement"
prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 6.0, "FatRequirement"
prob += lpSum([fibrePercent[i] * ingredient_vars[i] for i in Ingredients]) <= 2.0, "FibreRequirement"
prob += lpSum([saltPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 0.4, "SaltRequirement"

在这个例子中,您可以看到如何使用变量字典和参数字典来定义问题。在

然后你必须使用csv模块来读取参数数据(我建议csv.DictReader执行此操作)

不过,您是对的,在文档中有一个特定的示例会很好。在

相关问题 更多 >

    热门问题