简体   繁体   English

RecursionError 使用 PuLP 优化

[英]RecursionError using PuLP Optimization

I'm trying slowly scale up an optimization problem, and on the stage where I'm trying to bring a user inputted value into a comparison constraint.我正在尝试慢慢扩大优化问题,并且在我试图将用户输入的值带入比较约束的阶段。 However, a recursion error is being thrown.但是,会引发递归错误。 I'm not sure I understand why a recursion error is happening and how to fix it (see first line of Constraint section).我不确定我是否理解为什么会发生递归错误以及如何修复它(请参阅约束部分的第一行)。 It works when I hard code a value, but when I use the user input it throws an error.当我硬编码一个值时它可以工作,但是当我使用用户输入时它会抛出一个错误。

Also as a bonus, if anyone knows how to loop the constraint I would be very grateful.另外作为奖励,如果有人知道如何循环约束,我将不胜感激。 I have been banging my head on this for a while.我一直在努力解决这个问题。 Eventually this will have 10 employees and up to 55 different parts, so I figure looping through when adding constraints would be much easier.最终这将有 10 名员工和多达 55 个不同的部分,所以我认为在添加约束时循环会容易得多。 I've look online, but haven't found anything help yet.我在网上查过,但还没有找到任何帮助。

# Initial User Input
total_parts = int(input("Enter number of different parts: "))
partno = []
quantity = []
for i in range(total_parts):
    pn = input("Enter Part Number : ").strip()
    qty = input("Enter Qty of Part Number : ")
    partno.append(pn)
    quantity.append(qty)

# Pulp Model
model = pulp.LpProblem("Part production", pulp.LpMinimize)

# Construct decision variable lists
part_numbers = partno
employees = ['Employee A', 'Employee B', 'Employee C']

qty_produced = pulp.LpVariable.dicts("production",
                                     ((i, j) for i in part_numbers for j in employees),
                                     lowBound=0,
                                     cat='Continuous')
# Objective Function
model += ((
    (pulp.lpSum(
        ((10 * qty_produced[part_numbers[0], employees[0]])/.5)
        +((10 * qty_produced[part_numbers[0], employees[1]])/.85)
       + ((10 * qty_produced[part_numbers[0], employees[2]])/.65)
        +((30 * qty_produced[part_numbers[1], employees[0]])/.85)
       + ((30 * qty_produced[part_numbers[1], employees[1]])/.75)
        +((30 * qty_produced[part_numbers[1], employees[2]])/.95)
))/6))

# Constraints
model += pulp.lpSum([qty_produced[part_numbers[0], j] for j in employees]) >= quantity[0]
model += pulp.lpSum([qty_produced[part_numbers[1], j] for j in employees]) >= 10
model += ((10 * qty_produced[part_numbers[0], employees[0]])/.5) + ((30 * qty_produced[part_numbers[1], employees[0]])/.85) <= 530
model += ((10 * qty_produced[part_numbers[0], employees[1]])/.5) + ((30 * qty_produced[part_numbers[1], employees[1]])/.85) <= 530
model += ((10 * qty_produced[part_numbers[0], employees[2]])/.5) + ((30 * qty_produced[part_numbers[1], employees[2]])/.85) <= 530

# Solve problem
model.solve()
pulp.LpStatus[model.status]

for var in qty_produced:
    var_value = qty_produced[var].varValue
    print("The quantity of {1} produced by {0} is {2}".format(var[1], var[0], var_value))

avg_time = pulp.value(model.objective)

You are on the right track here, but a few issues.你在这里是在正确的轨道上,但有一些问题。 I'm not sure where you would pop a recursion error here.我不确定你会在哪里弹出递归错误。 You'd have to post the full code and the error message w/ line reference for help on that.您必须发布完整的代码和带有行参考的错误消息以获得帮助。

On your inputs... I don't see where you are converting qty to an integer?根据您的输入...我看不到您将 qty 转换为 integer 的位置? Recall, all keyboard inputs are taken in as strings, so a conversion is necessary.回想一下,所有键盘输入都是作为字符串输入的,因此需要进行转换。 While you are debugging though, it would be wisest just to make some dictionaries at the start to hold the data so you are not repeatedly typing it in. Later, you can construct the same dictionaries/lists from keyboard inputs on the fly if desired.但是,在调试时,最明智的做法是在开始时制作一些字典来保存数据,这样您就不会重复输入它。稍后,如果需要,您可以即时从键盘输入构建相同的字典/列表。 Also include your other model parameters such as the coefficients you are using in your obj function and the quantity limits.还包括您的其他 model 参数,例如您在 obj function 中使用的系数和数量限制。 That will allow you to clean up your constraints.这将允许你清理你的约束。 Here is an idea to show what I'm thinking...这是一个展示我的想法的想法......

import pulp

## Data

employees = {'bob', 'cindy', 'tom'}
parts = {'A19', 'X22'}
max_quantities = {  'A19': 200,
                    'X22': 150}

## Model
prob = pulp.LpProblem('sample', pulp.LpMinimize)

X = pulp.LpVariable.dicts("produce", [(e, p) for e in employees for p in parts],
                            lowBound=0, cat="Integer")

# max_production
for p in parts:
    prob += pulp.lpSum(X[e, p] for e in employees) <= max_quantities[p]

print(prob)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM