Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4585 was 4566, checked in by swagner, 14 years ago

Worked on OKB (#1174)

File size: 11.1 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.VS2008ImageLibrary.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    public OKBExperiment()
151      : base() {
152      name = ItemName;
153      description = ItemDescription;
154    }
155    [StorableConstructor]
156    private OKBExperiment(bool deserializing) : base(deserializing) { }
157
158    [StorableHook(HookType.AfterDeserialization)]
159    private void Initialize() {
160      RegisterAlgorithmEvents();
161    }
162
163    public void Load(Algorithm algorithm) {
164      if (algorithm == null) {
165        Algorithm = null;
166        AlgorithmId = 0;
167      } else {
168        try {
169          if (AlgorithmId != algorithm.Id) {
170            AlgorithmData algorithmData = OKBClient.Instance.GetAlgorithmData(algorithm.Id);
171            using (MemoryStream stream = new MemoryStream(algorithmData.Data)) {
172              Algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
173              Algorithm.StoreAlgorithmInEachRun = true;
174            }
175          }
176          AlgorithmId = algorithm.Id;
177        }
178        catch (Exception ex) {
179          Algorithm = null;
180          AlgorithmId = 0;
181          OnExceptionOccurred(ex);
182        }
183      }
184    }
185    public void Load(Problem problem) {
186      if (problem == null) {
187        Problem = null;
188        ProblemId = 0;
189      } else {
190        try {
191          if (ProblemId != problem.Id) {
192            ProblemData problemData = OKBClient.Instance.GetProblemData(problem.Id);
193            using (MemoryStream stream = new MemoryStream(problemData.Data)) {
194              Problem = XmlParser.Deserialize<IProblem>(stream);
195            }
196          }
197          ProblemId = problem.Id;
198        }
199        catch (Exception ex) {
200          Problem = null;
201          ProblemId = 0;
202          OnExceptionOccurred(ex);
203        }
204      }
205    }
206
207    public void Prepare(bool clearRuns) {
208      if (Algorithm != null) Algorithm.Prepare(clearRuns);
209    }
210    public void Prepare() {
211      if (Algorithm != null) Algorithm.Prepare();
212    }
213    public void Start() {
214      if (Algorithm != null) Algorithm.Start();
215    }
216    public void Pause() {
217      if (Algorithm != null) Algorithm.Pause();
218    }
219    public void Stop() {
220      if (Algorithm != null) Algorithm.Stop();
221    }
222
223    #region Events
224    public event EventHandler AlgorithmIdChanged;
225    private void OnAlgorithmIdChanged() {
226      EventHandler handler = AlgorithmIdChanged;
227      if (handler != null) handler(this, EventArgs.Empty);
228    }
229    public event EventHandler ProblemIdChanged;
230    private void OnProblemIdChanged() {
231      EventHandler handler = ProblemIdChanged;
232      if (handler != null) handler(this, EventArgs.Empty);
233    }
234    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
235    private void OnExceptionOccurred(Exception exception) {
236      EventHandler<EventArgs<Exception>> handler = ExceptionOccurred;
237      if (handler != null) handler(this, new EventArgs<Exception>(exception));
238    }
239    public event EventHandler ExecutionStateChanged;
240    private void OnExecutionStateChanged() {
241      EventHandler handler = ExecutionStateChanged;
242      if (handler != null) handler(this, EventArgs.Empty);
243    }
244    public event EventHandler ExecutionTimeChanged;
245    private void OnExecutionTimeChanged() {
246      EventHandler handler = ExecutionTimeChanged;
247      if (handler != null) handler(this, EventArgs.Empty);
248    }
249    public event EventHandler Prepared;
250    private void OnPrepared() {
251      EventHandler handler = Prepared;
252      if (handler != null) handler(this, EventArgs.Empty);
253    }
254    public event EventHandler Started;
255    private void OnStarted() {
256      EventHandler handler = Started;
257      if (handler != null) handler(this, EventArgs.Empty);
258    }
259    public event EventHandler Paused;
260    private void OnPaused() {
261      EventHandler handler = Paused;
262      if (handler != null) handler(this, EventArgs.Empty);
263    }
264    public event EventHandler Stopped;
265    private void OnStopped() {
266      EventHandler handler = Stopped;
267      if (handler != null) handler(this, EventArgs.Empty);
268    }
269
270    private void RegisterAlgorithmEvents() {
271      if (Algorithm != null) {
272        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
273        Algorithm.ExecutionStateChanged += new EventHandler(algorithm_ExecutionStateChanged);
274        Algorithm.ExecutionTimeChanged += new EventHandler(algorithm_ExecutionTimeChanged);
275        Algorithm.ItemImageChanged += new EventHandler(algorithm_ItemImageChanged);
276        Algorithm.Paused += new EventHandler(algorithm_Paused);
277        Algorithm.Prepared += new EventHandler(algorithm_Prepared);
278        Algorithm.Started += new EventHandler(algorithm_Started);
279        Algorithm.Stopped += new EventHandler(algorithm_Stopped);
280        Algorithm.Runs.ItemsAdded += new Collections.CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
281      }
282    }
283    private void DeregisterAlgorithmEvents() {
284      if (Algorithm != null) {
285        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(algorithm_ExceptionOccurred);
286        Algorithm.ExecutionStateChanged -= new EventHandler(algorithm_ExecutionStateChanged);
287        Algorithm.ExecutionTimeChanged -= new EventHandler(algorithm_ExecutionTimeChanged);
288        Algorithm.ItemImageChanged -= new EventHandler(algorithm_ItemImageChanged);
289        Algorithm.Paused -= new EventHandler(algorithm_Paused);
290        Algorithm.Prepared -= new EventHandler(algorithm_Prepared);
291        Algorithm.Started -= new EventHandler(algorithm_Started);
292        Algorithm.Stopped -= new EventHandler(algorithm_Stopped);
293        Algorithm.Runs.ItemsAdded -= new Collections.CollectionItemsChangedEventHandler<IRun>(Runs_ItemsAdded);
294      }
295    }
296    private void algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
297      OnExceptionOccurred(e.Value);
298    }
299    private void algorithm_ExecutionStateChanged(object sender, EventArgs e) {
300      OnExecutionStateChanged();
301    }
302    private void algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
303      OnExecutionTimeChanged();
304    }
305    private void algorithm_ItemImageChanged(object sender, EventArgs e) {
306      OnItemImageChanged();
307    }
308    private void algorithm_Paused(object sender, EventArgs e) {
309      OnPaused();
310    }
311    private void algorithm_Prepared(object sender, EventArgs e) {
312      OnPrepared();
313    }
314    private void algorithm_Started(object sender, EventArgs e) {
315      OnStarted();
316    }
317    private void algorithm_Stopped(object sender, EventArgs e) {
318      OnStopped();
319    }
320    private void Runs_ItemsAdded(object sender, Collections.CollectionItemsChangedEventArgs<IRun> e) {
321      try {
322        foreach (Run run in e.Items)
323          OKBClient.Instance.Store(AlgorithmId, ProblemId, run);
324      }
325      catch (Exception ex) {
326        OnExceptionOccurred(ex);
327      }
328    }
329    #endregion
330  }
331}
Note: See TracBrowser for help on using the repository browser.