From: Eugene Petkevich Date: Mon, 15 Aug 2022 15:20:34 +0000 (+0300) Subject: Add markov chain class X-Git-Url: https://git.zuelum.org/?a=commitdiff_plain;h=8ca26c606a1b9cf650f306243369a602f1711ac0;p=scprojects.git Add markov chain class --- diff --git a/extensions/classes/markov.sc b/extensions/classes/markov.sc new file mode 100644 index 0000000..677b003 --- /dev/null +++ b/extensions/classes/markov.sc @@ -0,0 +1,24 @@ +MarkovModel { + var states, <>curIndex, <>weights; + + *new {|states, startIndex, weights=nil| + ^super.newCopyArgs(states.size, states, startIndex, weights).init; + } + + init { + if (weights.isNil) { + weights = Array2D(numStates, numStates); + numStates.do{|i| + numStates.do{|j| + weights[i, j] = 5.rand; + }; + }; + }; + } + + next { + var prevIndex = curIndex; + curIndex = weights.rowAt(prevIndex).normalizeSum.windex; + ^states[prevIndex]; + } +} \ No newline at end of file