- Timestamp:
- 02/13/14 17:28:30 (11 years ago)
- Location:
- branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeDiscreteEventSimulation.cs
r10450 r10454 6 6 using HeuristicLab.Optimization; 7 7 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 8 using HeuristicLab.Random; 8 9 9 10 namespace HeuristicLab.SimulationCore.Samples { 10 [Item("Game of Life (discrete event)", "A discrete event based implementation of the game of life simulation.")] 11 [Creatable("Simulations")] 12 public class GameOfLifeDiscreteEventSimulation : DiscreteEventSimulation<GameOfLifeModel> { 13 private GameOfLifeScenario Scenario { 14 get { return Problem as GameOfLifeScenario; } 15 set { Problem = value; } 16 } 17 18 private GameOfLifeDiscreteEventSimulation(GameOfLifeDiscreteEventSimulation original, Cloner cloner) 19 : base(original, cloner) { } 20 public GameOfLifeDiscreteEventSimulation() { 21 Scenario = new GameOfLifeScenario(); 22 InitialAction = new InitializeGameOfLifeBoardAction() { AliveRate = Scenario.AliveRateParameter.Value.Value }; 23 Activities = new IActivity<GameOfLifeModel>[] { new CurrentTimeMonitor() }; 24 Reporters = new IReporter<GameOfLifeModel>[] { new GameOfLifeStateReporter() }; 25 Initialize(); 26 } 27 28 private void Initialize() { 29 Scenario.WidthParameter.ValueChanged += ValueChanged; 30 if (Scenario.WidthParameter.Value != null) Scenario.WidthParameter.Value.ValueChanged += ParametersChanged; 31 Scenario.HeightParameter.ValueChanged += ValueChanged; 32 if (Scenario.HeightParameter.Value != null) Scenario.HeightParameter.Value.ValueChanged += ParametersChanged; 33 Scenario.AliveRateParameter.ValueChanged += ValueChanged; 34 if (Scenario.AliveRateParameter.Value != null) Scenario.AliveRateParameter.Value.ValueChanged += ParametersChanged; 35 } 36 37 38 private void ValueChanged(object sender, EventArgs e) { 39 if (Scenario.WidthParameter.Value != null) Scenario.WidthParameter.Value.ValueChanged += ParametersChanged; 40 if (Scenario.HeightParameter.Value != null) Scenario.HeightParameter.Value.ValueChanged += ParametersChanged; 41 if (Scenario.AliveRateParameter.Value != null) Scenario.AliveRateParameter.Value.ValueChanged += ParametersChanged; 42 ParametersChanged(sender, e); 43 } 44 45 private void ParametersChanged(object sender, EventArgs e) { 46 Model = new GameOfLifeModel(Scenario.WidthParameter.Value.Value, Scenario.HeightParameter.Value.Value); 47 ((InitializeGameOfLifeBoardAction)InitialAction).AliveRate = Scenario.AliveRateParameter.Value.Value; 48 } 49 50 protected override void OnPrepared() { 51 Model = new GameOfLifeModel(Scenario.WidthParameter.Value.Value, Scenario.HeightParameter.Value.Value); 52 base.OnPrepared(); 53 } 54 55 public override IDeepCloneable Clone(Cloner cloner) { 56 return new GameOfLifeDiscreteEventSimulation(this, cloner); 57 } 58 } 59 11 12 #region Model 60 13 [StorableClass] 61 14 public sealed class GameOfLifeModel : IModel { … … 79 32 } 80 33 } 81 82 [StorableClass] 83 sealed class CurrentTimeMonitor : Activity<GameOfLifeModel> { 84 public override IEnumerable<Guid> MonitoredActionIds { 85 get { return new[] { InitializeGameOfLifeBoardAction.Id, ModelTimeUpdateAction<GameOfLifeModel>.Id }; } 34 #endregion 35 36 #region Simulation 37 [Item("Game of Life (discrete event)", "A discrete event based implementation of the game of life simulation.")] 38 [Creatable("Simulations")] 39 public class GameOfLifeDiscreteEventSimulation : DiscreteEventSimulation<GameOfLifeModel> { 40 public override Type ProblemType { get { return typeof(GameOfLifeScenario); } } 41 42 private GameOfLifeScenario Scenario { 43 get { return (GameOfLifeScenario)base.Problem; } 44 set { base.Problem = value; } 45 } 46 47 private GameOfLifeDiscreteEventSimulation(GameOfLifeDiscreteEventSimulation original, Cloner cloner) 48 : base(original, cloner) { 49 RegisterScenarioEventHandlers(); 50 } 51 public GameOfLifeDiscreteEventSimulation() { 52 PrepareSimulation(); 53 Scenario = new GameOfLifeScenario(); 54 Parameterize(); 55 RegisterScenarioEventHandlers(); 56 } 57 58 public override IDeepCloneable Clone(Cloner cloner) { 59 return new GameOfLifeDiscreteEventSimulation(this, cloner); 60 } 61 62 [StorableHook(HookType.AfterDeserialization)] 63 private void AfterDeserialization() { 64 RegisterScenarioEventHandlers(); 65 } 66 67 private void PrepareSimulation() { 68 InitialAction = new InitializeGameOfLifeBoardAction(); 69 Activities = new IActivity<GameOfLifeModel>[] { new BoardActivity() }; 70 Reporters = new IReporter<GameOfLifeModel>[] { new GameOfLifeStateReporter() }; 71 } 72 73 private void RegisterScenarioEventHandlers() { 74 Scenario.WidthParameter.Value.ValueChanged += ParametersChanged; 75 Scenario.HeightParameter.Value.ValueChanged += ParametersChanged; 76 Scenario.AliveRateParameter.Value.ValueChanged += ParametersChanged; 77 } 78 79 protected override void OnProblemChanged() { 80 base.OnProblemChanged(); 81 Parameterize(); 82 RegisterScenarioEventHandlers(); 83 } 84 85 protected override void OnPrepared() { 86 base.OnPrepared(); 87 PrepareSimulation(); 88 } 89 90 private void ParametersChanged(object sender, EventArgs e) { 91 Parameterize(); 92 } 93 94 private void Parameterize() { 95 Model = new GameOfLifeModel(Scenario.WidthParameter.Value.Value, Scenario.HeightParameter.Value.Value); 96 ((InitializeGameOfLifeBoardAction)InitialAction).AliveRate = Scenario.AliveRateParameter.Value.Value; 97 } 98 } 99 #endregion 100 101 #region Activities 102 [StorableClass] 103 sealed class BoardActivity : Activity<GameOfLifeModel> { 104 public override IEnumerable<Type> MonitoredActions { 105 get { return new[] { typeof(InitializeGameOfLifeBoardAction), typeof(UpdateGameOfLifeBoardAction) }; } 86 106 } 87 107 88 108 [StorableConstructor] 89 private CurrentTimeMonitor(bool deserializing) : base(deserializing) { }90 private CurrentTimeMonitor(CurrentTimeMonitororiginal, Cloner cloner) : base(original, cloner) { }91 public CurrentTimeMonitor() : base(new SortedListEventQueue<GameOfLifeModel>()) { }109 private BoardActivity(bool deserializing) : base(deserializing) { } 110 private BoardActivity(BoardActivity original, Cloner cloner) : base(original, cloner) { } 111 public BoardActivity() : base(new SortedListEventQueue<GameOfLifeModel>()) { } 92 112 93 113 public override void ManageEvents(GameOfLifeModel model, IAction<GameOfLifeModel> lastAction) { 94 EventQueue.Push(new Event<GameOfLifeModel>(model.CurrentTime + 1, new UpdateGameOfLifeBoardAction())); 95 } 96 97 public override IDeepCloneable Clone(Cloner cloner) { 98 return new CurrentTimeMonitor(this, cloner); 99 } 100 } 101 114 EventQueue.Push(model.CurrentTime + 1, new UpdateGameOfLifeBoardAction()); 115 } 116 117 public override IDeepCloneable Clone(Cloner cloner) { 118 return new BoardActivity(this, cloner); 119 } 120 } 121 #endregion 122 123 #region Reporters 102 124 [StorableClass] 103 125 sealed class GameOfLifeStateReporter : Reporter<GameOfLifeModel> { … … 123 145 } 124 146 } 125 147 #endregion 148 149 #region Actions 126 150 [StorableClass] 127 151 sealed class InitializeGameOfLifeBoardAction : Action<GameOfLifeModel> { 128 public static readonly Guid Id = new Guid("a6d37f71-ccef-4c2a-b11b-793fda0f9f4f");129 public override Guid ActionId {130 get { return Id; }131 }132 133 152 [Storable] 134 153 public double AliveRate { get; set; } 135 154 136 [StorableConstructor]137 private InitializeGameOfLifeBoardAction(bool deserializing) : base(deserializing) { }138 155 private InitializeGameOfLifeBoardAction(InitializeGameOfLifeBoardAction original, Cloner cloner) 139 156 : base(original, cloner) { … … 145 162 146 163 public override void Execute(GameOfLifeModel model) { 147 var random = new Random();164 var random = new FastRandom(); 148 165 var rows = model.Grid.GetLength(0); 149 166 var cols = model.Grid.GetLength(1); … … 161 178 [StorableClass] 162 179 sealed class UpdateGameOfLifeBoardAction : Action<GameOfLifeModel> { 163 public static readonly Guid Id = new Guid("323bb2ef-bc66-488f-ac88-798b91565e62");164 public override Guid ActionId {165 get { return Id; }166 }167 168 [StorableConstructor]169 private UpdateGameOfLifeBoardAction(bool deserializing) : base(deserializing) { }170 180 private UpdateGameOfLifeBoardAction(UpdateGameOfLifeBoardAction original, Cloner cloner) : base(original, cloner) { } 171 181 public UpdateGameOfLifeBoardAction() { } … … 195 205 } 196 206 } 207 #endregion 197 208 } -
branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeScenario.cs
r6624 r10454 20 20 #endregion 21 21 22 using System; 23 using System.Collections.Generic; 24 using System.Linq; 25 using System.Text; 22 using HeuristicLab.Common; 26 23 using HeuristicLab.Core; 27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;28 using HeuristicLab.Common;29 24 using HeuristicLab.Data; 30 25 using HeuristicLab.Parameters; 26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 31 27 32 28 namespace HeuristicLab.SimulationCore.Samples { 33 29 [Item("GameOfLifeScenario", "A game of life scenario.")] 34 30 [StorableClass] 35 public sealed class GameOfLifeScenario : Scenario {36 public ValueParameter<IntValue> WidthParameter {37 get { return ( ValueParameter<IntValue>)Parameters["Width"]; }31 public sealed class GameOfLifeScenario : Scenario { 32 public IFixedValueParameter<IntValue> WidthParameter { 33 get { return (IFixedValueParameter<IntValue>)Parameters["Width"]; } 38 34 } 39 public ValueParameter<IntValue> HeightParameter {40 get { return ( ValueParameter<IntValue>)Parameters["Height"]; }35 public IFixedValueParameter<IntValue> HeightParameter { 36 get { return (IFixedValueParameter<IntValue>)Parameters["Height"]; } 41 37 } 42 public ValueParameter<PercentValue> AliveRateParameter {43 get { return ( ValueParameter<PercentValue>)Parameters["AliveRate"]; }38 public IFixedValueParameter<PercentValue> AliveRateParameter { 39 get { return (IFixedValueParameter<PercentValue>)Parameters["AliveRate"]; } 44 40 } 45 41 46 42 public GameOfLifeScenario() 47 43 : base() { 48 Parameters.Add(new ValueParameter<IntValue>("Width", "The width value.", new IntValue(100)));49 Parameters.Add(new ValueParameter<IntValue>("Height", "The height value.", new IntValue(100)));50 Parameters.Add(new ValueParameter<PercentValue>("AliveRate", "The rate of the initialyy alive cells.", new PercentValue(0.5)));44 Parameters.Add(new FixedValueParameter<IntValue>("Width", "The width value.", new IntValue(100))); 45 Parameters.Add(new FixedValueParameter<IntValue>("Height", "The height value.", new IntValue(100))); 46 Parameters.Add(new FixedValueParameter<PercentValue>("AliveRate", "The rate of the initialyy alive cells.", new PercentValue(0.5))); 51 47 } 52 48 [StorableConstructor] -
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 } -
branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/HeuristicLab.SimulationCore.Samples-3.3.csproj
r10450 r10454 155 155 <Private>False</Private> 156 156 </Reference> 157 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL"> 158 <SpecificVersion>False</SpecificVersion> 159 <HintPath>..\..\..\..\trunk\sources\bin\HeuristicLab.Random-3.3.dll</HintPath> 160 </Reference> 157 161 <Reference Include="System" /> 158 162 <Reference Include="System.Core" /> … … 164 168 </ItemGroup> 165 169 <ItemGroup> 170 <Compile Include="CardGameSimulation.cs" /> 166 171 <Compile Include="GameOfLifeAgent.cs" /> 167 172 <Compile Include="GameOfLifeDiscreteEventSimulation.cs" />
Note: See TracChangeset
for help on using the changeset viewer.