From 8ca26c606a1b9cf650f306243369a602f1711ac0 Mon Sep 17 00:00:00 2001 From: Eugene Petkevich Date: Mon, 15 Aug 2022 18:20:34 +0300 Subject: [PATCH] Add markov chain class --- extensions/classes/markov.sc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 extensions/classes/markov.sc 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 -- 2.17.1