total-serialism icon indicating copy to clipboard operation
total-serialism copied to clipboard

Markov Chain

Open tmhglnd opened this issue 4 years ago • 3 comments

Add a markov-chain set of methods to the library. Probably easiest in the form of a class that can learn and generate through seperate methods.

Some ideas:

const Algo = require('total-serialism').Algorithmic;
let myMarkov = Algo.markov();
// => make an instance of a markov-chain

myMarkov.train([0, 1, 1, 2, 1, 3, 2, 3, 1, 3, 0]);
// => train the Markov Chain with an array of values

myMarkov.next();
// => generates the next value

let values = myMarkov.generate(12);
// => generates 12 values as an array

tmhglnd avatar May 20 '20 20:05 tmhglnd

Currently It appears that the Markov chain moves to a random state if it reaches a condition where it becomes stuck and cannot progress to a new state:

https://github.com/tmhglnd/total-serialism/blob/b1557fd1c7951579c6ad40f0026c29562ac621b2/src/gen-stochastic.js#L291

I think it might be useful to flag this event and / or provide a method for choosing the chain's next step. (For example, move to an arbitrary state, mutate the transition table in some way, etc.). I think the easiest way to do this would be to make state an object with a key devoted to flagging dead ends in the chain.

So for example we could change this:

https://github.com/tmhglnd/total-serialism/blob/b1557fd1c7951579c6ad40f0026c29562ac621b2/src/gen-stochastic.js#L246

to this:

this._state = {currentState: null, stuck: false}

PleatherStarfish avatar May 21 '20 21:05 PleatherStarfish

Thanks for the suggestion! I'll have a look

tmhglnd avatar May 22 '20 08:05 tmhglnd

It's been a while since I've worked on this. At the moment I'm a bit uncertain on how to proceed. On the one hand I think it would be good for advanced users to have the flexibility to decide what to do when the chain is stuck (maybe in the form of a callback function that can be written as argument in the next() function for example?). If I would work with this object and stuck-state I think this kind of breaks the usability for beginning users that might not have so many needs. Any ideas?

tmhglnd avatar Nov 10 '20 18:11 tmhglnd