aalpy.automata.MarkovChain
View Source
import random from aalpy.base import Automaton, AutomatonState class McState(AutomatonState): def __init__(self, state_id, output): super().__init__(state_id) self.output = output # transitions is a list of tuples (Node(output), probability) self.transitions = list() class MarkovChain(Automaton): """Markov Decision Process.""" def __init__(self, initial_state, states: list): super().__init__(initial_state, states) def reset_to_initial(self): self.current_state = self.initial_state def step(self, letter=None): """Next step is determined based on transition probabilities of the current state. Args: letter: input Returns: output of the current state """ if not self.current_state.transitions: return self.current_state.output probability_distributions = [i[1] for i in self.current_state.transitions] states = [i[0] for i in self.current_state.transitions] new_state = random.choices(states, probability_distributions, k=1)[0] self.current_state = new_state return self.current_state.output def step_to(self, input): """Performs a step on the automaton based on the input `inp` and output `out`. Args: input: input Returns: output of the reached state, None otherwise """ for s in self.current_state.transitions: if s[0].output == input: self.current_state = s[0] return self.current_state.output return None
View Source
class McState(AutomatonState): def __init__(self, state_id, output): super().__init__(state_id) self.output = output # transitions is a list of tuples (Node(output), probability) self.transitions = list()
Helper class that provides a standard way to create an ABC using inheritance.
View Source
def __init__(self, state_id, output): super().__init__(state_id) self.output = output # transitions is a list of tuples (Node(output), probability) self.transitions = list()
Single state of an automaton. Each state consists of a state id, a dictionary of transitions, where the keys are inputs and the values are the corresponding target states, and a prefix that leads to the state from the initial state.
Args:
state_id(Any): used for graphical representation of the state. A good practice is to keep it unique.
View Source
class MarkovChain(Automaton): """Markov Decision Process.""" def __init__(self, initial_state, states: list): super().__init__(initial_state, states) def reset_to_initial(self): self.current_state = self.initial_state def step(self, letter=None): """Next step is determined based on transition probabilities of the current state. Args: letter: input Returns: output of the current state """ if not self.current_state.transitions: return self.current_state.output probability_distributions = [i[1] for i in self.current_state.transitions] states = [i[0] for i in self.current_state.transitions] new_state = random.choices(states, probability_distributions, k=1)[0] self.current_state = new_state return self.current_state.output def step_to(self, input): """Performs a step on the automaton based on the input `inp` and output `out`. Args: input: input Returns: output of the reached state, None otherwise """ for s in self.current_state.transitions: if s[0].output == input: self.current_state = s[0] return self.current_state.output return None
Markov Decision Process.
View Source
def __init__(self, initial_state, states: list): super().__init__(initial_state, states)
Args:
initial_state (AutomatonState): initial state of the automaton
states (list) : list containing all states of the automaton
View Source
def reset_to_initial(self): self.current_state = self.initial_state
Resets the current state of the automaton to the initial state
View Source
def step(self, letter=None): """Next step is determined based on transition probabilities of the current state. Args: letter: input Returns: output of the current state """ if not self.current_state.transitions: return self.current_state.output probability_distributions = [i[1] for i in self.current_state.transitions] states = [i[0] for i in self.current_state.transitions] new_state = random.choices(states, probability_distributions, k=1)[0] self.current_state = new_state return self.current_state.output
Next step is determined based on transition probabilities of the current state.
Args:
letter: input
Returns:
output of the current state
View Source
def step_to(self, input): """Performs a step on the automaton based on the input `inp` and output `out`. Args: input: input Returns: output of the reached state, None otherwise """ for s in self.current_state.transitions: if s[0].output == input: self.current_state = s[0] return self.current_state.output return None
Performs a step on the automaton based on the input inp
and output out
.
Args:
input: input
Returns:
output of the reached state, None otherwise