简体   繁体   中英

Creating a conditional constraint for a specific Python Pulp Maximization problem

I can't manage to find a way to create a constraint that sounds like this: for example I have 2 variables, one is a regular product and the other one is a super rare product. In order to have a super rare product, you will need to have already 25 of the regular version of that product. This can be stackable (eg if the algorithm select 75 of that regular product, it can have 3 super rare). The reason for this is that the super rare is more profitable, so if I place it without any constraints, it will select only the super rare ones. Any ideas on how to write such a constraint?

Thanks in advance!

Part of the code:

hwProblem = LpProblem("HotWheels", LpMaximize)

# Variables
jImportsW_blister = LpVariable("HW J-Imports w/ blister", lowBound=20, cat=LpInteger)  # regular product
jImportsTH = LpVariable("HW J-Imports treasure hunt", lowBound=None, cat=LpInteger)  # super rare product

# Objective Function
hwProblem += 19 * jImportsW_blister + 350 * jImportsTH  # profit for each type of product

# Constraints
hwProblem += jImportsW_blister <= 50, "HW J-Imports maximum no. of products"
hwProblem += jImportsTH <= jImportsW_blister / 25
                            # ^this is where the error is happening

There's a few "missing pieces" here regarding the structure of your model, but in general, you can limit the "super rare" ( SR ) by doing something like:

prob += SR <= R / 25

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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