Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.OneMax/3.3/Analyzers/MultiPopulationBestOneMaxSolutionAnalyzer.cs @ 3642

Last change on this file since 3642 was 3642, checked in by svonolfe, 14 years ago

Added anaylzers for the onemax problem (#999)

File size: 4.5 KB
Line 
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;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Optimization;
27using HeuristicLab.Data;
28using HeuristicLab.Core;
29using HeuristicLab.Operators;
30using HeuristicLab.Encodings.BinaryVectorEncoding;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33
34namespace HeuristicLab.Problems.OneMax.Analyzers {
35  /// <summary>
36  /// An operator for analyzing the best solution for a OneMax problem.
37  /// </summary>
38  [Item("MultiPopulationBestOneMaxSolutionAnalyzer", "An operator for analyzing the best solution for a OneMax problem.")]
39  [StorableClass]
40  class MultiPopulationBestOneMaxSolutionAnalyzer : SingleSuccessorOperator, IBestOneMaxSolutionAnalyzer, IMultiPopulationAnalyzer {
41
42    public ILookupParameter<ItemArray<ItemArray<BinaryVector>>> BinaryVectorParameter {
43      get { return (ILookupParameter<ItemArray<ItemArray<BinaryVector>>>)Parameters["BinaryVector"]; }
44    }
45    ILookupParameter IBestOneMaxSolutionAnalyzer.BinaryVectorParameter {
46      get { return BinaryVectorParameter; }
47    }
48    public ILookupParameter<ItemArray<ItemArray<DoubleValue>>> QualityParameter {
49      get { return (ILookupParameter<ItemArray<ItemArray<DoubleValue>>>)Parameters["Quality"]; }
50    }
51    ILookupParameter IBestOneMaxSolutionAnalyzer.QualityParameter {
52      get { return QualityParameter; }
53    }
54    public ILookupParameter<OneMaxSolution> BestSolutionParameter {
55      get { return (ILookupParameter<OneMaxSolution>)Parameters["BestSolution"]; }
56    }
57    public IValueLookupParameter<ResultCollection> ResultsParameter {
58      get { return (IValueLookupParameter<ResultCollection>)Parameters["Results"]; }
59    }
60
61    public MultiPopulationBestOneMaxSolutionAnalyzer()
62      : base() {
63      Parameters.Add(new SubScopesLookupParameter<ItemArray<BinaryVector>>("BinaryVector", "The Onemax solutions from which the best solution should be visualized."));
64
65      Parameters.Add(new SubScopesLookupParameter<ItemArray<DoubleValue>>("Quality", "The qualities of the Onemax solutions which should be visualized."));
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."));
68    }
69
70    public override IOperation Apply() {
71      ItemArray<ItemArray<BinaryVector>> binaryVectors = BinaryVectorParameter.ActualValue;
72      ItemArray<ItemArray<DoubleValue>> qualities = QualityParameter.ActualValue;
73      ResultCollection results = ResultsParameter.ActualValue;
74
75      DoubleValue bestQuality = new DoubleValue(double.MaxValue);
76      BinaryVector bestBinaryVector = null;
77
78      for (int i = 0; i < qualities.Length; i++) {
79        for (int j = 0; j < qualities[i].Length; j++) {
80          if (qualities[i][j].Value < bestQuality.Value) {
81            bestQuality = qualities[i][j];
82            bestBinaryVector = binaryVectors[i][j];
83          }
84        }
85      }
86
87      OneMaxSolution solution = BestSolutionParameter.ActualValue;
88      if (solution == null) {
89        solution = new OneMaxSolution(bestBinaryVector, bestQuality);
90        BestSolutionParameter.ActualValue = solution;
91
92        results.Add(new Result("Best OneMax Solution", solution));
93      } else {
94        if (solution.Quality.Value > bestQuality.Value) {
95          solution.BinaryVector = bestBinaryVector;
96          solution.Quality = bestQuality;
97
98          results["Best OneMax Solution"].Value = solution;
99        }
100      }
101
102      return base.Apply();
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.