使用Mystic如何一次约束超过10个变量?

huangapple go评论54阅读模式
英文:

How do you constrain more than 10 variables at a time using Mystic?

问题

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?

英文:

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?

答案1

得分: 3

我是mystic的作者。乍看之下,这似乎是与命名变量相关的错误。

请注意,您没有使用编号变量,而错误提示/假定您正在使用它们。

>>> 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

...添加一个K会导致您报告的错误。

但是,如果我们添加一些编号的变量(特别是至少B0,如跟踪建议的那样),我们可以超过10个变量:

>>> 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

...或者如果我们使用所有编号的变量:

>>> 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

显然,无论var中的第二个变量是什么(例如Bx),都需要包含类似命名的变量(即B0x0)。所以... 这对我来说是意外的... 因此,看起来您发现了一个错误。

更新: 该错误现在应该已经修复。mystic的新版本即将发布,不过代码已经在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'
英文:

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'

huangapple
  • 本文由 发表于 2023年2月24日 05:12:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定