public class Markov extends AlgorithmLearn
AiHeuristicConst
.BREADTH
and
DEPTH
, to return a tree structure that retrieves the n most probable next states
and repeats this d times, where each next state is determined only by the previous one.
Note that this should be passed as part of the dataset, not as part of the initialisation.
Note that the model is stored as a Tree
and so you should be able to save it to
a file and then read it in again. The graph is simply a count of the frequency of states that
follow other states.
Some help with ideas from http://herselfsai.com/2007/02/hidden-markov-models.html.
Modifier and Type | Field and Description |
---|---|
protected int |
breadth
Breadth to search to
|
protected int |
depth
Depth to search to
|
protected Tree |
markovModel
The Markov model
|
evalMetric
config, mathCompare, valueType
Constructor and Description |
---|
Markov()
Create a new instance of Markov.
|
Markov(java.lang.String thisValueType)
Create a new instance of Markov.
|
Markov(java.lang.String thisValueType,
FunctionLearn thisEvalMetric)
Create a new instance of Markov.
|
Modifier and Type | Method and Description |
---|---|
ReplySet |
evaluate(MetricDataset dataset)
Evaluate the dataset and return the result.
|
<T> ReplySet |
evaluate(T dataset)
Return a value based on the function evaluation.
|
Tree |
getModel()
Get the markov model that is learned.
|
protected java.util.ArrayList |
getNextStates(TreeNode thisNode,
int thisDepth)
Evaluate the dataset and return the result.
|
protected void |
initialise()
Initialise the function values, setting the config parameters or other.
|
void |
setModel(Tree theMarkovModel)
Set the model to an already existing one.
|
ReplySet |
train(MetricDataset dataset)
Train the function based on the supervised training method, using the dataset as input.
|
void |
trainText(java.lang.String thisDocument)
Train the function based on the contents of the text document.
|
checkValueType, createFunction, createFunction, createFunction, getConfigParams, innerObject, isLegalNumber, setConfigParams, setEvaluator, setValueType
protected int breadth
protected int depth
protected Tree markovModel
public Markov() throws java.lang.Exception
evalMetric
is set to
the default 'count of number of occurrences' FunctionCount
and the
mathematical comparisons is set to the default SimpleMathCompare
one.java.lang.Exception
- any error.public Markov(java.lang.String thisValueType) throws java.lang.Exception
FunctionCount
.thisValueType
- the type of object being evaluated. Can be null if set later or not used.java.lang.Exception
- any error.public Markov(java.lang.String thisValueType, FunctionLearn thisEvalMetric) throws java.lang.Exception
thisValueType
- the type of object being evaluated. Can be null if set later or not used.thisEvalMetric
- the learning update function.java.lang.Exception
- any error.protected void initialise() throws java.lang.Exception
initialise
in class Function
java.lang.Exception
- any error.public ReplySet train(MetricDataset dataset) throws java.lang.Exception
train
in class AlgorithmLearn
dataset
- the dataset, or any ArrayList
list of dataset, etc. These can
be in the form of String-based lists (text documents) In that case the document gets
parsed into individual words or terms. Alternatively, a Tree
, or ArrayList of Trees
can be submitted and the links between the graph nodes used instead.((Boolean)MetricValue.getValue()).booleanValue()
to retrieve the value.java.lang.Exception
- any error.public void trainText(java.lang.String thisDocument) throws java.lang.Exception
thisDocument
- the next document to train with. This is tokenized first.java.lang.Exception
- any error.public <T> ReplySet evaluate(T dataset) throws java.lang.Exception
dataset
- the value to pass through the function. This can be, for example,
a single String
value representing a single state, or a MetricDataset
.
If the MetricDataset is used, it should contain a single MetricValue
, where
only the value
(objValue) is used. You can then add additional parameters,
such as breadth or depth, to return a TreeNode
structure that includes more than
one linked state.TreeNode
representing a section of a state-graph.
The node value is the next state and the count is the evaluation value associated with it.java.lang.Exception
- any error.public ReplySet evaluate(MetricDataset dataset) throws java.lang.Exception
evaluate
in interface FunctionDef
evaluate
in class Function
dataset
- the dataset to evaluate.TreeNode
.java.lang.Exception
- any error.protected java.util.ArrayList getNextStates(TreeNode thisNode, int thisDepth) throws java.lang.Exception
thisNode
- the node to search from.thisDepth
- the current search depth.java.lang.Exception
- any error.public void setModel(Tree theMarkovModel)
theMarkovModel
- the markov model.public Tree getModel()