Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Optimization/3.3/Run.cs @ 4156

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

Persisted the color of a run (ticket #1125)

File size: 4.5 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.Collections.Generic;
24using System.Drawing;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Optimization {
30  /// <summary>
31  /// Represents the parameters and results of an algorithm run.
32  /// </summary>
33  [Item("Run", "The parameters and results of an algorithm run.")]
34  [StorableClass]
35  public sealed class Run : NamedItem, IRun {
36    [Storable]
37    private IAlgorithm algorithm;
38    public IAlgorithm Algorithm {
39      get { return algorithm; }
40    }
41    [Storable]
42    private Dictionary<string, IItem> parameters;
43    public IDictionary<string, IItem> Parameters {
44      get { return parameters; }
45    }
46    [Storable]
47    private Dictionary<string, IItem> results;
48    public IDictionary<string, IItem> Results {
49      get { return results; }
50    }
51
52    [Storable]
53    private Color color;
54    public Color Color {
55      get { return this.color; }
56      set {
57        if (color != value) {
58          this.color = value;
59          this.OnChanged();
60        }
61      }
62    }
63    private bool visible = true;
64    public bool Visible {
65      get { return this.visible; }
66      set {
67        if (visible != value) {
68          this.visible = value;
69          this.OnChanged();
70        }
71      }
72    }
73    public event EventHandler Changed;
74    private void OnChanged() {
75      EventHandler handler = Changed;
76      if (handler != null)
77        handler(this, EventArgs.Empty);
78    }
79
80    [StorableConstructor]
81    private Run(bool deserializing) : base(deserializing) { }
82    public Run()
83      : base() {
84      name = ItemName;
85      description = ItemDescription;
86      color = Color.Black;
87      algorithm = null;
88      parameters = new Dictionary<string, IItem>();
89      results = new Dictionary<string, IItem>();
90    }
91    public Run(IAlgorithm algorithm)
92      : base() {
93      if (algorithm == null) throw new ArgumentNullException();
94      name = algorithm.Name + " Run (" + algorithm.ExecutionTime.ToString() + ")";
95      description = ItemDescription;
96      color = Color.Black;
97      Initialize(algorithm);
98    }
99    public Run(string name, IAlgorithm algorithm)
100      : base(name) {
101      if (algorithm == null) throw new ArgumentNullException();
102      color = Color.Black;
103      description = ItemDescription;
104      Initialize(algorithm);
105    }
106    public Run(string name, string description, IAlgorithm algorithm)
107      : base(name, description) {
108      if (algorithm == null) throw new ArgumentNullException();
109      color = Color.Black;
110      Initialize(algorithm);
111    }
112
113    private void Initialize(IAlgorithm algorithm) {
114      IAlgorithm clone = (IAlgorithm)algorithm.Clone();
115      parameters = new Dictionary<string, IItem>();
116      results = new Dictionary<string, IItem>();
117      clone.CollectParameterValues(parameters);
118      clone.CollectResultValues(results);
119      if (clone.StoreAlgorithmInEachRun) {
120        clone.Prepare(true);
121        this.algorithm = clone;
122      }
123    }
124
125    [StorableHook(HookType.AfterDeserialization)]
126    private void AfterDeserializationHook() {
127      if (color == Color.Empty) color = Color.Black;
128    }
129
130    public override IDeepCloneable Clone(Cloner cloner) {
131      Run clone = (Run)base.Clone(cloner);
132      clone.algorithm = (IAlgorithm)cloner.Clone(algorithm);
133      foreach (string key in parameters.Keys)
134        clone.parameters.Add(key, (IItem)cloner.Clone(parameters[key]));
135      foreach (string key in results.Keys)
136        clone.results.Add(key, (IItem)cloner.Clone(results[key]));
137      return clone;
138    }
139  }
140}
Note: See TracBrowser for help on using the repository browser.