Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
02/13/14 17:28:30 (11 years ago)
Author:
abeham
Message:

#1610: updated core, implemented card game sample

Location:
branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent
Files:
1 added
1 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Action.cs

    r10450 r10454  
    66  [StorableClass]
    77  public abstract class Action<TModel> : IAction<TModel> where TModel : IModel {
    8     public abstract Guid ActionId { get; }
    98    public IActivity<TModel> Mandate { get; protected set; }
    109
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Activity.cs

    r10450 r10454  
    1515    }
    1616
    17     public virtual IEnumerable<Guid> MonitoredActionIds {
    18       get { return new Guid[0]; }
     17    public virtual IEnumerable<Type> MonitoredActions {
     18      get { return new Type[0]; }
    1919    }
    2020
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/CompositeEventQueue.cs

    r10450 r10454  
    4141    }
    4242
     43    public void Clear() {
     44      foreach (var queue in queues) {
     45        queue.Clear();
     46      }
     47    }
     48
    4349    [StorableConstructor]
    4450    private CompositeEventQueue(bool deserializing) : base(deserializing) { }
  • 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      }
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Event.cs

    r10450 r10454  
    1111
    1212    #region Storable Properties
    13     [Storable]
    1413    // ReSharper disable UnusedMember.Local
    1514    // ReSharper disable UnusedParameter.Local
     15    [Storable]
    1616    private double StorableTime {
    1717      get { return time; }
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Interfaces/IAction.cs

    r10450 r10454  
    1 using System;
    2 using HeuristicLab.Common;
     1using HeuristicLab.Common;
    32
    43namespace HeuristicLab.SimulationCore {
    54  public interface IAction<TModel> : IDeepCloneable, IContent where TModel : IModel {
    6     Guid ActionId { get; }
    75    IActivity<TModel> Mandate { get; }
    86
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Interfaces/IActivity.cs

    r10450 r10454  
    77    where TModel : IModel {
    88    IEventQueue<TModel> EventQueue { get; }
    9     IEnumerable<Guid> MonitoredActionIds { get; }
     9    IEnumerable<Type> MonitoredActions { get; }
    1010
    1111    void ManageEvents(TModel model, IAction<TModel> lastAction);
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/Interfaces/IEventQueue.cs

    r10450 r10454  
    88    IEvent<TModel> Peek();
    99    IEvent<TModel> Pop();
    10     void Push(IEvent<TModel> @event);
     10    void Push(double time, IAction<TModel> action);
    1111
    1212    IEvent<TModel> ChangeTime(IEvent<TModel> @event, double newTime);
  • branches/SimulationCore/HeuristicLab.SimulationCore/3.3/DiscreteEvent/SortedListEventQueue.cs

    r10450 r10454  
    2626    }
    2727
    28     public void Push(IEvent<TModel> @event) {
    29       if (queue.ContainsKey(@event.Time))
    30         queue[@event.Time].Add(@event);
    31       else queue.Add(@event.Time, new List<IEvent<TModel>>() { @event });
     28    public void Push(double time, IAction<TModel> action) {
     29      PushEvent(time, action);
     30    }
     31
     32    private IEvent<TModel> PushEvent(double time, IAction<TModel> action) {
     33      IEvent<TModel> result = new Event<TModel>(time, action);
     34      if (queue.ContainsKey(time))
     35        queue[time].Add(result);
     36      else queue.Add(time, new List<IEvent<TModel>>() { result });
     37      return result;
    3238    }
    3339
    3440    public IEvent<TModel> ChangeTime(IEvent<TModel> @event, double newTime) {
    3541      Remove(@event);
    36       var newEvent = new Event<TModel>(newTime, @event.Action);
    37       Push(newEvent);
    38       return newEvent;
     42      return PushEvent(newTime, @event.Action);
    3943    }
    4044
Note: See TracChangeset for help on using the changeset viewer.