简体   繁体   English

使用 Python 的纸浆时如何创建基于决策变量的约束程序

[英]How to create a program for constraints based on decision variables when using Python's pulp

Introduction介绍

I would like to create a "switch using a decision variable (if syntax)" using Python's pulp.我想使用 Python 的纸浆创建一个“使用决策变量(如果语法)的开关”。 Here, "switch using decision variables (if syntax)" means, for example, "when x (x is an integer greater than or equal to 0) and y (y is a binary variable of 0, 1) are decision variables, if x is an integer greater than or equal to 1, y outputs 1, and if x is 0, y outputs 0. The following is a simple example. The formula is shown in Problem Formulation 3 (image) attached below.这里,“使用决策变量(if语法)切换”的意思是,例如,“当x(x是大于或等于0的整数)和y(y是0、1的二进制变量)是决策变量时,如果x为大于等于1的整数,y输出1,如果x为0,y输出0,下面是一个简单的例子,公式如下图问题公式3(图片)所示。

The following is a simple example of how we tried to create a "switch with decision variables (if syntax)" and the results we achieved.以下是我们如何尝试创建“带有决策变量(if 语法)的开关”的简单示例以及我们取得的结果。

The examples were created by referring to the examples in "Introduction to Operations Research" (Tokai University Press) .示例是参考“运筹学导论”(东海大学出版社)中的示例创建的。

An ice cream shop is planning to produce two kinds of ice cream: espresso ice cream and raspberry ice cream.一家冰淇淋店计划生产两种冰淇淋:浓缩咖啡冰淇淋和覆盆子冰淇淋。 However, he cannot produce as much as he wants because he is limited to producing 8000 cc of milk and working for 360 minutes.然而,他不能生产出他想要的那么多,因为他只能生产 8000 毫升的牛奶和工作 360 分钟。 With this amount of milk and time required for each ice cream, what is the plan to increase production to maximize profits?有了每个冰淇淋所需的牛奶量和时间,增加产量以最大化利润的计划是什么? Today, however, the quantity of raspberries (the ingredients) for one serving of raspberry ice cream will expire.然而,今天,一份覆盆子冰淇淋的​​覆盆子(成分)数量将过期。 Therefore, you need to produce at least one raspberry ice cream so that it does not go to waste.因此,您需要至少生产一种覆盆子冰淇淋,以免浪费。

Product name产品名称 Quantity of milk needed所需奶量 Working time工作时间 Profit利润
Espresso ice cream咖啡冰淇淋 100cc 100cc 7 minutes 7 分钟 50 yen 50日元
Raspberry ice cream覆盆子冰淇淋 150cc 150cc 5 minutes 5分钟 10 yen 10日元

The above problem setup can be formulated as follows上面的问题设置可以表述如下

Problem Formulation1问题表述1

As a Python program, it can be expressed as follows作为一个Python程序,可以表示如下

import pulp

problem = pulp.LpProblem('ice', pulp.LpMaximize)

# Define the decision variables
x_e = pulp.LpVariable('x_e', lowBound=0, cat=pulp.LpInteger) 
x_r = pulp.LpVariable('x_r', lowBound=0, cat=pulp.LpInteger) 

# Set the objective function
problem += 50*x_e +10*x_r

# Set constraints
problem += 100*x_e + 150* x_r <= 8000
problem += 7*x_e + 5*x_r <= 360

## Newly added constraint
problem += x_r >= 1

# # optimize
problem.solve()

# # print the result
print("Espresso",x_e.value(), "pieces")
print("raspberry",x_r.value(), "pieces")
print("profit",pulp.value(problem.objective), "yen")

The result of running the above program is as follows.运行上述程序的结果如下。 We were able to maximize our profit while not discarding the raspberries that were about to expire (the amount of one raspberry ice cream).我们能够在不丢弃即将过期的覆盆子(一个覆盆子冰淇淋的​​量)的同时实现利润最大化。

Espresso 50.0 pieces
raspberry 2.0 pieces
profit 2520.0 yen

Challenge: After introducing a constraint that functions as a "switch with decision variables (if syntax)" of my own creation (Ice Cream Production Problem)挑战:在引入了我自己创建的“带有决策变量(如果语法)的开关”的约束之后(冰淇淋生产问题)

In the previous chapter, we added the following constraints to the original basic problem在上一章中,我们在原来的基本问题上增加了以下约束

Problem Formulation2问题表述2

#Newly added constraints
problem += x_r >= 1

In order to align with the theme of this question, we will rewrite this constraint as a "switch with decision variables (if syntax)" constraint like the following为了与这个问题的主题保持一致,我们将这个约束重写为“带有决策变量的开关(如果语法)”约束,如下所示

Problem Formulation3问题表述3

import pulp

problem = pulp.LpProblem('ice', pulp.LpMaximize)

# Define a decision variable
x_e = pulp.LpVariable('x_e', lowBound=0, cat=pulp.LpInteger) 
x_r = pulp.LpVariable('x_r', lowBound=0, cat=pulp.LpInteger)

### Newly added decision variable
y_r = pulp.LpVariable('y_r', lowBound=0, cat=pulp.LpBinary)

# Set the objective function
problem += 50*x_e +10*x_r

# Set constraints
problem += 100*x_e + 150* x_r <= 8000
problem += 7*x_e + 5*x_r <= 360

### Newly added constraint
if x_r.value() >= 1:
  y_r=1
  problem += y_r == 1

# # Optimize
problem.solve()

# Print the result
print("Espresso",x_e.value(), "pieces")
print("raspberry",x_r.value(), "pieces")
print("profit",pulp.value(problem.objective), "yen")

The result is as follows, and I got an error.结果如下,我得到了一个错误。

   if x_r.value() >= 1:
TypeError: '>=' not supported between instances of 'NoneType' and 'int'

I tried to work on it in the above, but I could not create a switch (if syntax) using decision variables.我试图在上面处理它,但我无法使用决策变量创建开关(如果语法)。

Is it impossible to create and solve such a constraint (= switch (if syntax) using decision variables) in pulp?是否不可能在纸浆中创建和解决这样的约束(= 使用决策变量切换(如果语法))? (I'm sorry for my lack of study, but is it a non-linear problem and cannot be expressed in pulp?) (I'm sorry for my lack of study, but maybe it's a non-linear problem and can't be expressed in pulp?) Or is it just a badly written program on my part? (抱歉我没学好,但它是非线性问题,不能用纸浆表达吗?)(对不起我没学,但也许是非线性问题,不能用纸浆表达?)或者它只是我编写的一个糟糕的程序?

If you could please tell me the cause and solution, I would appreciate it.如果您能告诉我原因和解决方案,我将不胜感激。 (If possible, I would like to use pulp, which I am familiar with, but if it is possible to write the program without pulp, I would like to challenge it, so please let me know.) (如果可以,我想用我熟悉的纸浆,但如果没有纸浆可以写程序,我想挑战一下,所以请告诉我。)

You cannot add a constraint based on x_r.value() because this won't be available until the problem has been solved.您不能添加基于x_r.value()的约束,因为在问题解决之前这将不可用。

A better approach would be to connect x_r and y_r through two additional constraints:更好的方法是通过两个附加约束连接x_ry_r

problem += M*y_r >= x_r
problem += y_r <= x_r

Here M is a big enough number, with your data setting M = min(8000/150, 360/5) would suffice.这里M是一个足够大的数字,您的数据设置M = min(8000/150, 360/5)就足够了。

Try:尝试:

   if x_r.value() >= 1:

You forgot the parenthesis I believe你忘了括号我相信

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

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