Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.SimulationCore.Samples/3.3/SimpleSimulation.cs @ 8114

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

Added game of life simulation sample (#1610)

File size: 2.6 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.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Parameters;
30using HeuristicLab.Data;
31using HeuristicLab.Optimization;
32using System.Threading;
33
34namespace HeuristicLab.SimulationCore.Samples {
35  [Item("SimpleSimulation", "A simple simulation.")]
36  [Creatable("Simulations")]
37  [StorableClass]
38  public sealed class SimpleSimulation: Simulation {
39    public override Type ProblemType {
40      get { return typeof(SimpleScenario); }
41    }
42
43    private SimpleScenario Scenario {
44      get {
45        return Problem as SimpleScenario;
46      }
47    }
48   
49    public ValueParameter<IntValue> IterationsParameter {
50      get { return (ValueParameter<IntValue>)Parameters["Iterations"]; }
51    }
52   
53    public SimpleSimulation() : base() {
54      Parameters.Add(new ValueParameter<IntValue>("Iterations", "The number of iterations.", new IntValue(100)));
55    }
56    [StorableConstructor]
57    private SimpleSimulation(bool deserializing) : base(deserializing) { }
58    private SimpleSimulation(SimpleSimulation original, Cloner cloner)
59      : base(original, cloner) {
60    }
61    public override IDeepCloneable Clone(Cloner cloner) {
62      return new SimpleSimulation(this, cloner);
63    }
64
65    protected override void Run(CancellationToken cancellationToken) {
66      IntValue counter = new IntValue(Scenario.InitialValueParameter.Value.Value);
67      Results.Add(new Result("Count", counter));
68
69      int iterations = IterationsParameter.Value.Value;
70      for (int i = 0; i < iterations; i++) {
71        counter.Value++;
72        System.Threading.Thread.Sleep(250);
73      }
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.