Module: Stamina::Automaton::Metrics

Included in:
Stamina::Automaton
Defined in:
lib/stamina-core/stamina/automaton/metrics.rb

Overview

Provides useful metric methods on automata.

This module is automatically included by Automaton and is not intended to be used directly.

Instance Method Summary collapse

Instance Method Details

#accepting_ratioObject

Number of accepting states over all states



31
32
33
# File 'lib/stamina-core/stamina/automaton/metrics.rb', line 31

def accepting_ratio
  states.select{|s|s.accepting?}.size.to_f/state_count.to_f
end

#alphabet_sizeObject

Returns the number of letters of the alphabet.



14
15
16
# File 'lib/stamina-core/stamina/automaton/metrics.rb', line 14

def alphabet_size
  alphabet.size
end

#avg_degreeObject Also known as: avg_out_degree, avg_in_degree

Returns the average degree of states, that is, edge_count/state_count



22
23
24
# File 'lib/stamina-core/stamina/automaton/metrics.rb', line 22

def avg_degree
  edge_count.to_f/state_count.to_f
end

#depth(key = :depth) ⇒ Object

Computes the depth of the automaton.

The depth of an automaton is defined as the length of the longest shortest path from the initial state to a state.

This method has a side effect on state marks, as it keeps the depth of each state as a mark under key, which defaults to :depth.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/stamina-core/stamina/automaton/metrics.rb', line 51

def depth(key = :depth)
  algo = Stamina::Utils::Decorate.new
  algo.set_suppremum do |d0,d1|
    # Unreached state is MAX (i.e. nil is +INF); we look at the min depth for each state
    (d0.nil? or d1.nil?) ? (d0 || d1) : (d0 <= d1 ? d0 : d1)
  end
  algo.set_propagate{|d,e| d+1 }
  algo.set_initiator{|s| s.initial? ? 0 : nil }
  algo.set_start_predicate{|s| s.initial? }
  algo.call(self, key)
  deepest = states.max do |s0,s1|
    # do not take unreachable states into account: -1 is taken if nil is encountered
    (s0[:depth] || -1) <=> (s1[:depth] || -1)
  end
  deepest[:depth]
end

#error_ratioObject

Number of error states over all states



38
39
40
# File 'lib/stamina-core/stamina/automaton/metrics.rb', line 38

def error_ratio
  states.select{|s|s.error?}.size.to_f/state_count.to_f
end