TypeError: Non-constant expressions cannot be multiplied

1,254 views
Skip to first unread message

Aleksandra Firkowska

unread,
Jul 9, 2018, 11:14:37 AM7/9/18
to pulp-or-discuss
Hi, 

I keep getting an error as I know that PuLP doesn't allow to multiply the variables together as it is not linear problem anymore. However I have to do it somehow, and I am not sure how.

I have 3 decision variables:

X=pulp.LpVariable.dicts('X',((row['b'],row['t'],row['c'],row['d']) for index, row in btcd.iterrows()),lowBound=0,cat='Continuous')

#Create slack and surplus variables
Slack=pulp.LpVariable.dicts('Slack',((row['t'],row['c'],row['d']) for index, row in btcd.iterrows()),lowBound=0,cat='Continuous')
Surplus=pulp.LpVariable.dicts('Surplus',((row['t'],row['c'],row['d']) for index, row in btcd.iterrows()),lowBound=0,cat='Continuous')

I want to add a following constraint:

for k in X_target_tcd.keys():
    tcd = int(str(k[0])+str(k[1])+str(k[2]))
    model+=SM_ideal.loc[tcd,"SM"]== ((Q.loc[tcd,"0"]+X_target_tcd[k])/ (Q.loc[0,"0"]+X_target) )
                                                           + (Slack[k]-Surplus[k])

The problem is dividing the X_target_tcd by X_target as these are decision variables 

X_target is the sum of all X_target_tcd


I have tried rearranging it:
model+=SM_ideal.loc[tcd,"SM"]*TOTAL==Q.loc[tcd,"0"]+X_target_tcd[k]+ (Q.loc[0,"0"]+ X_target) *(Slack[k]-Surplus[k])

However I am now multiplying the X_target by the Slack and Surplus which are also decision variables and therefore getting the same error..

Is there a way around this?

Thanks in advance!

Franco Peschiera

unread,
Jul 10, 2018, 5:40:46 AM7/10/18
to pulp-or-discuss

Hey there Aleksandra,

Are the slack variables only used in this constraint? If so, why not simply have the following (based on your second formulation):


model+=SM_ideal.loc[tcd,"SM"]*TOTAL==Q.loc[tcd,"0"]+X_target_tcd[k]+ (Slack[k]-Surplus[k])


Usually, the slack variables are only slack and need no multiplication. From my experience, it’s even better to make them always have the “real slack” of the constraint. That way you can later really understand what’s going on. If you have them in the objective function and you need to scale them, you can always multiply them with a parameter.


Franco

Aleksandra Firkowska

unread,
Jul 10, 2018, 5:59:47 AM7/10/18
to pulp-or-discuss
Hi Franco,

the slacks are also used in the objective, where I am trying to minimise them. If I do it without multiplying them by the TOTAL it is not formulated correctly and doesn't give me the solutions I want.

model+= pulp.lpSum ([ weight * (Slack[k]+Surplus[k]0 for k in X_target_tcd.keys()
                                    + [(Q.loc[t,"max"] - Q.loc[t,"0"] + X_target_t[t])) for t in X_target_t.keys()])

so if I don't multiply them by TOTAL the solution given is not correct..

Krishna Kottakki

unread,
Jul 10, 2018, 6:31:29 AM7/10/18
to pulp-or...@googlegroups.com
Hello  Aleksandra Firkowska, 

Please use:  pulp.lpDot (lpVariable1, lpVariable2)

Regards, 
Krishna  


--
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pulp-or-discuss@googlegroups.com.
Visit this group at https://groups.google.com/group/pulp-or-discuss.
For more options, visit https://groups.google.com/d/optout.

Aleksandra Firkowska

unread,
Jul 10, 2018, 9:03:28 AM7/10/18
to pulp-or-discuss
Thank you Krishna, could you please however tell me what this actually does and where should I use it ?

Thank you
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discu...@googlegroups.com.
To post to this group, send email to pulp-or...@googlegroups.com.

Krishna Kottakki

unread,
Jul 10, 2018, 10:43:25 AM7/10/18
to pulp-or...@googlegroups.com
Hi  Aleksandra,

Please check in the following way:

pulp.lpDot(weight , (Slack[k]+Surplus[k]0 for k in X_target_tcd.keys()))

weight &  Slack[k] + .... will get multiplied by means of lpDot 

Let me know if the error still persists.

Regards, 
Krishna 












--
With regards, 
Krishna Kumar Kottakki
---------------------------
---------------------------
Research Scientist
Gyan Data Private Limited 
Chennai, India.
Mobile: 9920914995

To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pulp-or-discuss@googlegroups.com.

Aleksandra Firkowska

unread,
Jul 11, 2018, 10:14:15 AM7/11/18
to pulp-or-discuss
I don't really understand what this is meant to do..
Do I need to create variables in a certain way, or do I just put this in my objective or constraint ?

All I found online was
 lpDot() --given two lists of the form [a1, a2, ..., an] and [ x1, x2, ..., xn] will construct a linear epression to be used as a constraint or variable

Does this function allow for multiplication and division of decision variables ?

Thank you for all your help

Krishna Kottakki

unread,
Jul 11, 2018, 11:16:45 AM7/11/18
to pulp-or...@googlegroups.com
Hello Aleskandra,

It's not a pre-requisite that the two variables [a1] and [x1] to be lpVariables. Even when one of these were constant lpDot() can be used. I could see in your case, there's an issue of multiplication between a constant and lpVariable, where you can make use of the same function. 

Regards, 
Krishna 

To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pulp-or-discuss@googlegroups.com.

Aleksandra Firkowska

unread,
Jul 11, 2018, 11:21:15 AM7/11/18
to pulp-or-discuss
I think multiplication by constant and variable is not the issue here, but a multiplication of variable by another variable, hence it is not linear.
My decision variables are X, slack and surplus.

However I try to rearrange my equation the X needs to always be multiplied by slack and surplus, where the error comes up.

Does the lpDot() allow multiplication of variables ?

Krishna Kottakki

unread,
Jul 11, 2018, 11:37:28 AM7/11/18
to pulp-or...@googlegroups.com
Yes. It will allow the multiplication of lpVariables. 


To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pulp-or-discuss@googlegroups.com.

Aleksandra Firkowska

unread,
Jul 12, 2018, 12:09:31 PM7/12/18
to pulp-or-discuss
Unfortunately I still get an error when I try to multiple lpVariable by lpVariable...

Clay Campaigne

unread,
Jul 12, 2018, 1:10:23 PM7/12/18
to pulp-or...@googlegroups.com
I'm pretty sure you can't multiply lpVariables -- PuLP is a linear programming wrapper, and does not currently support quadratic expressions. ​I would​ be very surprised to see an example (or documentation) proving otherwise.

For example, try the following code:
import pulp as pp

var1 = pp.LpVariable('var1')
var2 = pp.LpVariable('var2')
exp1 = pp.LpAffineExpression(var1)
try:
    c = pp.lpDot(var1,var2)
    print("successfully multiplied lpVariables, obtaining {}".format(c.__repr__()))
except Exception as e:
    print("Error: {}".format(e))
try:
    c = pp.lpDot(1.0, var1)
    print("Successfully multiplied lpVariable and constant, obtaining expression '{}'".format(c.__repr__()))
except Exception as e:
    print("Error: {}".format(e))  
try:
    c = pp.lpDot(var1, 1.0)
    print("Successfully multiplied lpVariable and constant, obtaining expression '{}'".format(c.__repr__()))
except Exception as e:
    print("Error: {}".format(e))  
try:
    c = pp.lpDot(2.0, 1.0)
    print("Successfully multiplied constant and constant, obtaining expression '{}'".format(c.__repr__()))
except Exception as e:
    print("Error: {}".format(e)) 

​Clay
Clay Campaigne
Cell Phone: (773) 732-9406

Stuart Mitchell

unread,
Jul 12, 2018, 6:42:55 PM7/12/18
to pulp-or...@googlegroups.com
No pulp can't handle non-linear expressions.

Stu

Stuart Mitchell
PhD Engineering Science
Extraordinary Freelance Programmer and Optimisation Guru

To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pulp-or-discuss@googlegroups.com.
--
Clay Campaigne
Cell Phone: (773) 732-9406

--
You received this message because you are subscribed to the Google Groups "pulp-or-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to pulp-or-discuss@googlegroups.com.

Franco Peschiera

unread,
Jul 13, 2018, 3:44:00 AM7/13/18
to pulp-or...@googlegroups.com
Usually you can reformulate a multiplication of variables to another, more complex, expression that is equivalent but linear.

I know AIMMS (another modelling language/ software) has some general (that is: language agnostic) documentation regarding different tricks, including one about variable multiplication:


Having said that, I still think it could be possible to do some slight changes to your variables' meaning to get the good solution without the need to multiply any variable. But for that, you need to maybe take a look at the complete model globally and not just one constraint / variable.

cheers,

Franco




--
You received this message because you are subscribed to a topic in the Google Groups "pulp-or-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pulp-or-discuss/JXJAsxl2pPw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pulp-or-discuss+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages