Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/13/14 17:28:30 (11 years ago)
Author:
abeham
Message:

#1610: updated core, implemented card game sample

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  
    66using HeuristicLab.Optimization;
    77using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     8using HeuristicLab.Random;
    89
    910namespace 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
    6013  [StorableClass]
    6114  public sealed class GameOfLifeModel : IModel {
     
    7932    }
    8033  }
    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) }; }
    86106    }
    87107
    88108    [StorableConstructor]
    89     private CurrentTimeMonitor(bool deserializing) : base(deserializing) { }
    90     private CurrentTimeMonitor(CurrentTimeMonitor original, 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>()) { }
    92112
    93113    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
    102124  [StorableClass]
    103125  sealed class GameOfLifeStateReporter : Reporter<GameOfLifeModel> {
     
    123145    }
    124146  }
    125 
     147  #endregion
     148
     149  #region Actions
    126150  [StorableClass]
    127151  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 
    133152    [Storable]
    134153    public double AliveRate { get; set; }
    135154
    136     [StorableConstructor]
    137     private InitializeGameOfLifeBoardAction(bool deserializing) : base(deserializing) { }
    138155    private InitializeGameOfLifeBoardAction(InitializeGameOfLifeBoardAction original, Cloner cloner)
    139156      : base(original, cloner) {
     
    145162
    146163    public override void Execute(GameOfLifeModel model) {
    147       var random = new Random();
     164      var random = new FastRandom();
    148165      var rows = model.Grid.GetLength(0);
    149166      var cols = model.Grid.GetLength(1);
     
    161178  [StorableClass]
    162179  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) { }
    170180    private UpdateGameOfLifeBoardAction(UpdateGameOfLifeBoardAction original, Cloner cloner) : base(original, cloner) { }
    171181    public UpdateGameOfLifeBoardAction() { }
     
    195205    }
    196206  }
     207  #endregion
    197208}
  • branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeScenario.cs

    r6624 r10454  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
     22using HeuristicLab.Common;
    2623using HeuristicLab.Core;
    27 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    28 using HeuristicLab.Common;
    2924using HeuristicLab.Data;
    3025using HeuristicLab.Parameters;
     26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    3127
    3228namespace HeuristicLab.SimulationCore.Samples {
    3329  [Item("GameOfLifeScenario", "A game of life scenario.")]
    3430  [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"]; }
    3834    }
    39     public ValueParameter<IntValue> HeightParameter {
    40       get { return (ValueParameter<IntValue>)Parameters["Height"]; }
     35    public IFixedValueParameter<IntValue> HeightParameter {
     36      get { return (IFixedValueParameter<IntValue>)Parameters["Height"]; }
    4137    }
    42     public ValueParameter<PercentValue> AliveRateParameter {
    43       get { return (ValueParameter<PercentValue>)Parameters["AliveRate"]; }
     38    public IFixedValueParameter<PercentValue> AliveRateParameter {
     39      get { return (IFixedValueParameter<PercentValue>)Parameters["AliveRate"]; }
    4440    }
    4541
    4642    public GameOfLifeScenario()
    4743      : 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)));
    5147    }
    5248    [StorableConstructor]
  • branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeSimulation.cs

    r7204 r10454  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Linq;
    25 using System.Text;
    26 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     24using System.Threading;
     25using HeuristicLab.Analysis;
    2726using HeuristicLab.Common;
    2827using HeuristicLab.Core;
    29 using HeuristicLab.Parameters;
    3028using HeuristicLab.Data;
    3129using HeuristicLab.Optimization;
    32 using System.Threading;
     30using HeuristicLab.Parameters;
     31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     32using HeuristicLab.Random;
    3333using HeuristicLab.SimulationCore.AgentBased;
    34 using HeuristicLab.Analysis;
    3534
    3635namespace HeuristicLab.SimulationCore.Samples {
     
    3837  [Creatable("Simulations")]
    3938  [StorableClass]
    40   public sealed class GameOfLifeSimulation: AgentBasedSimulation {
     39  public sealed class GameOfLifeSimulation : AgentBasedSimulation {
    4140    public override Type ProblemType {
    4241      get { return typeof(GameOfLifeScenario); }
     
    4847      }
    4948    }
    50    
     49
    5150    public ValueParameter<IntValue> IterationsParameter {
    5251      get { return (ValueParameter<IntValue>)Parameters["Iterations"]; }
    5352    }
    54    
    55     public GameOfLifeSimulation() : base() {
     53
     54    public GameOfLifeSimulation()
     55      : base() {
    5656      Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of iterations.", new IntValue(100)));
    5757
     
    7272    }
    7373
    74     private GameOfLifeAgent GetAgent(int x, int y) {     
     74    private GameOfLifeAgent GetAgent(int x, int y) {
    7575      int width = Scenario.WidthParameter.Value.Value;
    7676      int height = Scenario.HeightParameter.Value.Value;
     
    7979        throw new Exception("invalid index");
    8080
    81       int index = x + y * width; 
     81      int index = x + y * width;
    8282
    8383      return Agents[index] as GameOfLifeAgent;
     
    8686    private void Initialize() {
    8787      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);
    9490      }
    9591    }
     
    10399    }
    104100
    105     void HeightParameter_ValueChanged(object sender, EventArgs e) {
    106       CreateAgents();
    107     }
    108 
    109     void WidthParameter_ValueChanged(object sender, EventArgs e) {
    110       CreateAgents();
    111     }
    112 
    113101    public IEnumerable<GameOfLifeAgent> GetNeighbors(GameOfLifeAgent agent) {
    114102      int width = Scenario.WidthParameter.Value.Value;
    115103      int height = Scenario.HeightParameter.Value.Value;
    116104      int index = Agents.IndexOf(agent);
    117      
     105
    118106      List<GameOfLifeAgent> result = new List<GameOfLifeAgent>();
    119107
     
    159147      int height = Scenario.HeightParameter.Value.Value;
    160148
    161       Random rand = new Random();
     149      var rand = new FastRandom();
    162150
    163151      for (int i = 0; i < width * height; i++) {
     
    190178
    191179      Results["Cells"].Value = cells;
    192      
     180
    193181      IntValue currentIterations = (Results["CurrentIterations"].Value as IntValue);
    194182      currentIterations.Value++;
     
    206194        Results.Add(new Result("CurrentIterations", new IntValue(0)));
    207195        Results.Add(new Result("Cells", new HeatMap(width, height)));
    208       }     
     196      }
    209197
    210198      for (int i = start; i < iterations; i++) {
    211199        cancellationToken.ThrowIfCancellationRequested();
    212        
     200
    213201        base.Step();
    214         UpdateResults();       
     202        UpdateResults();
    215203      }
    216204    }
  • branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/HeuristicLab.SimulationCore.Samples-3.3.csproj

    r10450 r10454  
    155155      <Private>False</Private>
    156156    </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>
    157161    <Reference Include="System" />
    158162    <Reference Include="System.Core" />
     
    164168  </ItemGroup>
    165169  <ItemGroup>
     170    <Compile Include="CardGameSimulation.cs" />
    166171    <Compile Include="GameOfLifeAgent.cs" />
    167172    <Compile Include="GameOfLifeDiscreteEventSimulation.cs" />
Note: See TracChangeset for help on using the changeset viewer.