5.2.4. MarkovChooser

A common technique Skaion uses when making choices about what to do next is to use a Markov transition matrix, where the chance of any particular next state is based on the current state. This file provides a class implementing ChooserBase using such Markove matrices.

class MarkovChooser.MarkovChooser(dist='uniform', args=(), seed=None)

This implements ChooserBase to make choices based on the current state. This assumes that it is a 1st order matrix.

Parameters:

  • dist: the name of the distrbution to use

  • args: a tuple of any arguments the distribution needs during initialization

  • seed: the seed to give to the random number generator

get_all_choices()

Return a set of all possible choices in this matrix.

get_keys()

Return: a list of all the keys in the transition matrix

get_values()

Return a set of all values that could be states in the matrix

is_state(state)

Parameters:

  • state: a potential state in the matrix

Return: True iff state is an actual state, False otherwise

load_from_db(cursor, query, func=None)

Load the values based on the results of a DB query rather than reading from a file.

Parameters:

  • cursor: a database object conforming to the python DB-API

  • query: an SQL query that will return th eresults as a Markov matrix

  • func: optionally a value that will transform state names taking the state name as an argument and returning the transformed name

load_from_file(path, func=None)

Load our transition matrices from the file, including the special “init” state which is used when there is no known current state, such as when the first choice is made or when a state has no valid next states.

Parameters:

  • path: the path to the file to load

  • func: optionally a function to that will take the name of the current state as an argument

load_string(line, func=None)

Load a state from a string. The string should look like a line from a markov file.

Parameters:

  • line: the string to load

  • func: optionally a function transforming state names

next_choice()

Return: the next choice

num_choices()

Return: the number of potential states in the transition matrix

reset()

Reset the markov chain to its initial state

set_state(state)

Update the current state. This is to support external modification of the state changes, as someone may wish to make a particular choice at some point in time and then continue from there.

This call will reset back to the initial state if the given state is not valid for this Chooser.

Parameters:

  • state: the new current state

MarkovChooser.test(iters)

Repeat the test some number of times and make sure it each method of loading produces expected results at some scale.

Parameters:

  • iters: the int number of iterations to repeat the test