PageRenderTime 24ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/NosoiFigthers/EpidemicSimulatorTest/Program.cs

#
C# | 307 lines | 127 code | 18 blank | 162 comment | 9 complexity | ef64609296bf1b25a83f4afc4c963baa MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using EpidemicSimulator;
  7. using EpidemicSimulator.stopconditions;
  8. using QuickGraph;
  9. using QuickGraph.Serialization;
  10. using System.Xml;
  11. using GraphInformation;
  12. using ComplexNetGeneratorLib;
  13. using EpidemicSimulator.strategies;
  14. using System.Threading;
  15. namespace EpidemicSimulatorTest
  16. {
  17. class Program
  18. {
  19. static void testSimulation()
  20. {
  21. SimulationSetup simsetup = new SimulationSetup(new StreamReader(File.OpenRead("terror.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("aids.graphml.xml")).ReadToEnd(), 1, 1);
  22. Simulation simulation = SimulationFactory.createSimulation(simsetup, "/");
  23. simulation.addStopCondition(new MaxSimStepNumberStopCondition(100));
  24. int i = 1;
  25. do
  26. {
  27. simulation.nextSimStep();
  28. simulation.saveSimulationResultToGraphML("simstep" + i + ".graphml.xml");
  29. i++;
  30. } while (simulation.simulationRunning);
  31. }
  32. static void Main(string[] args)
  33. {
  34. testAsyncSimulation();
  35. /*var g = new AdjacencyGraph<DiseaseModelNode, DiseaseModelEdge>();
  36. DiseaseModelNode node1 = new DiseaseModelNode();
  37. node1.stateName = "Susceptible";
  38. node1.stateClass = "EpidemicSimulator.states.SusceptibleState";
  39. node1.nodeColor = "yellow";
  40. DiseaseModelNode node2 = new DiseaseModelNode();
  41. node2.stateName = "Infectious";
  42. node2.stateClass = "EpidemicSimulator.states.InfectiousState";
  43. node2.nodeColor = "red";
  44. DiseaseModelEdge edge1 = new DiseaseModelEdge(node1, node2);
  45. edge1.transitionProbability = 0.3;
  46. DiseaseModelEdge edge2 = new DiseaseModelEdge(node2, node1);
  47. edge2.transitionProbability = 0.9;
  48. g.AddVerticesAndEdge(edge1);
  49. g.AddVerticesAndEdge(edge2);
  50. var vertexIdentity = new VertexIdentity<DiseaseModelNode>(DiseaseModelNode.vertexIdentity);
  51. var edgeIdentity = new EdgeIdentity<DiseaseModelNode, DiseaseModelEdge>(DiseaseModelEdge.edgeIdentity<DiseaseModelNode, DiseaseModelEdge>);
  52. using (var writer = new XmlTextWriter("sis.graphml", Encoding.UTF8))
  53. {
  54. writer.WriteStartDocument();
  55. g.SerializeToGraphML(writer, vertexIdentity, edgeIdentity);
  56. //writer.WriteEndDocument();
  57. }*/
  58. /*Simulation simulation = SimulationFactory.createSimulation(new StreamReader(File.OpenRead("terror.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("sis.graphml.xml")).ReadToEnd(), "/");
  59. simulation.addStopCondition(new MaxSimStepNumberStopCondition(100));
  60. simulation.runSimulation();
  61. simulation.saveSimulationResultToGraphML("test1.graphml");
  62. var g = simulation.getSimulationResultForSilverlight();
  63. var vertexIdentity = new VertexIdentity<GraphElement>(vertexIdentityFunc);
  64. var edgeIdentity = new EdgeIdentity<GraphElement, Edge<GraphElement>>(edgeIdentityFunc<GraphElement, Edge<GraphElement>>);
  65. using (var writer = new XmlTextWriter("test2.graphml", Encoding.UTF8))
  66. {
  67. writer.WriteStartDocument();
  68. g.SerializeToGraphML(writer, vertexIdentity, edgeIdentity);
  69. }*/
  70. //simulation.reset();
  71. /*Console.WriteLine("Podaj liczbe wezlow sieci:");
  72. int n = int.Parse(Console.ReadLine());
  73. ComplexNetGenerator generator = new ComplexNetGenerator();
  74. ComplexNet network = generator.generateScaleFree(n, 1, 1);
  75. StreamWriter writer = new StreamWriter("ScaleFree.graphml.xml", false, Encoding.UTF8);
  76. network.saveAsGraphML(writer);
  77. writer.Close();
  78. StreamWriter output1 = new StreamWriter("output1.txt", false, Encoding.UTF8);
  79. SimulationSetup simsetup = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 500);
  80. simsetup.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  81. simsetup.addStopCondition(new NoInfectedNodesStopCondition());
  82. PersentageStateChangePlan infectionPlan = new PersentageStateChangePlan(10, new RandomInfectionStrategy());
  83. simsetup.addStateChangePlan(infectionPlan);
  84. Simulation simulation = SimulationFactory.createSimulation(simsetup, "/");
  85. generateReports(simulation, output1);
  86. StreamWriter output2 = new StreamWriter("output2.txt", false, Encoding.UTF8);
  87. SimulationSetup simsetup2 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 500);
  88. simsetup2.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  89. simsetup2.addStopCondition(new NoInfectedNodesStopCondition());
  90. PersentageStateChangePlan infectionPlan2 = new PersentageStateChangePlan(10, new DegreeCentralityInfectionStrategy());
  91. simsetup2.addStateChangePlan(infectionPlan2);
  92. Simulation simulation2 = SimulationFactory.createSimulation(simsetup2, "/");
  93. generateReports(simulation2, output2);
  94. StreamWriter output3 = new StreamWriter("output3.txt", false, Encoding.UTF8);
  95. SimulationSetup simsetup3 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 500);
  96. simsetup3.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  97. simsetup3.addStopCondition(new NoInfectedNodesStopCondition());
  98. PersentageStateChangePlan infectionPlan3 = new PersentageStateChangePlan(10, new BetweennessCentralityInfectionStrategy());
  99. simsetup3.addStateChangePlan(infectionPlan3);
  100. Simulation simulation3 = SimulationFactory.createSimulation(simsetup3, "/");
  101. generateReports(simulation3, output3);*/
  102. //---------------------------------------------------------------------------------------
  103. /*
  104. StreamWriter output4 = new StreamWriter("output4.txt", false, Encoding.UTF8);
  105. SimulationSetup simsetup4 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 5);
  106. simsetup4.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  107. simsetup4.addStopCondition(new NoInfectedNodesStopCondition());
  108. PersentageStateChangePlan infectionPlan4 = new PersentageStateChangePlan(1, new RandomInfectionStrategy());
  109. PersentageStateChangePlan inoculationPlan4 = new PersentageStateChangePlan(1, new DegreeCentralityInoculationStrategy());
  110. simsetup4.addStateChangePlan(inoculationPlan4);
  111. simsetup4.addStateChangePlan(infectionPlan4);
  112. Simulation simulation4 = SimulationFactory.createSimulation(simsetup4, "/");
  113. generateReports(simulation4, output4);
  114. StreamWriter output5 = new StreamWriter("output5.txt", false, Encoding.UTF8);
  115. SimulationSetup simsetup5 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 5);
  116. simsetup5.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  117. simsetup5.addStopCondition(new NoInfectedNodesStopCondition());
  118. PersentageStateChangePlan infectionPlan5 = new PersentageStateChangePlan(1, new DegreeCentralityInfectionStrategy());
  119. PersentageStateChangePlan inoculationPlan5 = new PersentageStateChangePlan(1, new DegreeCentralityInoculationStrategy());
  120. simsetup5.addStateChangePlan(inoculationPlan5);
  121. simsetup5.addStateChangePlan(infectionPlan5);
  122. Simulation simulation5 = SimulationFactory.createSimulation(simsetup5, "/");
  123. generateReports(simulation5, output5);
  124. StreamWriter output6 = new StreamWriter("output6.txt", false, Encoding.UTF8);
  125. SimulationSetup simsetup6 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 5);
  126. simsetup6.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  127. simsetup6.addStopCondition(new NoInfectedNodesStopCondition());
  128. PersentageStateChangePlan infectionPlan6 = new PersentageStateChangePlan(1, new BetweennessCentralityInfectionStrategy());
  129. PersentageStateChangePlan inoculationPlan6 = new PersentageStateChangePlan(1, new DegreeCentralityInoculationStrategy());
  130. simsetup6.addStateChangePlan(inoculationPlan6);
  131. simsetup6.addStateChangePlan(infectionPlan6);
  132. Simulation simulation6 = SimulationFactory.createSimulation(simsetup6, "/");
  133. generateReports(simulation6, output6);
  134. //---------------------------------------------------------------------------------------
  135. StreamWriter output7 = new StreamWriter("output7.txt", false, Encoding.UTF8);
  136. SimulationSetup simsetup7 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 5);
  137. simsetup7.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  138. simsetup7.addStopCondition(new NoInfectedNodesStopCondition());
  139. PersentageStateChangePlan infectionPlan7 = new PersentageStateChangePlan(1, new RandomInfectionStrategy());
  140. PersentageStateChangePlan inoculationPlan7 = new PersentageStateChangePlan(1, new BetweennessCentralityInoculationStrategy());
  141. simsetup7.addStateChangePlan(inoculationPlan7);
  142. simsetup7.addStateChangePlan(infectionPlan7);
  143. Simulation simulation7 = SimulationFactory.createSimulation(simsetup7, "/");
  144. generateReports(simulation7, output7);
  145. StreamWriter output8 = new StreamWriter("output8.txt", false, Encoding.UTF8);
  146. SimulationSetup simsetup8 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 5);
  147. simsetup8.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  148. simsetup8.addStopCondition(new NoInfectedNodesStopCondition());
  149. PersentageStateChangePlan infectionPlan8 = new PersentageStateChangePlan(1, new DegreeCentralityInfectionStrategy());
  150. PersentageStateChangePlan inoculationPlan8 = new PersentageStateChangePlan(1, new BetweennessCentralityInoculationStrategy());
  151. simsetup8.addStateChangePlan(inoculationPlan8);
  152. simsetup8.addStateChangePlan(infectionPlan8);
  153. Simulation simulation8 = SimulationFactory.createSimulation(simsetup8, "/");
  154. generateReports(simulation8, output8);
  155. StreamWriter output9 = new StreamWriter("output9.txt", false, Encoding.UTF8);
  156. SimulationSetup simsetup9 = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 5);
  157. simsetup9.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  158. simsetup9.addStopCondition(new NoInfectedNodesStopCondition());
  159. PersentageStateChangePlan infectionPlan9 = new PersentageStateChangePlan(1, new BetweennessCentralityInfectionStrategy());
  160. PersentageStateChangePlan inoculationPlan9 = new PersentageStateChangePlan(1, new BetweennessCentralityInoculationStrategy());
  161. simsetup9.addStateChangePlan(inoculationPlan9);
  162. simsetup9.addStateChangePlan(infectionPlan9);
  163. Simulation simulation9 = SimulationFactory.createSimulation(simsetup9, "/");
  164. generateReports(simulation9, output9);
  165. */
  166. }
  167. public static string vertexIdentityFunc<TVertex>(TVertex v)
  168. {
  169. return v.ToString();
  170. }
  171. public static string edgeIdentityFunc<TVertex, TEdge>(TEdge edge)
  172. where TEdge : EdgeWithAttributes.EdgeWithAttributes<GraphElement>
  173. {
  174. return edge.Source.Id.ToString() + "-" + edge.Target.Id.ToString();
  175. }
  176. public static void generateReports(Simulation simulation, StreamWriter output)
  177. {
  178. output.Write("Krok symulacji");
  179. foreach (DiseaseModelNode diseaseState in simulation.diseaseModelGraph.Vertices)
  180. {
  181. output.Write(";" + diseaseState.stateName);
  182. }
  183. output.WriteLine();
  184. var socialNetwork = simulation.socialNetwork;
  185. output.Write(0);
  186. foreach (DiseaseModelNode diseaseState in simulation.diseaseModelGraph.Vertices)
  187. {
  188. int counter = 0;
  189. foreach (SocialNetworkNode node in socialNetwork.Vertices)
  190. {
  191. if (node.state.stateNode.stateName == diseaseState.stateName)
  192. {
  193. counter++;
  194. }
  195. }
  196. output.Write(";" + counter.ToString());
  197. }
  198. output.WriteLine();
  199. int i = 1;
  200. do
  201. {
  202. simulation.nextSimStep();
  203. socialNetwork = simulation.getSimulationResult();
  204. output.Write(i);
  205. foreach (DiseaseModelNode diseaseState in simulation.diseaseModelGraph.Vertices)
  206. {
  207. int counter = 0;
  208. foreach (SocialNetworkNode node in socialNetwork.Vertices)
  209. {
  210. if (node.state.stateNode.stateName == diseaseState.stateName)
  211. {
  212. counter++;
  213. }
  214. }
  215. output.Write(";"+counter.ToString());
  216. }
  217. output.WriteLine();
  218. i++;
  219. } while (simulation.simulationRunning);
  220. output.Close();
  221. }
  222. private static void testAsyncSimulation()
  223. {
  224. ComplexNetGenerator generator = new ComplexNetGenerator();
  225. ComplexNet network = generator.generateScaleFree(100, 1, 1);
  226. StreamWriter writer = new StreamWriter("ScaleFree.graphml.xml", false, Encoding.UTF8);
  227. network.saveAsGraphML(writer);
  228. writer.Close();
  229. StreamWriter output = new StreamWriter("output.txt", false, Encoding.UTF8);
  230. SimulationSetup simsetup = new SimulationSetup(new StreamReader(File.OpenRead("ScaleFree.graphml.xml")).ReadToEnd(), new StreamReader(File.OpenRead("ebola.graphml.xml")).ReadToEnd(), 500, 1);
  231. simsetup.addStopCondition(new MaxSimStepNumberStopCondition(1000));
  232. simsetup.addStopCondition(new NoInfectedNodesStopCondition());
  233. PersentageStateChangePlan infectionPlan = new PersentageStateChangePlan(10, new RandomInfectionStrategy());
  234. simsetup.addStateChangePlan(infectionPlan);
  235. SimulationManager simManager = new SimulationManager(simsetup, 20, "/");
  236. simManager.runSimulationAsync();
  237. while (true)
  238. {
  239. Thread.Sleep(100);
  240. ExperimentState[] exStates = simManager.getExperimentState();
  241. bool allStopped = true;
  242. foreach (ExperimentState exState in exStates)
  243. {
  244. if (exState.running) allStopped = false;
  245. }
  246. if (allStopped) break;
  247. }
  248. EpidemicSimulator.reports.SimulationSimpleReport report = simManager.getSimulationReport();
  249. output.WriteLine("simTime = " + report.simTime);
  250. output.WriteLine();
  251. foreach(var dataSet in report.dataSets)
  252. {
  253. output.WriteLine(dataSet.label + " " + dataSet.color + " " + dataSet.value.ToString());
  254. }
  255. output.Close();
  256. }
  257. }
  258. }