Skip to content

Commit 4ae9d6f

Browse files
committed
added basic sampling functionality
1 parent 398f9ad commit 4ae9d6f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

aalpy/utils/Sampling.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from random import randint, choices, random
2+
3+
from aalpy import MooreMachine, Dfa, NDMooreMachine, Mdp, MarkovChain
4+
from aalpy.base import Automaton, DeterministicAutomaton
5+
6+
7+
def sample_with_length_limits(alphabet, nr_samples, min_len, max_len):
8+
return [choices(alphabet, k = randint(min_len, max_len)) for _ in range(nr_samples)]
9+
10+
11+
def sample_with_term_prob(alphabet, nr_samples, term_prob):
12+
ret = []
13+
for _ in range(nr_samples):
14+
k = 0
15+
while term_prob < random():
16+
k += 1
17+
ret.append(choices(alphabet, k=k))
18+
return ret
19+
20+
21+
def get_complete_sample(automaton: DeterministicAutomaton):
22+
alphabet = automaton.get_input_alphabet()
23+
automaton.compute_prefixes()
24+
char_set = automaton.compute_characterization_set()
25+
infixes = [(x,) for x in alphabet] + [tuple()]
26+
return [state.prefix + infix + suffix for state in automaton.states for suffix in char_set for infix in infixes]
27+
28+
29+
def get_io_traces(automaton: Automaton, input_traces: list) -> list:
30+
moore_automata = (MooreMachine, NDMooreMachine, Mdp, MarkovChain)
31+
is_moore = isinstance(automaton, moore_automata)
32+
33+
traces = []
34+
for input_trace in input_traces:
35+
output_trace = automaton.execute_sequence(automaton.initial_state, input_trace)
36+
trace = list(zip(input_trace, output_trace))
37+
if is_moore:
38+
trace = [automaton.initial_state.output] + trace
39+
traces.append(trace)
40+
return traces

0 commit comments

Comments
 (0)