3

I'm trying to constrain an overall budget using mystic, where each individual x has an associated function that is ultimately being maximized.

I have set bounds such that each individual spend can only go up or down at a maximum of 30%. I am not trying to set a constraint so the overall budget is not exceeded.

equations = """A + B + C + D + E + F + G + H + I + J  < %s  """%(budget)

var = list('ABCDEFGHIJKLMNOPQ')
eqns = ms.simplify(equations, variables=var, all=True)
constrain = ms.generate_constraint(ms.generate_solvers(eqns, var), join=my.constraints.and_)

The above works as expected, but as soon as I add an 11th (K) to the addition, the ms.simplify funciton throws the following error: NameError: name 'B0' is not defined.

Is there a way to constrain more than 10 values at once?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
JBrack
  • 31
  • 2

1 Answers1

3

I'm the mystic author. At first blush, it looks like a bug with named variables.

Note that you aren't using numbered variables, which the error is suggesting/assuming you do.

>>> import mystic.symbolic as ms
>>> equations = """A + B + C + D + E + F + G + H + I + J  < 1000"""
>>> var = list('ABCDEFGHIJKLMNOPQ')
>>> eqns = ms.simplify(equations, variables=var, all=True)
>>> print(eqns)
A < -B - C - D - E - F - G - H - I - J + 1000

... adding in a K produces the error you reported.

However, we can go above 10 variables if we add some numbered variables (in particular, at least B0, as suggested by the traceback):

>>> equations = """A + B + C + D + E + F + G + H + I + J + B0 + B1 < 1000"""
>>> var = list('ABCDEFGHIJ') + ['B0','B1']
>>> eqns = ms.simplify(equations, variables=var, all=True)
>>> print(eqns)
A < -B - B0 - B1 - C - D - E - F - G - H - I - J + 1000

...or if we use all numbered variables:

>>> equations = """x0 + x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10 + x11 < 1000"""
>>> var = ['x%s' % i for i in range(12)]
>>> eqns = ms.simplify(equations, variables=var, all=True)
>>> print(eqns)
x0 < -x1 - x10 - x11 - x2 - x3 - x4 - x5 - x6 - x7 - x8 - x9 + 1000

Apparently, whatever the second variable in var is (e.g. B or x), then you need to include a similarly named variable (i.e. B0 or x0). So... that is unexpected to me... thus, it looks like you found a bug.

UPDATE: the bug should be fixed now. A new release of mystic is due out shortly, however the code is patched on GitHub.

>>> import mystic.symbolic as ms
>>> equations = """A + B + C + D + E + F + G + H + I + J + K + L < 1000"""
>>> var = list('ABCDEFGHIJKLMNOPQ')
>>> ms.simplify(equations, variables=var, all=True)
'A < -B - K - L - C - D - E - F - G - H - I - J + 1000'
>>> import mystic as my
>>> my.__version__
'0.4.1.dev0'
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139