Class: FiniteMDP::ArrayModel::StateActionMap
- Inherits:
-
Object
- Object
- FiniteMDP::ArrayModel::StateActionMap
- Defined in:
- lib/finite_mdp/array_model.rb
Overview
Map between states and actions and their corresponding indexes. This is used with an FiniteMDP::ArrayModel, which works only with the indexes internally.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#map ⇒ Object
readonly
Returns the value of attribute map.
Class Method Summary collapse
-
.from_model(model, ordered = nil) ⇒ Object
Build from a model.
Instance Method Summary collapse
- #actions(state) ⇒ Object
- #add(state, actions) ⇒ Object
-
#initialize(map = []) ⇒ StateActionMap
constructor
A new instance of StateActionMap.
- #state(index) ⇒ Object
- #state_action_index(state, action) ⇒ Object
- #state_index(state) ⇒ Object
- #states ⇒ Object
Constructor Details
#initialize(map = []) ⇒ StateActionMap
Returns a new instance of StateActionMap.
22 23 24 |
# File 'lib/finite_mdp/array_model.rb', line 22 def initialize(map = []) @map = map end |
Instance Attribute Details
#map ⇒ Object (readonly)
Returns the value of attribute map.
26 27 28 |
# File 'lib/finite_mdp/array_model.rb', line 26 def map @map end |
Class Method Details
.from_model(model, ordered = nil) ⇒ Object
Build from a model.
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/finite_mdp/array_model.rb', line 62 def self.from_model(model, ordered = nil) model_states = model.states ordered = model_states.first.respond_to?(:>=) if ordered.nil? map = ordered ? OrderedStateActionMap.new : StateActionMap.new model_states.each do |state| map.add(state, model.actions(state)) end map end |
Instance Method Details
#actions(state) ⇒ Object
36 37 38 39 |
# File 'lib/finite_mdp/array_model.rb', line 36 def actions(state) _state, actions = @map[state_index(state)] actions end |
#add(state, actions) ⇒ Object
28 29 30 |
# File 'lib/finite_mdp/array_model.rb', line 28 def add(state, actions) @map << [state, actions] end |
#state(index) ⇒ Object
46 47 48 |
# File 'lib/finite_mdp/array_model.rb', line 46 def state(index) @map[index][0] end |
#state_action_index(state, action) ⇒ Object
41 42 43 44 |
# File 'lib/finite_mdp/array_model.rb', line 41 def state_action_index(state, action) index = state_index(state) [index, @map[index][1].index(action)] end |
#state_index(state) ⇒ Object
50 51 52 |
# File 'lib/finite_mdp/array_model.rb', line 50 def state_index(state) @map.index { |test_state, _actions| test_state == state } end |
#states ⇒ Object
32 33 34 |
# File 'lib/finite_mdp/array_model.rb', line 32 def states @map.map { |state, _actions| state } end |