[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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Core;
|
---|
| 29 | using HeuristicLab.Operators;
|
---|
| 30 | using HeuristicLab.Encodings.BinaryVectorEncoding;
|
---|
| 31 | using HeuristicLab.Parameters;
|
---|
| 32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 33 |
|
---|
| 34 | namespace HeuristicLab.Problems.OneMax.Analyzers {
|
---|
| 35 | /// <summary>
|
---|
| 36 | /// An operator for analyzing the best solution for a OneMax problem.
|
---|
| 37 | /// </summary>
|
---|
| 38 | [Item("BestOneMaxSolutionAnalyzer", "An operator for analyzing the best solution for a OneMax problem.")]
|
---|
| 39 | [StorableClass]
|
---|
[3667] | 40 | class BestOneMaxSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {
|
---|
[3642] | 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 | }
|
---|
| 54 |
|
---|
| 55 | public BestOneMaxSolutionAnalyzer()
|
---|
| 56 | : base() {
|
---|
[3667] | 57 | Parameters.Add(new ScopeTreeLookupParameter<BinaryVector>("BinaryVector", "The Onemax solutions from which the best solution should be visualized."));
|
---|
[3642] | 58 |
|
---|
[3667] | 59 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The qualities of the Onemax solutions which should be visualized."));
|
---|
[3642] | 60 | Parameters.Add(new LookupParameter<OneMaxSolution>("BestSolution", "The best Onemax solution."));
|
---|
| 61 | Parameters.Add(new ValueLookupParameter<ResultCollection>("Results", "The result collection where the Onemax solution should be stored."));
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | public override IOperation Apply() {
|
---|
[3667] | 65 | ItemArray<BinaryVector> binaryVectors = BinaryVectorParameter.ActualValue;
|
---|
| 66 | ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
|
---|
[3642] | 67 | ResultCollection results = ResultsParameter.ActualValue;
|
---|
| 68 |
|
---|
[3667] | 69 | int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;
|
---|
| 70 |
|
---|
[3642] | 71 | OneMaxSolution solution = BestSolutionParameter.ActualValue;
|
---|
| 72 | if (solution == null) {
|
---|
[3667] | 73 | solution = new OneMaxSolution(binaryVectors[i], QualityParameter.ActualValue[i]);
|
---|
[3642] | 74 | BestSolutionParameter.ActualValue = solution;
|
---|
| 75 | results.Add(new Result("Best OneMax Solution", solution));
|
---|
| 76 | } else {
|
---|
[3667] | 77 | solution.BinaryVector = binaryVectors[i];
|
---|
| 78 | solution.Quality = QualityParameter.ActualValue[i];
|
---|
[3642] | 79 |
|
---|
| 80 | results["Best OneMax Solution"].Value = solution;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return base.Apply();
|
---|
| 84 | }
|
---|
| 85 | }
|
---|
| 86 | }
|
---|