Have LpAffineExpression as an exponent

863 views
Skip to first unread message

Charith Peris

unread,
Feb 12, 2017, 4:11:19 PM2/12/17
to pulp-or-discuss
Hi,

I am very new to pulp and have not completely grasped how the LpAffineExpression variables should be used. I have got a code (shown below) which works fine when the score in the objective function is assigned a simple expression (first commented out "score"). However, I want the score to decrease as a function of the distances (as shown in the third "score"). But when I do this pulp tells me "AttributeError: 'LpAffineExpression' object has no attribute 'exp'". Even I try a simple divide operation (as shown in the second commented out "score") it tell me that "TypeError: Expressions cannot be divided by a non-constant expression". Can someone please help me to get to the third score expression in my objective?

The code generates its own data so can be run easily in order to reproduce the error.

Thanks,
Charith


```
Enter code here...

import pandas as pd
import numpy as np
import random
from pulp import LpInteger, LpVariable, LpProblem, lpSum, LpMinimize, LpStatus

# Test data set
s
= 20
m1
= list(np.random.choice(10, size=s, replace=True))
dist1
= list(np.random.choice(1000, size=s, replace=True))
dist2
= list(np.random.choice(1000, size=s, replace=True))


ms
= pd.DataFrame(
   
{'index': m1,
     
'dist1' : dist1,
     
'dist2' : dist2
   
})

def objective_function(row, x1, x2):
   
    index
= ms["index"].ix[row]
    dist1
= ms["dist1"].ix[row]
    dist2
= ms["dist2"].ix[row]
   
   
#score = index*(x1 + x2) + (dist1*x1 + dist2*x2)
   
#score = index*(x1 + x2)/(dist1*x1 + dist2*x2)
    score
= index*(x1 + x2)/ ( 1 + np.exp(dist1*x1 + dist2*x2))
   
   
return score

rowset
=  list(np.arange(len(ms)))
min_rows
= 5
max_rows
= 5

# Define variable
x1
= LpVariable.dicts('res1', rowset, lowBound = 0, upBound = 1, cat = LpInteger)
x2
= LpVariable.dicts('res2', rowset, lowBound = 0, upBound = 1, cat = LpInteger)

# Define model
prob
= LpProblem("Model", LpMinimize)

# Input objective function
prob
+= lpSum([objective_function(row, x1[row], x2[row]) for row in rowset])

# Input constraints to model
# The algorithm should turn on at least the minimum number of rows
prob
+= lpSum([x1[row] for row in rowset]) \
   
>= min_rows, "Minimum_rows_set1"
   
# The algorithm should turn on at least the minimum number of rows
prob
+= lpSum([x2[row] for row in rowset]) \
   
>= min_rows, "Minimum_rows_set2"

for row in rowset:
    prob
+= lpSum([x1[row] + x2[row]]) \
       
<= 1, "Choice_of_resources_%s"%row
   
# Solve the optimization problem
prob
.solve()

# Print chosen rows
print ("Rows for set1:")
for row in rowset:
   
if x1[row].value():
       
print(row)
       
print ("Rows for set2:")
for row in rowset:
   
if x2[row].value():
       
print(row)


Stuart Mitchell

unread,
Feb 12, 2017, 4:16:03 PM2/12/17
to pulp-or...@googlegroups.com
Hi Pulp only supports Linear Programming problems. Thus it is only possible to multiply variables and expressions by constants and add or subtract them from each other.

Stu

--
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.



--
Stuart Mitchell
PhD Engineering Science
Extraordinary Freelance Programmer and Optimisation Guru
Reply all
Reply to author
Forward
0 new messages