Is there some way to use variable names in mystic constraints?
i.e.
equations = '''
abs(x0 - init_velocity) <= acceleration_constraint
abs(x1 - init_heading) <= turning_constraint
'''
The above causes this error:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3418, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-68-254fb3031193>", line 14, in <module>
eqn = simplify(equations, all=True)
File "<string>", line 120, in simplify
File "<string>", line 93, in _simplify
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mystic/symbolic.py", line 381, in equals
after, before = eval(after,{},locals_), eval(before,{},locals_)
File "<string>", line unknown
^
SyntaxError: unexpected EOF while parsing
EDIT:
Code used below is trying to minimize l2 norm between current local point (x_0, y_0) and next local point (x_1, y_1) while following constraints (physical acceleration and turning radius). This distance is minimized with respect to velocity and heading.
def formulate_objective(curr_x, curr_y, desired_x, desired_y):
def objective(x):
next_x = curr_x + x[0] * np.cos(np.radians(x[1]))
next_y = curr_y + x[0] * np.sin(np.radians(x[1]))
return np.linalg.norm(np.vstack([desired_x - next_x, desired_y - next_y]), ord=2)
return objective
objective = formulate_objective(init_x, init_y, desired_x, desired_y)
equations = '''
abs(x0 - iv) <= ac
abs(x1 - ih) <= tc
'''
var_dict = dict(iv=init_velocity, ih=init_heading,
ac=acceleration_constraint, tc=turning_constraint)
eqn = simplify(equations, locals=var_dict)
cf = generate_constraint(generate_conditions(eqn, nvars=2), join=and_)
x0 = [init_velocity, init_heading]
fmin(objective, x0=x0, constraint=cf)
You should be able to use the locals
keyword to make substitutions for names that are not optimization variable names.
>>> import mystic.symbolic as ms
>>> equations = '''
... x0 + x1 = A
... x1 * x0 = B
... '''
>>>
>>> eqn = ms.simplify(equations, locals=dict(A=7,B=12))
>>> print(eqn)
x0 = 7 - x1
x0 != 0
x1 = 12/x0
>>>