Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Optimization/3.3/Run.cs @ 17203

Last change on this file since 17203 was 16956, checked in by abeham, 6 years ago

#2457: merged trunk into branch

File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.ComponentModel;
25using System.Drawing;
26using HeuristicLab.Collections;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HEAL.Attic;
30
31namespace HeuristicLab.Optimization {
32  /// <summary>
33  /// Represents the parameters and results of an algorithm run.
34  /// </summary>
35  [Item("Run", "The parameters and results of an algorithm run.")]
36  [StorableType("C06974F6-2516-492F-9AE9-517AED845260")]
37  public sealed class Run : NamedItem, IRun, IStorableContent {
38    public string Filename { get; set; }
39
40    [StorableConstructor]
41    private Run(StorableConstructorFlag _) : base(_) { }
42    private Run(Run original, Cloner cloner)
43      : base(original, cloner) {
44      color = original.color;
45      algorithm = cloner.Clone(original.algorithm);
46
47      parameters = new ObservableDictionary<string, IItem>();
48      foreach (string key in original.parameters.Keys)
49        parameters.Add(key, cloner.Clone(original.parameters[key]));
50
51      results = new ObservableDictionary<string, IItem>();
52      foreach (string key in original.results.Keys)
53        results.Add(key, cloner.Clone(original.results[key]));
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new Run(this, cloner);
58    }
59
60    public Run()
61      : base() {
62      name = ItemName;
63      description = ItemDescription;
64      color = Color.Black;
65      algorithm = null;
66      parameters = new ObservableDictionary<string, IItem>();
67      results = new ObservableDictionary<string, IItem>();
68    }
69    public Run(IAlgorithm algorithm)
70      : base() {
71      if (algorithm == null) throw new ArgumentNullException();
72      name = algorithm.Name + " Run (" + algorithm.ExecutionTime.ToString() + ")";
73      description = ItemDescription;
74      color = Color.Black;
75      Initialize(algorithm);
76    }
77    public Run(string name, IAlgorithm algorithm)
78      : base(name) {
79      if (algorithm == null) throw new ArgumentNullException();
80      color = Color.Black;
81      description = ItemDescription;
82      Initialize(algorithm);
83    }
84    public Run(string name, string description, IAlgorithm algorithm)
85      : base(name, description) {
86      if (algorithm == null) throw new ArgumentNullException();
87      color = Color.Black;
88      Initialize(algorithm);
89    }
90
91    private void Initialize(IAlgorithm algorithm) {
92      parameters = new ObservableDictionary<string, IItem>();
93      results = new ObservableDictionary<string, IItem>();
94
95      if (algorithm.StoreAlgorithmInEachRun) {
96        var clone = (IAlgorithm)algorithm.Clone();
97        clone.CollectParameterValues(parameters);
98        clone.CollectResultValues(results);
99        clone.Runs.Clear();
100        this.algorithm = clone;
101      } else {
102        var par = new Dictionary<string, IItem>();
103        var res = new Dictionary<string, IItem>();
104        algorithm.CollectParameterValues(par);
105        algorithm.CollectResultValues(res);
106        var cloner = new Cloner();
107        foreach (var k in par) parameters.Add(k.Key, cloner.Clone(k.Value));
108        foreach (var k in res) results.Add(k.Key, cloner.Clone(k.Value));
109      }
110    }
111    [StorableHook(HookType.AfterDeserialization)]
112    private void AfterDeserialization() {
113      if (color == Color.Empty) color = Color.Black;
114    }
115
116    [Storable]
117    private IAlgorithm algorithm;
118    public IAlgorithm Algorithm {
119      get { return algorithm; }
120    }
121
122    [Storable(Name = "parameters")]
123    private IDictionary<string, IItem> StorableParameters {
124      get { return parameters; }
125      set {
126        if (!(value is IObservableDictionary<string, IItem>))
127          parameters = new ObservableDictionary<string, IItem>(value);
128        else parameters = (IObservableDictionary<string, IItem>)value;
129      }
130    }
131    private IObservableDictionary<string, IItem> parameters;
132    public IObservableDictionary<string, IItem> Parameters {
133      get { return parameters; }
134      private set {
135        if (parameters != value) {
136          parameters = value;
137          OnPropertyChanged("Parameters");
138        }
139      }
140    }
141
142    [Storable(Name = "results")]
143    private IDictionary<string, IItem> StorableResults {
144      get { return results; }
145      set {
146        if (!(value is IObservableDictionary<string, IItem>))
147          results = new ObservableDictionary<string, IItem>(value);
148        else results = (IObservableDictionary<string, IItem>)value;
149      }
150    }
151    private IObservableDictionary<string, IItem> results;
152    public IObservableDictionary<string, IItem> Results {
153      get { return results; }
154      private set {
155        if (results != value) {
156          results = value;
157          OnPropertyChanged("Results");
158        }
159      }
160    }
161
162    [Storable]
163    private Color color;
164    public Color Color {
165      get { return this.color; }
166      set {
167        if (color != value) {
168          this.color = value;
169          OnPropertyChanged("Color");
170        }
171      }
172    }
173    private bool visible = true;
174    public bool Visible {
175      get { return this.visible; }
176      set {
177        if (visible != value) {
178          this.visible = value;
179          OnPropertyChanged("Visible");
180        }
181      }
182    }
183
184    public event PropertyChangedEventHandler PropertyChanged;
185    private void OnPropertyChanged(string property) {
186      var handler = PropertyChanged;
187      if (handler != null) handler(this, new PropertyChangedEventArgs(property));
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.