def make_crane3(index, slots, containers, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name state1 = aut.add_new_state(True, 1) container_states = [] for i in range(containers): container_states.append(aut.add_new_state(False, i + 2)) aut.initial = state1 functions = ["-pick-", "-drop-"] eventlist = {} crane = "C" + str(index) for func in functions: eventlist[func] = [] for i in range(1, slots + 1): for j in range(1, 3): for k in range(1, containers + 1): loc = str(i) + "-" + str(j) + "-" + str(k) event = crane + func + loc aut.alphabet.add(get_event(coll, event)) eventlist[func].append(event) for func in functions: if "-pick-" == func: for event in eventlist[func]: pred, succ = state1, container_states[int(event.split('-')[-1])-1] aut.add_edge_data(pred, succ, get_event(coll, event), 1) else: for event in eventlist[func]: pred, succ = container_states[int(event.split('-')[-1])-1], state1 aut.add_edge_data(pred, succ, get_event(coll, event), 1) for slot in range(1, slots + 1): aut.alphabet.add(get_event(coll, crane + "-switch-" + str(slot))) aut.add_edge_data(state1, state1, get_event(coll, crane + "-switch-" + str(slot)), 1) return aut
def add_weights(aut, edge_weight=0): """ Add weights to unweighted automaton L{aut}. @param aut: Unweighted automaton. @type aut: L{Automaton} @param edge_weight: Weight of each edge. @type edge_weight: C{int} @return: Equivalent weighted automaton. @rtype: L{WeightedAutomaton} """ waut = weighted_structure.WeightedAutomaton(aut.alphabet, aut.collection) waut.set_kind(aut.aut_kind) for stat in aut.get_states(): waut.add_new_state(stat.marked, stat.number) waut.set_initial(waut.get_state(aut.initial.number)) for stat in aut.get_states(): wsrc = waut.get_state(stat.number) for edge in stat.get_outgoing(): wdest = waut.get_state(edge.succ.number) waut.add_edge_data(wsrc, wdest, edge.label, edge_weight) return waut
def make_crane(index, slots, containers, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name state1 = aut.add_new_state(True, 1) state2 = aut.add_new_state(False, 2) aut.initial = state1 functions = ["-pick-", "-drop-"] eventlist = {} crane = "C" + str(index) for func in functions: eventlist[func] = [] for i in range(1, slots + 1): for j in range(1, 3): for k in range(1, containers + 1): loc = str(i) + "-" + str(j) + "-" + str(k) event = crane + func + loc aut.alphabet.add(get_event(coll, event)) eventlist[func].append(event) for func in functions: pred = None succ = None if "-pick-" == func: pred, succ = state1, state2 else: pred, succ = state2, state1 for event in eventlist[func]: aut.add_edge_data(pred, succ, get_event(coll, event), 1) return aut
def make_crane_location(cranes, slot, containers, last, init, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name state1 = aut.add_new_state(True, 1) statedict = {} for i in range(2, cranes + 2): statedict[str(i)] = aut.add_new_state(True, i) print "init: " + str(init) print aut._states aut.initial = aut.get_state(int(init)) functions = ["-pick-", "-drop-"] eventlist = {} craneenter = [] craneleave = [] for l in range(1, cranes+1): crane = "C" + str(l) eventlist[crane] = [] i = slot craneenter.append([]) craneleave.append([]) if slot != 1: event = crane + "-move-" + str(slot-1) + "-" + str(slot) craneenter[l-1].append(event) aut.alphabet.add(get_event(coll, event)) event = crane + "-move-" + str(slot) + "-" + str(slot-1) craneleave[l-1].append(event) aut.alphabet.add(get_event(coll, event)) if not last: event = crane + "-move-" + str(slot+1) + "-" + str(slot) craneenter[l-1].append(event) aut.alphabet.add(get_event(coll, event)) event = crane + "-move-" + str(slot) + "-" + str(slot+1) craneleave[l-1].append(event) aut.alphabet.add(get_event(coll, event)) for func in functions: for j in range(1, 3): for k in range(1, containers + 1): loc = str(i) + "-" + str(j) + "-" + str(k) first = False event = crane + func + loc aut.alphabet.add(get_event(coll, event)) eventlist[crane].append(event) for i in range(2, cranes + 2): pred = statedict[str(i)] succ = statedict[str(i)] crane = "C" + str(i-1) for event in eventlist[crane]: aut.add_edge_data(pred, succ, get_event(coll, event), 1) for i in range(len(craneenter)): empty = state1 full = statedict[str(i + 2)] for event in craneenter[i]: aut.add_edge_data(empty, full, get_event(coll, event), 1) for event in craneleave[i]: aut.add_edge_data(full, empty, get_event(coll, event), 1) return aut
def make_crane_restriction(crane, slot, init, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name state1 = aut.add_new_state(True, 1) state2 = aut.add_new_state(True, 2) aut.initial = aut.get_state(int(init)) event = crane + "-move-" + str(slot) + "-" + str(slot+1) aut.alphabet.add(get_event(coll, event)) aut.add_edge_data(state1, state2, get_event(coll, event), 1) event = crane + "-move-" + str(slot+1) + "-" + str(slot) aut.alphabet.add(get_event(coll, event)) aut.add_edge_data(state2, state1, get_event(coll, event), 1) return aut
def make_container(index, pick_up, drop_off, next, cranes, slots, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name state1 = aut.add_new_state(False, 1) aut.initial = state1 state2 = aut.add_new_state(True, 2) statedict = {} for i in range(3, 3 + cranes): statedict[str(i)] = aut.add_new_state(False, i) functions = ["-pick-", "-drop-"] picklist = [] droplist = [] nextlist = [] if next != None else None first = True for l in range(1, cranes + 1): crane = "C" + str(l) for func in functions: for i in range(1, slots + 1): for j in range(1, 3): loc = str(i) + "-" + str(j) + "-" + str(index) event = crane + func + loc aut.alphabet.add(get_event(coll, event)) for l in range(1, cranes + 1): crane = "C" + str(l) event = crane + "-pick-" + pick_up + str(index) aut.alphabet.add(get_event(coll, event)) picklist.append(get_event(coll, event)) event = crane + "-drop-" + drop_off + str(index) aut.alphabet.add(get_event(coll, event)) droplist.append(get_event(coll, event)) if nextlist != None: event = crane + "-pick-" + pick_up + str(next) aut.alphabet.add(get_event(coll, event)) nextlist.append(get_event(coll, event)) pred = state1 succ = 3 for (crane, event) in enumerate(picklist): aut.add_edge_data(pred, statedict[str(succ + crane)], event, 1) pred = 3 succ = state2 for (crane, event) in enumerate(droplist): aut.add_edge_data(statedict[str(pred + crane)], succ, event, 1) if nextlist != None: for event in nextlist: aut.add_edge_data(state2, state2, event, 1) for crane in range(cranes): crane = statedict[str(crane + 3)] aut.add_edge_data(crane, crane, event, 1) return aut
def make_move_order(SC_auts, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name aut.initial = aut.add_new_state(True, 1) picks = [] for SC_aut in SC_auts: pick_events = [evt for evt in SC_aut.alphabet if "pick" in evt.name] aut.alphabet.update(pick_events) picks.append(pick_events) state = aut.initial while picks: pick_events = picks.pop(random.randint(0, len(picks)-1)) pick_event = random.sample(pick_events, 1)[0] new_state = aut.add_new_state(True, aut.get_num_states() + 1) aut.add_edge_data(state, new_state, pick_event, 1) state = new_state return aut
def create_local_automaton(aut, local_alphabet, start_state, end_state): # creating temporary automaton temp_coll = collection.Collection() temp_automaton = weighted_structure.WeightedAutomaton( local_alphabet, temp_coll) for state in aut.get_states(): ns = temp_automaton.add_new_state(state == end_state, state.number) for state in aut.get_states(): for edge in state.get_outgoing(): if edge.label in local_alphabet: new_edge = edge.copy( temp_automaton.get_state(edge.pred.number), temp_automaton.get_state(edge.succ.number)) temp_automaton.add_edge(new_edge) temp_automaton.initial = temp_automaton.get_state(start_state.number) temp_automaton.reduce(True, True) if temp_automaton.get_num_states() == 0: return None else: return temp_automaton
def reduce_sup(wsup, weight_map): """ Construct a new automaton containing only edges that lead to a decrease in weight. @param wsup: Existing automaton. @type wsup: L{WeightedAutomaton} @param weight_map: Mapping of states in L{wsup} to their weight. @type weight_map: C{dict} of L{State} to C{int} @return: Reduced weighted automaton. @rtype: L{WeightedAutomaton} """ new_aut = weighted_structure.WeightedAutomaton(wsup.alphabet, wsup.collection) old_initial = wsup.initial initial = new_aut.add_new_state(old_initial.marked, old_initial.number) new_aut.set_initial(initial) notdone = [(old_initial, initial)] while len(notdone) > 0: old_state, new_state = notdone.pop() for edge in old_state.get_outgoing(): if weight_map[old_state] < weight_map[edge.succ] + edge.weight: continue if new_aut.has_state(edge.succ.number): new_succ = new_aut.get_state(edge.succ.number) else: new_succ = new_aut.add_new_state(edge.succ.marked, edge.succ.number) notdone.append((edge.succ, new_succ)) new_aut.add_edge_data(new_state, new_succ, edge.label, edge.weight) return new_aut
def setup(self, props): self.aut = weighted_structure.WeightedAutomaton(props.alphabet, props.coll)
from automata import collection, weighted_structure, weighted_frontend, data_structure def dothis(local_alphabet): print local_alphabet.pop() coll = collection.Collection() comp_list = weighted_frontend.load_weighted_automata(coll, "plant1.cfg", False, True) local_alphabet = comp_list[0].alphabet print local_alphabet dothis(local_alphabet) print local_alphabet temp_coll = collection.Collection() temp_automaton = weighted_structure.WeightedAutomaton(local_alphabet, temp_coll) # temp_automaton.add_new_state(False, comp_list[0].initial.number) for state in comp_list[0].get_states(): ns = temp_automaton.add_new_state(state.marked, state.number) for state in comp_list[0].get_states(): for edge in state.get_outgoing(): if edge.label in local_alphabet: print "this happends" new_edge = edge.copy(temp_automaton.get_state(edge.pred.number), temp_automaton.get_state(edge.succ.number)) temp_automaton.add_edge(new_edge) temp_automaton.initial = temp_automaton.get_state(comp_list[0].initial.number) temp_automaton.reduce(True, True) temp_req = data_structure.Automaton(set([next(iter(local_alphabet))]),
def make_crane_location3(cranes, slot, containers, last, init, coll, name): aut = weighted_structure.WeightedAutomaton(set(), coll) aut.name = name state1 = aut.add_new_state(True, 1) statedict = {} for i in range(2, cranes * 3, 3): statedict[str(i)] = aut.add_new_state(True, i) statedict[str(i+1)] = aut.add_new_state(True, i+1) statedict[str(i+2)] = aut.add_new_state(True, i+2) print "init: " + str(init) print aut._states if init == 1: aut.initial = aut.get_state(int(init)) else: init = (int(init) - 1)*3 + 1 aut.initial = aut.get_state(int(init)) functions = ["-pick-", "-drop-"] eventlist = {} craneenter = [] craneleave = [] for l in range(1, cranes+1): crane = "C" + str(l) eventlist[crane] = [] aut.alphabet.add(get_event(coll, crane + "-switch-" + str(slot))) i = slot craneenter.append([]) craneleave.append([]) if slot != 1: event = crane + "-move-" + str(slot-1) + "-" + str(slot) craneenter[l-1].append(event) aut.alphabet.add(get_event(coll, event)) event = crane + "-move-" + str(slot) + "-" + str(slot-1) craneleave[l-1].append(event) aut.alphabet.add(get_event(coll, event)) if not last: event = crane + "-move-" + str(slot+1) + "-" + str(slot) craneenter[l-1].append(event) aut.alphabet.add(get_event(coll, event)) event = crane + "-move-" + str(slot) + "-" + str(slot+1) craneleave[l-1].append(event) aut.alphabet.add(get_event(coll, event)) for func in functions: for j in range(1, 3): for k in range(1, containers + 1): loc = str(i) + "-" + str(j) + "-" + str(k) first = False event = crane + func + loc aut.alphabet.add(get_event(coll, event)) eventlist[crane].append(event) for i in range(2, cranes *3, 3): pred = statedict[str(i)] succ = statedict[str(i+2)] crane = "C" + str((i/3)+1) aut.add_edge_data(pred, succ, get_event(coll, crane + "-switch-" + str(slot)), 1) for event in eventlist[crane]: aut.add_edge_data(pred, succ, get_event(coll, event), 1) pred = statedict[str(i+1)] aut.add_edge_data(pred, succ, get_event(coll, crane + "-switch-" + str(slot)), 1) for event in eventlist[crane]: aut.add_edge_data(pred, succ, get_event(coll, event), 1) pred = statedict[str(i+2)] for event in eventlist[crane]: aut.add_edge_data(pred, succ, get_event(coll, event), 1) for i in range(len(craneenter)): empty = state1 full1 = statedict[str((i*3) + 2)] full2 = statedict[str((i*3) + 3)] full3 = statedict[str((i*3) + 4)] for event in craneenter[i]: print event split = event.split("-") if int(split[-2]) < int(split[-1]): aut.add_edge_data(empty, full1, get_event(coll, event), 1) else: aut.add_edge_data(empty, full2, get_event(coll, event), 1) for event in craneleave[i]: split = event.split("-") aut.add_edge_data(full3, empty, get_event(coll, event), 1) if int(split[-2]) < int(split[-1]): aut.add_edge_data(full1, empty, get_event(coll, event), 1) else: aut.add_edge_data(full2, empty, get_event(coll, event), 1) return aut
def synchronousproduct_weighted_epucks(auts, depth): initstate = [] newalphabet = set() transmap = [] eventweight = {} enabled = [] print "begin transmap" for aut in auts: newalphabet.update(aut.alphabet) collissions = set() num_colls = 0 for i in range(len(auts)): aut = auts[i] initstate.append(aut.initial) collissions.update(aut.initial.collisions) num_colls += len(aut.initial.collisions) #newalphabet.update(aut.alphabet) transmap.append({}) enabled.append({}) for state in aut.get_states(): enabled[i][state] = set() for edge in state.get_outgoing(): eventweight[edge.label] = edge.weight if (state,edge.label) not in transmap[i]: transmap[i][state,edge.label] = [] transmap[i][state,edge.label].append(edge.succ) enabled[i][state].add(edge.label) for i in range(len(auts)): nonlocalevents = newalphabet.difference(auts[i].alphabet) for state in auts[i].get_states(): enabled[i][state].update(nonlocalevents) print "end transmap" inittuple = tuple(initstate) dictionary = {} aut_sync = weighted_structure.WeightedAutomaton(newalphabet, aut.collection) initstate = aut_sync.add_new_state(allmarked(inittuple), aut_sync.get_num_states()) assert(len(collissions) == num_colls) initstate.collisions = collissions aut_sync.initial = initstate dictionary[inittuple] = initstate tovisit = [inittuple] transitions = 0 edgestoadd = [] print "add edges" succcalctime = 0 addstatetime = 0 calccurrenab = 0 count = 0 while len(tovisit) != 0: count += 1 statetuple = tovisit.pop() currenabled = set(enabled[0][statetuple[0]]) for i in range(1,len(enabled)): currenabled.intersection_update(enabled[i][statetuple[i]]) for event in currenabled: hassuccs = True successorlists = [] indexs = [] for i in range(len(transmap)): indexs.append(0) if event not in auts[i].alphabet: successorlists.append([statetuple[i]]) continue if (statetuple[i], event) not in transmap[i]: hassuccs = False break successorlists.append(transmap[i][(statetuple[i], event)]) if not hassuccs: continue #successors = calcsuccessors(statetuple, event, transmap, auts) successors = [] while True: succtuple = [] needtoincrement = True for i in xrange(len(indexs)): succtuple.append(successorlists[i][indexs[i]]) if needtoincrement: indexs[i] += 1 if indexs[i] >= len(successorlists[i]): indexs[i] = 0 else: needtoincrement = False successors.append(hashcachingtuple(succtuple)) if needtoincrement: break for succtuple in successors: collissions = set() num_colls = 0 for state in succtuple: collissions.update(state.collisions) num_colls += len(state.collisions) if num_colls != len(collissions): continue if succtuple not in dictionary: newstate = aut_sync.add_new_state(allmarked(succtuple), aut_sync.get_num_states()) dictionary[succtuple] = newstate tovisit.append(succtuple) newstate.collisions = collissions edgestoadd.append(weighted_structure.WeightedEdge(dictionary[statetuple], dictionary[succtuple], event, eventweight[event])) #print aut_sync #if count % 500 == 0: #print "succcalctime" +str(succcalctime) #print "addstatetime" +str(addstatetime) #print "calccurrenab" +str(calccurrenab) print "end add edges" print "added edges" aut_sync.add_edges(edgestoadd) print "end add edges" #if depth == 5: #auia = aieia return aut_sync