Class: FiniteMDP::ArrayModel::StateActionMap

Inherits:
Object
  • Object
show all
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

OrderedStateActionMap

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#mapObject (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.

Parameters:

  • model (Model)
  • ordered (Boolean) (defaults to: nil)

    assume states are orderable; default is to inspect the first state


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

#statesObject


32
33
34
# File 'lib/finite_mdp/array_model.rb', line 32

def states
  @map.map { |state, _actions| state }
end