Free cookie consent management tool by TermsFeed Policy Generator

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

#1610: updated core, implemented card game sample

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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    }
Note: See TracChangeset for help on using the changeset viewer.