def join_afn(self, afn, current_state_id, afn_id, token): new_states = set() initial_state = State(current_state_id + 1, [], True, False, -1) final_state = State(current_state_id + 2, [], False, True, token) for a_s in self.states: if a_s.is_accept_state: a_s.add_transition( Transition(chr(400), chr(400), {final_state})) a_s.is_accept_state = False if a_s.is_initial_state: initial_state.add_transition( Transition(chr(400), chr(400), {a_s})) a_s.is_initial_state = False for a_s in afn.states: if a_s.is_accept_state: a_s.add_transition( Transition(chr(400), chr(400), {final_state})) a_s.is_accept_state = False if a_s.is_initial_state: initial_state.add_transition( Transition(chr(400), chr(400), {a_s})) a_s.is_initial_state = False new_alphabet = list(set(self.alphabet) | set(afn.alphabet)) new_states.update(self.states) new_states.update(afn.states) new_states.add(initial_state) new_states.add(final_state) a = AFN(afn_id, initial_state, new_alphabet, {final_state}, new_states) return a
def P2turnTransactions(self, tensor1_bef, tensor1_new, tensor2_bef, tensor2_new, A1, A2, R1, R2, state): #If P2 won with its previous move update both P1 and P2 Q values if (R2 == 1): self.approximator.addExperience( Transition( tensor1_bef, A1, -1 * R2, tensor1_new, False)) #Store transition information from P1 interactions self.approximator.addExperience( Transition( tensor2_bef, A2, R2, tensor2_new, False)) #Store transition information from P2 interactions self.matchRecord[2] += 1 self.totalGameLength += 42 - state.movesLeft return True #Game ended #Update weights for P1 actions if (state.movesLeft > 0): #Game continues self.approximator.addExperience( Transition(tensor1_bef, A1, R1, tensor1_new, state.avMoves) ) #Store transition information from P1 interactions else: #Game tied self.approximator.addExperience( Transition( tensor1_bef, A1, R1, tensor1_new, False)) #Store transition information from P1 interactions self.approximator.addExperience( Transition( tensor2_bef, A2, R2, tensor2_new, False)) #Store transition information from P2 interactions self.matchRecord[1] += 1 self.totalGameLength += 42 return True #Game ended return False #Game continues
def predict_svm(self, stack, buff, labels, previous_transitions, arcs, input_sentence): if len(stack) < 2 and len(buff) > 0: return (Transition(Transition.Shift, None)) features = self.extract_features(None, stack, buff, labels, previous_transitions, arcs, input_sentence) feat_vect = [ features.get(feat, 0) for feat in self.master_feats.keys() ] predicted_transition_id = self.svm_model.predict(feat_vect) predicted_transition = self.svm_id_to_label_transition[ predicted_transition_id[0]] if len(buff) == 0: if predicted_transition == Transition(Transition.Shift, None): """ probabs2 = self.svm_model.predict_proba(feat_vect) # Works ONLY for one vs. one SVC and NOT for LinearSVC regular_probab_array = [] for i in range(len(probabs2[0])): regular_probab_array.append(probabs2[0][i]) best_arc = self.get_best_arc(regular_probab_array) assert best_arc > 1, "Invalid arc proposed" return(self.svm_id_to_label_transition[best_arc]) """ probabs = self.svm_model.decision_function( feat_vect) #Works for both LinearSVC and SVC regular_dist_array = [] for i in range(len(probabs[0])): regular_dist_array.append(abs(probabs[0][i])) best_arc = self.get_best_arc(regular_dist_array) assert best_arc > 1, "Invalid arc proposed" return (self.svm_id_to_label_transition[best_arc]) return (self.svm_id_to_label_transition[predicted_transition_id[0]])
def predict(self, stack, buff, labels, previous_transitions): if not self.hasLearned: print >> sys.stderr, "len(X): " + str(len(self.X)) print >> sys.stderr, "len(Y): " + str(len(self.Y)) self.sess.run(self.train_step, feed_dict={ self.x: self.X, self.y_: self.Y }) self.hasLearned = True bestTransitionType = self.sess.run(self.getBestLabel, feed_dict={ self.x: [ self.createFeatureVector( stack, buff, labels, previous_transitions) ] }) print >> sys.stderr, 'bestTransitionType: %d' % bestTransitionType[0] if len(stack) < 2 and bestTransitionType[0] != 0: return (0, Transition(0, None)) if len(buff) == 0 and bestTransitionType[0] == 0: # print >> sys.stderr, 'changing shift to sth else' # return (0, Transition(1, None)) if probs[1] > probs[2] else (0, Transition(2, None)) return (0, Transition(1, None)) return (0, Transition(bestTransitionType[0], None))
def optional(self): newStart = State() newAccept = State() start = deepcopy(self.start) accepts = deepcopy(self.accepts) newStart.addTransition(Transition(epsilon, start)) newStart.addTransition(Transition(epsilon, newAccept)) for a in accepts: a.setToken(-1) a.addTransition(Transition(epsilon, newAccept)) newStates = set([newStart, start, newAccept]) for a in accepts: newStates.add(a) for s in self.states: if (s.equals(start) == False): for a in self.accepts: if (s.equals(a) == False): newStates.add(s) #Free memory del start del accepts return NFA(newStates, NFA.addNewAlphabet(newStates), newStart, set([newAccept]))
def optional_operator(self, afn_id, current_state_id, token): new_initial_state = State(current_state_id + 1, [], True, False, -1) new_final_state = State(current_state_id + 2, [], False, True, token) new_states = set() for t in self.states: if t.is_accept_state: new_initial_state.add_transition( Transition(chr(400), chr(400), {self.initial_state})) new_initial_state.add_transition( Transition(chr(400), chr(400), {new_final_state})) t.add_transition( Transition(chr(400), chr(400), {new_final_state})) t.is_accept_state = False self.initial_state.is_initial_state = False new_states.update(self.states) new_states.add(new_initial_state) new_states.add(new_final_state) a = AFN(afn_id, new_initial_state, self.alphabet, {new_final_state}, new_states) return a
def make_transitions(rules, alphabet): transitions = [] state_from = state_to = 's0' print("Type2") # Строим команды типа 2 для всех терминальных символов for alpha in alphabet: stack_from = inp = alpha stack_to = "" t = Transition(state_from, inp, stack_from, state_to, [stack_to]) print(t) transitions.append(t) # Добавляем переход в конечное состояние print('Type3 - Final state') inp = stack_to = '' stack_from = MARKER_STACK t = Transition(state_from, inp, stack_from, state_to, [stack_to]) print(t) transitions.append(t) print('Commands:\nType1') # Строим команды типа 1, '' - empty symb state_from = state_to = 's0' for rule in rules: stack_from = rule.left stack_to = rule.right inp = '' t = Transition(state_from, inp, stack_from, state_to, stack_to) print(t) transitions.append(t) print() return transitions
def build_automata(self, language, counter=0, checkArr=None): if checkArr == None: q0 = self.initial check = [] dfa_states = [] toState = Transition(start=q0, transition=None, end=q0) toState.set_initial(True) dfa_states.append(toState) #check.append(toState) elif len(checkArr) > 0: S = [] check = checkArr dfa_states = copy.copy(checkArr) else: print("AFD", check) return "finished" #print("States", dfa_states) for toState in dfa_states: if toState.get_mark(): #toState = dfa_states.pop() continue toState.set_mark(True) #marcamos #obtenemos move de toState for letter in language: if letter != "&" and letter != BuilderEnum.HASH.value: #get traversal pasa a ser la union de los follow pos de cada uno de los elem res = self.traverse(toState.get_end(), letter) if len(res) > 0: is_in_dfa = self.search_dfa_state(res, check) if not is_in_dfa: #Creamos y pusheamos el estado al array y al dfa toPush_arr = Transition(start=toState.get_end(), transition=letter, end=res) toPush_arr.set_index(counter) counter += 1 self.fn.append(toPush_arr) check.append(toPush_arr) else: createState = Transition(start=toState.get_end(), transition=letter, end=res) createState.set_index(counter) counter += 1 if not self.check_existence(createState): self.fn.append(createState) else: continue is_over = self.is_over(check) if not is_over: self.build_automata(checkArr=check, language=language ,counter=counter) else: #print("TRANS",self.fn) return "finished"
def add_arc(self, arc, value): up = True if arc.contains_arc(self): up = False if arc == self: new_trans = Transition(self.position, self.position, value, True, up) else: new_trans = Transition(self.position, arc.position, value, False, up) self.arcs[arc] = new_trans
def possible_transitions(self, stack, buff): possible_transitions = [] if len(buff) >= 1: possible_transitions.append(Transition(Transition.Shift, None)) if len(stack) >= 2: for label in self.label_set: possible_transitions.append( Transition(Transition.LeftArc, label)) possible_transitions.append( Transition(Transition.RightArc, label)) assert len(possible_transitions) > 0 return possible_transitions
def join(self, nfaB): #Create a deepcopy of start states from both NFAs startA = deepcopy(self.start) startB = deepcopy(nfaB.getStart()) #Create a deepcopy of accept states acceptsA = deepcopy(self.accepts) acceptsB = deepcopy(nfaB.getAccepts()) #Create new start, accept state and new states set newStart = State() newAccept = State() #Add epsilon transitions to new start state newStart.addTransition(Transition(epsilon, startA)) newStart.addTransition(Transition(epsilon, startB)) #Add epsilon transition to new accept state for a in acceptsA: a.setToken(-1) a.addTransition(Transition(epsilon, newAccept)) for b in acceptsB: b.setToken(-1) b.addTransition(Transition(epsilon, newAccept)) #Add updated "accept" states (no longer accepted), new start and new accept newStates = set([newStart, newAccept]) for a in acceptsA: newStates.add(a) for b in acceptsB: newStates.add(b) #Add the remaining states for e1 in self.states: for a in self.accepts: if (e1.equals(a) == False): newStates.add(e1) for e2 in nfaB.getStates(): for b in nfaB.getAccepts(): if (e2.equals(b) == False): newStates.add(e2) #Free memory del startA del startB del acceptsA del acceptsB return NFA(newStates, NFA.addNewAlphabet(newStates), newStart, set([newAccept]))
def Transition(self): if self.peek().type == "IDENTIFIER": t = self.advance() self.info("Parsed Transition variable", t.value) return Transition(var=t.value) elif self.peek().type == "LPAREN": self.expect("LPAREN") form = self.Formation() self.expect("COMA") time = self.advance().value self.expect("RPAREN") return Transition(formation=form, time=time) else: raise Exception("Incorrect transition declaration")
def join_automata(self, automata, operation): alphabet = automata.get_alphabet() current_states = [ AutomataActions().join_states_operation( automata.get_initial_nodes(), operation) ] new_transitions = [] for cn in current_states: for a in alphabet: states = AutomataActions().join_states_operation( AutomataActions().get_next_dot_state( cn, a, automata.transitionList), operation) if not self.state_exists_in_automata(states.stateName, current_states): current_states.append(states) if not self.check_dfa_transition_in_automata( cn, a, new_transitions): new_transitions.append(Transition(cn, states, a)) return AutomataActions().transformation_save_automata( current_states, new_transitions, "NFA")
def createGraph(trace_list, time_list): """ Creates a graph based on a list of traces and timestamps. """ graph = {} time_list = timeProcessing(time_list) for trace, time in zip(trace_list, time_list): for i in range(len(trace) - 1): path = (trace[i], trace[i + 1]) r_path = tuple(reversed(path)) if path not in graph: graph[path] = Transition(path) graph[r_path] = Transition(r_path) graph[path].add(1, time[i]) graph[r_path].add(1, time[i]) return graph
def __init__(self, data): # name self.instance_name = data['name'] # locations self.locations = { loc['id'] : Location( loc ) for loc in data['locations']} # outgoing transitions from each location for t in data['transitions']: tid = t['sid'] transition = Transition(t) self.locations[tid].add_exit_transition(transition) # current location setup self.loc_id = data['initialLocation']['id'] # initial location id self.current_location = self.locations[self.loc_id] # variable objects self.I = { i['name'] : Variable(i) for i in data['I']} self.O = { o['name'] : Variable(o) for o in data['O']} self.X = { x['name'] : Variable(x) for x in data['X_C']} # Simulink has multiple options for initialization # 1. reset on the first transition for fx in data['initialization']: if fx['LHS'] in self.X: self.X[fx['LHS']].set_current_value(fx['RHS']) elif fx['LHS'] in self.O: self.O[fx['LHS']].set_current_value(fx['RHS']) # 2. initial location entry actions for en in data['initialLocation']['entries']: if en['LHS'] in self.X: self.X[en['LHS']].set_current_value(fx['RHS']) elif en['LHS'] in self.O: self.O[en['LHS']].set_current_value(fx['RHS']) self.update_O(index = 0)
def _read_FA(self): with open(self.__file_name, 'r') as f: # read first line => the initial state initial_state = f.readline() initial_state = initial_state.strip('\n').split(',') self.__init_state = initial_state # read second line => the states states = f.readline() states = states.strip('\n').split(',') self.__states.extend(states) # read third line => the final states final_states = f.readline() final_states = final_states.strip('\n').split(',') self.__final_states.extend(final_states) # read fourth line => the alphabet alphabet = f.readline() alphabet = alphabet.strip('\n').split(',') self.__alphabet.extend(alphabet) # read the remaining lines => the transitions for line in f: transition_line = line.strip('\n').split(',') # print(transition_line[0]+ " " + transition_line[1] + " " + transition_line[2:].__str__()) if transition_line[0] and transition_line[1] not in self.__states: raise UnknownSymbol("Check the transition to be correct!!!") self.__transitions.append(Transition(transition_line[0], transition_line[1], transition_line[2:]))
def random_walk(self, iters): """ Generate new plan states by random walking and stop after given iterations. Note that this is not searching because the walking is not directed by the objective value. Args: iters (int): how many steps before stopping """ # instantialize a transition object to handle actions transition = Transition(silent=self.silent, pr_actions=[1., 0., 0., 0.]) for i in range(iters): if not self.silent: print("\n\n------------------- Iter: %d --------------------" % i) # randomly pick a action with different weighting # apply the picked action to picked room and get new state action = random_pick(transition.actions, p=transition.pr_actions) if not self.silent: print("\nSelected action:", action) getattr(transition, action)(self) if not self.silent: print("\nRoom states after action:") self._pprint_rooms() # parse the stats and evaluate the stats # plot the plan every 20 iterations self._parse() self._evaluate() if i % self.plot_freq == 0: self._plot_intermediate(i) self.plot_fig.ioff()
def learn(self): if len(self.memory) < self.batch_size: return # Should we update our target model? if self.learn_step_counter % self.target_udpate_counter == 0: self.target_model.load_state_dict(self.eval_model.state_dict()) self.learn_step_counter += 1 # Gives 128 transitions which we combine into one transitions = self.memory.sample( self.batch_size) # transitions.len = 128 batch = Transition( *zip(*transitions)) # batch = transition, each element.len = 128 state_batch = torch.cat(batch.state) action_batch = torch.cat(batch.action) reward_batch = torch.cat(batch.reward) next_state_batch = torch.cat(batch.next_state) q_eval = self.eval_model(state_batch).gather(1, action_batch) q_next = self.target_model(next_state_batch).detach().max(1)[0].view( self.batch_size, 1) new_q = (q_next * self.gamma) + reward_batch loss = self.loss_func(q_eval, new_q) self.optimizer.zero_grad() loss.backward() self.optimizer.step()
def nfa_to_dfa(self, nfa_automata): new_states = [nfa_automata.get_initial_node()] new_transitions = [] alphabet = nfa_automata.get_alphabet() for ns in new_states: for s in alphabet: next_states = [] states = AutomataActions().get_next_dot_state( ns, s, nfa_automata.transitionList) if len(states) == 0: continue for nsa in states: if nsa not in next_states: next_states.append(nsa) state = AutomataActions().join_states(next_states) if not self.state_exists_in_automata(state.stateName, new_states): new_states.append(state) if not self.check_dfa_transition_in_automata( ns, s, new_transitions): new_transitions.append(Transition(ns, state, s)) return AutomataActions().transformation_save_automata( new_states, new_transitions, "NFA")
def parse_state_machine(cls, inputString): tokens = Tokenizer(inputString) number_of_states = 0 state_machine_id = 0 state_machine = StateMachine() while (tokens.has_more_tokens()): current_token = tokens.peek() if (current_token == '--fsm-config'): tokens.next_token() state_machine_id = tokens.next_token() elif (current_token == '--nstates'): tokens.next_token() number_of_states = tokens.next_token() else: new_state = State(tokens) state_machine.add_state(new_state) # Create transitions token = tokens.peek() while (re.match(r'--n(\d+)x(\d+)', token) != None and re.match(r'--s(\d+)', token) == None): new_transition = Transition(tokens) state_machine.add_transition(new_transition) token = tokens.peek() return state_machine
def concatenate_afn(self, afn, afn_id, token): for s in self.states: if s.is_accept_state: for t in afn.initial_state.transitions: s.add_transition(t) for x in afn.states: for tx in x.transitions: if {afn.initial_state}.issubset(tx.destination_states): x.add_transition( Transition(tx.symbol_1, tx.symbol_2, {s})) s.is_accept_state = False s.token = 0 afn.states.remove(afn.initial_state) for a_s in afn.accept_states: a_s.token = token for a_s in afn.states: if a_s.is_accept_state: a_s.token = token new_alphabet = list(set(self.alphabet) | set(afn.alphabet)) new_states = set() new_states.update(self.states) new_states.update(afn.states) a = AFN(afn_id, self.initial_state, new_alphabet, afn.accept_states, new_states) return a
def setTransitions(self, transitions): pairs = self.getPairsFromDescription(transitions) transitions = [] for i in pairs: state, move, writeSymbol = int(i[0]), int(i[1]) % 2, int(i[1]) % 3 transitions.append(Transition(state, move, writeSymbol)) self.transitions = self.convert1dArrayTo2dArray(transitions)
def push(self, *args): if len(self.memory) < self.capacity: self.memory.append(None) self.memory[self.position] = Transition(*args) self.position = (self.position + 1) % self.capacity self.counter += 1
def addTransition(self, src, dest): if self._canAddTransition(src, dest): newTransition = Transition(src,dest,self.handler) self.transitions.append(newTransition) return newTransition else: return None
def loadFile(fpath): gc = FSMConfig() tree = ET.parse(fpath) root = tree.getroot() if (root.tag == "FSM"): #First Pass - data for child in root: if (child.tag == "data"): newID = int(child.attrib["id"]) gc.allData[newID] = Data(newID) gc.allData[newID].parseCfg(child) # Second Pass - States for child in root: if (child.tag == "state"): newID = int(child.attrib["id"]) gc.allStates[newID] = State(newID) gc.allStates[newID].parseCfg(child) # Third Pass - Transitions for child in root: if (child.tag == "transition"): newID = int(child.attrib["id"]) gc.allTransitions[newID] = Transition(newID) gc.allTransitions[newID].parseCfg(child) # Fourth Pass - global configuration for child in root: if (child.tag == "config"): parseGlobalCfg(child) gc.configFPath = fpath
def __init__(self, data): # name self.instance_name = data['name'] # locations self.locations = { loc['id']: Location(loc) for loc in data['locations'] } # register exiting transitions to each location object for t in data['transitions']: tid = t['sid'] transition = Transition(t) self.locations[tid].add_outgoing_transition(transition) # current location setup loc_id = data['initialLocation'] # initial location id self.current_location = self.locations[loc_id] # variable objects self.I = {i['name']: float(i['initialValue']) for i in data['I']} self.O = {o['name']: float(o['initialValue']) for o in data['O']} self.X = {x['name']: float(x['initialValue']) for x in data['X_C']} # Simulink has multiple options for initialization # 1. reset on the first transition for fx in data['initialization']: if fx['LHS'] in self.X: self.X[fx['LHS']] = fx['RHS'] elif fx['LHS'] in self.O: self.O[fx['LHS']] = fx['RHS'] # 2. initial location entry actions initLocID = data['initialLocation'] self.locations[initLocID].en(self.I, self.O, self.X) # evaluate "en" of a location self.update_O()
def createDFA(self, tokens): language = [] for token in tokens: if token.get_type() == "SYMBOL" and (token.get_value() != "&"): if token.get_value() not in language: language.append(token.get_value()) self.language = language self.build_automata(language) for x in self.fn: #if self.final in x.get_end(): z, isCommon = self.interset(self.final.keys(), x.get_end()) if isCommon: #creamos estado nuevo f = Transition(start=x.get_end(), transition=None, end=None, kind={self.final[z[0]]: z[0]}) f.set_final(True) self.fn.append(f) self.finalDFA.append(f) arr = self.find_transition_to_end(x.get_end()) for i in arr: i.type = {self.final[z[0]]: z[0]} return
def __init__(self, event_manager, size, pos, text1, text2): self.event_manager = event_manager self.was_clicked = False self.depressed = False self.width = size[0] self.height = size[1] self.side_size = (self.width - self.height) + 2 * self.padding self.middle_size = self.height self.height = self.height self.toggle_length = 2 * self.side_size + self.middle_size self.transition = Transition(0, self.side_size) surface_size = (self.toggle_length + self.side_size, self.height) super().__init__(pos, surface_size) self.rendered_text1_size = self.main_font.size(text1) self.rendered_text2_size = self.main_font.size(text2) self.pre_mask = pygame.Surface((self.toggle_length, self.height)) self.pre_mask_x = 0 pygame.draw.rect(self.pre_mask, pygame.Color(200, 0, 0), pygame.Rect((0, 0), (self.side_size, self.height))) pygame.draw.rect( self.pre_mask, pygame.Color(0, 200, 0), pygame.Rect((self.side_size + self.middle_size, 0), (self.side_size, self.height))) pygame.draw.rect( self.pre_mask, self.middle_colour, pygame.Rect((self.side_size, 0), (self.height, self.height))) rendered_text1 = self.main_font.render(text1, True, self.font_colour) rendered_text2 = self.main_font.render(text2, True, self.font_colour) self.pre_mask.blit( rendered_text1, ((self.side_size / 2) - self.rendered_text1_size[0] / 2, (self.height / 2) - (self.rendered_text1_size[1] / 2))) self.pre_mask.blit( rendered_text2, (self.side_size + self.middle_size + (self.side_size / 2) - self.rendered_text2_size[0] / 2, (self.height / 2) - (self.rendered_text1_size[1] / 2))) self.mask_layer = pygame.Surface(surface_size, SRCALPHA) pygame.draw.rect(self.mask_layer, (0, 0, 0), pygame.Rect((0, 0), (self.side_size, self.height))) pygame.draw.rect( self.mask_layer, (0, 0, 0), pygame.Rect((self.toggle_length, 0), (self.side_size, self.height))) self.set_colorkey((0, 0, 0))
def createBasic(symbol): e1 = State() e2 = State() t1 = Transition(symbol, e2) e1.addTransition(t1) states = set([e1, e2]) alphabet = set([symbol]) return NFA(states, alphabet, e1, set([e2]))
def receive_last_reward(self, final_state, turn, reward): if self.is_training: state_one_hot = transform_board(self.board_size, final_state, turn) self.experiencedModel.add_experience(Transition(self.prev_state[turn], self.prev_action[turn], reward, state_one_hot, np.zeros(0))) self.fit_model() self.prev_state[turn] = None self.prev_action[turn] = None