Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Optimization/3.3/Algorithms/HeuristicOptimizationAlgorithm.cs @ 15603

Last change on this file since 15603 was 15603, checked in by abeham, 7 years ago

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

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// A base class for heuristic optimization algorithms.
31  /// </summary>
32  [Item("Heuristic Optimization Algorithm", "A base class for heuristic optimization algorithms.")]
33  [StorableClass]
34  public abstract class HeuristicOptimizationAlgorithm : Algorithm {
35    public new IHeuristicOptimizationProblem Problem {
36      get { return (IHeuristicOptimizationProblem)base.Problem; }
37      set { base.Problem = value; }
38    }
39
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    }
62
63    [StorableConstructor]
64    protected HeuristicOptimizationAlgorithm(bool deserializing) : base(deserializing) { }
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    }
79
80    #region Events
81    protected override void DeregisterProblemEvents() {
82      Problem.SolutionCreatorChanged -= new EventHandler(Problem_SolutionCreatorChanged);
83      Problem.EvaluatorChanged -= new EventHandler(Problem_EvaluatorChanged);
84      base.DeregisterProblemEvents();
85    }
86    protected override void RegisterProblemEvents() {
87      base.RegisterProblemEvents();
88      Problem.SolutionCreatorChanged += new EventHandler(Problem_SolutionCreatorChanged);
89      Problem.EvaluatorChanged += new EventHandler(Problem_EvaluatorChanged);
90    }
91
92    protected virtual void Problem_SolutionCreatorChanged(object sender, EventArgs e) { }
93    protected virtual void Problem_EvaluatorChanged(object sender, EventArgs e) { }
94    #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    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.