public static void UsingAGraphTypeHelper() { //Create a clique helper Clique <Guid> cliqueHelper = new Clique <Guid>(VertexFactory: new StatefulVertexFactory <VertexState>(new EnumState(VertexState.HEALTHY)), EdgeFactory: new DefaultEdgeFactory(), IdentityProvider: new DefaultIdentityProvider()); //Use it to construct a graph Graph <Guid> cliqueGraph = cliqueHelper.Create(100).Graph; //StatefulVertexFactory gives all vertices a default value of VertexState.HEALTHY. //For the simulation we need at least one mutant. The first vertex will do. StatefulVertex <Guid, VertexState> v = cliqueGraph.Vertices.OfType <StatefulVertex <Guid, VertexState> >().First(); v.State.ChangeStateValue(VertexState.MUTANT); MoranProcessRunner moranProcess = new MoranProcessRunner(new StateSelector(), new VertexSelector(), new VictimSelector()); MoranProcessResult result = moranProcess.RunOn(cliqueGraph, 1000, 10000, 3.0d); Console.WriteLine("UsingAGraphTypeHelper :: Result"); Console.WriteLine("\t Repetitions " + result.RepetitionsPerformed); Console.WriteLine("\t Fixations " + result.Fixations); Console.WriteLine("\t Extinctions " + result.Extinctions); Console.WriteLine("\t Timeouts" + result.Timeout); Console.WriteLine("\t p(Fixation) " + result.FixationProbability); Console.WriteLine("\t p(Extinction) " + result.ExtinctionProbability); Console.WriteLine("\t p(Timeout) " + result.TimeoutProbability); }
public MoranProcessResult RunOn <TIdent>(Graph <TIdent> G, int Repetitions, int Iterations, double MutantFitness) where TIdent : struct { MoranProcessResult result = new MoranProcessResult(); result.RepetitionsPerformed = Repetitions; for (int rep = 0; rep < Repetitions; rep++) { Graph <TIdent> repGraph = (Graph <TIdent>)G.Clone(); int iter = 0; while (iter < Iterations && !graphFixated(repGraph) && !graphExtinct(repGraph)) { IEnumerable <TIdent> targetState = _stateSelector.Select(repGraph, _random, MutantFitness); TIdent vertex = _vertexSelector.Select(targetState, repGraph, _random); IEnumerable <TIdent> destinationVertices = repGraph.VerticesConnectedToVertex(vertex); TIdent victim = _victimSelector.Select(destinationVertices, repGraph, _random); //It's possible for there to be no victim. //Consider the end of a chain where there are no neighbours //No action should be taken, but it should still take up an iteration if (!default(TIdent).Equals(victim)) { StatefulVertex <TIdent, VertexState> vert = (StatefulVertex <TIdent, VertexState>)repGraph.FindVertex(vertex); StatefulVertex <TIdent, VertexState> vict = (StatefulVertex <TIdent, VertexState>)repGraph.FindVertex(victim); vict.State.ChangeStateValue(vert.State.CurrentState); } iter++; } if (!graphFixated(repGraph) && !graphExtinct(repGraph)) { result.Timeout++; } else { if (graphFixated(repGraph) && !graphExtinct(repGraph)) { result.Fixations++; } else if (!graphFixated(repGraph) && graphExtinct(repGraph)) { result.Extinctions++; } } } return(result); }
public static void UsingAManuallyBuiltGraph() { IIdentityProvider <Guid> identity = new DefaultIdentityProvider(); Graph <Guid> graph = new Graph <Guid>(identity); StatefulVertex <Guid, VertexState> v1 = new StatefulVertex <Guid, VertexState>(identity, new EnumState(VertexState.MUTANT)); StatefulVertex <Guid, VertexState> v2 = new StatefulVertex <Guid, VertexState>(identity, new EnumState(VertexState.MUTANT)); StatefulVertex <Guid, VertexState> v3 = new StatefulVertex <Guid, VertexState>(identity, new EnumState(VertexState.MUTANT)); StatefulVertex <Guid, VertexState> v4 = new StatefulVertex <Guid, VertexState>(identity, new EnumState(VertexState.MUTANT)); StatefulVertex <Guid, VertexState> v5 = new StatefulVertex <Guid, VertexState>(identity, new EnumState(VertexState.MUTANT)); graph.AddVertex(v1); graph.AddVertex(v2); graph.AddVertex(v3); graph.AddVertex(v4); graph.AddVertex(v5); graph.AddEdge(new Edge <Guid>(v1, v2, identity), false); graph.AddEdge(new Edge <Guid>(v1, v3, identity), false); graph.AddEdge(new Edge <Guid>(v1, v4, identity), false); graph.AddEdge(new Edge <Guid>(v4, v5, identity), false); graph.AddEdge(new Edge <Guid>(v4, v3, identity), false); MoranProcessRunner moranProcess = new MoranProcessRunner(new StateSelector(), new VertexSelector(), new VictimSelector()); MoranProcessResult result = moranProcess.RunOn(graph, 1000, 10000, 3.0d); Console.WriteLine("UsingAManuallyBuiltGraph :: Result"); Console.WriteLine("\t Repetitions " + result.RepetitionsPerformed); Console.WriteLine("\t Fixations " + result.Fixations); Console.WriteLine("\t Extinctions " + result.Extinctions); Console.WriteLine("\t Timeouts" + result.Timeout); Console.WriteLine("\t p(Fixation) " + result.FixationProbability); Console.WriteLine("\t p(Extinction) " + result.ExtinctionProbability); Console.WriteLine("\t p(Timeout) " + result.TimeoutProbability); }
private static GraphSchema CreateFromGeneric <TDto>(IClassCache cache) { var vertices = new List <StatefulVertex <ClassInfo> >(); var edges = new List <Edge>(); var types = new HashSet <Type>(); // Helper funcs // get the class info from type via the cache. ClassInfoUtility GetClassInfoFromType(Type t) { var hash = t.GetHashCode(); if (cache.HasKey(hash)) { return(cache.Get(hash)); } return(cache.GetOrAdd(t)); } bool VerticesContainType(Type type) => vertices.Any(v => v.Vertex.Value.TypeId == type.GetHashCode()); StatefulVertex <ClassInfo> GetOrAddVertex(Type type) { StatefulVertex <ClassInfo> vertex = null; if (VerticesContainType(type)) { vertex = vertices.First(v => v.Vertex.Value.TypeId == type.GetHashCode()); } else { vertex = new StatefulVertex <ClassInfo>(new Vertex(new ClassInfo(GetClassInfoFromType(type))), type); vertices.Add(vertex); types.Add(type); } return(vertex); } // initialize root. GetOrAddVertex(typeof(TDto)); while (vertices.Any(v => v.Color != StatefulVertexStateType.Discovered)) { var vertex = vertices.First(v => v.Color == StatefulVertexStateType.UnReached); vertex.Color = StatefulVertexStateType.Identified; var classInfo = GetClassInfoFromType(vertex.Type); var tentativeConnectionToType = new List <Edge>(); void EstablishConnection(Type connectionToType, Property property) { var toVertex = GetOrAddVertex(connectionToType); var edge = new Edge(vertex.Vertex as Vertex, toVertex.Vertex as Vertex, property); if (!edges.Contains(edge)) { edges.Add(edge); } } foreach (var childNavigationProperty in vertex.Vertex.Value.Properties.Where(p => p.IsNavigationProperty)) { var type = classInfo.NavigationProperties.FirstOrDefault(x => x.Name == childNavigationProperty.Name)?.PropertyType; if (type is null) { continue; } EstablishConnection(type, childNavigationProperty); } foreach (var childCollection in vertex.Vertex.Value.Properties.Where(p => p.IsCollection)) { var type = classInfo.Collections.FirstOrDefault(x => x.Name == childCollection.Name)?.PropertyType.GetGenericArguments()[0]; if (type is null) { continue; } EstablishConnection(type, childCollection); } vertex.Color = StatefulVertexStateType.Discovered; } return(new GraphSchema(vertices.Select(v => v.Vertex as Vertex), edges)); }