Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1610: Added a base infrastructure for discrete event simulation

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