I am trying to write my first Gurobi optimization code and this is where I am stuck with:
I have the following dictionary for my first subscript:
input for k in range(1,11):
i[k] = int(k)
print i
output {1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10}
And, I have the following dictionaries for my second subscript:
c_il = {1: 2, 2: 1, 3: 1, 4: 4, 5: 3, 6: 4, 7: 3, 8: 2, 9: 1, 10: 4}
c_iu = {1: 3, 2: 2, 3: 2, 4: 5, 5: 4, 6: 5, 7: 4, 8: 3, 9: 2, 10: 5}
I am trying to create variables as following:
x = m.addVars(i, c_il, vtype=GRB.BINARY, name="x")
x = m.addVars(i, c_iu, vtype=GRB.BINARY, name="x")
Apparently, it is not giving what I am looking for. What I am looking for is x_(i),(c_il) and x_(i),(c_iu); ignore parenthesis.
More clearly, the following is what I am trying to obtain by using dicts i, c_il, and c_iu:
{1: <gurobi.Var x[1,2]>,
2: <gurobi.Var x[2,1]>,
3: <gurobi.Var x[3,1]>,
4: <gurobi.Var x[4,5]>,
5: <gurobi.Var x[5,3]>,
6: <gurobi.Var x[6,4]>,
7: <gurobi.Var x[7,3]>,
8: <gurobi.Var x[8,2]>,
9: <gurobi.Var x[9,1]>,
10: <gurobi.Var x[10,4]>,
11: <gurobi.Var x[1,3]>,
12: <gurobi.Var x[2,2]>,
13: <gurobi.Var x[3,2]>,
14: <gurobi.Var x[4,5]>,
15: <gurobi.Var x[5,4]>,
16: <gurobi.Var x[6,5]>,
17: <gurobi.Var x[7,4]>,
18: <gurobi.Var x[8,3]>,
19: <gurobi.Var x[9,2]>,
20: <gurobi.Var x[10,5]>}
Since I am using dictionaries everywhere, I want to keep it consistent by continuing to use dictionaries so that I can do multiplications and additions with my parameters which are all in dictionaries. Is there any way to create these variables with m.addVars or m.addVar?
Thanks!
Edit: Modified to make it more clear.
It looks like you want to create 10 variables that are indexed by something. The best way to do this is to create the two indexes as lists. If you want x[12], x[21], then write:
from gurobipy import *
m = Model()
il = [ 12, 21, 31, 44, 53, 64, 73, 82, 91, 104 ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")
And if you want to write x[1,2], x[2,1], then write:
from gurobipy import *
m = Model()
il = [ (1,2), (2,1), (3,1), (4,4), (5,3), (6,4), (7,3), (8,2), (9,1), (10,4) ]
x = m.addVars(il, vtype=GRB.BINARY, name="x")
After a few years of experience, I can easily write the below as an answer. Since the past myself was concerned with keeping the dictionaries as is (I highly criticize and question...), a quick solution is as follows.
x = {}
for (i,j) in c_il.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_iu.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
Alternatively,
x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in c_il.items()}
for (i,j) in c_iu.items():
x[i,j] = m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
One liner alternative:
x = {(i,j): m.addVar(vtype=GRB.BINARY, name="x%s"%str([i,j]))
for (i,j) in [(k,l) for (k,l) in c_il.items()] + [(k,l) for (k,l) in c_iu.items()]}
Related
Comprehensions show unusual interactions with scoping. Is this the expected behavior?
x = "original value"
squares = [x**2 for x in range(5)]
print(x) # Prints 4 in Python 2!
At the risk of whining, this is a brutal source of errors. As I write new code, I just occasionally find very weird errors due to rebinding -- even now that I know it's a problem. I need to make a rule like "always preface temp vars in list comprehensions with underscore", but even that's not foolproof.
The fact that there's this random time-bomb waiting kind of negates all the nice "ease of use" of list comprehensions.
List comprehensions leak the loop control variable in Python 2 but not in Python 3. Here's Guido van Rossum (creator of Python) explaining the history behind this:
We also made another change in Python
3, to improve equivalence between list
comprehensions and generator
expressions. In Python 2, the list
comprehension "leaks" the loop control
variable into the surrounding scope:
x = 'before'
a = [x for x in 1, 2, 3]
print x # this prints '3', not 'before'
This was an artifact of the original
implementation of list comprehensions;
it was one of Python's "dirty little
secrets" for years. It started out as
an intentional compromise to make list
comprehensions blindingly fast, and
while it was not a common pitfall for
beginners, it definitely stung people
occasionally. For generator
expressions we could not do this.
Generator expressions are implemented
using generators, whose execution
requires a separate execution frame.
Thus, generator expressions
(especially if they iterate over a
short sequence) were less efficient
than list comprehensions.
However, in Python 3, we decided to
fix the "dirty little secret" of list
comprehensions by using the same
implementation strategy as for
generator expressions. Thus, in Python
3, the above example (after
modification to use print(x) :-) will
print 'before', proving that the 'x'
in the list comprehension temporarily
shadows but does not override the 'x'
in the surrounding scope.
Yes, list comprehensions "leak" their variable in Python 2.x, just like for loops.
In retrospect, this was recognized to be a mistake, and it was avoided with generator expressions. EDIT: As Matt B. notes it was also avoided when set and dictionary comprehension syntaxes were backported from Python 3.
List comprehensions' behavior had to be left as it is in Python 2, but it's fully fixed in Python 3.
This means that in all of:
list(x for x in a if x>32)
set(x//4 for x in a if x>32) # just another generator exp.
dict((x, x//16) for x in a if x>32) # yet another generator exp.
{x//4 for x in a if x>32} # 2.7+ syntax
{x: x//16 for x in a if x>32} # 2.7+ syntax
the x is always local to the expression while these:
[x for x in a if x>32]
set([x//4 for x in a if x>32]) # just another list comp.
dict([(x, x//16) for x in a if x>32]) # yet another list comp.
in Python 2.x all leak the x variable to the surrounding scope.
UPDATE for Python 3.8: PEP 572 introduced := assignment operator that deliberately leaks out of comprehensions and generator expressions! This leaking was motivated by essentially 2 use cases: capturing a "witness" from early-terminating functions like any() and all():
if any((comment := line).startswith('#') for line in lines):
print("First comment:", comment)
else:
print("There are no comments")
and updating mutable state:
total = 0
partial_sums = [total := total + v for v in values]
See Appendix B for exact scoping. The variable is assigned in closest surrounding def or lambda, unless that function declares it nonlocal or global.
Yes, assignment occurs there, just like it would in a for loop. No new scope is being created.
This is definitely the expected behavior: on each cycle, the value is bound to the name you specify. For instance,
>>> x=0
>>> a=[1,54,4,2,32,234,5234,]
>>> [x for x in a if x>32]
[54, 234, 5234]
>>> x
5234
Once that's recognized, it seems easy enough to avoid: don't use existing names for the variables within comprehensions.
Interestingly this doesn't affect dictionary or set comprehensions.
>>> [x for x in range(1, 10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> x
9
>>> {x for x in range(1, 5)}
set([1, 2, 3, 4])
>>> x
9
>>> {x:x for x in range(1, 100)}
{1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11, 12: 12, 13: 13, 14: 14, 15: 15, 16: 16, 17: 17, 18: 18, 19: 19, 20: 20, 21: 21, 22: 22, 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 28: 28, 29: 29, 30: 30, 31: 31, 32: 32, 33: 33, 34: 34, 35: 35, 36: 36, 37: 37, 38: 38, 39: 39, 40: 40, 41: 41, 42: 42, 43: 43, 44: 44, 45: 45, 46: 46, 47: 47, 48: 48, 49: 49, 50: 50, 51: 51, 52: 52, 53: 53, 54: 54, 55: 55, 56: 56, 57: 57, 58: 58, 59: 59, 60: 60, 61: 61, 62: 62, 63: 63, 64: 64, 65: 65, 66: 66, 67: 67, 68: 68, 69: 69, 70: 70, 71: 71, 72: 72, 73: 73, 74: 74, 75: 75, 76: 76, 77: 77, 78: 78, 79: 79, 80: 80, 81: 81, 82: 82, 83: 83, 84: 84, 85: 85, 86: 86, 87: 87, 88: 88, 89: 89, 90: 90, 91: 91, 92: 92, 93: 93, 94: 94, 95: 95, 96: 96, 97: 97, 98: 98, 99: 99}
>>> x
9
However it has been fixed in 3 as noted above.
some workaround, for python 2.6, when this behaviour is not desirable
# python
Python 2.6.6 (r266:84292, Aug 9 2016, 06:11:56)
Type "help", "copyright", "credits" or "license" for more information.
>>> x=0
>>> a=list(x for x in xrange(9))
>>> x
0
>>> a=[x for x in xrange(9)]
>>> x
8
In python3 while in list comprehension the variable is not getting change after it's scope over but when we use simple for-loop the variable is getting reassigned out of scope.
i = 1
print(i)
print([i in range(5)])
print(i)
Value of i will remain 1 only.
Now just use simply for loop the value of i will be reassigned.
I have a dataframe as :
A B C D
0 s 3 a
4 s 2 a
5 s 2 a
6 s 1 a
7 s 2 b
7 s 3 b
6 s 0 b
How can I create a new dataframe as the following?
A B C D
0 4 8 4-a
7 3 5 3-b
The new dataframe summarize the old one by grouped the elements of column "D", So "A" is the index, "B" is count of elements, "C" is sum of element where "D" has the same value.
Well, assuming that your data is stored in df, it's a multistep process which could be done like this
import pandas as pd
data = {'A': {0: 0, 1: 4, 2: 5, 3: 6, 4: 7, 5: 7, 6: 6},
'B': {0: 's', 1: 's', 2: 's', 3: 's', 4: 's', 5: 's', 6: 's'},
'C': {0: 3, 1: 2, 2: 2, 3: 1, 4: 2, 5: 3, 6: 0},
'D': {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'b', 5: 'b', 6: 'b'}}
df = pd.DataFrame(data)
# Handling column A (first index per value in D)
output_df = df.drop_duplicates(subset='D', keep='first')
# Itering through rows
for index, row in output_df.iterrows():
#Calcultating the counts in B
output_df.loc[index, 'B'] = df[df.D == row.D].B.count()
#Calcultating the sum in C
output_df.loc[index, 'C'] = df[df.D == row.D].C.sum()
#Finally changing values in D by concatenating values in B and D
output_df.loc[:, 'D'] = output_df.B.map(str) + "-" + output_df.D
Output :
A B C D
0 4 8 4-a
7 3 5 3-b
When I run
print df
the result
A B C D
0 4 8 4-a
7 3 5 3-b
when I select only one column
print df['D']
Nothing showing
print df.info()
Nothing showing
I couldn't understant what is wrong?
I set the data using this code
import pandas as pd
data = {'A': {0: 0, 1: 4, 2: 5, 3: 6, 4: 7, 5: 7, 6: 6},
'B': {0: 's', 1: 's', 2: 's', 3: 's', 4: 's', 5: 's', 6: 's'},
'C': {0: 3, 1: 2, 2: 2, 3: 1, 4: 2, 5: 3, 6: 0},
'D': {0: 'a', 1: 'a', 2: 'a', 3: 'a', 4: 'b', 5: 'b', 6: 'b'}}
df = pd.DataFrame(data)
# Handling column A (first index per value in D)
output_df = df.drop_duplicates(subset='D', keep='first')
# Itering through rows
for index, row in output_df.iterrows():
#Calcultating the counts in B
output_df.loc[index, 'B'] = df[df.D == row.D].B.count()
#Calcultating the sum in C
output_df.loc[index, 'C'] = df[df.D == row.D].C.sum()
#Finally changing values in D by concatenating values in B and D
output_df.loc[:, 'D'] = output_df.B.map(str) + "-" + output_df.D
I have below Python dictionary as source
d1 = {
'a': 1,
'b': 2,
'c': [{'d': 3, 'e': 4, 'un': 'wanted1', 'dont': 'needthis1'},
{'d': 5, 'e': 6, 'un': 'wanted2', 'dont': 'needthis2'}]
'xyz': 'abc',
'zxy': 'cab',
'wva': 'xyw'
}
And I want to copy the values of some specific keys to a different dict to form below target dictionary
d2 = {
'some_attr_1': 1,
'some_attr_x': 2,
'attr_some_z': [{'attr_x': 3, 'attrib': 4},
{'attr_x': 5, 'attrib': 6}]
}
Note:
I am not interested in all the attributes from source
for ex: I don't need keys xyz, zxy, etc
Basically, Want values for some keys in source to be mapped
to different keys in target dictionary.
My current approach is as below to have mapping between source and target dictionary keys.
attr_map1 = {
'some_attr_1': 'a',
'some_attr_x': 'b'
}
attr_map2 = {
'attr_x': 'd',
'attrib': 'e',
}
d2 = dict()
for k, v in attr_map1.items():
d2[k] = d1[v]
l1 = list()
for d_elem in d1['c']:
temp_dict = dict()
for k, v in attr_map2.items():
temp_dict[k] = d_elem[v]
l1.append(temp_dict)
d2['attr_some_z'] = l1
Is there any alternate, better and speedy approach to achieve this?
I am looking for a solution in Python 2.7.
thanks,
You can use recursion:
d1 = {'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4}, {'d': 5, 'e': 6}]}
def build(d):
return {f't_{a}':b if not isinstance(b, (dict, list)) else
list(map(build, b)) if isinstance(b, list) else build(b) for a, b in d.items()}
print(build(d1))
Output:
{
't_a': 1,
't_b': 2,
't_c': [
{'t_d': 3, 't_e': 4},
{'t_d': 5, 't_e': 6}
]
}
Edit: to run this solution in Python2, replace the f-string with simple concatenation:
d1 = {'a': 1, 'b': 2, 'c': [{'d': 3, 'e': 4}, {'d': 5, 'e': 6}]}
def build(d):
return {'t_'+a:b if not isinstance(b, (dict, list)) else
list(map(build, b)) if isinstance(b, list) else build(b) for a, b in d.items()}
I am generating x-axis values which is time form 1 to 23 and v-values which is a number of clients. I want to join these 2 lists as dictionary which is done but I am getting float values of x as I generate using linspace().
time_values = np.linspace(1,23,23) # print from 1,2...23
number_of_clients = [] # create empty list that will hold number of clients
for i in range(1,24,1):
rand_value = random.randint(1,20) # generate number of clients
number_of_clients.append(rand_value)
data = dict(zip(time_values,number_of_clients))
print data
output is
{1.0: 12, 2.0: 11, 3.0: 3, 4.0: 19, 5.0: 12, 6.0: 12, 7.0: 5, 8.0: 13, 9.0: 15, 10.0: 3, 11.0: 15, 12.0: 20, 13.0: 5, 14.0: 3, 15.0: 18, 16.0: 12, 17.0: 5, 18.0: 6, 19.0: 8, 20.0: 16, 21.0: 19, 22.0: 1, 23.0: 16}
how to convert 1.0 to 1 and so on.I have tried int(time_vlaues), but it did not worked
try astype method to convert numpy float array to int array:
time_values = np.linspace(1,23,23) # print from 1,2...23
number_of_clients = [] # create empty list that will hold number of clients
for i in range(1,24,1):
rand_value = random.randint(1,20) # generate number of clients
number_of_clients.append(rand_value)
data = dict(zip(time_values.astype(int),number_of_clients))
print(data)
or
time_values = np.linspace(1,23,23,dtype='int') # print from 1,2...23
number_of_clients = [] # create empty list that will hold number of clients
for i in range(1,24,1):
rand_value = random.randint(1,20) # generate number of clients
number_of_clients.append(rand_value)
data = dict(zip(time_values,number_of_clients))
print(data)
output:
{1: 17, 2: 6, 3: 8, 4: 3, 5: 12, 6: 11, 7: 18, 8: 1, 9: 8, 10: 1, 11: 17, 12: 2, 13: 5, 14: 6, 15: 1, 16: 8, 17: 19, 18: 2, 19: 13, 20: 15, 21: 16, 22: 17, 23: 14}