Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/FixedDataAnalysisAlgorithm.cs @ 6409

Last change on this file since 6409 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

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