Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3384 was 3376, checked in by swagner, 14 years ago

Moved interfaces and classes for deep cloning from HeuristicLab.Core to HeuristicLab.Common (#975).

File size: 3.4 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Optimization {
29  /// <summary>
30  /// Represents the parameters and results of an algorithm run.
31  /// </summary>
32  [Item("Run", "The parameters and results of an algorithm run.")]
33  [StorableClass]
34  public sealed class Run : NamedItem, IRun {
35    [Storable]
36    private IAlgorithm algorithm;
37    public IAlgorithm Algorithm {
38      get { return algorithm; }
39    }
40    [Storable]
41    private Dictionary<string, IItem> parameters;
42    public IDictionary<string, IItem> Parameters {
43      get { return parameters; }
44    }
45    [Storable]
46    private Dictionary<string, IItem> results;
47    public IDictionary<string, IItem> Results {
48      get { return results; }
49    }
50
51    public Run()
52      : base() {
53      name = ItemName;
54      description = ItemDescription;
55      algorithm = null;
56      parameters = new Dictionary<string, IItem>();
57      results = new Dictionary<string, IItem>();
58    }
59    public Run(IAlgorithm algorithm)
60      : base() {
61      if (algorithm == null) throw new ArgumentNullException();
62      name = algorithm.Name + " Run (" + algorithm.ExecutionTime.ToString() + ")";
63      description = ItemDescription;
64      Initialize((IAlgorithm)algorithm.Clone());
65    }
66    public Run(string name, IAlgorithm algorithm)
67      : base(name) {
68      if (algorithm == null) throw new ArgumentNullException();
69      description = ItemDescription;
70      Initialize((IAlgorithm)algorithm.Clone());
71    }
72    public Run(string name, string description, IAlgorithm algorithm)
73      : base(name, description) {
74      if (algorithm == null) throw new ArgumentNullException();
75      Initialize((IAlgorithm)algorithm.Clone());
76    }
77
78    private void Initialize(IAlgorithm algorithm) {
79      this.algorithm = algorithm;
80      this.algorithm.Runs.Clear();
81      parameters = new Dictionary<string, IItem>();
82      results = new Dictionary<string, IItem>();
83      this.algorithm.CollectParameterValues(parameters);
84      this.algorithm.CollectResultValues(results);
85    }
86
87    public override IDeepCloneable Clone(Cloner cloner) {
88      Run clone = (Run)base.Clone(cloner);
89      clone.algorithm = (IAlgorithm)cloner.Clone(algorithm);
90      foreach (string key in parameters.Keys)
91        clone.parameters.Add(key, (IItem)cloner.Clone(parameters[key]));
92      foreach (string key in results.Keys)
93        clone.results.Add(key, (IItem)cloner.Clone(results[key]));
94      return clone;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.