GurobiError: Unable to convert argument to an expression - python

can someone help me with this error?
Where shoudl I look when I get the error: GurobiError: Unable to convert argument to an expression.
I should add that i'm using Gurobi library in python
from gurobipy import*
m=Model('mymodel')
def label(c):
return "x" + str(c).translate(None, " '")
shifts = [1,2]
hours = [1,2]
games = ['bj', 'cr']
pits = [1,2]
order1 = [1,2]
order2 = [1,2,3]
combo, oi = multidict( {
(1,1,'bj',1,1,1): 100,
(1,1,'bj',1,1,2):200,
(1,1,'bj',1,1,3):200,
(1,1,'bj',1,2,1):50,
(1,1,'bj',1,2,2):70,
(1,1,'bj',1,2,3):70,
(1,1,'cr',1,1,1):400,
(1,1,'cr',1,1,2):450
})
combo= tuplelist(combo)
for s in shifts:
for t in hours:
for i in games:
for n in order1:
m.addConstr(quicksum(x[s,t,i,p,n,k] for s,t,i,p,n,k in combo.select(s,t,i,'*',n,'*'))- int(1)== 0, name=label((s,t,i,p,n,k))

Gurobi will complain if you try to add a constraint with no model variables. It looks like for some combination of variables the list enumeration will construct the empty list, ie:
m.addConstr(quicksum([]) - 1 == 0)
aka
m.addConstr(-1 == 0)
which is not possible. In fact gurobi will still throw an error in the following example as even though it is feasible the expression contains no variables.
m.addConstr(-1 <= 0)
To solve this just check the list is non-empty before adding the constraint.

This issue is fixed in Gurobi 6.0.0. There quicksum([]) returns a linear expression with value 0 (instead of a float 0.0) and this solves the original problem.
The problem in version 5.6.3 and before is the use of the so called TempConstr.
When you call addConstr, you can use the explicit "lefthandside", "operator", "righthandside" approach:
m.addConstr(quicksum([]), GRB.EQUAL, 0)
This will work without problems.
If you use TempConstr and use quicksum on an empty list, what really goes on is the following:
m.addConstr(quicksum([]) == 0)
quicksum returns 0, if the list is empty, so your statement is:
m.addConstr(0 == 0)
and 0 == 0 is converted to True, so you actually call:
m.addConstr(True)
and this is obviously something Gurobi cannot handle (but it could give a better error description).
To sum things up: If you use quicksum and there is a chance, that a list is empty, you can either check if the list is empty as suggested by blueenvelope, use the explicit approach or use a small wrapper method, if this happens quite often:
def add_constr(model, expression, name=""):
if expression is True:
return model.addConstr(0, GRB.EQUAL, 0, name)
elif expression is False:
raise Exception('`False` as constraint for {}'.format(name))
else:
return model.addConstr(expression, name)
This wrapper works for TempConstr as expression only.

Related

Python recursive function that retain variable values

I am brushing up a bit of good old algorithms, and doing it with python, since I use it more often nowadays.
I am facing an issue when running a recursive function; where the variable get reset every time that the recursive function call itself:
def recursive_me(mystring):
chars = len(mystring)
if chars is 0:
print("Done")
else:
first = int(str[0])
total = + first
print(total)
recursive_me(mystring[1:])
recursive_me("4567")
What I am doing here is to get a string made of digits; take the first, convert it to an int; and run recursively the function again, so I can take one digit at time from the string and sum all the values.
Ideally the output should show the total, while it add all the digits (4+5+6+7), although when the recursive function is called the first time, the function reset the total value.
Is common habit to use global variables when running operations with recursive functions or am I doing something wrong?
You can code as simply as this:
def recursive_me(mystring):
if mystring: # recursive case
return int(mystring[0]) + recursive_me(mystring[1:])
else: # base case
return 0
or
def recursive_me(mystring, total = 0):
if mystring: # recursive case
return recursive_me(mystring[1:], total + int(mystring[0]))
else: # base case
return total
although this won't help much in Python since it doesn't implement tail-call optimisation.
If you want to see the intermediate values, change the second version like so:
def recursive_me(mystring, total = 0):
if mystring: # recursive case
newtotal = total + int(mystring[0])
print(newtotal)
return recursive_me(mystring[1:], newtotal)
else: # base case
return total
then
4
9
15
22
22 # this is the return value; previous output is from `print()`
as a foreword: a lot of answers received meaningful edits in the meantime I was writing this answer. Don't hold it against me.
I'm throwing my two cents in here just because there's a lot of over-complicated answers.
This is a corrected copy-paste of the OP's effort.
def recursive_me(mystring, total=0):
chars = len(mystring)
if chars is 0:
print("Done")
return total
else:
first = int(mystring[0])
total += first
print(total)
recursive_me(mystring[1:], total)
first what happens is that we check the base case, if there's no left chars in the string. If the string length is 0 we return the total calculated ammount.
Otherwise, we turn the first of the chars into an int, and add it to total. The first error you have is that you wrote str[0]. str is a python built in type and the produced error would be something like "str is not subscriptable".
This error means that the str can't be operated on by "[]" operator. The same would happen if you tried doing 1[0] because 1 is a integer. The "[]" operator can only operate on lists, tuples and strings (I might've forgot some built-in type).
The second error you had was with the addition part. You had written total = + first but the operator you are looking for is the += which in fact is just a shortened way to write a = a+b.
Additionally, your original question was concerning about "python" forgetting the value of "total". This is because you have to either pass that value forward, or write your recursive function in a way that "forces" it to, what's called, evaluate your next call to your function on the spot.
In my example I'm sending the next call of the function recursive_me, the current total value. In the example given by #uselpa; above he's making python evaluate the next call to the function by putting it after operator +:
return int(mystring[0]) + recursive_me(mystring[1:])
this then gets to be (for recursive_me("4567"))
return int(4)+recursive_me("567")
return int(4)+int(5)+recursive_me("67")
....
return int(4)+int(5)+int(6)+int(7)+0
because python needs to return a value here, but the expression keeps calling new functions and python can't return until it evaluates all of them to a final number (in this case at least).
The common practice is to save these variables as parameters, and pass them along the chain. It seems in your case, you would want to pass total as an additional parameter, and update it as needed.
There's also a neat functional way to do it in python
t=raw_input()
print reduce(lambda a, b: a+b, map(int,t))
This is recursive in nature.
Some pointers:
Your default case should return an actual number (0 in your case) and not just print done.
total = + first is setting total to first, not adding first to total. You would need total += first to do the latter.
The trick with "retaining" the value of your current total is to "save" it in the recursive call-chain itself by passing it along with each call. You won't need a global variable or a default parameter to do this.
Here's a solution:
def recursive_me(mystring):
if not mystring: # True if mystring is empty
return 0
return int(mystring[0]) + recursive_me(mystring[1:])
print(recursive_me("4567")) # 22
Here is a solution that uses the LEGB scope rule to avoid creating a new string instance on every recursive call
def sum_str(mystring):
def recursive_me(pos):
cur_char = int(mystring[pos])
if pos:
return cur_char + recursive_me(pos-1)
else:
return cur_char
return recursive_me(len(mystring)-1)
s = '4567'
print('summing', s)
print(sum_str(s))
However, indexing can be avoided as well by iterating on the string
def sum_str(mystring):
def recursive_me(itx):
try:
cur_char = int(next(itx))
return cur_char + recursive_me(itx)
except StopIteration:
return 0
return recursive_me(iter(mystring))
Obviously, both solutions produce
summing 4567
22

Understanding a Python function

I need some help understanding a function that i want to use but I'm not entirely sure what some parts of it do. I understand that the function is creating dictionaries from reads out of a Fasta-file. From what I understand this is supposed to generate pre- and suffix dictionaries for ultimately extending contigs (overlapping dna-sequences).
The code:
def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
lenKeys = len(reads[0]) - lenSuffix
dict = {}
multipleKeys = []
i = 1
for read in reads:
if read[0:lenKeys] in dict:
multipleKeys.append(read[0:lenKeys])
else:
dict[read[0:lenKeys]] = read[lenKeys:]
if verbose:
print("\rChecking suffix", i, "of", len(reads), end = "", flush = True)
i += 1
for key in set(multipleKeys):
del(dict[key])
if verbose:
print("\nCreated", len(dict), "suffixes with length", lenSuffix, \
"from", len(reads), "Reads. (", len(reads) - len(dict), \
"unambigous)")
return(dict)
Additional Information: reads = readFasta("smallReads.fna", verbose = True)
This is how the function is called:
if __name__ == "__main__":
reads = readFasta("smallReads.fna", verbose = True)
suffixDicts = makeSuffixDicts(reads, 10)
The smallReads.fna file contains strings of bases (Dna):
"> read 1
TTATGAATATTACGCAATGGACGTCCAAGGTACAGCGTATTTGTACGCTA
"> read 2
AACTGCTATCTTTCTTGTCCACTCGAAAATCCATAACGTAGCCCATAACG
"> read 3
TCAGTTATCCTATATACTGGATCCCGACTTTAATCGGCGTCGGAATTACT
Here are the parts I don't understand:
lenKeys = len(reads[0]) - lenSuffix
What does the value [0] mean? From what I understand "len" returns the number of elements in a list.
Why is "reads" automatically a list? edit: It seems a Fasta-file can be declared as a List. Can anybody confirm that?
if read[0:lenKeys] in dict:
Does this mean "from 0 to 'lenKeys'"? Still confused about the value.
In another function there is a similar line: if read[-lenKeys:] in dict:
What does the "-" do?
def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
Here I don't understand the parameters: How can reads be a parameter? What is lenSuffix = 20 in the context of this function other than a value subtracted from len(reads[0])?
What is verbose? I have read about a "verbose-mode" ignoring whitespaces but i have never seen it used as a parameter and later as a variable.
The tone of your question makes me feel like you're confusing things like program features (len, functions, etc) with things that were defined by the original programmer (the type of reads, verbose, etc).
def some_function(these, are, arbitrary, parameters):
pass
This function defines a bunch of parameters. They don't mean anything at all, other than the value I give to them implicitly. For example if I do:
def reverse_string(s):
pass
s is probably a string, right? In your example we have:
def makeSuffixDict(reads, lenSuffix = 20, verbose = True):
lenKeys = len(reads[0]) - lenSuffix
...
From these two lines we can infer a few things:
the function will probably return a dictionary (from its name)
lenSuffix is an int, and verbose is a bool (from their default parameters)
reads can be indexed (string? list? tuple?)
the items inside reads have length (string? list? tuple?)
Since Python is dynamically typed, this is ALL WE CAN KNOW about the function so far. The rest would be explained by its documentation or the way it's called.
That said: let me cover all your questions in order:
What does the value [0] mean?
some_object[0] is grabbing the first item in a container. [1,2,3][0] == 1, "Hello, World!"[0] == "H". This is called indexing, and is governed by the __getitem__ magic method
From what I understand "len" returns the number of elements in a list.
len is a built-in function that returns the length of an object. It is governed by the __len__ magic method. len('abc') == 3, also len([1, 2, 3]) == 3. Note that len(['abc']) == 1, since it is measuring the length of the list, not the string inside it.
Why is "reads" automatically a list?
reads is a parameter. It is whatever the calling scope passes to it. It does appear that it expects a list, but that's not a hard and fast rule!
(various questions about slicing)
Slicing is doing some_container[start_idx : end_idx [ : step_size]]. It does pretty much what you'd expect: "0123456"[0:3] == "012". Slice indexes are considered to be zero-indexed and lay between the elements, so [0:1] is identical to [0], except that slices return lists, not individual objects (so 'abc'[0] == 'a' but 'abc'[0:1] == ['a']). If you omit either start or end index, it is treated as the beginning or end of the string respectively. I won't go into step size here.
Negative indexes count from the back, so '0123456'[-3:] == '456'. Note that [-0]is not the last value,[-1]is. This is contrasted with[0]` being the first value.
How can reads be a parameter?
Because the function is defined as makeSuffixDict(reads, ...). That's what a parameter is.
What is lenSuffix = 20 in the context of this function
Looks like it's the length of the expected suffix!
What is verbose?
verbose has no meaning on its own. It's just another parameter. Looks like the author included the verbose flag so you could get output while the function ran. Notice all the if verbose blocks seem to do nothing, just provide feedback to the user.

Python If Statement - Syntax Error

Currently stuck with this syntax error, error is posted below the code.
#property
def data_rows(self):
for d in rlgenevautils.to_csv_dict(self.data_file):
trade_dt = rlcore.str2dt(d['EventDate'])
settle_dt = rlcore.str2dt(d['ActualSettleDate'])
yield (str(d['_UDF_SGCP_ID_'])
,str(d['_UDF_Execution_ID_'])
,str(d['_UDF_PB_ID_'])
,str(d['_UDF_Fund_Admin_ID_'])
,str(d['_Portfolio_NameSort_'])
,str(d['_Strategy_Code_'])
,str(d['_LocationAccount_NameSort_'])
,str(d['_Broker_NameSort_'])
,str(d['_Investment_Code_'])
,trade_dt.isoformat(' ')
,settle_dt.isoformat(' ')
,rlcore.str2float(d['ABSQuantityForCalcCurrentFace'])
,max(rlcore.str2float(d['ABSQuantityForCalcCurrentFace']),rlcore.str2float(d['OriginalFace']))
,rlcore.str2float(d['ABSQuantityForCalcCurrentFace'])/max(rlcore.str2float(d['ABSQuantityForCalcCurrentFace']),rlcore.str2float(d['OriginalFace']))
,rlcore.str2float(d['Price'])
,rlcore.str2float(d['ABSAccruedInterestForCalcCurrentFace'])
,if str(d['_Investment_InvestmentGroup_']) == "AssetBacked":
rlcore.str2float(d['ABSQuantityForCalcCurrentFace']) * rlcore.str2float(d['Price']) / 100
else:
rlcore.str2float(d['NetCashAmount'])
,rlcore.str2float(d['ABSAccruedInterestForCalcCurrentFace']) + rlcore.str2float(d['txtNetCashPreAccrued'])
)
Traceback (most recent call last):
File ".\sg\rec_and_liquidity\geneva_trade.py", line 64
,if str(d['_Investment_InvestmentGroup_']) == "AssetBacked":
^
Code above, unable to figure out what my syntax error is on the if statement. Error message will be pasted as comment shortly
You can't include an if statement inside an expression like that. If you want to include it in an expression, you need to use a conditional expression:
(rlcore.str2float(d['ABSQuantityForCalcCurrentFace']) * rlcore.str2float(d['Price']) / 100) if str(d['_Investment_InvestmentGroup_']) == "AssetBacked" elserlcore.str2float(d['NetCashAmount'])
However, this is not very readable. It would be better to move your if statement to before the yield, assign the result to a variable, and use that variable in the yield.
'if' is a statement, and creating tuple this way you can use only expressions.
Change your code like follows:
if condition:
something
else:
something2
to
something if condition else something2
The problem is that you can't put a statement in the middle of an expression.
For simple cases, there is an if expression that, being an expression, can be used in the middle of an expression. In your case:
(rlcore.str2float(d['ABSQuantityForCalcCurrentFace']) * rlcore.str2float(d['Price']) / 100
if str(d['_Investment_InvestmentGroup_']) == "AssetBacked"
else rlcore.str2float(d['NetCashAmount']))
For more complicated cases, move the if statement upward and store a temporary value in a variable, then use that variable in the expression (exactly as you're already doing for, each, trade_dt):
if str(d['_Investment_InvestmentGroup_']) == "AssetBacked":
priceval = rlcore.str2float(d['ABSQuantityForCalcCurrentFace']) * rlcore.str2float(d['Price']) / 100
else:
priceval = rlcore.str2float(d['NetCashAmount'])
… then just use priceval in the yield.
However, no matter how you solve this, your code is a huge unreadable mess. You have at least three different conversion methods you're using repeatedly; if you discover that you're formatting dates or strings or whatever wrong, you'll need to change that in dozens of places. You'd probably do better off with a mapping of column names to either types, or converters, and then just generate the values by dynamically looking up each converter. For example:
_COLUMNS = {'_UDF_SGCP_ID_': str,
'_UDF_Execution_ID_': str,
# ...
'EventDate': datetime.datetime,
# ...
}
_CONVERTERS = {str: str,
datetime.datetime: lambda val: rlcore.str2dt(val).isoformat(),
# ...}
def _converted(d, col):
val = d[col]
converter = _CONVERTERS[_COLUMNS[col]]
return converter(val)
And now you can just do this:
yield(_converted(d, col) for col in (
'_UDF_SGCP_ID_',
'_UDF_Execution_ID_',
# ...
)

how to check if a const in z3 is a variable or a value?

just wondering in z3py , how do I check if a given constant expression is a variable or value ? For example
x = Int('x')
x_ = IntVal(7)
ColorVal, (White,Black) = EnumSort("ColorVal",["While","Black"])
mycolor = Const("mycolor",ColorVal)
So x,mycolor would all be variables and x_,True,False,White,Black would be values and not variables .
z3py has is_var predicate but for different purpose. This is useful if I want to rename all variables in a formula to something else.
What you call variables are (technically) called uninterpreted constants in Z3. Values (such as 1, true, #x01) are called interpreted constants. So, in principle, a fast way to check whether a is a "variable" can be done using the following piece of code:
is_const(a) and a.decl().kind() == Z3_OP_UNINTERPRETED
This piece of code works for everything, but datatypes. After trying your example, I realized that Z3 is incorrectly returning Z3_OP_UNINTERPRETED for datatype constructors. I fixed that for Z3 4.2.
In the meantime, you can use the following workaround where the function is_datatype_const_value(a) returns True is a is a constant constructor.
def is_datatype_sort(s):
return s.kind() == Z3_DATATYPE_SORT
def is_datatype_constructor(x):
s = x.sort()
if is_datatype_sort(s):
n = s.num_constructors()
f = x.decl()
for i in range(n):
c = s.constructor(i)
if eq(c, f):
return True
return False
# Return True if x is a constant constructor, that is, a constructor without arguments.
def is_datatype_const_value(x):
return is_const(x) and is_datatype_constructor(x)
Then, the following code catches all uninterpreted constants:
is_const(a) and a.decl().kind() == Z3_OP_UNINTERPRETED and not is_datatype_const_value(a)
The following link contains a complete example.
http://rise4fun.com/Z3Py/vjb
One way to do this for the integers is with is_int and is_int_value:
x = Int('x')
print "x types"
print is_int(x) # true, is of sort int
print is_int_value(x) # false, not a "value"
x_ = IntVal(7)
print "x_ types"
print is_int(x_) # true, is also of sort int
print is_int_value(x_) # true, is a value
For reals, you can do this using is_real to check the variable sort, and use (the disjunction of) is_algebraic_value and is_rational_value for the values (I didn't see a function like is_real_value in the API, but I think this disjunct will do it). For bitvectors, you can use is_bv_value for values, and is_bv to check the variable sort.
The .NET API has Expr.IsNumeral, and you can see how these are implemented in the API here (e.g., Expr.IsIntNum [the equivalent of the Python is_int_value] checks if both Expr.IsNumeral and Expr.IsInt are true): http://research.microsoft.com/en-us/um/redmond/projects/z3/_expr_8cs_source.html
I did not immediately see a way to do this for custom-defined enumeration sorts. As one alternative, you could encode your enum using bitvectors and compare variables / values using is_bv_value. As a better workaround though, you appear to need to use the more general algebraic datatypes and their automatically created "recognizers". The Python API does not seem to properly create the recognizers if you declare them as enum sorts. Here's one way to do it for what's effectively an enum sort (but declared as the more general datatype).
Z3Py encoding of the following: http://rise4fun.com/Z3Py/ELtn
ColorVal = Datatype('ColorVal')
ColorVal.declare('white')
ColorVal.declare('black')
ColorVal = ColorVal.create()
mycolor = Const("mycolor", ColorVal)
print ColorVal.recognizer(0) # is_white
print ColorVal.recognizer(1) # is_black
print simplify(ColorVal.is_white(mycolor)) # returns is_white(mycolor)
print simplify(ColorVal.is_black(mycolor)) # returns is_black(mycolor)
mycolorVal = ColorVal.white # set to value white
print simplify(ColorVal.is_white(mycolorVal)) # true
print simplify(ColorVal.is_black(mycolorVal)) # false
# compare "variable" versus "value" with return of is_white, is_black, etc.: if it gives a boolean value, it's a value, if not, it's a variable
print "var vs. value"
x = simplify(ColorVal.is_white(mycolor))
print is_true(x) or is_false(x) # returns false, since x is is_white(mycolor)
y = simplify(ColorVal.is_white(mycolorVal))
print is_true(y) or is_false(y) # true
ColorValEnum, (whiteEnum,blackEnum) = EnumSort("ColorValEnum",["whiteEnum","blackEnum"])
mycolorEnum = Const("mycolorEnum",ColorValEnum)
print ColorValEnum.recognizer(0) # is_whiteEnum
print ColorValEnum.recognizer(1) # is_blackEnum
# it appears that declaring an enum does not properly create the recognizers in the Python API:
#print simplify(ColorValEnum.is_whiteEnum(mycolorEnum)) # error: gives DatatypeSortRef instance has no attribute 'is_whiteEnum'
#print simplify(ColorValEnum.is_blackEnum(mycolorEnum)) # error: gives DatatypeSortRef instance has no attribute 'is_blackEnum'

Can't assign function call while changng a variable

I was checking a project that i have to turn in (it's a battleship game) and for some reason when it runs "through" the section bellow it says "can't assign function call" when it's a copy paste of a piece of just above (with a couple changes) and it gives no error. Do you see the error?
'''
elif y == "v":
if a + 3 > 4:
return "put the boat higher, here it leaves the board"
else:
board(a)(b) = "V"
a = a + 1
board(a)(b) = "V"
a = a + 1
board(a)(b) = "V"
return board
'''
First of all, I highly recommend you to use python 3, read this.
And I don't know what is board, so I will answer for two cases.
board is not a function, nested python list
In this case, just change () to [] to access array.
board is a function
In this case, you're definitely wrong. board() is a function call and will return function result. So, you cannot assign "V" into your function call. This is pretty natural.
Now, check out what is your case and happy coding.
Maybe instead of accessing a matrix with the [] operator you are making calls with (). So try replacing board(a)(b) with board[a][b] but without more information is really hard to tell.

Categories