Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/GameOfLifeAgent.cs @ 6739

Last change on this file since 6739 was 6623, checked in by svonolfe, 13 years ago

Added game of life simulation sample (#1610)

File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.SimulationCore.AgentBased;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Common;
30
31namespace HeuristicLab.SimulationCore.Samples {
32  [Item("GameOfLifeAgent", "A game of life agent.")]
33  [StorableClass]
34  public class GameOfLifeAgent: Item, IAgent {
35    [Storable]
36    private bool alive;
37    public bool Alive {
38      get {
39        return alive;
40      }
41      set {
42        alive = value;
43      }
44    }
45
46    [Storable]
47    private bool _alive;
48
49    [Storable]
50    private GameOfLifeSimulation simulation;
51    public GameOfLifeSimulation Simulation {
52      get {
53        return simulation;
54      }
55    }
56
57    public GameOfLifeAgent(GameOfLifeSimulation simulation) : base() {
58      this.simulation = simulation;
59      alive = _alive = false;
60    }
61    [StorableConstructor]
62    private GameOfLifeAgent(bool deserializing) : base(deserializing) { }
63    private GameOfLifeAgent(GameOfLifeAgent original, Cloner cloner)
64      : base(original, cloner) {
65      alive = original.alive;
66      _alive = original._alive;
67      simulation = original.simulation;
68    }
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new GameOfLifeAgent(this, cloner);
71    }
72
73    #region IAgent Members
74    public void InitStep() {
75    }
76
77    public void PerformStep() {
78      int aliveCount = 0;
79      foreach (GameOfLifeAgent neighbor in simulation.GetNeighbors(this)) {
80        if (neighbor.Alive)
81          aliveCount++;
82      }
83
84      if (!alive && aliveCount == 3)
85        _alive = true;
86      else if (alive && (aliveCount < 2 || aliveCount > 3))
87        _alive = false;
88    }
89
90    public void FinalizeStep() {
91      alive = _alive;
92    }
93    #endregion
94  }
95}
Note: See TracBrowser for help on using the repository browser.