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, 15 years ago

Enabled saving only for some specific items (#1193)

File size: 4.6 KB
RevLine 
[3260]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
[3280]22using System;
[3260]23using System.Collections.Generic;
[4068]24using System.Drawing;
[3376]25using HeuristicLab.Common;
[3260]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]
[4419]35  public sealed class Run : NamedItem, IRun, IStorableContent {
36    public string Filename { get; set; }
37
[4156]38    [StorableConstructor]
39    private Run(bool deserializing) : base(deserializing) { }
[3260]40    public Run()
[3280]41      : base() {
42      name = ItemName;
43      description = ItemDescription;
[4156]44      color = Color.Black;
[3280]45      algorithm = null;
[3260]46      parameters = new Dictionary<string, IItem>();
47      results = new Dictionary<string, IItem>();
48    }
[3280]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;
[4156]54      color = Color.Black;
[4102]55      Initialize(algorithm);
[3280]56    }
57    public Run(string name, IAlgorithm algorithm)
[3260]58      : base(name) {
[3280]59      if (algorithm == null) throw new ArgumentNullException();
[4156]60      color = Color.Black;
[3280]61      description = ItemDescription;
[4102]62      Initialize(algorithm);
[3260]63    }
[3280]64    public Run(string name, string description, IAlgorithm algorithm)
[3260]65      : base(name, description) {
[3280]66      if (algorithm == null) throw new ArgumentNullException();
[4156]67      color = Color.Black;
[4102]68      Initialize(algorithm);
[3280]69    }
70
71    private void Initialize(IAlgorithm algorithm) {
[4102]72      IAlgorithm clone = (IAlgorithm)algorithm.Clone();
[3260]73      parameters = new Dictionary<string, IItem>();
74      results = new Dictionary<string, IItem>();
[4102]75      clone.CollectParameterValues(parameters);
76      clone.CollectResultValues(results);
77      if (clone.StoreAlgorithmInEachRun) {
78        clone.Prepare(true);
79        this.algorithm = clone;
80      }
[3260]81    }
[4156]82    [StorableHook(HookType.AfterDeserialization)]
83    private void AfterDeserializationHook() {
84      if (color == Color.Empty) color = Color.Black;
85    }
86
[4200]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
[3260]131    public override IDeepCloneable Clone(Cloner cloner) {
[3280]132      Run clone = (Run)base.Clone(cloner);
[4161]133      clone.color = this.color;
[3280]134      clone.algorithm = (IAlgorithm)cloner.Clone(algorithm);
[3260]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.