Free cookie consent management tool by TermsFeed Policy Generator

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

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

Renamed VS2008ImageLibrary to VSImageLibrary (#1174)

File size: 11.2 KB
RevLine 
[4548]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
[4549]22using System;
23using System.Drawing;
24using System.IO;
25using HeuristicLab.Common;
[4548]26using HeuristicLab.Core;
27using HeuristicLab.Optimization;
[4549]28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Persistence.Default.Xml;
[4548]30
31namespace HeuristicLab.Clients.OKB {
[4549]32  [Item("OKB Experiment", "...")]
[4548]33  [Creatable("OKB")]
[4549]34  [StorableClass]
[4548]35  public sealed class OKBExperiment : NamedItem, IOptimizer, IStorableContent {
36    public string Filename { get; set; }
37
38    public override Image ItemImage {
39      get {
[4549]40        if (Algorithm != null) return Algorithm.ItemImage;
[5289]41        else return HeuristicLab.Common.Resources.VSImageLibrary.Event;
[4548]42      }
43    }
44
[4558]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    }
[4548]55    private IAlgorithm algorithm;
56    private IAlgorithm Algorithm {
57      get { return algorithm; }
[4549]58      set {
59        DeregisterAlgorithmEvents();
60        algorithm = value;
61        RegisterAlgorithmEvents();
[4558]62        if ((algorithm != null) && (problem != null)) {
63          algorithm.Problem = problem;
64          algorithm.Prepare(true);
65        }
[4549]66      }
[4548]67    }
[4558]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    }
[4548]78    private IProblem problem;
[4558]79    private IProblem Problem {
[4548]80      get { return problem; }
[4558]81      set {
[4549]82        problem = value;
[4558]83        if ((algorithm != null) && (problem != null)) {
84          algorithm.Problem = problem;
85          algorithm.Prepare(true);
86        }
[4549]87      }
[4548]88    }
89
[4553]90    public ExecutionState ExecutionState {
91      get {
[4558]92        if ((Algorithm != null) && (Problem != null)) return Algorithm.ExecutionState;
[4553]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    }
[4558]102    public IKeyedItemCollection<string, IParameter> AlgorithmParameters {
[4549]103      get {
104        if (Algorithm != null) return Algorithm.Parameters;
105        else return null;
106      }
[4548]107    }
[4558]108    public IKeyedItemCollection<string, IParameter> ProblemParameters {
109      get {
110        if (Problem != null) return Problem.Parameters;
111        else return null;
112      }
113    }
[4549]114    public ResultCollection Results {
115      get {
116        if (Algorithm != null) return Algorithm.Results;
117        else return null;
118      }
[4548]119    }
[4549]120    public RunCollection Runs {
121      get {
122        if (Algorithm != null) return Algorithm.Runs;
123        else return null;
124      }
[4548]125    }
126
[4558]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
[4918]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    }
[4549]160    public OKBExperiment()
161      : base() {
162      name = ItemName;
163      description = ItemDescription;
[4548]164    }
[4558]165
166    [StorableHook(HookType.AfterDeserialization)]
[4918]167    private void AfterDeserialization() {
[4553]168      RegisterAlgorithmEvents();
169    }
[4548]170
[4918]171    public override IDeepCloneable Clone(Cloner cloner) {
172      return new OKBExperiment(this, cloner);
173    }
174
[4553]175    public void Load(Algorithm algorithm) {
176      if (algorithm == null) {
[4558]177        Algorithm = null;
[4553]178        AlgorithmId = 0;
179      } else {
[4558]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);
[4566]185              Algorithm.StoreAlgorithmInEachRun = true;
[4558]186            }
187          }
188          AlgorithmId = algorithm.Id;
[4553]189        }
[4558]190        catch (Exception ex) {
191          Algorithm = null;
192          AlgorithmId = 0;
193          OnExceptionOccurred(ex);
194        }
[4549]195      }
[4548]196    }
[4553]197    public void Load(Problem problem) {
198      if (problem == null) {
[4558]199        Problem = null;
[4553]200        ProblemId = 0;
201      } else {
[4558]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;
[4553]210        }
[4558]211        catch (Exception ex) {
212          Problem = null;
213          ProblemId = 0;
214          OnExceptionOccurred(ex);
215        }
[4549]216      }
[4548]217    }
218
[4549]219    public void Prepare(bool clearRuns) {
220      if (Algorithm != null) Algorithm.Prepare(clearRuns);
221    }
[4548]222    public void Prepare() {
[4553]223      if (Algorithm != null) Algorithm.Prepare();
[4548]224    }
225    public void Start() {
[4553]226      if (Algorithm != null) Algorithm.Start();
[4548]227    }
[4553]228    public void Pause() {
229      if (Algorithm != null) Algorithm.Pause();
230    }
[4548]231    public void Stop() {
[4553]232      if (Algorithm != null) Algorithm.Stop();
[4548]233    }
234
[4549]235    #region Events
[4558]236    public event EventHandler AlgorithmIdChanged;
237    private void OnAlgorithmIdChanged() {
238      EventHandler handler = AlgorithmIdChanged;
[4549]239      if (handler != null) handler(this, EventArgs.Empty);
240    }
[4558]241    public event EventHandler ProblemIdChanged;
242    private void OnProblemIdChanged() {
243      EventHandler handler = ProblemIdChanged;
244      if (handler != null) handler(this, EventArgs.Empty);
245    }
[4549]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    }
[4548]281
[4549]282    private void RegisterAlgorithmEvents() {
283      if (Algorithm != null) {
[4558]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);
[4549]292      }
293    }
294    private void DeregisterAlgorithmEvents() {
295      if (Algorithm != null) {
[4558]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);
[4549]304      }
305    }
[4558]306    private void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
[4549]307      OnExceptionOccurred(e.Value);
308    }
[4558]309    private void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
[4549]310      OnExecutionStateChanged();
311    }
[4558]312    private void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
[4549]313      OnExecutionTimeChanged();
314    }
[4558]315    private void algorithm_ItemImageChanged(object sender, EventArgs e) {
[4549]316      OnItemImageChanged();
317    }
[4558]318    private void algorithm_Paused(object sender, EventArgs e) {
[4549]319      OnPaused();
320    }
[4558]321    private void algorithm_Prepared(object sender, EventArgs e) {
[4549]322      OnPrepared();
323    }
[4558]324    private void algorithm_Started(object sender, EventArgs e) {
[4549]325      OnStarted();
326    }
[4558]327    private void algorithm_Stopped(object sender, EventArgs e) {
[4549]328      OnStopped();
[4929]329
[4559]330      try {
[4929]331        OKBClient.Instance.AddRun(AlgorithmId, ProblemId, algorithm);
[4559]332      }
333      catch (Exception ex) {
334        OnExceptionOccurred(ex);
335      }
336    }
[4548]337    #endregion
338  }
339}
Note: See TracBrowser for help on using the repository browser.