Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Parameter-less Population Pyramid/HeuristicLab.Algorithms.ParameterlessPopulationPyramid/3.3/AlgorithmBase.cs @ 11733

Last change on this file since 11733 was 11635, checked in by mkommend, 10 years ago

#2282: Implemented base classes for algorithm and problems for PPP.

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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 System.Threading.Tasks;
25using HeuristicLab.Common;
26using HeuristicLab.Optimization;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Algorithms.ParameterlessPopulationPyramid {
30  [StorableClass]
31  public abstract class AlgorithmBase : Algorithm, IStorableContent {
32    public string Filename { get; set; }
33
34    #region Properties
35    public override Type ProblemType {
36      get { return typeof(BinaryVectorProblem); }
37    }
38    public new BinaryVectorProblem Problem {
39      get { return (BinaryVectorProblem)base.Problem; }
40      set { base.Problem = value; }
41    }
42    [Storable]
43    private ResultCollection results;
44    public override ResultCollection Results {
45      get { return results; }
46    }
47    #endregion
48
49    private DateTime lastUpdateTime;
50
51    [StorableConstructor]
52    protected AlgorithmBase(bool deserializing) : base(deserializing) { }
53    protected AlgorithmBase(AlgorithmBase original, Cloner cloner)
54      : base(original, cloner) {
55      results = cloner.Clone(original.Results);
56    }
57    public AlgorithmBase()
58      : base() {
59      results = new ResultCollection();
60    }
61
62    public override void Prepare() {
63      if (Problem != null) base.Prepare();
64      results.Clear();
65      OnPrepared();
66    }
67
68    public override void Start() {
69      base.Start();
70      var cancellationTokenSource = new CancellationTokenSource();
71
72      OnStarted();
73      Task task = Task.Factory.StartNew(Run, cancellationTokenSource.Token, cancellationTokenSource.Token);
74      task.ContinueWith(t => {
75        try {
76          t.Wait();
77        }
78        catch (AggregateException ex) {
79          try {
80            ex.Flatten().Handle(x => x is OperationCanceledException);
81          }
82          catch (AggregateException remaining) {
83            if (remaining.InnerExceptions.Count == 1) OnExceptionOccurred(remaining.InnerExceptions[0]);
84            else OnExceptionOccurred(remaining);
85          }
86        }
87        cancellationTokenSource.Dispose();
88        cancellationTokenSource = null;
89        OnStopped();
90      });
91    }
92    private void Run(object state) {
93      CancellationToken cancellationToken = (CancellationToken)state;
94      lastUpdateTime = DateTime.UtcNow;
95      System.Timers.Timer timer = new System.Timers.Timer(250);
96      timer.AutoReset = true;
97      timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
98      timer.Start();
99      try {
100        Run();
101      }
102      finally {
103        timer.Elapsed -= new System.Timers.ElapsedEventHandler(timer_Elapsed);
104        timer.Stop();
105        ExecutionTime += DateTime.UtcNow - lastUpdateTime;
106      }
107
108      cancellationToken.ThrowIfCancellationRequested();
109    }
110    protected abstract void Run();
111    #region Events
112    protected override void OnProblemChanged() {
113      Problem.Reset += new EventHandler(Problem_Reset);
114      base.OnProblemChanged();
115    }
116    private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {
117      System.Timers.Timer timer = (System.Timers.Timer)sender;
118      timer.Enabled = false;
119      DateTime now = DateTime.UtcNow;
120      ExecutionTime += now - lastUpdateTime;
121      lastUpdateTime = now;
122      timer.Enabled = true;
123    }
124    #endregion
125
126  }
127}
Note: See TracBrowser for help on using the repository browser.