Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Optimization/3.3/Run.cs @ 5134

Last change on this file since 5134 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

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