Synthia
Generic and flexible data structure generator
MarkovReader.java
Go to the documentation of this file.
1 package ca.uqac.lif.synthia.tree;
2 
3 import java.util.ArrayDeque;
4 import java.util.HashSet;
5 import java.util.Queue;
6 import java.util.Set;
7 
8 import ca.uqac.lif.synthia.Picker;
10 
11 /**
12  * Converts a graph into an equivalent Markov chain. Each vertex of the
13  * graph is turned into a state of the Markov chain by overriding method
14  * {@link #getPicker(Object) getPicker()}. Transitions in the chain mirror the
15  * adjacency relation of the graph, assigning equal probability to each
16  * outgoing edge.
17  *
18  * @author Sylvain HallĂ©
19  *
20  * @param <T> The type of the nodes in the graph.
21  * @param <U> The type of the nodes in the Markov chain.
22  */
23 public class MarkovReader<T,U> extends GraphCrawler<T>
24 {
25  /**
26  * Converts a graph into a {@linkplain MarkovChain Markov chain}.
27  * @param start The node of the graph that will be used as the initial
28  * state of the chain.
29  * @param float_source A source of floating-point numbers, used to
30  * instantiate the Markov chain.
31  * @return The resulting Markov chain
32  */
33  public MarkovChain<U> asMarkovChain(Node<T> start, Picker<Float> float_source)
34  {
35  MarkovChain<U> markov = new MarkovChain<U>(float_source);
36  Set<Node<T>> visited = new HashSet<Node<T>>();
37  Queue<Node<T>> to_visit = new ArrayDeque<Node<T>>();
38  to_visit.add(start);
39  while (!to_visit.isEmpty())
40  {
41  Node<T> current = to_visit.remove();
42  if (visited.contains(current))
43  {
44  continue;
45  }
46  visited.add(current);
47  int id = getId(current);
48  markov.add(id, getPicker(current.getLabel()));
49  float degree = current.getChildren().size();
50  for (Node<T> child : current.getChildren())
51  {
52  int t_id = getId(child);
53  markov.add(id, t_id, 1f / degree);
54  if (!visited.contains(child) && !to_visit.contains(child))
55  {
56  to_visit.add(child);
57  }
58  }
59  }
60  return markov;
61  }
62 
63  protected Picker<U> getPicker(T t)
64  {
65  return null;
66  }
67 }
ca.uqac.lif.synthia.Picker
Picks an object.
Definition: Picker.java:36
ca.uqac.lif.synthia.tree.MarkovReader
Converts a graph into an equivalent Markov chain.
Definition: MarkovReader.java:23
ca.uqac.lif.synthia.sequence.MarkovChain.add
MarkovChain< T > add(int source, int destination, Number probability)
Adds a new transition to the machine.
Definition: MarkovChain.java:97
ca.uqac
ca.uqac.lif.synthia
Definition: Bounded.java:19
ca.uqac.lif.synthia.tree.Node.getLabel
T getLabel()
Gets the label associated to this node.
Definition: Node.java:78
ca.uqac.lif.synthia.tree.Node.getChildren
List< Node< T > > getChildren()
Gets the children of this node.
Definition: Node.java:69
ca.uqac.lif
ca
ca.uqac.lif.synthia.tree.GraphCrawler.getId
int getId(Node< T > n)
Definition: GraphCrawler.java:20
ca.uqac.lif.synthia.sequence.MarkovChain
Generates a sequence of objects by a random walk in a Markov chain.
Definition: MarkovChain.java:47
ca.uqac.lif.synthia.tree.GraphCrawler
Definition: GraphCrawler.java:6
ca.uqac.lif.synthia.tree.Node
Simple implementation of a labeled nodel.
Definition: Node.java:31
ca.uqac.lif.synthia.tree.MarkovReader.getPicker
Picker< U > getPicker(T t)
Definition: MarkovReader.java:63
ca.uqac.lif.synthia.sequence
Pickers related to the generation of a sequence of values.
Definition: BehaviorTree.java:19
ca.uqac.lif.synthia.tree.MarkovReader.asMarkovChain
MarkovChain< U > asMarkovChain(Node< T > start, Picker< Float > float_source)
Converts a graph into a {@linkplain MarkovChain Markov chain}.
Definition: MarkovReader.java:33