writing constraints/expressions with multiple indices in pulp

2,650 views
Skip to first unread message

nikhil khinchi

unread,
Oct 6, 2014, 4:48:13 PM10/6/14
to pulp-or...@googlegroups.com

Hi, 

I need to model a linear equation which has several constraints summing over multiple indices. for example

where xcrop is the decision variable while landreq and agarea are parameters indexed by multiple indexes. since numpy n dimensional array only index by integers while my index have strings values (lists), is there anyway to write this constraint in pulp.out of  l,t,c,a,fs,p,k,tm only p is integer type while others are strings values for example  c={maize, wheat, rice}. pls also a suggest a method for reliably converting the strings value indexes into integers since I need to read parameters data form excel sheet and stored in numpy ndarrays.


Regards
Nikhil

Stuart Mitchell

unread,
Oct 6, 2014, 6:01:23 PM10/6/14
to pulp-or...@googlegroups.com
do you need to use Numpy?

I would suggest using normal python dictionaries for all variables and parameters.

If you are interoperating with Excel I suggest you use http://solverstudio.org/

the constraint you have shown above will endup looking something like.

for l in L:
    for a in A:
         for fs in FS:
            .....
                prob += lpSum(xcrop[l,t2,c,a,fs,p,k] * landreq[tm,c] for t2 in ta[t,a] for c in C) <= agarea[l,a] * fsize[f,s]


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-discu...@googlegroups.com.
To post to this group, send email to pulp-or...@googlegroups.com.
Visit this group at http://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

nikhil khinchi

unread,
Oct 7, 2014, 6:41:47 AM10/7/14
to pulp-or...@googlegroups.com
Hi,

Thanks for the swift reply. I do have a question about using python dictionaries for parameter or (constants to be multiplied with decision variables).  I think they (python dictionaries) support only a single key value pair, since I have several seperate indices mapping to value for ex. 3 different indices for a parameter. one option is to concatenate the three index, use their all possible combinations as a list and us the elements of the list as index for a dictionary for ex. {'L1','c1,''2010'};40.56  - from a three seperate lists for L {L1,L2,L3,L4...} and likewise. 

but ideally I'd like to use a data structure wherin I can access the values in the form parameter1[L1][c1][2010] = 40.56 so that I can model the constraints in a fashion similar to mathematical formulation. would a python matrix work in this case. I do numpy ndarray work but require integer indexes. I am new to both python and pulp. 

I have already written a code on coopr pyomo but since coopr has a sketchy support for building instances from data contained in spreadsheet (especially for large datasets). moreover there are certain aspects/functionalities requried since I have build a UI over it owing to which I don't want to depend on the package it self in the event it may pose problems later. 

Regards
Nikhil

--
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/9ULfddjsNJw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pulp-or-discu...@googlegroups.com.

Toby Davies

unread,
Oct 7, 2014, 6:25:04 PM10/7/14
to pulp-or...@googlegroups.com
Nikhil,
There is nothing stopping you from using tuples as keys. I've given an example of creating and acessing a key from the dictionary

# Creating a literal dictionary
parameter1 = {
    ('L1', 'c1', '2010'): 40.56,
   
('L1', 'c1', '2011'): 42.56
}

# Using the data:
my_function(parameter1['L1', 'c1', '2010'])

# Adding a new key/value pair
parameter1['L2', 'c1', '2010'] = 9.3

# Overwriting an existing key
parameter1['L1', 'c1', '2010'] = 52.3

Hope this helps,
Toby Davies


--
Toby Davies
Computer Scientist

nikhil khinchi

unread,
Oct 8, 2014, 6:42:50 AM10/8/14
to pulp-or...@googlegroups.com
Hi Tobias,

Thanks for the swift reply, but the issue here is that I need to something like

parameter1['L1'] ['c1'] ['2010'] = 52.3 

since I need to loop over certain indexes in order to  build constraint. I don't know but would using tuples made up of combinations of different indices as keys allow me to generate constaints from the abstract formulation given below..
                                                                                            

I need to generate constrints where landreq is the parameter, read it's data from excel spreasheet and loop over only one of the indices - parameter is multiplied by xcrop over only some indices (a subset of indices over which the parameter is defined). which is why I felt numpy arrays suited more since using muliple key dictionaries would make the code mroe readable and reproducible for third person. It's just my thought. 

I found this example though - https://code.google.com/p/pulp-or/wiki/ATransportationProblem check out the section on costs - makeDict , I will have to check whether it solves the issue or not, what are you thoughts.

p.s. I am looking into solverstudio too but there are some issues with running example model (coopr) so I am in touch Dr.andrew mason on the same. I have wriiten a full code for pyomo but since pyomo has poor support for large spreadsheet data set. so I decided to use pulp + xlrd (or any good python-excel utility) since the eventual aim is to build an exceutable GUI over the entire thing.

regards
Nikhil

nikhil khinchi

unread,
Oct 8, 2014, 6:47:29 AM10/8/14
to pulp-or...@googlegroups.com
Hi toby,

Apologies for the messing up the addressing, guilty to have committed type error in hurry to reply. 

Regards
Nikhil

Stuart Mitchell

unread,
Oct 8, 2014, 4:48:40 PM10/8/14
to pulp-or...@googlegroups.com
See below

On Wed, Oct 8, 2014 at 11:42 PM, nikhil khinchi <nikhilk...@gmail.com> wrote:
Hi Tobias,

Thanks for the swift reply, but the issue here is that I need to something like

parameter1['L1'] ['c1'] ['2010'] = 52.3 

since I need to loop over certain indexes in order to  build constraint. I don't know but would using tuples made up of combinations of different indices as keys allow me to generate constaints from the abstract formulation given below..
                                                                                            
what is wrong with the code I already suggested
for l in L:
    for a in A:
         for fs in FS:
            .....
                prob += lpSum(xcrop[l,t2,c,a,fs,p,k] * landreq[tm,c] for t2 in ta[t,a] for c in C) <= agarea[l,a] * fsize[f,s]

note the .... should be replaced with more for loops 

I need to generate constrints where landreq is the parameter, read it's data from excel spreasheet and loop over only one of the indices - parameter is multiplied by xcrop over only some indices (a subset of indices over which the parameter is defined). which is why I felt numpy arrays suited more since using muliple key dictionaries would make the code mroe readable and reproducible for third person. It's just my thought. 

I found this example though - https://code.google.com/p/pulp-or/wiki/ATransportationProblem check out the section on costs - makeDict , I will have to check whether it solves the issue or not, what are you thoughts.

MakeDict will probably do what you want, but since then I have changed my mind and think that a dictionary with tuple keys is the best way to manage multidimensional data.

Stuart Mitchell

unread,
Oct 10, 2014, 5:28:06 AM10/10/14
to pulp-or...@googlegroups.com
Hi if you look at my example you will see that I use several values as the key to a dictionary,

xcrop[l,t2,c,a,fs,p,k]

to do this just define a dictionary

xcrop = {}

then in this case to define the variable

for l in L:
   for t in T:
       .....
           xcrop[l,t,c,a,fs,p,k] = LpVariable()

syntactically this just uses the tuple (l,t,c,a,fs,p,k) as the key.

I think this is the best way to do it.
and infact is more similar to the mathematical formulation then parameter1[L1][c1][2010] = 40.56

nikhil khinchi

unread,
Oct 10, 2014, 6:19:18 AM10/10/14
to pulp-or...@googlegroups.com

Sir,

Thanks for the swift reply. Unfortunately I am a bit preoccupied with mid semester exams. I can give you updates after you 12th.

Regards
Nikhil

nikhil khinchi

unread,
Oct 15, 2014, 11:19:56 AM10/15/14
to pulp-or...@googlegroups.com
Hi Stuart, 

I got an error with what you suggested above using the LpVariable as a dictionary. pls scroll below to see full code snippet.

l_test = ['sanganer','mansarovar']
t_test = ['T1']
c_test = ['wheat']
a_test = ['ir']
fs_test = ['small']
p_test = [2010,2020]
k_test = ['Climate2.6','Climate6.0'

from pulp import *

xcrop = {}
for l in l_test:
    for t in t_test:
        for c in c_test:
            for a in a_test:
                for fs in fs_test:
                    for p in p_test:
                        for k in k_test:
                            xcrop[l,t,c,a,fs,p,k] = LpVariable()
                            
Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2883, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-26-6abf3d10ab4a>", line 8, in <module>
    xcrop[l,t,c,a,fs,p,k] = LpVariable()
TypeError: __init__() takes at least 2 arguments (1 given)

Regards
Nikhil



nikhil khinchi

unread,
Oct 15, 2014, 11:21:20 AM10/15/14
to pulp-or...@googlegroups.com
I used to arguments for Lowbound and continous, here no error returned but not sure if the it was constructed properly.

xcrop = {}
for l in l_test:
    for t in t_test:
        for c in c_test:
            for a in a_test:
                for fs in fs_test:
                    for p in p_test:
                        for k in k_test:
                            xcrop[l,t,c,a,fs,p,k] = LpVariable(0,None,cat=LpContinuous)
xcrop
Out[21]: 
{('mansarovar',
  'T1',
  'wheat',
  'ir',
  'small',
  2010,
  'Climate2.6'): <repr(<pulp.pulp.LpVariable at 0x7e006d8>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('mansarovar',
  'T1',
  'wheat',
  'ir',
  'small',
  2010,
  'Climate6.0'): <repr(<pulp.pulp.LpVariable at 0x7e00710>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('mansarovar',
  'T1',
  'wheat',
  'ir',
  'small',
  2020,
  'Climate2.6'): <repr(<pulp.pulp.LpVariable at 0x7e00748>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('mansarovar',
  'T1',
  'wheat',
  'ir',
  'small',
  2020,
  'Climate6.0'): <repr(<pulp.pulp.LpVariable at 0x7e00780>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('sanganer',
  'T1',
  'wheat',
  'ir',
  'small',
  2010,
  'Climate2.6'): <repr(<pulp.pulp.LpVariable at 0x7e005f8>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('sanganer',
  'T1',
  'wheat',
  'ir',
  'small',
  2010,
  'Climate6.0'): <repr(<pulp.pulp.LpVariable at 0x7e00630>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('sanganer',
  'T1',
  'wheat',
  'ir',
  'small',
  2020,
  'Climate2.6'): <repr(<pulp.pulp.LpVariable at 0x7e00668>) failed: TypeError: __repr__ returned non-string (type NoneType)>,
 ('sanganer',
  'T1',
  'wheat',
  'ir',
  'small',
  2020,
  'Climate6.0'): <repr(<pulp.pulp.LpVariable at 0x7e006a0>) failed: TypeError: __repr__ returned non-string (type NoneType)>}

Regard
Nikhil

Stuart Mitchell

unread,
Oct 15, 2014, 4:44:15 PM10/15/14
to pulp-or...@googlegroups.com
Hi it looks like you have found a bug, 
pls give the variable a name  

LpVariable(name='xcrop_%s_%s_%s_%s_%s_%s_%s' % (l,t,c,a,fs,p,k), lowBound=0, upBound=None)



--
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-discu...@googlegroups.com.
To post to this group, send email to pulp-or...@googlegroups.com.
Visit this group at http://groups.google.com/group/pulp-or-discuss.
For more options, visit https://groups.google.com/d/optout.

nikhil khinchi

unread,
Oct 30, 2014, 1:12:36 PM10/30/14
to pulp-or...@googlegroups.com, Stuart Mitchell
Sir, 

I have come across a particular error in -  print "Status:", LpStatus[prob.status] as seen below

"""
Traceback (most recent call last):
  File "C:\Anaconda\lib\site-packages\IPython\core\interactiveshell.py", line 2883, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-183-8e412a6115c6>", line 1, in <module>
    print "Status:", LpStatus[prob.status]
AttributeError: 'dict' object has no attribute 'status'
"""

though I have been able to extract model solution and save it in a file. I don't know is this a cause for concern. PFA attached code

Regards
Nikhil

--
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/9ULfddjsNJw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pulp-or-discu...@googlegroups.com.
test.py

Stuart Mitchell

unread,
Oct 30, 2014, 5:36:36 PM10/30/14
to pulp-or...@googlegroups.com
in your code prob is a dictionary of variables for some reason

you want

    print "Status:", LpStatus[model.status]

Reply all
Reply to author
Forward
0 new messages