Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.OneMax/3.3/Analyzers/BestOneMaxSolutionAnalyzer.cs @ 11642

Last change on this file since 11642 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

File size: 5.3 KB
RevLine 
[3642]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3642]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;
[4722]23using HeuristicLab.Common;
[4068]24using HeuristicLab.Core;
[3642]25using HeuristicLab.Data;
[4068]26using HeuristicLab.Encodings.BinaryVectorEncoding;
[3642]27using HeuristicLab.Operators;
[4068]28using HeuristicLab.Optimization;
[3642]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
[3797]32namespace HeuristicLab.Problems.OneMax {
[3642]33  /// <summary>
34  /// An operator for analyzing the best solution for a OneMax problem.
35  /// </summary>
36  [Item("BestOneMaxSolutionAnalyzer", "An operator for analyzing the best solution for a OneMax problem.")]
37  [StorableClass]
[4722]38  public class BestOneMaxSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
[7172]39    public virtual bool EnabledByDefault {
40      get { return true; }
41    }
42
[3787]43    public LookupParameter<BoolValue> MaximizationParameter {
44      get { return (LookupParameter<BoolValue>)Parameters["Maximization"]; }
45    }
[3667]46    public ScopeTreeLookupParameter<BinaryVector> BinaryVectorParameter {
47      get { return (ScopeTreeLookupParameter<BinaryVector>)Parameters["BinaryVector"]; }
[3642]48    }
[3667]49    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
50      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
[3642]51    }
[3667]52    public LookupParameter<OneMaxSolution> BestSolutionParameter {
53      get { return (LookupParameter<OneMaxSolution>)Parameters["BestSolution"]; }
[3642]54    }
[3667]55    public ValueLookupParameter<ResultCollection> ResultsParameter {
56      get { return (ValueLookupParameter<ResultCollection>)Parameters["Results"]; }
[3642]57    }
[3787]58    public LookupParameter<DoubleValue> BestKnownQualityParameter {
59      get { return (LookupParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
60    }
[3642]61
[4722]62    [StorableConstructor]
63    protected BestOneMaxSolutionAnalyzer(bool deserializing) : base(deserializing) { }
64    protected BestOneMaxSolutionAnalyzer(BestOneMaxSolutionAnalyzer original, Cloner cloner) : base(original, cloner) { }
[3642]65    public BestOneMaxSolutionAnalyzer()
66      : base() {
[3787]67      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem is a maximization problem."));
[3667]68      Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The Onemax solutions from which the best solution should be visualized."));
69      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the Onemax solutions which should be visualized."));
[3642]70      Parameters.Add(new LookupParameter<OneMaxSolution>("BestSolution", "The best Onemax solution."));
71      Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the Onemax solution should be stored."));
[3787]72      Parameters.Add(new LookupParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution."));
[3642]73    }
74
[4722]75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new BestOneMaxSolutionAnalyzer(this, cloner);
77    }
78
[3642]79    public override IOperation Apply() {
[3667]80      ItemArray<BinaryVector> binaryVectors = BinaryVectorParameter.ActualValue;
81      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
[3642]82      ResultCollection results = ResultsParameter.ActualValue;
[3787]83      bool max = MaximizationParameter.ActualValue.Value;
84      DoubleValue bestKnownQuality = BestKnownQualityParameter.ActualValue;
[3642]85
[3787]86      int i = -1;
87      if (!max)
88        i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
89      else i = qualities.Select((x, index) => new { index, x.Value }).OrderByDescending(x => x.Value).First().index;
[3667]90
[3787]91      if (bestKnownQuality == null ||
92          max && qualities[i].Value > bestKnownQuality.Value ||
93          !max && qualities[i].Value < bestKnownQuality.Value) {
94        BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i].Value);
95      }
96
[3642]97      OneMaxSolution solution = BestSolutionParameter.ActualValue;
98      if (solution == null) {
[3787]99        solution = new OneMaxSolution((BinaryVector)binaryVectors[i].Clone(), new DoubleValue(qualities[i].Value));
[3642]100        BestSolutionParameter.ActualValue = solution;
101        results.Add(new Result("Best OneMax Solution", solution));
[4068]102      } else {
[3787]103        if (max && qualities[i].Value > solution.Quality.Value ||
104          !max && qualities[i].Value < solution.Quality.Value) {
105          solution.BinaryVector = (BinaryVector)binaryVectors[i].Clone();
106          solution.Quality = new DoubleValue(qualities[i].Value);
107        }
[3642]108      }
109
110      return base.Apply();
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.