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/3.3/DiscreteEvent/DiscreteEventSimulation.cs

    r10450 r10454  
    22using System.Collections.Generic;
    33using System.Linq;
     4using System.Text;
    45using System.Threading;
    56using HeuristicLab.Common;
     
    3233    }
    3334
     35    [StorableConstructor]
     36    protected DiscreteEventSimulation(bool deserializing) : base(deserializing) { }
    3437    protected DiscreteEventSimulation(DiscreteEventSimulation<TModel> original, Cloner cloner)
    3538      : base(original, cloner) {
     
    3942      Model = cloner.Clone(original.Model);
    4043    }
    41 
    4244    protected DiscreteEventSimulation() {
    4345      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();
    4452    }
    4553
     
    4755      CompositeEventQueue = new CompositeEventQueue<TModel>(Activities.Select(x => x.EventQueue));
    4856
    49       var actionMonitor = new Dictionary<Guid, List<IActivity<TModel>>>();
     57      var log = new StringBuilder();
     58
     59      var actionMonitor = new Dictionary<Type, List<IActivity<TModel>>>();
    5060      foreach (var activity in Activities) {
    51         foreach (var actionId in activity.MonitoredActionIds) {
     61        foreach (var actionId in activity.MonitoredActions) {
    5262          if (!actionMonitor.ContainsKey(actionId)) actionMonitor[actionId] = new List<IActivity<TModel>>();
    5363          actionMonitor[actionId].Add(activity);
     
    5565      }
    5666
    57       if (InitialAction != null) {
     67      if (CompositeEventQueue.Empty && InitialAction != null) {
    5868        InitialAction.Execute(Model);
    5969        CallActivities(actionMonitor, InitialAction);
     
    6272      while (!CompositeEventQueue.Empty && Model.CurrentTime < MaximumTime) {
    6373        var @event = CompositeEventQueue.Pop();
     74        if (@event.Time < Model.CurrentTime) continue;
     75        log.AppendLine(@event.Action.GetType().Name);
     76        Model.CurrentTime = @event.Time;
     77
    6478        var nextEvent = CompositeEventQueue.Empty ? null : CompositeEventQueue.Peek();
    6579        var action = @event.Action;
    6680
    6781        action.Execute(Model);
    68         CallActivities(actionMonitor, action);
    6982
    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 
     83        if (nextEvent == null || @event.Time < nextEvent.Time) {
    7884          foreach (var reporter in Reporters)
    7985            reporter.UpdateReporting(Model, Results);
    8086        }
     87
     88        CallActivities(actionMonitor, action);
     89
    8190        cancellationToken.ThrowIfCancellationRequested();
    8291      };
    8392    }
    8493
    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])
     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)])
    88102          activity.ManageEvents(Model, action);
    89103      }
Note: See TracChangeset for help on using the changeset viewer.