LearnTA  0.0.1
equivalence_oracle_by_random_test.hh
1 
6 #pragma once
7 
8 #include <optional>
9 #include <utility>
10 #include <random>
11 
12 #include "timed_automaton.hh"
13 #include "timed_word.hh"
14 #include "equivalence_oracle.hh"
15 #include "timed_automaton_runner.hh"
16 
17 namespace learnta {
22  const std::vector<Alphabet> alphabet;
23  const TimedAutomaton automaton;
24  const int maxTests;
25  const int maxLength;
26  const double maxDuration;
27  public:
28  EquivalenceOracleByRandomTest(std::vector<Alphabet> alphabet, TimedAutomaton automaton,
29  const int maxTests, const int maxLength, const int maxDuration) :
30  alphabet(std::move(alphabet)),
31  automaton(std::move(automaton)),
32  maxTests(maxTests),
33  maxLength(maxLength),
34  maxDuration(maxDuration) {}
35 
39  [[nodiscard]] std::optional<TimedWord> findCounterExample(const TimedAutomaton &hypothesis) override {
40  ++eqQueryCount;
41  static std::random_device rng;
42  std::mt19937 engine(rng());
43 
44  TimedAutomatonRunner runner(automaton);
45  TimedAutomatonRunner hypothesisRunner(hypothesis);
46  auto durationDist = std::uniform_real_distribution<double>(0, maxDuration);
47  auto actionDist = std::uniform_int_distribution(0, int(alphabet.size() - 1));
48 
49  for (int i = 0; i < maxTests; ++i) {
50  runner.pre();
51  hypothesisRunner.pre();
52  std::string word;
53  std::vector<double> durations;
54  for (int j = 0; j < maxLength; ++j) {
55  const double duration = durationDist(engine);
56  durations.push_back(duration);
57  if (runner.step(duration) != hypothesisRunner.step(duration)) {
58  return TimedWord{word, durations};
59  }
60  const auto wordIndex = actionDist(engine);
61  word.push_back(alphabet.at(wordIndex));
62  if (runner.step(alphabet.at(wordIndex)) != hypothesisRunner.step(alphabet.at(wordIndex))) {
63  durations.push_back(0);
64  return TimedWord{word, durations};
65  }
66  }
67  const double duration = durationDist(engine);
68  durations.push_back(duration);
69  if (runner.step(duration) != hypothesisRunner.step(duration)) {
70  return TimedWord{word, durations};
71  }
72 
73  runner.post();
74  hypothesisRunner.post();
75  }
76 
77  return std::nullopt;
78  }
79  };
80 }
The equivalence oracle by random test.
Definition: equivalence_oracle_by_random_test.hh:21
std::optional< TimedWord > findCounterExample(const TimedAutomaton &hypothesis) override
Make an equivalence query.
Definition: equivalence_oracle_by_random_test.hh:39
Interface of the equivalence oracle.
Definition: equivalence_oracle.hh:17
Class to execute a timed automaton.
Definition: timed_automaton_runner.hh:22
bool step(char action) override
Feed a discrete action.
Definition: timed_automaton_runner.hh:80
void post() override
The function should be called after feeding each timed word.
Definition: timed_automaton_runner.hh:51
void pre() override
Reset the configuration.
Definition: timed_automaton_runner.hh:41
A timed word.
Definition: timed_word.hh:25
Definition: experiment_runner.hh:23
A timed automaton.
Definition: timed_automaton.hh:213