reimplement terms lens to work around _tuple bug + start milp

This commit is contained in:
Marcell Vazquez-Chanlatte 2016-07-10 12:06:49 -07:00
parent 2e3ff68be6
commit bed8c51756
3 changed files with 32 additions and 36 deletions

View file

@ -17,7 +17,7 @@ from funcy import flatten
import numpy as np
from lenses import lens
from sympy import Symbol
from sympy import Function
from stl import stl
@ -45,13 +45,14 @@ terms = (term __ pm __ terms) / term
var = id time?
time = prime / time_index
time_index = "[" "t" __ pm __ const "]"
time_index = "(" "t" __ pm __ const ")"
prime = "'"
pm = "+" / "-"
dt = "dt"
unbound = "?"
id = ("x" / "u" / "w") ~r"[a-zA-z\d]*"
id = ("x" / "u" / "w") (aZ / ~r"\d")*
aZ = (~r"[a-z]" / ~r"A-z")
const = ~r"[\+\-]?\d*(\.\d+)?"
op = ">=" / "<=" / "<" / ">" / "="
_ = ~r"\s"+
@ -96,18 +97,17 @@ class STLVisitor(NodeVisitor):
visit_and = partialmethod(binop_visitor, op=stl.And)
def visit_id(self, name, _):
return Symbol(name.text)
return Function(name.text)(stl.t_sym)
def visit_var(self, _, children):
iden, time_node = children
time_node = list(flatten(time_node))
time = time_node[0] if len(time_node) > 0 else stl.t_sym
return stl.Var(iden, time)
return iden.subs(stl.t_sym, time)
def visit_time_index(self, _, children):
return children[3]* children[5]
return stl.t_sym + children[3]* children[5]
def visit_prime(self, *_):
return -stl.dt_sym
@ -121,7 +121,7 @@ class STLVisitor(NodeVisitor):
def visit_term(self, _, children):
coeffs, var = children
c = coeffs[0] if coeffs else 1
return lens(var).id*c
return var*c
def visit_coeff(self, _, children):
dt, coeff, *_ = children
@ -131,14 +131,14 @@ class STLVisitor(NodeVisitor):
def visit_terms(self, _, children):
if isinstance(children[0], list):
term, _1, sgn ,_2, terms = children[0]
terms = lens(terms)[0].id * sgn
terms = lens(terms)[0]*sgn
return [term] + terms
else:
return children
def visit_lineq(self, _, children):
terms, _1, op, _2, const = children
return stl.LinEq(tuple(terms), op, const[0])
return stl.LinEq(sum(terms), op, const[0])
def visit_pm(self, node, _):
return 1 if node.text == "+" else -1