Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/12/18 13:33:23 (7 years ago)
Author:
abeham
Message:

#1614: Implemented changed behavior to measure execution time (cf. #2869)

Location:
branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/Algorithm.cs

    r15287 r15603  
    6464      }
    6565    }
    66 
    67     [Storable]
    68     private TimeSpan executionTime;
    69     public TimeSpan ExecutionTime {
    70       get { return executionTime; }
    71       protected set {
    72         executionTime = value;
    73         OnExecutionTimeChanged();
    74       }
    75     }
     66   
     67    public abstract TimeSpan ExecutionTime { get; }
    7668
    7769    public virtual Type ProblemType {
     
    133125      : base() {
    134126      executionState = ExecutionState.Stopped;
    135       executionTime = TimeSpan.Zero;
    136127      storeAlgorithmInEachRun = false;
    137128      runsCounter = 0;
     
    141132      : base(name) {
    142133      executionState = ExecutionState.Stopped;
    143       executionTime = TimeSpan.Zero;
    144134      storeAlgorithmInEachRun = false;
    145135      runsCounter = 0;
     
    149139      : base(name, parameters) {
    150140      executionState = ExecutionState.Stopped;
    151       executionTime = TimeSpan.Zero;
    152141      storeAlgorithmInEachRun = false;
    153142      runsCounter = 0;
     
    157146      : base(name, description) {
    158147      executionState = ExecutionState.Stopped;
    159       executionTime = TimeSpan.Zero;
    160148      storeAlgorithmInEachRun = false;
    161149      runsCounter = 0;
     
    165153      : base(name, description, parameters) {
    166154      executionState = ExecutionState.Stopped;
    167       executionTime = TimeSpan.Zero;
    168155      storeAlgorithmInEachRun = false;
    169156      runsCounter = 0;
     
    181168      if (ExecutionState == ExecutionState.Started) throw new InvalidOperationException(string.Format("Clone not allowed in execution state \"{0}\".", ExecutionState));
    182169      executionState = original.executionState;
    183       executionTime = original.executionTime;
    184170      problem = cloner.Clone(original.problem);
    185171      storeAlgorithmInEachRun = original.storeAlgorithmInEachRun;
     
    259245      if (handler != null) handler(this, EventArgs.Empty);
    260246    }
    261     public event EventHandler ExecutionTimeChanged;
    262     protected virtual void OnExecutionTimeChanged() {
    263       EventHandler handler = ExecutionTimeChanged;
    264       if (handler != null) handler(this, EventArgs.Empty);
    265     }
    266247    public event EventHandler ProblemChanged;
    267248    protected virtual void OnProblemChanged() {
     
    276257    public event EventHandler Prepared;
    277258    protected virtual void OnPrepared() {
    278       ExecutionTime = TimeSpan.Zero;
    279259      foreach (IStatefulItem statefulObject in this.GetObjectGraphObjects(new HashSet<object>() { Runs }).OfType<IStatefulItem>()) {
    280260        statefulObject.InitializeState();
     
    347327    }
    348328    #endregion
     329
     330    [Obsolete("Deprecate, does nothing, needs to be removed from IExecutable")]
     331    public event EventHandler ExecutionTimeChanged;
    349332  }
    350333}
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/BasicAlgorithm.cs

    r15367 r15603  
    2121
    2222using System;
    23 using System.Linq;
    2423using System.Threading;
    2524using HeuristicLab.Common;
     
    3231
    3332    private bool pausePending;
    34     private DateTime lastUpdateTime;
     33    private bool executionTimeActive;
    3534
    3635    public string Filename { get; set; }
     
    5251    }
    5352
     53    [Storable]
     54    private TimeSpan executionTime;
     55    private DateTime startDate;
     56    public override TimeSpan ExecutionTime {
     57      get { return executionTime + (executionTimeActive ? DateTime.UtcNow - startDate : TimeSpan.Zero); }
     58    }
     59
    5460    [StorableConstructor]
    5561    protected BasicAlgorithm(bool deserializing) : base(deserializing) { }
     
    5864      results = cloner.Clone(original.Results);
    5965      initialized = original.initialized;
     66      executionTime = original.executionTime;
    6067    }
    6168    protected BasicAlgorithm()
    6269      : base() {
    6370      results = new ResultCollection();
     71      executionTime = TimeSpan.Zero;
    6472    }
    6573
    6674    public override void Prepare() {
     75      executionTime = TimeSpan.Zero;
    6776      if (Problem == null) return;
    6877      base.Prepare();
     
    7382
    7483    public override void Start(CancellationToken cancellationToken) {
     84      startDate = DateTime.UtcNow;
     85      executionTimeActive = true;
    7586      base.Start(cancellationToken);
    7687      CancellationTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
     
    117128    private void Run(object state) {
    118129      CancellationToken cancellationToken = (CancellationToken)state;
    119       lastUpdateTime = DateTime.UtcNow;
    120       System.Timers.Timer timer = new System.Timers.Timer(250);
    121       timer.AutoReset = true;
    122       timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
    123       timer.Start();
    124       try {
    125         if (!initialized)
    126           Initialize(cancellationToken);
    127         initialized = true;
    128         Run(cancellationToken);
    129       }
    130       finally {
    131         timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
    132         timer.Stop();
    133         ExecutionTime += DateTime.UtcNow - lastUpdateTime;
    134       }
     130      if (!initialized)
     131        Initialize(cancellationToken);
     132      initialized = true;
     133      Run(cancellationToken);
    135134    }
    136135
     
    138137    protected abstract void Run(CancellationToken cancellationToken);
    139138
    140     #region Events
    141     private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
    142       System.Timers.Timer timer = (System.Timers.Timer)sender;
    143       timer.Enabled = false;
    144       DateTime now = DateTime.UtcNow;
    145       ExecutionTime += now - lastUpdateTime;
    146       lastUpdateTime = now;
    147       timer.Enabled = true;
     139    protected override void OnPaused() {
     140      executionTime += DateTime.UtcNow - startDate;
     141      executionTimeActive = false;
     142      base.OnPaused();
    148143    }
    149     #endregion
    150144
     145    protected override void OnStopped() {
     146      if (executionTimeActive) { // don't do if going from pause to stop
     147        executionTime += DateTime.UtcNow - startDate;
     148        executionTimeActive = false;
     149      }
     150      base.OnStopped();
     151    }
    151152  }
    152153}
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/EngineAlgorithm.cs

    r15287 r15603  
    7171    }
    7272
     73    public override TimeSpan ExecutionTime {
     74      get { return engine.ExecutionTime; }
     75    }
     76
    7377    public override ResultCollection Results {
    7478      get {
     
    195199    private void RegisterEngineEvents() {
    196200      Engine.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
    197       Engine.ExecutionTimeChanged += new EventHandler(Engine_ExecutionTimeChanged);
    198201      Engine.Paused += new EventHandler(Engine_Paused);
    199202      Engine.Prepared += new EventHandler(Engine_Prepared);
     
    203206    private void DeregisterEngineEvents() {
    204207      Engine.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Engine_ExceptionOccurred);
    205       Engine.ExecutionTimeChanged -= new EventHandler(Engine_ExecutionTimeChanged);
    206208      Engine.Paused -= new EventHandler(Engine_Paused);
    207209      Engine.Prepared -= new EventHandler(Engine_Prepared);
     
    211213    private void Engine_ExceptionOccurred(object sender, EventArgs<Exception> e) {
    212214      OnExceptionOccurred(e.Value);
    213     }
    214     private void Engine_ExecutionTimeChanged(object sender, EventArgs e) {
    215       ExecutionTime = Engine.ExecutionTime;
    216215    }
    217216    private void Engine_Paused(object sender, EventArgs e) {
  • branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs

    r14185 r15603  
    2121
    2222using System;
     23using System.Threading;
    2324using HeuristicLab.Common;
    2425using HeuristicLab.Core;
     
    3738    }
    3839
    39     protected HeuristicOptimizationAlgorithm() : base() { }
    40     protected HeuristicOptimizationAlgorithm(string name) : base(name) { }
    41     protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) { }
    42     protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) { }
    43     protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) { }
     40    [Storable]
     41    private TimeSpan executionTime;
     42    private DateTime startDate;
     43    public override TimeSpan ExecutionTime {
     44      get { return executionTime + (ExecutionState == ExecutionState.Started ? DateTime.UtcNow - startDate : TimeSpan.Zero); }
     45    }
     46
     47    protected HeuristicOptimizationAlgorithm() : base() {
     48      executionTime = TimeSpan.Zero;
     49    }
     50    protected HeuristicOptimizationAlgorithm(string name) : base(name) {
     51      executionTime = TimeSpan.Zero;
     52    }
     53    protected HeuristicOptimizationAlgorithm(string name, ParameterCollection parameters) : base(name, parameters) {
     54      executionTime = TimeSpan.Zero;
     55    }
     56    protected HeuristicOptimizationAlgorithm(string name, string description) : base(name, description) {
     57      executionTime = TimeSpan.Zero;
     58    }
     59    protected HeuristicOptimizationAlgorithm(string name, string description, ParameterCollection parameters) : base(name, description, parameters) {
     60      executionTime = TimeSpan.Zero;
     61    }
    4462
    4563    [StorableConstructor]
    4664    protected HeuristicOptimizationAlgorithm(bool deserializing) : base(deserializing) { }
    47     protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner) : base(original, cloner) { }
     65    protected HeuristicOptimizationAlgorithm(HeuristicOptimizationAlgorithm original, Cloner cloner)
     66      : base(original, cloner) {
     67      executionTime = original.executionTime;
     68    }
     69
     70    public override void Prepare() {
     71      executionTime = TimeSpan.Zero;
     72      base.Prepare();
     73    }
     74
     75    public override void Start(CancellationToken cancellationToken) {
     76      startDate = DateTime.UtcNow;
     77      base.Start(cancellationToken);
     78    }
    4879
    4980    #region Events
     
    6293    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
    6394    #endregion
     95
     96    protected override void OnPaused() {
     97      var elapsed = DateTime.UtcNow - startDate;
     98      executionTime += elapsed;
     99      base.OnPaused();
     100    }
     101
     102    protected override void OnStopped() {
     103      if (ExecutionState == ExecutionState.Started) {
     104        var elapsed = DateTime.UtcNow - startDate;
     105        executionTime += elapsed;
     106      }
     107      base.OnStopped();
     108    }
    64109  }
    65110}
Note: See TracChangeset for help on using the changeset viewer.