Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB/HeuristicLab.Clients.OKB-3.3/OKBExperiment.cs @ 5586

Last change on this file since 5586 was 5289, checked in by swagner, 14 years ago

Renamed VS2008ImageLibrary to VSImageLibrary (#1174)

File size: 11.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Drawing;
24using System.IO;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Persistence.Default.Xml;
30
31namespace HeuristicLab.Clients.OKB {
32  [Item("OKB Experiment", "...")]
33  [Creatable("OKB")]
34  [StorableClass]
35  public sealed class OKBExperiment : NamedItem, IOptimizer, IStorableContent {
36    public string Filename { get; set; }
37
38    public override Image ItemImage {
39      get {
40        if (Algorithm != null) return Algorithm.ItemImage;
41        else return HeuristicLab.Common.Resources.VSImageLibrary.Event;
42      }
43    }
44
45    private long algorithmId;
46    public long AlgorithmId {
47      get { return algorithmId; }
48      private set {
49        if (algorithmId != value) {
50          algorithmId = value;
51          OnAlgorithmIdChanged();
52        }
53      }
54    }
55    private IAlgorithm algorithm;
56    private IAlgorithm Algorithm {
57      get { return algorithm; }
58      set {
59        DeregisterAlgorithmEvents();
60        algorithm = value;
61        RegisterAlgorithmEvents();
62        if ((algorithm != null) && (problem != null)) {
63          algorithm.Problem = problem;
64          algorithm.Prepare(true);
65        }
66      }
67    }
68    private long problemId;
69    public long ProblemId {
70      get { return problemId; }
71      private set {
72        if (problemId != value) {
73          problemId = value;
74          OnProblemIdChanged();
75        }
76      }
77    }
78    private IProblem problem;
79    private IProblem Problem {
80      get { return problem; }
81      set {
82        problem = value;
83        if ((algorithm != null) && (problem != null)) {
84          algorithm.Problem = problem;
85          algorithm.Prepare(true);
86        }
87      }
88    }
89
90    public ExecutionState ExecutionState {
91      get {
92        if ((Algorithm != null) && (Problem != null)) return Algorithm.ExecutionState;
93        else return ExecutionState.Stopped;
94      }
95    }
96    public TimeSpan ExecutionTime {
97      get {
98        if (Algorithm != null) return Algorithm.ExecutionTime;
99        else return TimeSpan.Zero;
100      }
101    }
102    public IKeyedItemCollection<string, IParameter> AlgorithmParameters {
103      get {
104        if (Algorithm != null) return Algorithm.Parameters;
105        else return null;
106      }
107    }
108    public IKeyedItemCollection<string, IParameter> ProblemParameters {
109      get {
110        if (Problem != null) return Problem.Parameters;
111        else return null;
112      }
113    }
114    public ResultCollection Results {
115      get {
116        if (Algorithm != null) return Algorithm.Results;
117        else return null;
118      }
119    }
120    public RunCollection Runs {
121      get {
122        if (Algorithm != null) return Algorithm.Runs;
123        else return null;
124      }
125    }
126
127    #region Persistence Properties
128    [Storable(Name = "AlgorithmId")]
129    private long AlgorithmIdPersistence {
130      get { return algorithmId; }
131      set { algorithmId = value; }
132    }
133    [Storable(Name = "Algorithm")]
134    private IAlgorithm AlgorithmPersistence {
135      get { return Algorithm; }
136      set { Algorithm = value; }
137    }
138    [Storable(Name = "ProblemId")]
139    private long ProblemIdPersistence {
140      get { return problemId; }
141      set { problemId = value; }
142    }
143    [Storable(Name = "Problem")]
144    private IProblem ProblemPersistence {
145      get { return Problem; }
146      set { Problem = value; }
147    }
148    #endregion
149
150    [StorableConstructor]
151    private OKBExperiment(bool deserializing) : base(deserializing) { }
152    private OKBExperiment(OKBExperiment original, Cloner cloner)
153      : base(original, cloner) {
154      algorithmId = original.algorithmId;
155      algorithm = cloner.Clone(original.algorithm);
156      problemId = original.problemId;
157      problem = cloner.Clone(original.problem);
158      RegisterAlgorithmEvents();
159    }
160    public OKBExperiment()
161      : base() {
162      name = ItemName;
163      description = ItemDescription;
164    }
165
166    [StorableHook(HookType.AfterDeserialization)]
167    private void AfterDeserialization() {
168      RegisterAlgorithmEvents();
169    }
170
171    public override IDeepCloneable Clone(Cloner cloner) {
172      return new OKBExperiment(this, cloner);
173    }
174
175    public void Load(Algorithm algorithm) {
176      if (algorithm == null) {
177        Algorithm = null;
178        AlgorithmId = 0;
179      } else {
180        try {
181          if (AlgorithmId != algorithm.Id) {
182            AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(algorithm.Id);
183            using (MemoryStream stream = new MemoryStream(algorithmData.Data)) {
184              Algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
185              Algorithm.StoreAlgorithmInEachRun = true;
186            }
187          }
188          AlgorithmId = algorithm.Id;
189        }
190        catch (Exception ex) {
191          Algorithm = null;
192          AlgorithmId = 0;
193          OnExceptionOccurred(ex);
194        }
195      }
196    }
197    public void Load(Problem problem) {
198      if (problem == null) {
199        Problem = null;
200        ProblemId = 0;
201      } else {
202        try {
203          if (ProblemId != problem.Id) {
204            ProblemData problemData = OKBClient.Instance.GetProblemData(problem.Id);
205            using (MemoryStream stream = new MemoryStream(problemData.Data)) {
206              Problem = XmlParser.Deserialize<IProblem>(stream);
207            }
208          }
209          ProblemId = problem.Id;
210        }
211        catch (Exception ex) {
212          Problem = null;
213          ProblemId = 0;
214          OnExceptionOccurred(ex);
215        }
216      }
217    }
218
219    public void Prepare(bool clearRuns) {
220      if (Algorithm != null) Algorithm.Prepare(clearRuns);
221    }
222    public void Prepare() {
223      if (Algorithm != null) Algorithm.Prepare();
224    }
225    public void Start() {
226      if (Algorithm != null) Algorithm.Start();
227    }
228    public void Pause() {
229      if (Algorithm != null) Algorithm.Pause();
230    }
231    public void Stop() {
232      if (Algorithm != null) Algorithm.Stop();
233    }
234
235    #region Events
236    public event EventHandler AlgorithmIdChanged;
237    private void OnAlgorithmIdChanged() {
238      EventHandler handler = AlgorithmIdChanged;
239      if (handler != null) handler(this, EventArgs.Empty);
240    }
241    public event EventHandler ProblemIdChanged;
242    private void OnProblemIdChanged() {
243      EventHandler handler = ProblemIdChanged;
244      if (handler != null) handler(this, EventArgs.Empty);
245    }
246    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
247    private void OnExceptionOccurred(Exception exception) {
248      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
249      if (handler != null) handler(this, new EventArgs<Exception>(exception));
250    }
251    public event EventHandler ExecutionStateChanged;
252    private void OnExecutionStateChanged() {
253      EventHandler handler = ExecutionStateChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
255    }
256    public event EventHandler ExecutionTimeChanged;
257    private void OnExecutionTimeChanged() {
258      EventHandler handler = ExecutionTimeChanged;
259      if (handler != null) handler(this, EventArgs.Empty);
260    }
261    public event EventHandler Prepared;
262    private void OnPrepared() {
263      EventHandler handler = Prepared;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266    public event EventHandler Started;
267    private void OnStarted() {
268      EventHandler handler = Started;
269      if (handler != null) handler(this, EventArgs.Empty);
270    }
271    public event EventHandler Paused;
272    private void OnPaused() {
273      EventHandler handler = Paused;
274      if (handler != null) handler(this, EventArgs.Empty);
275    }
276    public event EventHandler Stopped;
277    private void OnStopped() {
278      EventHandler handler = Stopped;
279      if (handler != null) handler(this, EventArgs.Empty);
280    }
281
282    private void RegisterAlgorithmEvents() {
283      if (Algorithm != null) {
284        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
285        Algorithm.ExecutionStateChanged += new EventHandler(algorithm_ExecutionStateChanged);
286        Algorithm.ExecutionTimeChanged += new EventHandler(algorithm_ExecutionTimeChanged);
287        Algorithm.ItemImageChanged += new EventHandler(algorithm_ItemImageChanged);
288        Algorithm.Paused += new EventHandler(algorithm_Paused);
289        Algorithm.Prepared += new EventHandler(algorithm_Prepared);
290        Algorithm.Started += new EventHandler(algorithm_Started);
291        Algorithm.Stopped += new EventHandler(algorithm_Stopped);
292      }
293    }
294    private void DeregisterAlgorithmEvents() {
295      if (Algorithm != null) {
296        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
297        Algorithm.ExecutionStateChanged -= new EventHandler(algorithm_ExecutionStateChanged);
298        Algorithm.ExecutionTimeChanged -= new EventHandler(algorithm_ExecutionTimeChanged);
299        Algorithm.ItemImageChanged -= new EventHandler(algorithm_ItemImageChanged);
300        Algorithm.Paused -= new EventHandler(algorithm_Paused);
301        Algorithm.Prepared -= new EventHandler(algorithm_Prepared);
302        Algorithm.Started -= new EventHandler(algorithm_Started);
303        Algorithm.Stopped -= new EventHandler(algorithm_Stopped);
304      }
305    }
306    private void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
307      OnExceptionOccurred(e.Value);
308    }
309    private void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
310      OnExecutionStateChanged();
311    }
312    private void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
313      OnExecutionTimeChanged();
314    }
315    private void algorithm_ItemImageChanged(object sender, EventArgs e) {
316      OnItemImageChanged();
317    }
318    private void algorithm_Paused(object sender, EventArgs e) {
319      OnPaused();
320    }
321    private void algorithm_Prepared(object sender, EventArgs e) {
322      OnPrepared();
323    }
324    private void algorithm_Started(object sender, EventArgs e) {
325      OnStarted();
326    }
327    private void algorithm_Stopped(object sender, EventArgs e) {
328      OnStopped();
329
330      try {
331        OKBClient.Instance.AddRun(AlgorithmId, ProblemId, algorithm);
332      }
333      catch (Exception ex) {
334        OnExceptionOccurred(ex);
335      }
336    }
337    #endregion
338  }
339}
Note: See TracBrowser for help on using the repository browser.