generation.py 2.24 KB
Newer Older
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
1
from abc import ABCMeta, abstractmethod
2
3
4
5
6
7
from typing import List, Tuple

from encode.iora import IORAEncoder
from learn.algorithm import learn
from learn.ra import RALearner
from model.ra import Action
8
from sut import RASUT, RegisterMachineObservation, IORAObservation
9
10
11
from sut.stack import new_stack_sut
from test import IORATest

Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
12
13
14
15
16
17
18
19

class ObservationGeneration(metaclass=ABCMeta):

    @abstractmethod
    def generate_observations(self, max_depth) -> List[object]:
        pass

class ExhaustiveRAGenerator(ObservationGeneration):
20
    def __init__(self, sut:RASUT):
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
21
        self.sut = sut
22
23
        self.act_sigs = sut.input_interface()
        for sig in self.act_sigs:
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
24
25
26
            if sig.num_params > 1:
                raise Exception("This generator assumes at most one parameter per action")

27
    def generate_observations(self, max_depth, max_registers=3) -> List[Tuple[Action, Action]]:
28
29
30
        observations = self._generate_observations([IORAObservation([])], 0, max_depth, max_registers+1)
        print("\n".join([str(obs) for obs in observations]))
        obs_traces = [obs.trace() for obs in observations]
31
        return obs_traces
32
33


Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
34

35
36
    def _generate_observations(self, prev_obs:List[RegisterMachineObservation], crt_depth, max_depth, max_values) \
            -> List[RegisterMachineObservation]:
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
37
38
39
40
        if crt_depth > max_depth:
            return []
        else:
            new_obs = []
41
            for obs in prev_obs:
42
                num_val = max(obs.values()) + 1 if len(obs.values()) > 0 else 0
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
43
44
45
                for act_sig in self.act_sigs:
                    label = act_sig.label
                    if act_sig.num_params == 1:
46
                        for i in range(0, min(num_val+1, max_values)):
47
                            seq = obs.inputs()
48
                            seq.append(Action(label, i))
49
                            new_obs.append(self.sut.run(seq))
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
50
                    else:
51
                        seq = obs.inputs()
52
                        seq.append(Action(label, None))
53
                        new_obs.append(self.sut.run(seq))
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
54
55

            if crt_depth < max_depth:
56
57
                extended_obs = self._generate_observations(new_obs, crt_depth + 1, max_depth, max_values)
                new_obs.extend(extended_obs)
Paul Fiterau Brostean's avatar
Paul Fiterau Brostean committed
58
            return  new_obs