def case_1(): # Read the vehicle model vehicle = lomap.Ts() vehicle.read_from_file('./vehicle_1.txt') # Convert the vehicle model to an MDP vehicle_mdp = lomap.Markov() vehicle_mdp.mdp_from_det_ts(vehicle) # Read the models of the pedestrians targets = [] for i in range(1, 6): t = lomap.Markov() t.read_from_file('./target_%d.txt' % i) targets.append(t) formula = '! col U end' # Construct the full-state fsa using scheck # (full-state ensures proper MDP after product) logger.info('Formula: %s' % formula) fsa = lomap.Fsa() fsa.fsa_from_cosafe_formula(formula) fsa.add_trap_state() # Classical with lomap.Timer( 'Classical (non-incremental) Method: Case 1 - Avoid pedestrians'): lomap.classical_synthesis(vehicle_mdp, fsa, targets, set_props_1) pass # Incremental with lomap.Timer('Incremental Method: Case 1 - Avoid pedestrians'): lomap.incremental_synthesis(vehicle_mdp, fsa, targets, set_props_1)
def construct_local_fsa(self): ts = lomap.Ts() edges = [] init_state = None # Define the edges (hardcoded for now using dk.brics.automaton package # by Anders Moller, available at http://www.brics.dk/automaton/) if self.local_spec == '(assist|extinguish)*': edges += [('init', 'init', {'input': set(['assist'])})] edges += [('init', 'init', {'input': set(['extinguish'])})] init_state = 'init' elif self.local_spec == '(pickup.dropoff)*': edges += [('empty', 'cargo', {'input': set(['pickup'])})] edges += [('cargo', 'empty', {'input': set(['dropoff'])})] init_state = 'empty' elif self.local_spec == '(pickup1.dropoff1|pickup2.dropoff2)*': edges += [('empty', 'cargoa', {'input': set(['pickup1'])})] edges += [('cargoa', 'empty', {'input': set(['dropoff1'])})] edges += [('empty', 'cargob', {'input': set(['pickup2'])})] edges += [('cargob', 'empty', {'input': set(['dropoff2'])})] init_state = 'empty' else: assert False, 'Local spec %s is not implemented' % self.local_spec # Construct the ts for s, v, d in edges: ts.g.add_edge(s, v, None, d) # Set the initial state ts.init[init_state] = 1 return ts
def construct_global_ts(self): logger.debug('Constructing the global TS') ts = lomap.Ts() # ts.name: str - name of the model # ts.init: dict - initial distribution of the model # ts.final: set - accepting states # ts.g: a NetworkX multidigraph # state property 'prop' gives the propositions satisfied at that state. # edge properties 'weight' and 'control' give the weight of the edge and the corresponding control. ts.name = 'Global TS' # Initial state of the global transition system init_state = (self.quad.x, self.quad.y) ts.init[init_state] = 1 # Add the cells with global requests for cell in self.env.global_reqs: ts.g.add_node(cell, prop=self.env.global_reqs[cell]['reqs']) # Add edges between each global request cell for u, v in it.product(self.env.global_reqs, repeat=2): # Compute manhattan distance dist = abs(u[0] - v[0]) + abs(u[1] - v[1]) assert (u == v) != (dist > 0), 'Global requests cannot be co-located.' # Change self-loop weights to 1 to prevent infinite loops during planning dist = 1 if dist == 0 else dist logger.debug('%s -> %s (dist: %s)' % (u, v, dist)) ts.g.add_edge(u, v, weight=dist, control='N/A', label=dist) # If the init_state is not a global request, # add it to the global TS but define outgoing edges only if init_state not in self.env.global_reqs: ts.g.add_node(init_state, prop=set([])) for v in self.env.global_reqs: # Compute manhattan distance dist = abs(init_state[0] - v[0]) + abs(init_state[1] - v[1]) logger.debug('%s -> %s (dist: %s)' % (init_state, v, dist)) ts.g.add_edge(init_state, v, weight=dist, control='N/A', label=dist) return ts
def case_2b(): # Read the vehicle model vehicle = lomap.Ts() vehicle.read_from_file('./vehicle_1.txt') # Convert the vehicle model to an MDP vehicle_mdp = lomap.Markov() vehicle_mdp.mdp_from_det_ts(vehicle) # Read the models of the pedestrians targets = [] for i in range(1, 6): t = lomap.Markov() t.read_from_file('./target_%d.txt' % i) targets.append(t) formula = '( F catch0 | F catch1 | F catch2 | F catch3 ) & ( ! col4 U end )' # Construct the full-state fsa using scheck # (full-state ensures proper MDP after product) logger.info('Formula: %s' % formula) fsa = lomap.Fsa() fsa.fsa_from_cosafe_formula(formula) fsa.add_trap_state() # Classical with lomap.Timer( 'Classical (non-incremental) Method: Case 2b - Save at least one friendly' ): lomap.classical_synthesis(vehicle_mdp, fsa, targets, set_props_2) pass # Incremental targets_inc = [targets[-1]] + targets[:-1] assumed_props = [('ped0c2', 'ped1c2', 'ped2c2', 'ped3c2'), ('ped1c2', 'ped2c2', 'ped3c2'), ('ped2c2', 'ped3c2'), ('ped3c2'), ()] with lomap.Timer( 'Incremental Method: Case 2b - Save at least one friendly'): lomap.incremental_synthesis(vehicle_mdp, fsa, targets_inc, set_props_2, assumed_props) pass
def construct_local_ts(self): ts = lomap.Ts() # ts.name: str - name of the model # ts.init: dict - initial distribution of the model # ts.final: set - accepting states # ts.g: a NetworkX multidigraph # state property 'prop' gives the propositions satisfied at that state. # edge properties 'weight' and 'control' give the weight of the edge and the corresponding control. ts.name = 'Local TS' # Find the current cell of the quad (center cell of the sensing range) # with integer division center_cell = (self.quad.x, self.quad.y) ts.init[center_cell] = 1 # Define the vertices of the local TS (the sensing cells) for cx, cy in it.product(list(range(0, self.quad.sensing_range)), repeat=2): cell = self.quad.get_sensing_cell_global_coords((cx, cy)) props = self.quad.sensed[cx][cy]['local_reqs'] # Add this cell with sensed local requests ts.g.add_node(cell, prop=props) assert len(ts.g) == self.quad.sensing_range**2 # Define edges between the states of the local ts for cell in ts.g: # east, north, west, south, hold controls and corresponding neighbors x, y = cell controls = { 'e': (x + 1, y), 'n': (x, y + 1), 'w': (x - 1, y), 's': (x, y - 1), 'h': (x, y) } for ctrl in controls: neigh = controls[ctrl] # Skip this neighbor if it is not in the local TS if neigh not in ts.g: continue # Add the edge ts.g.add_edge(cell, neigh, weight=1, control=ctrl) return ts
def case_3b(): # Read the vehicle model vehicle = lomap.Ts() vehicle.read_from_file('./vehicle_2.txt') # Convert the vehicle model to an MDP vehicle_mdp = lomap.Markov() vehicle_mdp.mdp_from_det_ts(vehicle) # Read the models of the guards targets = [] for i in range(1, 7): t = lomap.Markov() t.read_from_file('./trap_%d.txt' % i) targets.append(t) # Reach end safely formula = '! unsafe U end' # Construct the full-state fsa using scheck # (full-state ensures proper MDP after product) logger.info('Formula: %s' % formula) fsa = lomap.Fsa() fsa.fsa_from_cosafe_formula(formula) fsa.add_trap_state() #fsa.visualize() # Classical with lomap.Timer('Classical (non-incremental) Method: Case 3b - Order 2'): lomap.classical_synthesis(vehicle_mdp, fsa, targets, set_props_3) pass # Incremental targets_inc = [ targets[2], targets[3], targets[0], targets[1], targets[5], targets[4] ] with lomap.Timer('Incremental Method: Case 3b - Order 2'): lomap.incremental_synthesis(vehicle_mdp, fsa, targets_inc, set_props_3) pass
def main(): Rho = namedtuple('Rho', ['lower', 'upper']) rhos = [Rho(lower=0.98, upper=1.04), Rho(lower=0.98, upper=1.04)] pdb.set_trace() # Case Study 1 # with lomap.Timer('IJRR 2013 Case-Study 1'): # r1 = lomap.Ts() # r2 = lomap.Ts() # r1.read_from_file('./robot_1.txt') # r2.read_from_file('./robot_2.txt') # ts_tuple = (r1, r2) # formula = '[]<>gather && [](r1gather -> X(!r1gather U r1upload)) && [](r2gather -> X(!r2gather U r2upload))' # opt_prop = set(['gather']) # logger.info('Formula: %s', formula) # logger.info('opt_prop: %s', opt_prop) # prefix_length, prefixes, suffix_cycle_cost, suffix_cycles = lomap.multi_agent_optimal_run(ts_tuple, formula, opt_prop) # logger.info('Cost: %d', suffix_cycle_cost) # logger.info('Prefix length: %d', prefix_length) # # Find the controls that will produce this run # control_prefixes = [] # control_suffix_cycles = [] # for i in range(0, len(ts_tuple)): # ts = ts_tuple[i] # control_prefixes.append(ts.controls_from_run(prefixes[i])) # control_suffix_cycles.append(ts.controls_from_run(suffix_cycles[i])) # logger.info('%s run prefix: %s', ts.name, prefixes[i]) # logger.info('%s control perfix: %s', ts.name, control_prefixes[i]) # logger.info('%s suffix cycle: %s', ts.name, suffix_cycles[i]) # logger.info('%s control suffix cycle: %s', ts.name, control_suffix_cycles[i]) # # logger.info('<><><> <><><> <><><>') # Case Study 4 with lomap.Timer('IJRR 2013 Case-Study 4'): r1 = lomap.Ts() r2 = lomap.Ts() r1.read_from_file('./robot_1.txt') r2.read_from_file('./robot_2.txt') ts_tuple = (r1, r2) pdb.set_trace() formula = '[]<>gather && [](gather->(r1gather4 && r2gather2)) && [](r1gather -> X(!r1gather U r1upload)) && [](r2gather -> X(!r2gather U r2upload))' opt_prop = set(['r1gather4', 'r2gather2']) logger.info('Formula: %s', formula) logger.info('opt_prop: %s', opt_prop) prefix_length, prefixes, suffix_cycle_cost, suffix_cycles = lomap.multi_agent_optimal_run( ts_tuple, formula, opt_prop) logger.info('Cost: %d', suffix_cycle_cost) logger.info('Prefix length: %d', prefix_length) # Find the controls that will produce this run control_prefixes = [] control_suffix_cycles = [] for i in range(0, len(ts_tuple)): ts = ts_tuple[i] control_prefixes.append(ts.controls_from_run(prefixes[i])) control_suffix_cycles.append(ts.controls_from_run( suffix_cycles[i])) logger.info('%s run prefix: %s', ts.name, prefixes[i]) logger.info('%s control perfix: %s', ts.name, control_prefixes[i]) logger.info('%s suffix cycle: %s', ts.name, suffix_cycles[i]) logger.info('%s control suffix cycle: %s', ts.name, control_suffix_cycles[i]) logger.info('<><><> <><><> <><><>')