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