Class: TransitionGraph
- Inherits:
-
Object
- Object
- TransitionGraph
- Defined in:
- lib/repositories/transition_graph.rb
Overview
This is generic state machine, useful for Monte Carlo methods. TransitionCache objects can crawl the state machine and optimize reward policies.
Examples:
TransitionGraph.new do |tg|
tg.add_transition(:some_transition, :high, :low, 0.7, 20)
tg.add_transition(:another_transition, :low, :high, 0.6, 40)
end
TransitionGraph.new([:some_transition, :high, :low, 0.7, 20], [:another_transition, :low, :high, 0.6, 40])
TransitionGraph.new(
{:name => :some_transition, :from_state => :high, :to_state => :low, :probability => 0.7, :reward => 20},
{:name => :another_transition, :from_state => :low, :to_state => :high, :probability => 0.6, :reward => 40}
)
Instance Attribute Summary collapse
-
#current_state ⇒ Object
readonly
Returns the value of attribute current_state.
Instance Method Summary collapse
-
#add_state(state) ⇒ Object
Adds a state, though orphaned (no transitions to or from the state).
-
#assert_transition(transition) ⇒ Object
(also: #add_transition, #add)
Adds a transition to the transition list.
-
#available_transitions_from(state) ⇒ Object
(also: #available, #available_from, #available_from_state, #from)
If there are any transitions available from this state, they are returned as an array of structs.
-
#find_all_transitions(transition) ⇒ Object
(also: #find_all)
Returns an array of found transitions, using any keys in the Transition struct.
-
#find_by_name(name) ⇒ Object
Returns only a single transition.
-
#find_transition(transition) ⇒ Object
(also: #find)
Finds the first matching transition.
-
#initialize(*transitions, &block) ⇒ TransitionGraph
constructor
A very robust constructor.
-
#remove_state(state) ⇒ Object
Removes a state and any transitions on the state.
-
#remove_transition(transition) ⇒ Object
(also: #remove)
Removes a transition.
-
#states(refresh = false) ⇒ Object
A list of all states available in the graph.
-
#transitions ⇒ Object
All transitions in the graph.
-
#transitions_to(state) ⇒ Object
(also: #to)
All transitions that lead to a given state.
Constructor Details
#initialize(*transitions, &block) ⇒ TransitionGraph
A very robust constructor. Takes arrays, hashes, a block, transitions, or a mix of the above.
47 48 49 50 51 |
# File 'lib/repositories/transition_graph.rb', line 47 def initialize(*transitions, &block) assert_transitions(transitions) self.instance_eval(&block) if block assert_states end |
Instance Attribute Details
#current_state ⇒ Object (readonly)
Returns the value of attribute current_state.
43 44 45 |
# File 'lib/repositories/transition_graph.rb', line 43 def current_state @current_state end |
Instance Method Details
#add_state(state) ⇒ Object
Adds a state, though orphaned (no transitions to or from the state)
132 133 134 135 |
# File 'lib/repositories/transition_graph.rb', line 132 def add_state(state) inferred_state = infer_state(state) self.states.upush! inferred_state if inferred_state end |
#assert_transition(transition) ⇒ Object Also known as: add_transition, add
Adds a transition to the transition list. It
75 76 77 78 79 80 |
# File 'lib/repositories/transition_graph.rb', line 75 def assert_transition(transition) self.transitions # Ensure we have an array to work with inferred_transiton = infer_transition(transition) self.transitions.upush! inferred_transiton if inferred_transiton inferred_transiton end |
#available_transitions_from(state) ⇒ Object Also known as: available, available_from, available_from_state, from
If there are any transitions available from this state, they are returned as an array of structs.
60 61 62 |
# File 'lib/repositories/transition_graph.rb', line 60 def available_transitions_from(state) find_all_transitions(:from_state => state) end |
#find_all_transitions(transition) ⇒ Object Also known as: find_all
Returns an array of found transitions, using any keys in the Transition struct.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/repositories/transition_graph.rb', line 93 def find_all_transitions(transition) transition = infer_transition(transition) return [] unless transition pattern = {} transition.each_pair do |k, v| pattern[k.to_sym] = v if v end return [] if pattern.empty? self.transitions.find_all do |t| (pattern[:name] ? t.name == pattern[:name] : true) and (pattern[:from_state] ? t.from_state == pattern[:from_state] : true) and (pattern[:to_state] ? t.to_state == pattern[:to_state] : true) and (pattern[:probability] ? t.probability == pattern[:probability] : true) and (pattern[:reward] ? t.reward == pattern[:reward] : true) end end |
#find_by_name(name) ⇒ Object
Returns only a single transition
114 115 116 |
# File 'lib/repositories/transition_graph.rb', line 114 def find_by_name(name) self.transitions.find {|t| t.name == name} end |
#find_transition(transition) ⇒ Object Also known as: find
Finds the first matching transition
119 120 121 |
# File 'lib/repositories/transition_graph.rb', line 119 def find_transition(transition) find_all_transitions(transition).first end |
#remove_state(state) ⇒ Object
Removes a state and any transitions on the state
138 139 140 141 142 |
# File 'lib/repositories/transition_graph.rb', line 138 def remove_state(state) self.from(state).each { |transition| remove(transition) } self.to(state).each { |transition| remove(transition) } self.states.delete(state) end |
#remove_transition(transition) ⇒ Object Also known as: remove
Removes a transition
86 87 88 89 |
# File 'lib/repositories/transition_graph.rb', line 86 def remove_transition(transition) transition = find_transition(transition) self.transitions.delete(transition) end |
#states(refresh = false) ⇒ Object
A list of all states available in the graph.
125 126 127 128 129 |
# File 'lib/repositories/transition_graph.rb', line 125 def states(refresh=false) @states ||= [] assert_states if refresh @states end |
#transitions ⇒ Object
All transitions in the graph.
54 55 56 |
# File 'lib/repositories/transition_graph.rb', line 54 def transitions @transitions ||= [] end |
#transitions_to(state) ⇒ Object Also known as: to
All transitions that lead to a given state
69 70 71 |
# File 'lib/repositories/transition_graph.rb', line 69 def transitions_to(state) find_all_transitions(:to_state => state) end |