Class: Zgomot::Comp::Markov
Instance Attribute Summary collapse
-
#current_state ⇒ Object
readonly
Returns the value of attribute current_state.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
Class Method Summary collapse
Instance Method Summary collapse
- #add(trans, &blk) ⇒ Object
- #init(state, args = {}) ⇒ Object
-
#initialize ⇒ Markov
constructor
A new instance of Markov.
- #next(args = {}) ⇒ Object
Constructor Details
#initialize ⇒ Markov
Returns a new instance of Markov.
13 14 15 |
# File 'lib/zgomot/comp/markov.rb', line 13 def initialize @current_state, @states = 0, [] end |
Instance Attribute Details
#current_state ⇒ Object (readonly)
Returns the value of attribute current_state.
11 12 13 |
# File 'lib/zgomot/comp/markov.rb', line 11 def current_state @current_state end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
11 12 13 |
# File 'lib/zgomot/comp/markov.rb', line 11 def states @states end |
Class Method Details
.mark ⇒ Object
6 7 8 |
# File 'lib/zgomot/comp/markov.rb', line 6 def mark new end |
Instance Method Details
#add(trans, &blk) ⇒ Object
17 18 19 |
# File 'lib/zgomot/comp/markov.rb', line 17 def add(trans, &blk) @states << {:trans=>sum_trans(trans), :blk => blk} end |
#init(state, args = {}) ⇒ Object
21 22 23 24 |
# File 'lib/zgomot/comp/markov.rb', line 21 def init(state, args={}) @current_state = state states[@current_state][:blk].call(args) end |
#next(args = {}) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/zgomot/comp/markov.rb', line 26 def next(args={}) r, state = rand, states[@current_state] @current_state = state[:trans].select{|t| r >= t}.count Zgomot.logger.info "CURRENT MARKOV STATE: #{current_state}" blk = states[@current_state][:blk] blk.arity > 0 ? blk.call(args) : blk.call end |