0

I am trying to optimise the problem below using Mystic. I am currently receiving an error that I don't understand and was hoping someone more familiar with the library could help.

def objective(x):
    x0,x1,x2,x3,x4,x5,x6,x7,x8 = x
    return x0**2 + x4**2 + x8**2
equations = '''
x0 + x1 + x2 - x3 - x6 - 20 == 0.0
x4 + x3 + x5 - x1 - x7 - 150 == 0.0
x8 + x6 + x7 - x2 - x5 + 100 == 0.0
x6 == 0
x7 == 0
x0 >= 10
x4 >= 60
'''
from mystic.symbolic import generate_conditions, generate_penalty
pf = generate_penalty(generate_conditions(equations), k=1e4)
from mystic.symbolic import generate_constraint, generate_solvers, solve
cf = generate_constraint(generate_solvers(solve(equations))

When calculating cf i receive an 'NotImplementedError:cannot simplify inequalities' and wanted to know why this could be? If anyone knows how i would extend this such that i can create the constraints through a function or in a different manner I would also be keen to know. Cheers

1 Answers1

0

I'm the mystic author. You should always first try just using solve(equations) and see what happens. It can fail to symbolically solve the equations due to the inequalities. If so, then try to do simplify(equalities) instead. That symbolically simplifies equations so there's only one variable on the LHS for each line. The inequality solver usually can then work in that case. If that fails, you can rewrite the equations so there's only one variable on the LHS.

>>> def objective(x):
...     x0,x1,x2,x3,x4,x5,x6,x7,x8 = x
...     return x0**2 + x4**2 + x8**2
... 
>>> import mystic
>>> equations = '''
... x0 + x1 + x2 - x3 - x6 - 20 == 0.0
... x4 + x3 + x5 - x1 - x7 - 150 == 0.0
... x8 + x6 + x7 - x2 - x5 + 100 == 0.0
... x6 == 0
... x7 == 0
... x0 >= 10
... x4 >= 60
... '''
>>> eqns = mystic.symbolic.simplify(equations)
>>> print(eqns)
x0 == -x1 - x2 + x3 + x6 + 20
x8 == x2 + x5 - x6 - x7 - 100
x4 >= 60
x7 == 0
x6 == 0
x0 >= 10
x4 == x1 - x3 - x5 + x7 + 150
>>> from mystic.symbolic import generate_constraint, generate_solvers 
>>> cf = generate_constraint(generate_solvers(eqns))
>>> cf([0,1,2,3,4,5,6,7,8])
[26, 1, 2, 3, 143, 5, 0, 0, -106]
>>> 
Mike McKerns
  • 33,715
  • 8
  • 119
  • 139
  • Hi @MikeMcKerns, Thanks for your help this seems to have fixed my issue. I was wondering if you'd also be able to recommend the best solver for this. I am currently using the line 'result = diffev2(objective, x0=bounds, bounds = bounds, constraints=cf' however this produces a result no where near the minimum for x0 **2 + x4 **2 + x8 **2. The bounds used are bounds = [(0,None)]*(no_of_variables). – PyBoy82362 Apr 21 '20 at 10:01
  • The two solvers that I generally try first are `diffev2` and/or one of the ensemble solvers (like `lattice`). The former is generally slower but capable of solving more difficult problems, while the latter (especially with a large number of solvers) is generally faster. – Mike McKerns Apr 21 '20 at 12:28