-1

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 (e.g. 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
Paul C
  • 27
  • 6

1 Answers1

1

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
AirSquid
  • 10,214
  • 2
  • 7
  • 31
  • thanks for your answer! I have one problem with this, if I place my variable name where you placed R, I have an error that it expected an int type instead of LpVariable type, how do I get the number of current LpVariables? – Paul C Dec 18 '22 at 17:04
  • 1
    Hmmm... there is something else going on there as that should work just fine. You should update your question to include everything needed (not the whole model) to produce the error. See: https://stackoverflow.com/help/minimal-reproducible-example – AirSquid Dec 18 '22 at 17:07
  • I've added a part of my code in the question – Paul C Dec 18 '22 at 17:16
  • works by transforming the equation in a linear one , so instead of SR <= R / 25 I used SR * 25 <= R – Paul C Dec 18 '22 at 17:49
  • 1
    weird. both of those equations are linear. Must be something odd w/ pulp's expressions. Of note, it also works if you multiply by (1/25) instead of divide by 25... it is all equivalent, must be something in the division statement that is causing pulp to barf. – AirSquid Dec 18 '22 at 17:55
  • 1
    Not a new problem... Here's a good commentary: https://stackoverflow.com/questions/33929353/how-to-use-a-variable-as-a-divisor-in-pulp/33942766#33942766 – AirSquid Dec 18 '22 at 18:01
  • Indeed it's weird, anyway, thanks for the help – Paul C Dec 19 '22 at 07:25