Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10186 was 9456, checked in by swagner, 12 years ago

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

File size: 5.2 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[9456]3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3260]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
[3280]22using System;
[3260]23using System.Collections.Generic;
[4068]24using System.Drawing;
[4825]25using System.Linq;
[3376]26using HeuristicLab.Common;
[3260]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]
[4419]36  public sealed class Run : NamedItem, IRun, IStorableContent {
37    public string Filename { get; set; }
38
[4156]39    [StorableConstructor]
40    private Run(bool deserializing) : base(deserializing) { }
[4722]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
[3260]58    public Run()
[3280]59      : base() {
60      name = ItemName;
61      description = ItemDescription;
[4156]62      color = Color.Black;
[3280]63      algorithm = null;
[3260]64      parameters = new Dictionary<string, IItem>();
65      results = new Dictionary<string, IItem>();
66    }
[3280]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;
[4156]72      color = Color.Black;
[4102]73      Initialize(algorithm);
[3280]74    }
75    public Run(string name, IAlgorithm algorithm)
[3260]76      : base(name) {
[3280]77      if (algorithm == null) throw new ArgumentNullException();
[4156]78      color = Color.Black;
[3280]79      description = ItemDescription;
[4102]80      Initialize(algorithm);
[3260]81    }
[3280]82    public Run(string name, string description, IAlgorithm algorithm)
[3260]83      : base(name, description) {
[3280]84      if (algorithm == null) throw new ArgumentNullException();
[4156]85      color = Color.Black;
[4102]86      Initialize(algorithm);
[3280]87    }
88
89    private void Initialize(IAlgorithm algorithm) {
[3260]90      parameters = new Dictionary<string, IItem>();
91      results = new Dictionary<string, IItem>();
[4825]92
93      if (algorithm.StoreAlgorithmInEachRun) {
94        IAlgorithm clone = (IAlgorithm)algorithm.Clone();
95        clone.CollectParameterValues(parameters);
96        clone.CollectResultValues(results);
[5195]97        clone.Runs.Clear();
[4102]98        this.algorithm = clone;
[4825]99      } else {
100        algorithm.CollectParameterValues(parameters);
101        algorithm.CollectResultValues(results);
102        Cloner cloner = new Cloner();
[4833]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);
[4102]105      }
[3260]106    }
[4156]107    [StorableHook(HookType.AfterDeserialization)]
[4722]108    private void AfterDeserialization() {
[4156]109      if (color == Color.Empty) color = Color.Black;
110    }
111
[4200]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    }
[3260]155  }
156}
Note: See TracBrowser for help on using the repository browser.