package de.ugoe.cs.eventbench.markov; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; import Jama.Matrix; import de.ugoe.cs.eventbench.data.Event; import de.ugoe.cs.util.console.Console; import edu.uci.ics.jung.graph.Graph; import edu.uci.ics.jung.graph.SparseMultigraph; import edu.uci.ics.jung.graph.util.EdgeType; public class MarkovModel implements DotPrinter { private State initialState; private State endState; private List states; private List stateIdList; private Random r; final static int MAX_STATDIST_ITERATIONS = 1000; /** *

* Default constructor. Creates a new random number generator. *

*/ public MarkovModel() { this(new Random()); } /** *

* Creates a new {@link MarkovModel} with a predefined random number generator. *

* * @param r random number generator */ public MarkovModel(Random r) { this.r = r; // defensive copy would be better, but constructor Random(r) does not seem to exist. } public void printRandomWalk() { State currentState = initialState; IMemory history = new IncompleteMemory(5); // this is NOT used here, just for testing ... history.add(currentState); Console.println(currentState.getId()); while(!currentState.equals(endState)) { currentState = currentState.getNextState(); Console.println(currentState.getId()); history.add(currentState); } } public List> randomSequence() { List> sequence = new LinkedList>(); State currentState = initialState; if( currentState.getAction()!=null ) { sequence.add(currentState.getAction()); } System.out.println(currentState.getId()); while(!currentState.equals(endState)) { currentState = currentState.getNextState(); if( currentState.getAction()!=null ) { sequence.add(currentState.getAction()); } System.out.println(currentState.getId()); } return sequence; } public void printDot() { int numUnprintableStates = 0; System.out.println("digraph model {"); for( State state : states ) { if( state instanceof DotPrinter ) { ((DotPrinter) state).printDot(); } else { numUnprintableStates++; } } System.out.println('}'); if( numUnprintableStates>0 ) { Console.println("" + numUnprintableStates + "/" + states.size() + "were unprintable!"); } } public Graph getGraph() { Graph graph = new SparseMultigraph(); for( State state : states) { try { SimpleState simpleState = (SimpleState) state; String from = simpleState.getShortId(); for( int i=0 ; i>> sequences) { Event fromElement = null; Event toElement = null; SimpleState fromState; SimpleState toState; states = new ArrayList(); stateIdList = new ArrayList(); initialState = new SimpleState("GLOBALSTARTSTATE", null); initialState.setRandom(r); states.add(initialState); stateIdList.add("GLOBALSTARTSTATE"); endState = new SimpleState("GLOBALENDSTATE", null); endState.setRandom(r); states.add(endState); stateIdList.add("GLOBALENDSTATE"); for( List> sequence : sequences ) { for( int i=0; i action) { SimpleState state = null; String id = action.getStandardId(); String idShort = action.getShortId(); int index = stateIdList.indexOf(id); if( index!=-1 ) { state = (SimpleState) states.get(index); } else { state = new SimpleState(id, action, idShort); state.setRandom(r); states.add(state); stateIdList.add(id); } return state; } /////////////////////////////////////////////////////////// // states must be SimpleState, this functions will throw bad cast exceptions public double calcEntropy() { int numStates = states.size(); // create transmission matrix Matrix transmissionMatrix = new Matrix(numStates, numStates); for( int i=0 ; i1 ) { stationaryMatrix = stationaryMatrix.times(stationaryMatrix); rank = stationaryMatrix.rank(); iter++; } if( rank!=1 ) { Console.traceln("rank: " + rank); Console.printerrln("Unable to calculate stationary distribution."); return Double.NaN; } double entropy = 0.0; for( int i=0 ; i