def _make_action_space(self) -> CompactSet: return CompactSet({ vexpr.Variable("w"): (self.min_w, self.max_w), # rotational velocity vexpr.Variable("a"): (-self.B, self.A), # translational acceleration })
def _make_action_space(self) -> Space: return vsrl.spaces.continuous.CompactSet( { vexpr.Variable("w"): (self.min_w, self.max_w), # rotational velocity vexpr.Variable("a"): (-self.B, self.A), # translational acceleration } )
def _make_action_space(self) -> Space: if self.continuous_action_space: return CompactSet({vexpr.Variable("acc"): (-self.B, self.A)}) else: accel_action = np.array([self.A]) decel_action = np.array([-self.B]) return FiniteSpace([accel_action, decel_action], [vexpr.Variable("acc")])
def _make_oracle_space(self) -> Space: return CompactSet( { vexpr.Variable("follower_pos"): (0, self._width), vexpr.Variable("rvel"): (-100, 100), vexpr.Variable("leader_pos"): (0, self._width), } )
def test_grouping(self): for n_str in self.numbers: assert vexpr.Number(float(n_str)) == parser.parse("(" + n_str + ")") for v in self.variables: assert vexpr.Variable(v) == parser.parse("(" + v + ")")
def state_constants(self): return { vexpr.Variable("A"): vexpr.Number(self.A), vexpr.Variable("B"): vexpr.Number(self.B), vexpr.Variable("T"): vexpr.Number(self.T), vexpr.Variable("safe_sep"): vexpr.Number(self._safe_sep), vexpr.Variable("min_w"): vexpr.Number(self.min_w), vexpr.Variable("max_w"): vexpr.Number(self.max_w), vexpr.Variable("num_obstacles"): vexpr.Number(self.num_obstacles), }
def _make_oracle_space(self) -> Space: """Contains an entry for the ego, the charger, every obstacle, initial pointmesses, and `self.num_pointmesses_on_collision` pointmesses per obstacle""" ranges: Dict[vexpr.Variable, Tuple[float, float]] = { vexpr.Variable("ego_x"): ( 0 + self.map_buffer, self._width - self.map_buffer, ), vexpr.Variable("ego_y"): ( 0 + self.map_buffer, self._width - self.map_buffer, ), vexpr.Variable("charger_x"): ( 0 + self.map_buffer, self._width - self.map_buffer, ), vexpr.Variable("charger_y"): ( 0 + self.map_buffer, self._width - self.map_buffer, ), vexpr.Variable("theta"): (-2 * pi, 2 * pi), vexpr.Variable("v"): (0, self.max_v), vexpr.Variable("w"): (self.min_w, self.max_w), } # Add obstacles to the oracle space. for i in range(1, self.num_obstacles + 1): ranges[vexpr.Variable(f"obs{i}_x")] = ( 0 + self.map_buffer, self._width - self.map_buffer, ) ranges[vexpr.Variable(f"obs{i}_y")] = ( 0 + self.map_buffer, self._height - self.map_buffer, ) for i in range( 1, self.num_initial_pointmess + self.num_pointmesses_on_collision * self.num_obstacles + 1, ): ranges[vexpr.Variable(f"pointmess{i}_x")] = ( 0 + self.map_buffer, self._width - self.map_buffer, ) ranges[vexpr.Variable(f"pointmess{i}_y")] = ( 0 + self.map_buffer, self._height - self.map_buffer, ) return CompactSet(ranges)
def _make_oracle_space(self) -> CompactSet: # make sure this stays in sync with the indices into the state array defined # in init ranges: Dict[vexpr.Variable, Tuple[float, float]] = { vexpr.Variable("ego_x"): ( 0 + self.map_buffer, self._width - self.map_buffer, ), vexpr.Variable("ego_y"): ( 0 + self.map_buffer, self._height - self.map_buffer, ), vexpr.Variable("goal_x"): ( 0 + self.map_buffer, self._width - self.map_buffer, ), vexpr.Variable("goal_y"): ( 0 + self.map_buffer, self._height - self.map_buffer, ), vexpr.Variable("theta"): (-2 * pi, 2 * pi), vexpr.Variable("sin_theta"): (-1, 1), vexpr.Variable("cos_theta"): (-1, 1), vexpr.Variable("v"): (0, self.max_v), vexpr.Variable("w"): (self.min_w, self.max_w), } # Add obstacles to the oracle space. for i in range(1, self.num_obstacles + 1): ranges[vexpr.Variable(f"obs{i}_x")] = ( 0 + self.map_buffer, self._width - self.map_buffer, ) ranges[vexpr.Variable(f"obs{i}_y")] = ( 0 + self.map_buffer, self._height - self.map_buffer, ) return CompactSet(ranges)
def test_neg(self): for v in self.variables: neg_variable = parser.parse("-" + v) assert isinstance(neg_variable, vexpr.Neg) assert vexpr.Variable(v) == neg_variable.child assert isinstance(parser.parse("-x^(-x)"), vexpr.Neg)
def test_variables(self): for v in self.variables: assert vexpr.Variable(v) == parser.parse(v)
def _convert(ast: parsimonious.nodes.Node) -> vexpr.Expression: if ast.expr_name == "term": assert len(ast.children) == 3 return _convert(ast.children[1]) elif ast.expr_name == "formula": assert len(ast.children) == 3 return _convert(ast.children[1]) elif ast.expr_name == "program": assert len(ast.children) == 3 return _convert(ast.children[1]) if ast.expr_name == "number": return vexpr.Number(float(ast.text)) elif ast.expr_name == "variable": return vexpr.Variable(ast.text) elif ast.expr_name == "neg": assert len(ast.children) == 2 return vexpr.Neg(_convert(ast.children[1])) elif (ast.expr_name == "term_group" or ast.expr_name == "formula_group" or ast.expr_name == "program_group"): assert len(ast.children) == 3 return _convert(ast.children[1]) elif ast.expr_name == "power": return functools.reduce(vexpr.Power, _collect_right_assoc(ast)) elif ast.expr_name == "mult": return functools.reduce(vexpr.Times, _collect_right_assoc(ast)) elif ast.expr_name == "divide": return functools.reduce(vexpr.Divide, _collect_right_assoc(ast)) elif ast.expr_name == "plus": return functools.reduce(vexpr.Plus, _collect_right_assoc(ast)) elif ast.expr_name == "minus": return functools.reduce(vexpr.Minus, _collect_right_assoc(ast)) elif ast.expr_name == "true": return vexpr.TrueF() elif ast.expr_name == "false": return vexpr.FalseF() elif ast.expr_name == "not": return vexpr.Not(_convert(ast.children[1])) elif ast.expr_name == "less": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.Less(left, right) elif ast.expr_name == "lesseq": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.LessEq(left, right) elif ast.expr_name == "greater": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.Greater(left, right) elif ast.expr_name == "greatereq": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.GreaterEq(left, right) elif ast.expr_name == "equal": left = _convert(ast.children[0]) right = _convert(ast.children[2]) return vexpr.Eq(left, right) elif ast.expr_name == "and": return functools.reduce(vexpr.And, _collect_right_assoc(ast)) elif ast.expr_name == "or": return functools.reduce(vexpr.Or, _collect_right_assoc(ast)) elif ast.expr_name == "imply": return functools.reduce(vexpr.Imply, _collect_right_assoc(ast)) elif ast.expr_name == "equiv": return functools.reduce(vexpr.Equiv, _collect_right_assoc(ast)) elif ast.expr_name == "box": return vexpr.Box(_convert(ast.children[1]), _convert(ast.children[3])) elif ast.expr_name == "variable_list": # variable_list = variable / (("{" ~"\s*")? variable+ (~"\s*" "}")?) if len(ast.children) == 1: return [_convert(ast.children[0])] else: vs = [] for c in ast.children[0].children[1]: vs.append(_convert(c)) return vs elif ast.expr_name == "forall": vs = _convert(ast.children[1]) f = _convert(ast.children[3]) return vexpr.Forall(set(vs), f) elif ast.expr_name == "exists": vs = _convert(ast.children[1]) f = _convert(ast.children[3]) return vexpr.Forall(set(vs), f) elif ast.expr_name == "assignment": return vexpr.Assign(_convert(ast.children[0]), _convert(ast.children[2])) elif ast.expr_name == "test": return vexpr.Test(_convert(ast.children[1])) elif ast.expr_name == "star": return vexpr.Loop(_convert(ast.children[1])) elif ast.expr_name == "choice": return functools.reduce(vexpr.Choice, _collect_right_assoc(ast)) elif ast.expr_name == "seqcomp": return functools.reduce(vexpr.SequentialCompose, _collect_right_assoc(ast)) elif ast.expr_name == "assignment": return vexpr.Box(_convert(ast.children[0]), _convert(ast.children[2])) elif ast.expr_name == "atomic_ode": return vexpr.AtomicODE(_convert(ast.children[1]), _convert(ast.children[3])) elif ast.expr_name == "diff_product": return functools.reduce(vexpr.DiffPair, _collect_right_assoc(ast)) elif ast.expr_name == "ode_system": f = _convert(ast.children[3]) ode = ast.children[1] assert len(ode.children) == 1 ode = ode.children[0] assert ode.expr_name == "diff_product" or ode.expr_name == "atomic_ode" return vexpr.ODESystem(_convert(ode), f) elif ast.expr_name == "ode_system_no_evdom": ode = ast.children[1] assert len(ode.children) == 1 ode = ode.children[0] assert ode.expr_name == "diff_product" or ode.expr_name == "atomic_ode" return vexpr.ODESystem(_convert(ode), vexpr.TrueF()) else: if len(ast.children) != 1: raise ParserException( f"Successfully parsed into a parsimonious library node, but " f"_convert does not know how to handle expr_name: {ast.expr_name}" ) else: return _convert(ast.children[0])