#region License Information /* HeuristicLab * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.PermutationEncoding; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.AlgorithmBehavior.Analyzers { [Item("AfterCrossoverSolutionCachingAnalyzer", "An operator that records solutions.")] [StorableClass] public class AfterCrossoverSolutionCachingAnalyzer : SingleSuccessorOperator { private const string ResultsParameterName = "Results"; #region Parameter properties public ILookupParameter ResultsParameter { get { return (ILookupParameter)Parameters[ResultsParameterName]; } } public ILookupParameter GenerationsParameter { get { return (ILookupParameter)Parameters["Generations"]; } } public ILookupParameter SolutionParameter { get { return (ILookupParameter)Parameters["Solution"]; } } #endregion #region Properties public ResultCollection Results { get { return ResultsParameter.ActualValue; } } #endregion [StorableConstructor] private AfterCrossoverSolutionCachingAnalyzer(bool deserializing) : base(deserializing) { } private AfterCrossoverSolutionCachingAnalyzer(AfterCrossoverSolutionCachingAnalyzer original, Cloner cloner) : base(original, cloner) { } public AfterCrossoverSolutionCachingAnalyzer() : base() { Parameters.Add(new LookupParameter(ResultsParameterName, "The results collection where the analysis values should be stored.")); Parameters.Add(new LookupParameter("Generations", "Nr of generations.")); Parameters.Add(new LookupParameter("Solution")); SolutionParameter.ActualName = "TSPTour"; } public override IDeepCloneable Clone(Cloner cloner) { return new AfterCrossoverSolutionCachingAnalyzer(this, cloner); } public override IOperation Apply() { var solution = SolutionParameter.ActualValue; var parent2 = ExecutionContext.Scope.SubScopes.First(); var parent1 = ExecutionContext.Scope.SubScopes.Last(); PermutationSolutionDictionary solDict; if (!Results.ContainsKey("SolutionCache")) { solDict = new PermutationSolutionDictionary(); Results.Add(new Result("SolutionCache", solDict)); } else { solDict = (PermutationSolutionDictionary)Results["SolutionCache"].Value; } PermutationInformation info = new PermutationInformation(); info.Generation = GenerationsParameter.ActualValue.Value; info.ProducedBy = ProducedBy.Crossover; info.ParentList = new List(); //TODO: solution name should be a parameter var p1Pw = solDict.GetPermutationWrapperOfPermutation(parent1.Variables["TSPTour"].Value as Permutation); var p2Pw = solDict.GetPermutationWrapperOfPermutation(parent2.Variables["TSPTour"].Value as Permutation); info.ParentList.Add(p1Pw); info.ParentList.Add(p2Pw); solDict.Add(new PermutationWrapper((Permutation)solution.Clone(new Cloner())), info); return base.Apply(); } } }