Changeset 10454 for branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeSimulation.cs
- Timestamp:
- 02/13/14 17:28:30 (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeSimulation.cs
r7204 r10454 22 22 using System; 23 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Text; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 24 using System.Threading; 25 using HeuristicLab.Analysis; 27 26 using HeuristicLab.Common; 28 27 using HeuristicLab.Core; 29 using HeuristicLab.Parameters;30 28 using HeuristicLab.Data; 31 29 using HeuristicLab.Optimization; 32 using System.Threading; 30 using HeuristicLab.Parameters; 31 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 32 using HeuristicLab.Random; 33 33 using HeuristicLab.SimulationCore.AgentBased; 34 using HeuristicLab.Analysis;35 34 36 35 namespace HeuristicLab.SimulationCore.Samples { … … 38 37 [Creatable("Simulations")] 39 38 [StorableClass] 40 public sealed class GameOfLifeSimulation : AgentBasedSimulation {39 public sealed class GameOfLifeSimulation : AgentBasedSimulation { 41 40 public override Type ProblemType { 42 41 get { return typeof(GameOfLifeScenario); } … … 48 47 } 49 48 } 50 49 51 50 public ValueParameter<IntValue> IterationsParameter { 52 51 get { return (ValueParameter<IntValue>)Parameters["Iterations"]; } 53 52 } 54 55 public GameOfLifeSimulation() : base() { 53 54 public GameOfLifeSimulation() 55 : base() { 56 56 Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of iterations.", new IntValue(100))); 57 57 … … 72 72 } 73 73 74 private GameOfLifeAgent GetAgent(int x, int y) { 74 private GameOfLifeAgent GetAgent(int x, int y) { 75 75 int width = Scenario.WidthParameter.Value.Value; 76 76 int height = Scenario.HeightParameter.Value.Value; … … 79 79 throw new Exception("invalid index"); 80 80 81 int index = x + y * width; 81 int index = x + y * width; 82 82 83 83 return Agents[index] as GameOfLifeAgent; … … 86 86 private void Initialize() { 87 87 if (Scenario != null) { 88 Scenario.WidthParameter.ValueChanged += new EventHandler(WidthParameter_ValueChanged); 89 if(Scenario.WidthParameter.Value != null) 90 Scenario.WidthParameter.Value.ValueChanged += new EventHandler(Width_ValueChanged); 91 Scenario.HeightParameter.ValueChanged += new EventHandler(HeightParameter_ValueChanged); 92 if (Scenario.HeightParameter.Value != null) 93 Scenario.HeightParameter.Value.ValueChanged += new EventHandler(Height_ValueChanged); 88 Scenario.WidthParameter.Value.ValueChanged += new EventHandler(Width_ValueChanged); 89 Scenario.HeightParameter.Value.ValueChanged += new EventHandler(Height_ValueChanged); 94 90 } 95 91 } … … 103 99 } 104 100 105 void HeightParameter_ValueChanged(object sender, EventArgs e) {106 CreateAgents();107 }108 109 void WidthParameter_ValueChanged(object sender, EventArgs e) {110 CreateAgents();111 }112 113 101 public IEnumerable<GameOfLifeAgent> GetNeighbors(GameOfLifeAgent agent) { 114 102 int width = Scenario.WidthParameter.Value.Value; 115 103 int height = Scenario.HeightParameter.Value.Value; 116 104 int index = Agents.IndexOf(agent); 117 105 118 106 List<GameOfLifeAgent> result = new List<GameOfLifeAgent>(); 119 107 … … 159 147 int height = Scenario.HeightParameter.Value.Value; 160 148 161 Random rand = newRandom();149 var rand = new FastRandom(); 162 150 163 151 for (int i = 0; i < width * height; i++) { … … 190 178 191 179 Results["Cells"].Value = cells; 192 180 193 181 IntValue currentIterations = (Results["CurrentIterations"].Value as IntValue); 194 182 currentIterations.Value++; … … 206 194 Results.Add(new Result("CurrentIterations", new IntValue(0))); 207 195 Results.Add(new Result("Cells", new HeatMap(width, height))); 208 } 196 } 209 197 210 198 for (int i = start; i < iterations; i++) { 211 199 cancellationToken.ThrowIfCancellationRequested(); 212 200 213 201 base.Step(); 214 UpdateResults(); 202 UpdateResults(); 215 203 } 216 204 }
Note: See TracChangeset
for help on using the changeset viewer.