Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/DiscreteEventSimulation.cs @ 10454

Last change on this file since 10454 was 10454, checked in by abeham, 10 years ago

#1610: updated core, implemented card game sample

File size: 4.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading;
6using HeuristicLab.Common;
7using HeuristicLab.Core;
8using HeuristicLab.Data;
9using HeuristicLab.Parameters;
10using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
11
12namespace HeuristicLab.SimulationCore {
13  [StorableClass]
14  public abstract class DiscreteEventSimulation<TModel> : Simulation, IDiscreteEventSimulation<TModel> where TModel : class, IModel {
15    [Storable]
16    public IAction<TModel> InitialAction { get; protected set; }
17    [Storable]
18    public IEnumerable<IActivity<TModel>> Activities { get; protected set; }
19    [Storable]
20    public IEnumerable<IReporter<TModel>> Reporters { get; protected set; }
21    [Storable]
22    public TModel Model { get; protected set; }
23
24    protected CompositeEventQueue<TModel> CompositeEventQueue { get; private set; }
25
26    protected IFixedValueParameter<DoubleValue> MaximumTimeParameter {
27      get { return (IFixedValueParameter<DoubleValue>)Parameters["MaximumTime"]; }
28    }
29
30    public double MaximumTime {
31      get { return MaximumTimeParameter.Value.Value; }
32      set { MaximumTimeParameter.Value.Value = value; }
33    }
34
35    [StorableConstructor]
36    protected DiscreteEventSimulation(bool deserializing) : base(deserializing) { }
37    protected DiscreteEventSimulation(DiscreteEventSimulation<TModel> original, Cloner cloner)
38      : base(original, cloner) {
39      InitialAction = cloner.Clone(original.InitialAction);
40      Activities = original.Activities.Select(cloner.Clone).ToList();
41      Reporters = original.Reporters.Select(cloner.Clone).ToList();
42      Model = cloner.Clone(original.Model);
43    }
44    protected DiscreteEventSimulation() {
45      Parameters.Add(new FixedValueParameter<DoubleValue>("MaximumTime", "The simulation stops after reaching this time.", new DoubleValue(1000)));
46    }
47
48    protected override void OnPrepared() {
49      base.OnPrepared();
50      if (CompositeEventQueue != null)
51        CompositeEventQueue.Clear();
52    }
53
54    protected override void Run(CancellationToken cancellationToken) {
55      CompositeEventQueue = new CompositeEventQueue<TModel>(Activities.Select(x => x.EventQueue));
56
57      var log = new StringBuilder();
58
59      var actionMonitor = new Dictionary<Type, List<IActivity<TModel>>>();
60      foreach (var activity in Activities) {
61        foreach (var actionId in activity.MonitoredActions) {
62          if (!actionMonitor.ContainsKey(actionId)) actionMonitor[actionId] = new List<IActivity<TModel>>();
63          actionMonitor[actionId].Add(activity);
64        }
65      }
66
67      if (CompositeEventQueue.Empty && InitialAction != null) {
68        InitialAction.Execute(Model);
69        CallActivities(actionMonitor, InitialAction);
70      }
71
72      while (!CompositeEventQueue.Empty && Model.CurrentTime < MaximumTime) {
73        var @event = CompositeEventQueue.Pop();
74        if (@event.Time < Model.CurrentTime) continue;
75        log.AppendLine(@event.Action.GetType().Name);
76        Model.CurrentTime = @event.Time;
77
78        var nextEvent = CompositeEventQueue.Empty ? null : CompositeEventQueue.Peek();
79        var action = @event.Action;
80
81        action.Execute(Model);
82
83        if (nextEvent == null || @event.Time < nextEvent.Time) {
84          foreach (var reporter in Reporters)
85            reporter.UpdateReporting(Model, Results);
86        }
87
88        CallActivities(actionMonitor, action);
89
90        cancellationToken.ThrowIfCancellationRequested();
91      };
92    }
93
94    private void CallActivities(Dictionary<Type, List<IActivity<TModel>>> actionMonitor, IAction<TModel> action) {
95      if (actionMonitor.ContainsKey(action.GetType())) {
96        foreach (var activity in actionMonitor[action.GetType()])
97          activity.ManageEvents(Model, action);
98      }
99
100      if (actionMonitor.ContainsKey(typeof(object))) {
101        foreach (var activity in actionMonitor[typeof(object)])
102          activity.ManageEvents(Model, action);
103      }
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.