Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4440 was 4419, checked in by swagner, 14 years ago

Enabled saving only for some specific items (#1193)

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