Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Optimization/3.3/Run.cs @ 9503

Last change on this file since 9503 was 9456, checked in by swagner, 11 years ago

Updated copyright year and added some missing license headers (#1889)

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