Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 4711 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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