Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OKB (trunk integration)/HeuristicLab.Clients.OKB/3.3/RunCreation/OKBAlgorithm.cs @ 5902

Last change on this file since 5902 was 5902, checked in by swagner, 13 years ago

Worked on OKB (#1174)

File size: 15.7 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.Collections.Generic;
24using System.Drawing;
25using System.IO;
26using System.Linq;
27using HeuristicLab.Collections;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Optimization;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.Persistence.Default.Xml;
33
34namespace HeuristicLab.Clients.OKB.RunCreation {
35  [Item("OKB Algorithm", "An algorithm which is stored in the OKB.")]
36  [Creatable("Optimization Knowledge Base (OKB)")]
37  [StorableClass]
38  public sealed class OKBAlgorithm : Item, IAlgorithm, IStorableContent {
39    public string Filename { get; set; }
40
41    private long algorithmId;
42    public long AlgorithmId {
43      get { return algorithmId; }
44    }
45    private IAlgorithm algorithm;
46    private IAlgorithm Algorithm {
47      get { return algorithm; }
48      set {
49        if (value == null) throw new ArgumentNullException("Algorithm", "Algorithm cannot be null.");
50        if (value != algorithm) {
51          CancelEventArgs<string> e = new CancelEventArgs<string>(value.Name);
52          OnNameChanging(e);
53          if (!e.Cancel) {
54            IProblem problem = algorithm.Problem;
55            DeregisterAlgorithmEvents();
56            algorithm = value;
57            RegisterAlgorithmEvents();
58
59            OnToStringChanged();
60            OnItemImageChanged();
61            OnNameChanged();
62            OnDescriptionChanged();
63            OnAlgorithmChanged();
64            OnStoreAlgorithmInEachRunChanged();
65
66            try {
67              algorithm.Problem = problem;
68            }
69            catch (ArgumentException) {
70              algorithm.Problem = null;
71            }
72            algorithm.Prepare(true);
73          }
74        }
75      }
76    }
77
78    public IEnumerable<IOptimizer> NestedOptimizers {
79      get {
80        // inner algorithm cannot be accessed directly
81        return Enumerable.Empty<IOptimizer>();
82      }
83    }
84
85    public override Image ItemImage {
86      get { return Algorithm.ItemImage; }
87    }
88
89    public string Name {
90      get { return Algorithm.Name; }
91      set { throw new NotSupportedException("Name cannot be changed."); }
92    }
93    public string Description {
94      get { return Algorithm.Description; }
95      set { throw new NotSupportedException("Description cannot be changed."); }
96    }
97    public bool CanChangeName {
98      get { return false; }
99    }
100    public bool CanChangeDescription {
101      get { return false; }
102    }
103
104    public IKeyedItemCollection<string, IParameter> Parameters {
105      get { return Algorithm.Parameters; }
106    }
107
108    public ExecutionState ExecutionState {
109      get { return Algorithm.ExecutionState; }
110    }
111    public TimeSpan ExecutionTime {
112      get { return Algorithm.ExecutionTime; }
113    }
114
115    public Type ProblemType {
116      get { return Algorithm.ProblemType; }
117    }
118    public IProblem Problem {
119      get { return Algorithm.Problem; }
120      set { Algorithm.Problem = value; }
121    }
122
123    public ResultCollection Results {
124      get { return Algorithm.Results; }
125    }
126
127    private RunCollection runs;
128    public RunCollection Runs {
129      get { return runs; }
130    }
131    private bool storeRunsAutomatically;
132    public bool StoreRunsAutomatically {
133      get { return storeRunsAutomatically; }
134      set {
135        if (value != storeRunsAutomatically) {
136          storeRunsAutomatically = value;
137          OnStoreRunsAutomaticallyChanged();
138        }
139      }
140    }
141    public bool StoreAlgorithmInEachRun {
142      get { return Algorithm.StoreAlgorithmInEachRun; }
143      set { Algorithm.StoreAlgorithmInEachRun = value; }
144    }
145
146    #region Persistence Properties
147    [Storable(Name = "AlgorithmId")]
148    private long StorableAlgorithmId {
149      get { return algorithmId; }
150      set { algorithmId = value; }
151    }
152    [Storable(Name = "Algorithm")]
153    private IAlgorithm StorableAlgorithm {
154      get { return algorithm; }
155      set {
156        algorithm = value;
157        RegisterAlgorithmEvents();
158      }
159    }
160    [Storable(Name = "Runs")]
161    private RunCollection StorableRuns {
162      get { return runs; }
163      set {
164        runs = value;
165        RegisterRunsEvents();
166      }
167    }
168    [Storable(Name = "StoreRunsAutomatically")]
169    private bool StorableStoreRunsAutomatically {
170      get { return storeRunsAutomatically; }
171      set { storeRunsAutomatically = value; }
172    }
173    #endregion
174
175    [StorableConstructor]
176    private OKBAlgorithm(bool deserializing) : base(deserializing) { }
177    private OKBAlgorithm(OKBAlgorithm original, Cloner cloner)
178      : base(original, cloner) {
179      algorithmId = original.algorithmId;
180      algorithm = cloner.Clone(original.algorithm);
181      RegisterAlgorithmEvents();
182      runs = cloner.Clone(original.runs);
183      storeRunsAutomatically = original.storeRunsAutomatically;
184      RegisterRunsEvents();
185    }
186    public OKBAlgorithm()
187      : base() {
188      algorithmId = -1;
189      algorithm = new EmptyAlgorithm("No algorithm selected. Please choose an algorithm from the OKB.");
190      RegisterAlgorithmEvents();
191      runs = new RunCollection();
192      storeRunsAutomatically = true;
193      RegisterRunsEvents();
194    }
195
196    public override IDeepCloneable Clone(Cloner cloner) {
197      return new OKBAlgorithm(this, cloner);
198    }
199
200    public void Load(long algorithmId) {
201      if (this.algorithmId != algorithmId) {
202        IAlgorithm algorithm;
203        byte[] algorithmData = RunCreationClient.GetAlgorithmData(algorithmId);
204        using (MemoryStream stream = new MemoryStream(algorithmData)) {
205          algorithm = XmlParser.Deserialize<IAlgorithm>(stream);
206        }
207        this.algorithmId = algorithmId;
208        Algorithm = algorithm;
209      }
210    }
211
212    public IAlgorithm CloneAlgorithm() {
213      return (IAlgorithm)Algorithm.Clone();
214    }
215
216    public void CollectParameterValues(IDictionary<string, IItem> values) {
217      Algorithm.CollectParameterValues(values);
218    }
219    public void CollectResultValues(IDictionary<string, IItem> values) {
220      Algorithm.CollectResultValues(values);
221    }
222
223    public void Prepare() {
224      Algorithm.Prepare();
225    }
226    public void Prepare(bool clearRuns) {
227      if (clearRuns) runs.Clear();
228      Algorithm.Prepare(clearRuns);
229    }
230    public void Start() {
231      Algorithm.Start();
232    }
233    public void Pause() {
234      Algorithm.Pause();
235    }
236    public void Stop() {
237      Algorithm.Stop();
238    }
239
240    #region Events
241    public event EventHandler AlgorithmChanged;
242    private void OnAlgorithmChanged() {
243      EventHandler handler = AlgorithmChanged;
244      if (handler != null) handler(this, EventArgs.Empty);
245    }
246    public event EventHandler<CancelEventArgs<string>> NameChanging;
247    private void OnNameChanging(CancelEventArgs<string> e) {
248      var handler = NameChanging;
249      if (handler != null) handler(this, e);
250    }
251    public event EventHandler NameChanged;
252    private void OnNameChanged() {
253      var handler = NameChanged;
254      if (handler != null) handler(this, EventArgs.Empty);
255    }
256    public event EventHandler DescriptionChanged;
257    private void OnDescriptionChanged() {
258      var handler = DescriptionChanged;
259      if (handler != null) handler(this, EventArgs.Empty);
260    }
261    public event EventHandler ExecutionStateChanged;
262    private void OnExecutionStateChanged() {
263      var handler = ExecutionStateChanged;
264      if (handler != null) handler(this, EventArgs.Empty);
265    }
266    public event EventHandler ExecutionTimeChanged;
267    private void OnExecutionTimeChanged() {
268      var handler = ExecutionTimeChanged;
269      if (handler != null) handler(this, EventArgs.Empty);
270    }
271    public event EventHandler ProblemChanged;
272    private void OnProblemChanged() {
273      var handler = ProblemChanged;
274      if (handler != null) handler(this, EventArgs.Empty);
275    }
276    public event EventHandler StoreRunsAutomaticallyChanged;
277    private void OnStoreRunsAutomaticallyChanged() {
278      var handler = StoreRunsAutomaticallyChanged;
279      if (handler != null) handler(this, EventArgs.Empty);
280    }
281    public event EventHandler StoreAlgorithmInEachRunChanged;
282    private void OnStoreAlgorithmInEachRunChanged() {
283      var handler = StoreAlgorithmInEachRunChanged;
284      if (handler != null) handler(this, EventArgs.Empty);
285    }
286    public event EventHandler Prepared;
287    private void OnPrepared() {
288      var handler = Prepared;
289      if (handler != null) handler(this, EventArgs.Empty);
290    }
291    public event EventHandler Started;
292    private void OnStarted() {
293      var handler = Started;
294      if (handler != null) handler(this, EventArgs.Empty);
295    }
296    public event EventHandler Paused;
297    private void OnPaused() {
298      var handler = Paused;
299      if (handler != null) handler(this, EventArgs.Empty);
300    }
301    public event EventHandler Stopped;
302    private void OnStopped() {
303      var handler = Stopped;
304      if (handler != null) handler(this, EventArgs.Empty);
305    }
306    public event EventHandler<EventArgs<Exception>> ExceptionOccurred;
307    private void OnExceptionOccurred(Exception exception) {
308      var handler = ExceptionOccurred;
309      if (handler != null) handler(this, new EventArgs<Exception>(exception));
310    }
311
312    private void RegisterAlgorithmEvents() {
313      if (Algorithm != null) {
314        Algorithm.ToStringChanged += new EventHandler(Algorithm_ToStringChanged);
315        Algorithm.ItemImageChanged += new EventHandler(Algorithm_ItemImageChanged);
316        Algorithm.NameChanging += new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
317        Algorithm.NameChanged += new EventHandler(Algorithm_NameChanged);
318        Algorithm.DescriptionChanged += new EventHandler(Algorithm_DescriptionChanged);
319        Algorithm.ExecutionStateChanged += new EventHandler(Algorithm_ExecutionStateChanged);
320        Algorithm.ExecutionTimeChanged += new EventHandler(Algorithm_ExecutionTimeChanged);
321        Algorithm.ProblemChanged += new EventHandler(Algorithm_ProblemChanged);
322        Algorithm.Runs.ItemsAdded += new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
323        Algorithm.StoreAlgorithmInEachRunChanged += new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
324        Algorithm.Prepared += new EventHandler(Algorithm_Prepared);
325        Algorithm.Started += new EventHandler(Algorithm_Started);
326        Algorithm.Paused += new EventHandler(Algorithm_Paused);
327        Algorithm.Stopped += new EventHandler(Algorithm_Stopped);
328        Algorithm.ExceptionOccurred += new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
329      }
330    }
331    private void DeregisterAlgorithmEvents() {
332      if (Algorithm != null) {
333        Algorithm.ToStringChanged -= new EventHandler(Algorithm_ToStringChanged);
334        Algorithm.ItemImageChanged -= new EventHandler(Algorithm_ItemImageChanged);
335        Algorithm.NameChanging -= new EventHandler<CancelEventArgs<string>>(Algorithm_NameChanging);
336        Algorithm.NameChanged -= new EventHandler(Algorithm_NameChanged);
337        Algorithm.DescriptionChanged -= new EventHandler(Algorithm_DescriptionChanged);
338        Algorithm.ExecutionStateChanged -= new EventHandler(Algorithm_ExecutionStateChanged);
339        Algorithm.ExecutionTimeChanged -= new EventHandler(Algorithm_ExecutionTimeChanged);
340        Algorithm.ProblemChanged -= new EventHandler(Algorithm_ProblemChanged);
341        Algorithm.Runs.ItemsAdded -= new CollectionItemsChangedEventHandler<IRun>(Algorithm_Runs_ItemsAdded);
342        Algorithm.StoreAlgorithmInEachRunChanged -= new EventHandler(Algorithm_StoreAlgorithmInEachRunChanged);
343        Algorithm.Prepared -= new EventHandler(Algorithm_Prepared);
344        Algorithm.Started -= new EventHandler(Algorithm_Started);
345        Algorithm.Paused -= new EventHandler(Algorithm_Paused);
346        Algorithm.Stopped -= new EventHandler(Algorithm_Stopped);
347        Algorithm.ExceptionOccurred -= new EventHandler<EventArgs<Exception>>(Algorithm_ExceptionOccurred);
348      }
349    }
350
351    private void Algorithm_ToStringChanged(object sender, EventArgs e) {
352      OnToStringChanged();
353    }
354    private void Algorithm_ItemImageChanged(object sender, EventArgs e) {
355      OnItemImageChanged();
356    }
357    private void Algorithm_NameChanging(object sender, CancelEventArgs<string> e) {
358      OnNameChanging(e);
359    }
360    private void Algorithm_NameChanged(object sender, EventArgs e) {
361      OnNameChanged();
362    }
363    private void Algorithm_DescriptionChanged(object sender, EventArgs e) {
364      OnDescriptionChanged();
365    }
366    private void Algorithm_ExecutionStateChanged(object sender, EventArgs e) {
367      OnExecutionStateChanged();
368    }
369    private void Algorithm_ExecutionTimeChanged(object sender, EventArgs e) {
370      OnExecutionTimeChanged();
371    }
372    private void Algorithm_ProblemChanged(object sender, EventArgs e) {
373      OnProblemChanged();
374    }
375    private void Algorithm_Runs_ItemsAdded(object sender, CollectionItemsChangedEventArgs<IRun> e) {
376      OKBProblem problem = Problem as OKBProblem;
377      foreach (IRun run in e.Items) {
378        if (problem != null) {
379          OKBRun okbRun = new OKBRun(AlgorithmId, problem.ProblemId, run);
380          runs.Add(okbRun);
381          if (StoreRunsAutomatically) {
382            try { okbRun.Store(); }
383            catch (Exception) { }
384          }
385        } else {
386          runs.Add(run);
387        }
388      }
389    }
390    private void Algorithm_StoreAlgorithmInEachRunChanged(object sender, EventArgs e) {
391      OnStoreAlgorithmInEachRunChanged();
392    }
393    private void Algorithm_Prepared(object sender, EventArgs e) {
394      OnPrepared();
395    }
396    private void Algorithm_Started(object sender, EventArgs e) {
397      OnStarted();
398    }
399    private void Algorithm_Paused(object sender, EventArgs e) {
400      OnPaused();
401    }
402    private void Algorithm_Stopped(object sender, EventArgs e) {
403      OnStopped();
404    }
405    private void Algorithm_ExceptionOccurred(object sender, EventArgs<Exception> e) {
406      OnExceptionOccurred(e.Value);
407    }
408
409    private void RegisterRunsEvents() {
410      runs.ItemsRemoved += new CollectionItemsChangedEventHandler<IRun>(runs_ItemsRemoved);
411      runs.CollectionReset += new CollectionItemsChangedEventHandler<IRun>(Runs_CollectionReset);
412    }
413    private void runs_ItemsRemoved(object sender, CollectionItemsChangedEventArgs<IRun> e) {
414      foreach (IRun run in e.Items) {
415        OKBRun okbRun = run as OKBRun;
416        if (okbRun != null)
417          Algorithm.Runs.Remove(okbRun.WrappedRun);
418        else
419          Algorithm.Runs.Remove(run);
420      }
421    }
422    private void Runs_CollectionReset(object sender, CollectionItemsChangedEventArgs<IRun> e) {
423      foreach (IRun run in e.OldItems) {
424        OKBRun okbRun = run as OKBRun;
425        if (okbRun != null)
426          Algorithm.Runs.Remove(okbRun.WrappedRun);
427        else
428          Algorithm.Runs.Remove(run);
429      }
430    }
431    #endregion
432  }
433}
Note: See TracBrowser for help on using the repository browser.