lernen 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.editorconfig +8 -0
- data/.rubocop.yml +34 -0
- data/.yardopts +3 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +29 -0
- data/lib/lernen/automaton.rb +101 -0
- data/lib/lernen/cex_processor.rb +61 -0
- data/lib/lernen/kearns_vazirani.rb +199 -0
- data/lib/lernen/lsharp.rb +335 -0
- data/lib/lernen/lstar.rb +169 -0
- data/lib/lernen/oracle.rb +116 -0
- data/lib/lernen/sul.rb +134 -0
- data/lib/lernen/version.rb +6 -0
- data/lib/lernen.rb +20 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c925ae55bb57b20dc2ef637e751882ec493d565b7f0b7a8348c858593ef0d5f4
|
4
|
+
data.tar.gz: dbfae4d012e582aeb909460eea27c30945a62e6586cd8ae7f9be48ac2c6dac6c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c0621a919ee2cebdc932f9aec31aac52ca7cfc528cd8f8f24ff4519e2d39769c4a6c2c5f9eeecad1394339afbe1625a89fb3acb418aa02e452eef2b78eb2a111
|
7
|
+
data.tar.gz: e88d2feb2c44e766e348c62c05ea15cc5eddcb816077e33db40071957047f31b7eccfeb2f9d8b1679c1907f6a57e116042dac009180b5cef2a4453da1c05f7d1
|
data/.editorconfig
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
syntax_tree: config/rubocop.yml
|
3
|
+
|
4
|
+
require:
|
5
|
+
- rubocop-minitest
|
6
|
+
- rubocop-rake
|
7
|
+
|
8
|
+
AllCops:
|
9
|
+
TargetRubyVersion: 3.1
|
10
|
+
NewCops: enable
|
11
|
+
|
12
|
+
Metrics/AbcSize:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/ClassLength:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/CyclomaticComplexity:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/MethodLength:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/ModuleLength:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Metrics/ParameterLists:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Metrics/PerceivedComplexity:
|
34
|
+
Enabled: false
|
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at make.just.on@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 TSUYUSATO Kitsune
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# Lernen
|
2
|
+
|
3
|
+
> a simple automata learning library.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
require "lernen"
|
9
|
+
|
10
|
+
alphabet = %w[0 1]
|
11
|
+
sul = Lernen::SUL.from_block { |inputs| inputs.count { _1 == "1" } % 4 == 3 }
|
12
|
+
oracle = Lernen::BreadthFirstExplorationOracle.new(alphabet, sul)
|
13
|
+
|
14
|
+
dfa = Lernen::LStar.learn(alphabet, sul, oracle, automaton_type: :dfa)
|
15
|
+
# => Lernen::DFA.new(
|
16
|
+
# 0,
|
17
|
+
# Set[3],
|
18
|
+
# {
|
19
|
+
# [0, "0"] => 0,
|
20
|
+
# [0, "1"] => 1,
|
21
|
+
# [1, "0"] => 1,
|
22
|
+
# [1, "1"] => 2,
|
23
|
+
# [2, "0"] => 2,
|
24
|
+
# [2, "1"] => 3,
|
25
|
+
# [3, "0"] => 3,
|
26
|
+
# [3, "1"] => 0
|
27
|
+
# }
|
28
|
+
# )
|
29
|
+
```
|
30
|
+
|
31
|
+
## Algorithms
|
32
|
+
|
33
|
+
Learnen supports these automata learning algorithms.
|
34
|
+
|
35
|
+
| Algorithm | Supported `automaton_type` |
|
36
|
+
|:----------------:|:--------------------------:|
|
37
|
+
| `LStar` | `:dfa`, `:moore`, `:mealy` |
|
38
|
+
| `KearnsVazirani` | `:dfa`, `:moore`, `:mealy` |
|
39
|
+
| `LSharp` | `:dfa`, `:moore`, `:mealy` |
|
40
|
+
|
41
|
+
## License
|
42
|
+
|
43
|
+
[MIT](https://opensource.org/license/MIT)
|
44
|
+
|
45
|
+
2024 (C) Hiroya Fujinami
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "minitest/test_task"
|
5
|
+
require "yard"
|
6
|
+
require "rubocop/rake_task"
|
7
|
+
require "syntax_tree/rake_tasks"
|
8
|
+
|
9
|
+
Minitest::TestTask.create
|
10
|
+
|
11
|
+
YARD::Rake::YardocTask.new do |t|
|
12
|
+
t.files = ["lib/**/*.rb"]
|
13
|
+
t.stats_options = ["--list-undoc"]
|
14
|
+
end
|
15
|
+
|
16
|
+
RuboCop::RakeTask.new { |t| t.options = %w[--fail-level W] }
|
17
|
+
|
18
|
+
[SyntaxTree::Rake::WriteTask, SyntaxTree::Rake::CheckTask].each do |task|
|
19
|
+
task.new do |t|
|
20
|
+
t.source_files =
|
21
|
+
FileList[
|
22
|
+
%w[Gemfile Rakefile *.gemspec bin/**/{console,rake} lib/**/*.rb test/**/*.rb example/**/*.rb tool/**/*.rb]
|
23
|
+
]
|
24
|
+
t.print_width = 120
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
task format: %w[rubocop:autocorrect_all stree:write]
|
29
|
+
task lint: %w[rubocop stree:check]
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lernen
|
4
|
+
# Automaton is an abstract class for automata.
|
5
|
+
#
|
6
|
+
# Note that this class is *abstract*. You should implement the following method:
|
7
|
+
#
|
8
|
+
# - `#step(state, input)`
|
9
|
+
class Automaton
|
10
|
+
# Computes a transition for the given `input` from the current `state`.
|
11
|
+
#
|
12
|
+
# This is *abstract*.
|
13
|
+
def step(_state, _input)
|
14
|
+
raise TypeError, "abstract method: `step`"
|
15
|
+
end
|
16
|
+
|
17
|
+
# Runs this automaton with the given input string and returns an output sequence
|
18
|
+
# and a state after running.
|
19
|
+
def run(inputs)
|
20
|
+
state = @initial_state
|
21
|
+
outputs = []
|
22
|
+
inputs.each do |input|
|
23
|
+
output, state = step(state, input)
|
24
|
+
outputs << output
|
25
|
+
end
|
26
|
+
[outputs, state]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
# DFA is a deterministic finite-state automaton.
|
31
|
+
class DFA < Automaton
|
32
|
+
def initialize(initial_state, accept_states, transitions)
|
33
|
+
super()
|
34
|
+
|
35
|
+
@initial_state = initial_state
|
36
|
+
@accept_states = accept_states
|
37
|
+
@transitions = transitions
|
38
|
+
end
|
39
|
+
|
40
|
+
attr_reader :initial_state, :accept_states, :transitions
|
41
|
+
|
42
|
+
# Computes a transition for the given `input` from the current `state`.
|
43
|
+
def step(state, input)
|
44
|
+
next_state = @transitions[[state, input]]
|
45
|
+
output = @accept_states.include?(next_state)
|
46
|
+
[output, next_state]
|
47
|
+
end
|
48
|
+
|
49
|
+
# Checks equality.
|
50
|
+
def ==(other)
|
51
|
+
initial_state == other.initial_state && accept_states == other.accept_states && transitions == other.transitions
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Moore is a deterministic Moore machine.
|
56
|
+
class Moore < Automaton
|
57
|
+
def initialize(initial_state, outputs, transitions)
|
58
|
+
super()
|
59
|
+
|
60
|
+
@initial_state = initial_state
|
61
|
+
@outputs = outputs
|
62
|
+
@transitions = transitions
|
63
|
+
end
|
64
|
+
|
65
|
+
attr_reader :initial_state, :outputs, :transitions
|
66
|
+
|
67
|
+
# Computes a transition for the given `input` from the current `state`.
|
68
|
+
def step(state, input)
|
69
|
+
next_state = @transitions[[state, input]]
|
70
|
+
output = @outputs[next_state]
|
71
|
+
[output, next_state]
|
72
|
+
end
|
73
|
+
|
74
|
+
# Checks equality.
|
75
|
+
def ==(other)
|
76
|
+
initial_state == other.initial_state && outputs == other.outputs && transitions == other.transitions
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Mealy is a deterministic Mealy machine.
|
81
|
+
class Mealy < Automaton
|
82
|
+
def initialize(initial_state, transitions)
|
83
|
+
super()
|
84
|
+
|
85
|
+
@initial_state = initial_state
|
86
|
+
@transitions = transitions
|
87
|
+
end
|
88
|
+
|
89
|
+
attr_reader :initial_state, :transitions
|
90
|
+
|
91
|
+
# Computes a transition for the given `input` from the current `state`.
|
92
|
+
def step(state, input)
|
93
|
+
@transitions[[state, input]]
|
94
|
+
end
|
95
|
+
|
96
|
+
# Checks equality.
|
97
|
+
def ==(other)
|
98
|
+
initial_state == other.initial_state && transitions == other.transitions
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lernen
|
4
|
+
# CexProcessor is a collection of implementations of couterexample processing procesudres.
|
5
|
+
module CexProcessor
|
6
|
+
# Processes a given `cex`. It returns a new prefix and suffix to advance a learning.
|
7
|
+
def self.process(sul, hypothesis, cex, state_to_prefix, cex_processing: :binary)
|
8
|
+
case cex_processing
|
9
|
+
in :linear
|
10
|
+
process_linear(sul, hypothesis, cex, state_to_prefix)
|
11
|
+
in :binary
|
12
|
+
process_binary(sul, hypothesis, cex, state_to_prefix)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# Processes a given `cex` by linear search.
|
17
|
+
def self.process_linear(sul, hypothesis, cex, state_to_prefix)
|
18
|
+
expected_output = sul.query(cex).last
|
19
|
+
|
20
|
+
current_state = hypothesis.initial_state
|
21
|
+
cex.each_with_index do |a, i|
|
22
|
+
_, next_state = hypothesis.step(current_state, a)
|
23
|
+
|
24
|
+
prefix = state_to_prefix[next_state]
|
25
|
+
suffix = cex[i + 1...]
|
26
|
+
return state_to_prefix[current_state], a, suffix if expected_output != sul.query(prefix + suffix).last
|
27
|
+
|
28
|
+
current_state = next_state
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Processes a given `cex` by binary search.
|
33
|
+
#
|
34
|
+
# It is known as the Rivest-Schapire (RS) technique.
|
35
|
+
def self.process_binary(sul, hypothesis, cex, state_to_prefix)
|
36
|
+
expected_output = sul.query(cex).last
|
37
|
+
|
38
|
+
low = 0
|
39
|
+
high = cex.size
|
40
|
+
|
41
|
+
while high - low > 1
|
42
|
+
mid = (low + high) / 2
|
43
|
+
prefix = cex[0...mid]
|
44
|
+
suffix = cex[mid...]
|
45
|
+
|
46
|
+
_, prefix_state = hypothesis.run(prefix)
|
47
|
+
if expected_output == sul.query(state_to_prefix[prefix_state] + suffix).last
|
48
|
+
low = mid
|
49
|
+
else
|
50
|
+
high = mid
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
prefix = cex[0...low]
|
55
|
+
suffix = cex[high...]
|
56
|
+
|
57
|
+
_, prefix_state = hypothesis.run(prefix)
|
58
|
+
[state_to_prefix[prefix_state], cex[low], suffix]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,199 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Lernen
|
4
|
+
# ClassificationTree is a classification tree implementation.
|
5
|
+
class ClassificationTree
|
6
|
+
Node = Data.define(:suffix, :edges)
|
7
|
+
Leaf = Data.define(:prefix)
|
8
|
+
|
9
|
+
private_constant :Node, :Leaf
|
10
|
+
|
11
|
+
def initialize(alphabet, sul, cex:, automaton_type:, cex_processing:)
|
12
|
+
@alphabet = alphabet
|
13
|
+
@sul = sul
|
14
|
+
@automaton_type = automaton_type
|
15
|
+
@cex_processing = cex_processing
|
16
|
+
|
17
|
+
@paths = {}
|
18
|
+
|
19
|
+
case @automaton_type
|
20
|
+
in :dfa | :moore
|
21
|
+
@root = Node[[], {}]
|
22
|
+
|
23
|
+
empty_out = sul.query_empty
|
24
|
+
@root.edges[empty_out] = Leaf[[]]
|
25
|
+
@paths[[]] = [empty_out]
|
26
|
+
|
27
|
+
cex_out = sul.query(cex).last
|
28
|
+
@root.edges[cex_out] = Leaf[cex]
|
29
|
+
@paths[cex] = [cex_out]
|
30
|
+
in :mealy
|
31
|
+
suffix = [cex.last]
|
32
|
+
@root = Node[suffix, {}]
|
33
|
+
|
34
|
+
suffix_out = sul.query(suffix).last
|
35
|
+
@root.edges[suffix_out] = Leaf[[]]
|
36
|
+
@paths[[]] = [suffix_out]
|
37
|
+
|
38
|
+
cex_out = sul.query(cex).last
|
39
|
+
@root.edges[cex_out] = Leaf[cex]
|
40
|
+
@paths[cex] = [cex_out]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns a prefix classified by `word`.
|
45
|
+
def sift(word)
|
46
|
+
node = @root
|
47
|
+
path = []
|
48
|
+
|
49
|
+
until node.is_a?(Leaf)
|
50
|
+
inputs = word + node.suffix
|
51
|
+
out = @sul.query(inputs).last
|
52
|
+
path << out
|
53
|
+
|
54
|
+
unless node.edges.include?(out)
|
55
|
+
node.edges[out] = Leaf[word]
|
56
|
+
@paths[word] = path
|
57
|
+
end
|
58
|
+
|
59
|
+
node = node.edges[out]
|
60
|
+
end
|
61
|
+
|
62
|
+
node.prefix
|
63
|
+
end
|
64
|
+
|
65
|
+
# Constructs a hypothesis automaton from this classification tree.
|
66
|
+
def to_hypothesis
|
67
|
+
transitions = {}
|
68
|
+
|
69
|
+
queue = []
|
70
|
+
prefix_to_state = {}
|
71
|
+
|
72
|
+
queue << []
|
73
|
+
prefix_to_state[[]] = prefix_to_state.size
|
74
|
+
|
75
|
+
until queue.empty?
|
76
|
+
prefix = queue.shift
|
77
|
+
state = prefix_to_state[prefix]
|
78
|
+
@alphabet.each do |input|
|
79
|
+
word = prefix + [input]
|
80
|
+
next_prefix = sift(word)
|
81
|
+
|
82
|
+
unless prefix_to_state.include?(next_prefix)
|
83
|
+
prefix_to_state[next_prefix] = prefix_to_state.size
|
84
|
+
queue << next_prefix
|
85
|
+
end
|
86
|
+
|
87
|
+
next_state = prefix_to_state[next_prefix]
|
88
|
+
case @automaton_type
|
89
|
+
in :dfa | :moore
|
90
|
+
transitions[[state, input]] = next_state
|
91
|
+
in :mealy
|
92
|
+
output = @sul.query(word).last
|
93
|
+
transitions[[state, input]] = [output, next_state]
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
state_to_prefix = prefix_to_state.to_h { |q, i| [i, q] }
|
99
|
+
automaton =
|
100
|
+
case @automaton_type
|
101
|
+
in :dfa
|
102
|
+
accept_states = state_to_prefix.to_a.filter { |(_, q)| @paths[q][0] }.to_set { |(i, _)| i }
|
103
|
+
DFA.new(0, accept_states, transitions)
|
104
|
+
in :moore
|
105
|
+
outputs = state_to_prefix.transform_values { |q| @paths[q][0] }
|
106
|
+
Moore.new(0, outputs, transitions)
|
107
|
+
in :mealy
|
108
|
+
Mealy.new(0, transitions)
|
109
|
+
end
|
110
|
+
|
111
|
+
[automaton, state_to_prefix]
|
112
|
+
end
|
113
|
+
|
114
|
+
# Update this classification tree by the given `cex`.
|
115
|
+
def process_cex(hypothesis, cex, state_to_prefix)
|
116
|
+
old_prefix, new_input, new_suffix =
|
117
|
+
CexProcessor.process(@sul, hypothesis, cex, state_to_prefix, cex_processing: @cex_processing)
|
118
|
+
|
119
|
+
_, old_prefix_state = hypothesis.run(old_prefix)
|
120
|
+
new_prefix = state_to_prefix[old_prefix_state] + [new_input]
|
121
|
+
new_prefix_out = @sul.query(new_prefix + new_suffix).last
|
122
|
+
|
123
|
+
_, old_node_state = hypothesis.run(old_prefix + [new_input])
|
124
|
+
old_node_prefix = state_to_prefix[old_node_state]
|
125
|
+
old_node_out = @sul.query(old_node_prefix + new_suffix).last
|
126
|
+
|
127
|
+
old_node_path = @paths[old_node_prefix]
|
128
|
+
parent = @root
|
129
|
+
old_node = @root.edges[old_node_path.first]
|
130
|
+
old_node_path[1..].each do |out|
|
131
|
+
parent = old_node
|
132
|
+
old_node = old_node.edges[out]
|
133
|
+
end
|
134
|
+
|
135
|
+
new_node = Node[new_suffix, {}]
|
136
|
+
parent.edges[old_node_path.last] = new_node
|
137
|
+
|
138
|
+
new_node.edges[new_prefix_out] = Leaf[new_prefix]
|
139
|
+
@paths[new_prefix] = old_node_path + [new_prefix_out]
|
140
|
+
|
141
|
+
new_node.edges[old_node_out] = Leaf[old_node_prefix]
|
142
|
+
@paths[old_node_prefix] = old_node_path + [old_node_out]
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# KearnsVazirani is an implementation of the Kearns-Vazirani automata learning algorithm.
|
147
|
+
module KearnsVazirani
|
148
|
+
# Runs the Kearns-Vazirani algoritghm and returns an inferred automaton.
|
149
|
+
def self.learn(alphabet, sul, oracle, automaton_type:, cex_processing: :binary, max_learning_rounds: nil)
|
150
|
+
hypothesis = construct_first_hypothesis(alphabet, sul, automaton_type)
|
151
|
+
cex = oracle.find_cex(hypothesis)
|
152
|
+
return hypothesis if cex.nil?
|
153
|
+
|
154
|
+
classification_tree = ClassificationTree.new(alphabet, sul, cex:, automaton_type:, cex_processing:)
|
155
|
+
learning_rounds = 0
|
156
|
+
|
157
|
+
loop do
|
158
|
+
break if max_learning_rounds && learning_rounds == max_learning_rounds
|
159
|
+
learning_rounds += 1
|
160
|
+
|
161
|
+
hypothesis, state_to_prefix = classification_tree.to_hypothesis
|
162
|
+
cex = oracle.find_cex(hypothesis)
|
163
|
+
break if cex.nil?
|
164
|
+
|
165
|
+
classification_tree.process_cex(hypothesis, cex, state_to_prefix)
|
166
|
+
end
|
167
|
+
|
168
|
+
hypothesis, = classification_tree.to_hypothesis
|
169
|
+
hypothesis
|
170
|
+
end
|
171
|
+
|
172
|
+
# Constructs the first hypothesis automaton.
|
173
|
+
def self.construct_first_hypothesis(alphabet, sul, automaton_type)
|
174
|
+
transitions = {}
|
175
|
+
alphabet.each do |a|
|
176
|
+
case automaton_type
|
177
|
+
in :dfa | :moore
|
178
|
+
transitions[[0, a]] = 0
|
179
|
+
in :mealy
|
180
|
+
out = sul.query([a]).last
|
181
|
+
transitions[[0, a]] = [out, 0]
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
case automaton_type
|
186
|
+
in :dfa
|
187
|
+
accept_states = sul.query_empty ? Set[0] : Set.new
|
188
|
+
DFA.new(0, accept_states, transitions)
|
189
|
+
in :moore
|
190
|
+
outputs = { 0 => sul.query_empty }
|
191
|
+
Moore.new(0, outputs, transitions)
|
192
|
+
in :mealy
|
193
|
+
Mealy.new(0, transitions)
|
194
|
+
end
|
195
|
+
end
|
196
|
+
|
197
|
+
private_class_method :construct_first_hypothesis
|
198
|
+
end
|
199
|
+
end
|