Accept a sequence of variable names
sin3000x opened this issue · 2 comments
While creating an array of variables x0, x1, x2 = intvar(0, 10, shape=3, name='x')
, it's fine to have the name of x0
as 'x[0]'
, x1
as 'x[1]'
and so on.
However, if the name is given as a sequence (of the same length of the array), it would be convenient to assign the name to each variable accordingly, rather than use indexing.
E.g., x, y = intvar(0, 10, shape=2, name=['x', 'y'])
should give x
the name 'x'
and y
the name 'y'
, but we have "['x', 'y'][0]"
and "['x', 'y'][1]"
currently, which is less readable...
I have been thinking about that, but I was held back because strings are enumerable so finding cases in which a string can be guessed to be a list of chars might mess up everything; unless of course we explicitly check that it is instance list/tuple as in your example, and that the dimensions map the shape. Then we can do things like x,y = intvar(0,10, shape=2, name=list("xy"))
which would be nice in some of our lecture examples too...
We would accept such a change, do you want to give it a try (cpmpy/expressions/variables.py) and submit a pull request?
Thanks for your reply. Things might be a bit tricky for higher dimensions (I haven't thought it through yet), but I'd be happy to give it a try when I have some time.