Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.MetaOptimization (trunk integration)/HeuristicLab.Problems.MetaOptimization/3.3/Analyzers/PopulationAnalyzer.cs @ 16674

Last change on this file since 16674 was 8576, checked in by jkarder, 12 years ago

#1853: created branch for MetaOptimization (trunk integration)

File size: 4.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.ParameterConfigurationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.MetaOptimization {
33  /// <summary>
34  /// An analyzer that records the best parameter configuration.
35  /// </summary>
36  [Item("PopulationAnalyzer", "An analyzer that records the best parameter configuration.")]
37  [StorableClass]
38  public sealed class PopulationAnalyzer : SingleSuccessorOperator, IAnalyzer {
39    public bool EnabledByDefault {
40      get { return true; }
41    }
42
43    public ScopeTreeLookupParameter<ParameterConfigurationTree> ParameterConfigurationParameter {
44      get { return (ScopeTreeLookupParameter<ParameterConfigurationTree>)Parameters["ParameterConfigurationTree"]; }
45    }
46    public ValueLookupParameter<ResultCollection> ResultsParameter {
47      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
48    }
49    public LookupParameter<RunCollection> PopulationParameter {
50      get { return (LookupParameter<RunCollection>)Parameters["Population"]; }
51    }
52    public LookupParameter<BoolValue> MaximizationParameter {
53      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
54    }
55
56    #region Constructors and Cloning
57    public PopulationAnalyzer()
58      : base() {
59      Parameters.Add(new ScopeTreeLookupParameter<ParameterConfigurationTree>("ParameterConfigurationTree", ""));
60      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", ""));
61      Parameters.Add(new LookupParameter<RunCollection>("Population", ""));
62      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "Set to false if the problem should be minimized."));
63    }
64
65    [StorableConstructor]
66    private PopulationAnalyzer(bool deserializing) : base(deserializing) { }
67    private PopulationAnalyzer(PopulationAnalyzer original, Cloner cloner) : base(original, cloner) { }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new PopulationAnalyzer(this, cloner);
70    }
71    #endregion
72
73    public override IOperation Apply() {
74      ResultCollection results = ResultsParameter.ActualValue;
75      ItemArray<ParameterConfigurationTree> parameterTrees = ParameterConfigurationParameter.ActualValue;
76      bool maximization = MaximizationParameter.ActualValue.Value;
77
78      int i = 0;
79      RunCollection rc = new RunCollection();
80      foreach (ParameterConfigurationTree pt in (maximization ? parameterTrees.OrderByDescending(x => x.Quality) : parameterTrees.OrderBy(x => x.Quality))) {
81        rc.Add(pt.ToRun(string.Format("Individual {0} ({1})", i, pt.ParameterInfoString), true));
82        i++;
83      }
84      if (PopulationParameter.ActualValue == null) {
85        PopulationParameter.ActualValue = rc;
86        results.Add(new Result("Population", rc));
87      } else {
88        PopulationParameter.ActualValue = rc;
89        results["Population"].Value = rc;
90      }
91
92      return base.Apply();
93    }
94  }
95}
Note: See TracBrowser for help on using the repository browser.