Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Optimization/3.3/Run.cs @ 17717

Last change on this file since 17717 was 16728, checked in by abeham, 6 years ago

#1614: updated to new persistence and .NET 4.6.1

File size: 6.3 KB
RevLine 
[3260]1#region License Information
2/* HeuristicLab
[16728]3 * Copyright (C) 2002-2019 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;
[11344]24using System.ComponentModel;
[4068]25using System.Drawing;
[11344]26using HeuristicLab.Collections;
[3376]27using HeuristicLab.Common;
[3260]28using HeuristicLab.Core;
[16728]29using HEAL.Attic;
[3260]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.")]
[16728]36  [StorableType("C06974F6-2516-492F-9AE9-517AED845260")]
[4419]37  public sealed class Run : NamedItem, IRun, IStorableContent {
38    public string Filename { get; set; }
39
[4156]40    [StorableConstructor]
[16728]41    private Run(StorableConstructorFlag _) : base(_) { }
[4722]42    private Run(Run original, Cloner cloner)
43      : base(original, cloner) {
44      color = original.color;
45      algorithm = cloner.Clone(original.algorithm);
46
[11344]47      parameters = new ObservableDictionary<string, IItem>();
[4722]48      foreach (string key in original.parameters.Keys)
49        parameters.Add(key, cloner.Clone(original.parameters[key]));
50
[11344]51      results = new ObservableDictionary<string, IItem>();
[4722]52      foreach (string key in original.results.Keys)
53        results.Add(key, cloner.Clone(original.results[key]));
54    }
[11344]55
[4722]56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new Run(this, cloner);
58    }
59
[3260]60    public Run()
[3280]61      : base() {
62      name = ItemName;
63      description = ItemDescription;
[4156]64      color = Color.Black;
[3280]65      algorithm = null;
[11344]66      parameters = new ObservableDictionary<string, IItem>();
67      results = new ObservableDictionary<string, IItem>();
[3260]68    }
[3280]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;
[4156]74      color = Color.Black;
[4102]75      Initialize(algorithm);
[3280]76    }
77    public Run(string name, IAlgorithm algorithm)
[3260]78      : base(name) {
[3280]79      if (algorithm == null) throw new ArgumentNullException();
[4156]80      color = Color.Black;
[3280]81      description = ItemDescription;
[4102]82      Initialize(algorithm);
[3260]83    }
[3280]84    public Run(string name, string description, IAlgorithm algorithm)
[3260]85      : base(name, description) {
[3280]86      if (algorithm == null) throw new ArgumentNullException();
[4156]87      color = Color.Black;
[4102]88      Initialize(algorithm);
[3280]89    }
90
91    private void Initialize(IAlgorithm algorithm) {
[11344]92      parameters = new ObservableDictionary<string, IItem>();
93      results = new ObservableDictionary<string, IItem>();
[4825]94
95      if (algorithm.StoreAlgorithmInEachRun) {
[11344]96        var clone = (IAlgorithm)algorithm.Clone();
[4825]97        clone.CollectParameterValues(parameters);
98        clone.CollectResultValues(results);
[5195]99        clone.Runs.Clear();
[4102]100        this.algorithm = clone;
[4825]101      } else {
[11344]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));
[4102]109      }
[3260]110    }
[4156]111    [StorableHook(HookType.AfterDeserialization)]
[4722]112    private void AfterDeserialization() {
[4156]113      if (color == Color.Empty) color = Color.Black;
114    }
115
[4200]116    [Storable]
117    private IAlgorithm algorithm;
118    public IAlgorithm Algorithm {
119      get { return algorithm; }
120    }
[11344]121
122    [Storable(Name = "parameters")]
123    private IDictionary<string, IItem> StorableParameters {
[4200]124      get { return parameters; }
[11344]125      set {
126        if (!(value is IObservableDictionary<string, IItem>))
127          parameters = new ObservableDictionary<string, IItem>(value);
128        else parameters = (IObservableDictionary<string, IItem>)value;
129      }
[4200]130    }
[11344]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 {
[4200]144      get { return results; }
[11344]145      set {
146        if (!(value is IObservableDictionary<string, IItem>))
147          results = new ObservableDictionary<string, IItem>(value);
148        else results = (IObservableDictionary<string, IItem>)value;
149      }
[4200]150    }
[11344]151    private IObservableDictionary<string, IItem> results;
152    public IObservableDictionary<string, IItem> Results {
153      get { return results; }
[11419]154      private set {
[11344]155        if (results != value) {
156          results = value;
157          OnPropertyChanged("Results");
158        }
159      }
160    }
[4200]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;
[11344]169          OnPropertyChanged("Color");
[4200]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;
[11344]179          OnPropertyChanged("Visible");
[4200]180        }
181      }
182    }
[11344]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));
[4200]188    }
[3260]189  }
190}
Note: See TracBrowser for help on using the repository browser.