def update_transition_system(self): controller = KerasController(keras_model=self.controller_keras_obj, cap_values=self.cap_values) tr = TFControlledTransitionRelation(dynamics_obj=self.overt_obj, controller_obj=controller) self.ts = TransitionSystem(states=tr.states, initial_set=self.init_set, transition_relation=tr)
def run(self): tr = TFControlledTransitionRelation(dynamics_obj=self.dynamics, controller_obj=self.controller_obj) init_set = dict(zip(tr.states.flatten(), self.init_range)) ts = TransitionSystem(states=tr.states, initial_set=init_set, transition_relation=tr) solver = self.solver prop = self.setup_property() algo = self.algo(ts=ts, prop=prop, solver=solver) return algo.check_invariant_until(self.n_steps)
def _simple_run(self, n_check_invariant): self.setup_controller_obj() self.setup_overt_dyn_obj() tr = TFControlledTransitionRelation(dynamics_obj=self.overt_dyn_obj, controller_obj=self.controller_obj, turn_max_to_relu=True) init_set = dict(zip(self.state_vars, self.init_range)) ts = TransitionSystem(states=tr.states, initial_set=init_set, transition_relation=tr) for c in ts.transition_relation.constraints: assert (not isinstance(c, MaxConstraint)) #solver = MarabouWrapper(n_worker=self.ncore) solver = GurobiPyWrapper() prop = self.setup_property() algo = BMC(ts=ts, prop=prop, solver=solver) return algo.check_invariant_until(n_check_invariant)
# print("single pendulum dynamics constraints = ", len(single_pendulum_dynamics.constraints)) # print("controler constraints = ", len(controller.constraints)) # create transition relation using controller and dynamics tr = TFControlledTransitionRelation(dynamics_obj=single_pendulum_dynamics, controller_obj=controller) # initial set x1_init_set = (0.5, 1) x2_init_set = (-0.5, 0.5) init_set = {states[0]: x1_init_set, states[1]: x2_init_set} # build the transition system as an (S, I(S), TR) tuple ts = TransitionSystem(states=tr.states, initial_set=init_set, transition_relation=tr) # solver solver = GurobiPyWrapper() #MarabouWrapper() prop_list = [] p1 = Constraint(ConstraintType('GREATER')) p1.monomials = [Monomial(1, states[0])] p1.scalar = 0.3 prop_list.append(p1) p2 = Constraint(ConstraintType('LESS')) p2.monomials = [Monomial(1, states[0])] p2.scalar = 1.15 prop_list.append(p2)
def test_marabou_interface(alpha, prop_desc, n_invar, with_relu=False, with_max=False): # create controller object, this is just a place holder. I will modify the object later. model = load_model( "../OverApprox/models/single_pend_nn_controller_lqr_data.h5") controller = KerasController(keras_model=model) # rewrite to make a simple controller that is always equal to alpha*x controller.control_outputs = [['c']] controller.state_inputs = [['xc']] fake_constraint = [] if with_relu: alpha_times_x = 'var1' monomial_list = [ Monomial(alpha, controller.state_inputs[0][0]), Monomial(-1, alpha_times_x) ] fake_constraint.append( Constraint(ConstraintType('EQUALITY'), monomial_list, 0.0)) relu_constraint = [ ReluConstraint(varin=alpha_times_x, varout=controller.control_outputs[0][0]) ] controller.constraints = relu_constraint + fake_constraint controller.relus = relu_constraint elif with_max: alpha_times_x = 'var1' monomial_list = [ Monomial(alpha, controller.state_inputs[0][0]), Monomial(-1, alpha_times_x) ] fake_constraint.append( Constraint(ConstraintType('EQUALITY'), monomial_list, 0.0)) max_second_arg = 'var2' fake_constraint.append( Constraint(ConstraintType('EQUALITY'), [Monomial(1, max_second_arg)], -1 / 2)) max_constraint = [ MaxConstraint(varsin=[alpha_times_x, max_second_arg], varout=controller.control_outputs[0][0]) ] controller.constraints = max_constraint + fake_constraint controller.relus = [] else: monomial_list = [ Monomial(-1, controller.control_outputs[0][0]), Monomial(alpha, controller.state_inputs[0][0]) ] fake_constraint = [ Constraint(ConstraintType('EQUALITY'), monomial_list, 0.0) ] controller.constraints = fake_constraint controller.relus = [] # create overt dynamics objects. this is just a place holder. I will modify the object later. overt_obj = OvertConstraint( "../OverApprox/models/single_pend_acceleration_overt.h5") # rewrite to make a simple controller that is always equal to x overt_obj.control_vars = [['cd']] overt_obj.state_vars = [['x']] overt_obj.output_vars = [['dx']] monomial_list2 = [ Monomial(1, overt_obj.control_vars[0][0]), Monomial(-1, overt_obj.output_vars[0][0]) ] fake_constraint2 = [ Constraint(ConstraintType('EQUALITY'), monomial_list2, 0.5) ] overt_obj.constraints = fake_constraint2 simple_dynamics = Dynamics(np.array(['x']), np.array(['cd'])) next_states = simple_dynamics.next_states.reshape(1, ) # x_next = x + dt*dx dt = 1 c1 = Constraint(ConstraintType('EQUALITY')) c1.monomials = [ Monomial(1, overt_obj.state_vars[0][0]), Monomial(dt, overt_obj.output_vars[0][0]), Monomial(-1, next_states[0]) ] simple_dynamics.constraints = [c1] + overt_obj.constraints print(len(simple_dynamics.constraints)) print(len(controller.constraints)) # create transition relation using controller and dynamics tr = TFControlledTransitionRelation(dynamics_obj=simple_dynamics, controller_obj=controller) # initial set init_set = {overt_obj.state_vars[0][0]: (0., 1.)} # build the transition system as an (S, I(S), TR) tuple ts = TransitionSystem(states=tr.states, initial_set=init_set, transition_relation=tr) # property x< 0.105, x' < 0.2 p = Constraint(ConstraintType(prop_desc["type"])) p.monomials = [Monomial(1, overt_obj.state_vars[0][0])] p.scalar = prop_desc["scalar"] # prop = ConstraintProperty([p], [overt_obj.state_vars[0][0]]) # solver solver = MarabouWrapper() algo = BMC(ts=ts, prop=prop, solver=solver) result, vals, stats = algo.check_invariant_until(n_invar) return result.name