-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneralRandomWalk.cs
188 lines (155 loc) · 5.8 KB
/
GeneralRandomWalk.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using RandomWalkFramework.RandomWalkInterface;
using RandomWalkFramework.RNG;
namespace RandomWalkFramework {
/// <summary>
/// General random walk class. Used as a base for specific random walks
/// </summary>
/// <typeparam name="TVertex">Type of vertices (states) encountered</typeparam>
/// <typeparam name="TEdge">Type of edges(transitions) encountered</typeparam>
public class RandomWalk<TVertex> : IRandomWalk<TVertex> {
/// <summary>
/// Gets the value which represents the "time" it takes to make a single step.
/// Defaults to 1 if the walk is discreet.
/// </summary>
public virtual decimal TimeIncrement { get { return 1.0M; } }
protected Random r = new MersenneTwister();
private IGraphQuerier<TVertex> Querier { get; set; }
/// <summary>
/// Constructs a general random walk
/// </summary>
/// <param name="entryPoint">Initial state</param>
/// <param name="gq">Querier on the graph</param>
/// <param name="name">The name of the walk. Key is the short name and value the long name</param>
public RandomWalk(TVertex entryPoint, IGraphQuerier<TVertex> gq, KeyValuePair<string, string> name) {
Querier = gq;
this.CurrentState = entryPoint;
this.InitialState = entryPoint;
this.PreviousState = default(TVertex);
this.Name = name;
}
/// <summary>
/// Constructs a general random walk
/// </summary>
/// <param name="entryPoint">Initial state</param>
/// <param name="gq">Querier on the graph</param>
/// <param name="name">The name of the walk. Key is the short name and value the long name</param>
public RandomWalk(TVertex entryPoint, IGraphQuerier<TVertex> gq) :
this(entryPoint,gq,gq.PolicyName) {
}
#region IRandomWalk<TVertex,TEdge, decimal> Members
public void Terminate() {
if (Terminated != null)
Terminated(this, EventArgs.Empty);
}
/// <summary>
/// Event should be raised when the walk terminates
/// </summary>
public event EventHandler Terminated;
public KeyValuePair<string, string> Name { get; protected set; }
/// <summary>
/// Event should be raised at each transition (step) the walk takes
/// </summary>
public event TransitionEvent<TVertex> Step;
/// <summary>
/// Additional initialization instructions go here
/// </summary>
public virtual void Initialize() {
this.CurrentState = this.InitialState;
TotalSteps = 0;
DiscreetSteps = 0;
}
/// <summary>
/// Chose the next state based on the current state.
/// </summary>
/// <param name="current">The current state of the walk. Provided to allow flexibility.</param>
/// <returns>Should return the next transition or default(TEdge) to wait</returns>
protected virtual TVertex ChooseNext(TVertex current) {
if (Querier.AdjecentDegree(current) > 0) {
return Querier.WeightedAdjacentEdge(current, (decimal)r.NextDouble());
} else {
return current;
}
}
/// <summary>
/// Simulates a transition of the walk
/// </summary>
/// <param name="From">Transition From</param>
/// <param name="To">Transition To</param>
/// <param name="TransitionRef">Transition along edge</param>
protected virtual void Transition(TVertex From, TVertex To) {
TotalSteps += TimeIncrement;
DiscreetSteps++;
PreviousState = From;
CurrentState = To;
OnStep(PreviousState, CurrentState, GetTransitionWeight(PreviousState, CurrentState));
}
/// <summary>
/// Simulates one step of the walk
/// </summary>
/// <returns>Returns the vertex that was reached after one step was simulated</returns>
public virtual TVertex NextSample() {
var next = ChooseNext(CurrentState);
Transition(CurrentState, next);
return CurrentState;
}
/// <summary>
/// Method to fire the step event
/// </summary>
/// <param name="CurrentState">The state that the walk was coming from</param>
/// <param name="NextState">The state the walk is moving to</param>
/// <param name="Transition">The edge the walk has taken</param>
/// <param name="TransitionWeight">The weight of the transition taken</param>
protected virtual void OnStep(TVertex PreviousState, TVertex CurrentState, decimal TransitionWeight) {
if (Step != null) {
Step(this, PreviousState, CurrentState, TransitionWeight);
}
}
/// <summary>
/// Holds the steps that the walk took
/// </summary>
public virtual ulong DiscreetSteps {
protected set;
get;
}
/// <summary>
/// Holds the time that the walk has been runing for a continous time walk.
/// This is the same as @property(DiscreetSteps) for a discreet time walk.
/// </summary>
public virtual decimal TotalSteps {
get;
protected set;
}
public TVertex CurrentState { get; protected set; }
public TVertex PreviousState { get; protected set; }
public TVertex InitialState { get; private set; }
/// <summary>
/// Gets the number of transitions that might be performed from a specific state
/// </summary>
public virtual int GetAdjacentTransitionCount(TVertex state) {
return Querier.AdjecentDegree(state);
}
/// <summary>
/// Gets an adjacent transtion
/// </summary>
/// <param name="state">The state from which to get the adjacent transition</param>
/// <param name="index">The index of the adjacent transition</param>
/// <returns>The adjacent transition</returns>
/// <exception cref="IndexOutOfRangeException">The index specified is out of range</exception>
public virtual TVertex GetAdjacentTransition(TVertex state, int index) {
return Querier.AdjecentEdge(state, index);
}
public virtual decimal GetStateWeight(TVertex state) {
return Querier.VertexWeight(state);
}
public virtual decimal GetTransitionWeight(TVertex source, TVertex target) {
return Querier.EdgeWeight(source, target);
}
#endregion
}
}