Class: Stamina::Abbadingo::RandomDFA

Inherits:
Object
  • Object
show all
Defined in:
lib/stamina-induction/stamina/abbadingo/random_dfa.rb

Overview

Generates a random DFA using the Abbadingo protocol.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :minimize => :hopcroft
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.execute(*args) ⇒ Object



49
50
51
# File 'lib/stamina-induction/stamina/abbadingo/random_dfa.rb', line 49

def self.execute(*args)
  new.execute(*args)
end

Instance Method Details

#execute(state_count = 64, accepting_ratio = 0.5, options = {}) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stamina-induction/stamina/abbadingo/random_dfa.rb', line 12

def execute(state_count = 64,
            accepting_ratio = 0.5,
            options = {})
  options = DEFAULT_OPTIONS.merge(options)

  # Built dfa
  dfa = Automaton.new

  # Generate 5/4*state_count states
  (state_count.to_f * 5.0 / 4.0).to_i.times do
    dfa.add_state(:initial   => false,
                  :accepting => (Kernel.rand <= accepting_ratio),
                  :error     => false)
  end

  # Generate all edges
  dfa.each_state do |source|
    ["0", "1"].each do |symbol|
      target = dfa.ith_state(Kernel.rand(dfa.state_count))
      dfa.connect(source, target, symbol)
    end
  end

  # Choose an initial state
  dfa.ith_state(Kernel.rand(dfa.state_count)).initial!

  # Minimize the automaton and return it
  case options[:minimize]
    when :hopcroft
      Stamina::Automaton::Minimize::Hopcroft.execute(dfa)
    when :pitchies
      Stamina::Automaton::Minimize::Pitchies.execute(dfa)
    else
      dfa
  end
end