Example #1
0
 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
Example #2
0
    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
Example #3
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))
Example #4
0
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
Example #5
0
    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]))
Example #6
0
    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
Example #7
0
 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]])
Example #8
0
    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 __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))
Example #10
0
 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
Example #11
0
    def __init__(self,
                 transitions,
                 init_stack,
                 init_state='s0',
                 input_str="",
                 verbose=False):
        self.transitions: list[Transition] = transitions

        self.state = Transition(init_state, input_str, list(init_stack))

        self.configurations: list[(str, str)] = []
        self.verbose = verbose
Example #12
0
 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
Example #13
0
    def __init__(self, randomized, seed=None):
        self.randomized = randomized
        self.seed = seed
        self.number_of_trials = 1440

        self.trials = 0
        self.task_order = []
        self.transition = Transition()
        self.position = 0
        self.target = 0
        self.task_index = 0
        self.reset_env()
Example #14
0
    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]))
Example #15
0
 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")
Example #16
0
    def __init__(self, size, main_colour, tab_colour):

        self.size = size

        self.colour = main_colour
        self.colourt = tab_colour

        self.size = size

        super().__init__([-size[0], 0], (size[0] + 10, size[1]))
        self.transition = Transition(self.pos[0], size[0])
        self.fill(main_colour)
        pygame.draw.rect(self, tab_colour,
                         pygame.Rect((size[0], 0), (10, size[1])))
    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
Example #18
0
    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")
Example #19
0
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 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()
Example #21
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:]))
Example #22
0
    def extract(self, obj):
        self.id = obj.get('id').strip()
        self.name = obj.get('name').strip()
        self.label = obj.get('label').lower()

        if self.name == START:
            self.parse_name = START
        else:
            self.parse_name = (obj.get('parse_name')
                               or "parse_" + obj.get('name')).strip()

        self.type = obj.get('type').lower().strip()
        self.parse_headers = []

        p_t = [v for k, v in obj.attrib.items() if k.startswith('trans_prev')]

        for t in p_t:
            if len(t.split(":")) == 4:
                header, field, value_type, value = t.split(":")

                tr = Transition.TransitionValue()

                tr.from_header = header
                tr.on_field = field
                tr.on_value_type = value_type
                tr.on_value = value
                self.default_trans[tr.from_header] = tr
            else:
                # problem, not en enough values
                continue
 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 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
Example #25
0
    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)
Example #26
0
    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()
Example #27
0
    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
Example #28
0
    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
Example #29
0
    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")
Example #30
0
    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 scores(self, X_i, possible_transitions=None):
		probs = self.internal_model.decision_function(X_i)[0]
		ans = []
		for i in range(len(self.internal_model.classes_)):
			transition = Transition.from_category(self.internal_model.classes_[i], score=probs[i])
			if possible_transitions == None or transition in possible_transitions:
				ans.append((probs[i], transition))
		return ans
Example #32
0
 def add_transition(self, letter, from_state_id, to_state_id):
     transition = Transition()
     transition.letter = letter
     transition.from_state_id = from_state_id
     transition.to_state_id = to_state_id
     self.transitions[from_state_id] = transition
	def predict(self, stack, buff, arcs, labels, previous_transitions):
		features = self.extract_features(None, stack, buff, arcs, labels, previous_transitions)
		X_i = self.x_vectorizer.transform(features)
		prediction = self.internal_model.predict(X_i)
		return Transition.from_category(prediction)
Example #34
0
 def __init__(self, name='', logger=logging, time=0.0, minimumStartingTime=-sys.maxint - 1, show=True):
     Transition.__init__(self, name=name, logger=logging, show=show)
     TimeNode.__init__(self, name=name, logger=logging, time=time)
     self.minimumStartingTime = minimumStartingTime
     """Transition can NOT fire before this time