0

I'm trying to linearize a multiplication constraint I have in pulp.

It looks something like this:

prob += (1 - Y) * (3 - X)

Which is equal to:

prob += 3 - 3*Y - X + X*Y

Where Y is a binary variable, In the formula everything is linear except for the following part: Y*X. I tried to solve this by using the formula I found in another question:

Link to the picture

Link to the question

So I tried using X1*X2 = Y, Using my code this gives X*Y = A. I used the log substitution method. This gave me the following code:

prob += math.log(float(A)) = math.log(float(X)) + math.log(float(y))

And the other constraint:

prob += 3 - 3*Y - X + A

Only this doesn't seem to work because a float can only be a string or a number and not a LpVariable. Is there a way to solve this problem using the first substitution method given in the image of the formula?

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • The transformations you show in the table are non-linear. PuLP requires the constraints to be represented by linear relationships between variables. Thus, they are not implementable, unfortunately. There are other potential transformations available that are linear but you require to include more information about the original problem. – pchtsp Oct 07 '21 at 14:40

1 Answers1

0

Based on the documentation on their PyPi page it looks like what you may need is to just call the value of your LpVariable. That will give you the number value that you can then use in your calculations

https://pypi.python.org/pypi/PuLP

You can get the value of the variables using value(). ex:

value(x) : 2.0

So your function may be able to be re-written as

prob += math.log(float(value(A))) = math.log(float(value(X))) + math.log(float(value(y)))

The float() declarations may also be unnecessary as well since it looks like LpVariables store their internal numbers as floats normally

huma474
  • 111
  • 3
  • Thanks, but when I do that it says: 'float() argument must be a string or a number, not 'NoneType' and if I remove the float() declarations it says 'a float is required'. I don't think taking the log of an LpVariable is possible. – WhatTheShrimp Nov 17 '16 at 06:38
  • Dug into their source code for the module, getting the value is a different call sorry. from their code over here - https://github.com/coin-or/pulp/blob/master/src/pulp/pulp.py - in the LpVariable Class : def value(self): return self.varValue so to get the value of a variable is x.value() and not value(x), that should give you your value, assuming the variable had a value assigned to it. you will need to keep the float() part though, internally there's no assignment of the value to a float type in the LpVariable class. – huma474 Nov 18 '16 at 01:25