Class: Lernen::Algorithm::KearnsVazirani::DiscriminationTree
- Inherits:
-
Object
- Object
- Lernen::Algorithm::KearnsVazirani::DiscriminationTree
- Defined in:
- lib/lernen/algorithm/kearns_vazirani/discrimination_tree.rb
Overview
DiscriminationTree is an implementation of discrimination tree data structure.
This data structure is used for Kearns-Vazirani algorithm.
Constant Summary collapse
- Node =
Data.define(:suffix, :branch)
- Leaf =
Data.define(:prefix)
Instance Method Summary collapse
-
#build_hypothesis ⇒ Object
Constructs a hypothesis automaton from this discrimination tree.
- #initialize(alphabet, sul, cex:, automaton_type:, cex_processing:) ⇒ DiscriminationTree constructor
-
#refine_hypothesis(cex, hypothesis, state_to_prefix) ⇒ Object
Update this classification tree by the given ‘cex`.
-
#sift(word) ⇒ Object
Returns a prefix discriminated by ‘word`.
Constructor Details
#initialize(alphabet, sul, cex:, automaton_type:, cex_processing:) ⇒ DiscriminationTree
: (
Array[In] alphabet,
System::SUL[In, Out] sul,
cex: Array[In],
automaton_type: :dfa | :mealy | :moore,
cex_processing: cex_processing_method
) -> void
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/lernen/algorithm/kearns_vazirani/discrimination_tree.rb', line 50 def initialize(alphabet, sul, cex:, automaton_type:, cex_processing:) @alphabet = alphabet @sul = sul @automaton_type = automaton_type @cex_processing = cex_processing @path_hash = {} case @automaton_type in :dfa | :moore @root = Node[[], {}] empty_out = sul.query_empty @root.branch[empty_out] = Leaf[[]] @path_hash[[]] = [empty_out] cex_out = sul.query_last(cex) @root.branch[cex_out] = Leaf[cex] @path_hash[cex] = [cex_out] in :mealy prefix = cex[0...-1] suffix = [cex.last] @root = Node[suffix, {}] suffix_out = sul.query_last(suffix) @root.branch[suffix_out] = Leaf[[]] @path_hash[[]] = [suffix_out] cex_out = sul.query_last(cex) @root.branch[cex_out] = Leaf[prefix] @path_hash[prefix] = [cex_out] end end |
Instance Method Details
#build_hypothesis ⇒ Object
Constructs a hypothesis automaton from this discrimination tree.
: () -> [Automaton::TransitionSystem[Integer, In, Out], Hash[Integer, Array]]
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/lernen/algorithm/kearns_vazirani/discrimination_tree.rb', line 111 def build_hypothesis transition_function = {} queue = [] prefix_to_state = {} state_to_prefix = {} queue << [] prefix_to_state[[]] = prefix_to_state.size state_to_prefix[state_to_prefix.size] = [] until queue.empty? prefix = queue.shift state = prefix_to_state[prefix] @alphabet.each do |input| word = prefix + [input] next_prefix = sift(word) unless prefix_to_state.include?(next_prefix) queue << next_prefix prefix_to_state[next_prefix] = prefix_to_state.size state_to_prefix[state_to_prefix.size] = next_prefix end next_state = prefix_to_state[next_prefix] case @automaton_type in :dfa | :moore transition_function[[state, input]] = next_state in :mealy output = @sul.query_last(word) transition_function[[state, input]] = [output, next_state] end end end automaton = case @automaton_type in :dfa accept_states = state_to_prefix.to_a.filter { |(_, prefix)| @path_hash[prefix][0] }.to_set { |(state, _)| state } Automaton::DFA.new(0, accept_states, transition_function) in :moore outputs = state_to_prefix.transform_values { |prefix| @path_hash[prefix][0] } Automaton::Moore.new(0, outputs, transition_function) in :mealy Automaton::Mealy.new(0, transition_function) end [automaton, state_to_prefix] end |
#refine_hypothesis(cex, hypothesis, state_to_prefix) ⇒ Object
Update this classification tree by the given ‘cex`.
: (
Array[In] cex,
Automaton::TransitionSystem[Integer, In, Out] hypothesis,
Hash[Integer, Array[In]] state_to_prefix
) -> void
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/lernen/algorithm/kearns_vazirani/discrimination_tree.rb', line 169 def refine_hypothesis(cex, hypothesis, state_to_prefix) state_to_prefix_lambda = ->(state) { state_to_prefix[state] } acex = CexProcessor::PrefixTransformerAcex.new(cex, @sul, hypothesis, state_to_prefix_lambda) n = CexProcessor.process(acex, cex_processing: @cex_processing) old_prefix = cex[0...n] new_input = cex[n] new_suffix = cex[n + 1...] _, old_state = hypothesis.run(old_prefix) # steep:ignore _, replace_state = hypothesis.step(old_state, new_input) new_prefix = state_to_prefix[old_state] + [new_input] new_out = @sul.query_last(new_prefix + new_suffix) # steep:ignore replace_prefix = state_to_prefix[replace_state] replace_out = @sul.query_last(replace_prefix + new_suffix) # steep:ignore replace_node_path = @path_hash[replace_prefix] replace_node_parent = @root replace_node = @root.branch[replace_node_path.first] # steep:ignore replace_node_path[1..].each do |out| # steep:ignore replace_node_parent = replace_node replace_node = replace_node.branch[out] # steep:ignore end new_node = Node[new_suffix, {}] # steep:ignore replace_node_parent.branch[replace_node_path.last] = new_node # steep:ignore new_node.branch[new_out] = Leaf[new_prefix] @path_hash[new_prefix] = replace_node_path + [new_out] # steep:ignore new_node.branch[replace_out] = Leaf[replace_prefix] @path_hash[replace_prefix] = replace_node_path + [replace_out] # steep:ignore end |
#sift(word) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lernen/algorithm/kearns_vazirani/discrimination_tree.rb', line 87 def sift(word) node = @root path = [] until node.is_a?(Leaf) full_word = word + node.suffix out = @sul.query_last(full_word) path << out unless node.branch.include?(out) node.branch[out] = Leaf[word] @path_hash[word] = path end node = node.branch[out] # steep:ignore end node.prefix # steep:ignore end |