#region License Information /* HeuristicLab * Copyright (C) 2002-2013 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.Optimization; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Analysis.SolutionCaching { //TODO: should implement a collection interface [Item("SolutionCache", "Stores solutions generated by an algorithm.")] [StorableClass] public abstract class SolutionCache : Problem, ISolutionCache where TKey : Item where TValue : SolutionInformation { #region IStorableContent Members public string Filename { get; set; } #endregion [Storable] protected Dictionary> solutionDictionary; public Dictionary> SolutionDictionary { get { return solutionDictionary; } } public SolutionCache() { InitializeSolutionDictionary(); } [StorableConstructor] protected SolutionCache(bool deserializing) : base(deserializing) { } protected SolutionCache(SolutionCache original, Cloner cloner) : base(original, cloner) { InitializeSolutionDictionary(); foreach (KeyValuePair> keyValuePair in original.solutionDictionary) { List lst = new List(); foreach (TValue value in keyValuePair.Value) { lst.Add((TValue)value.Clone(new Cloner())); } this.solutionDictionary.Add((TKey)keyValuePair.Key.Clone(new Cloner()), lst); } } protected virtual void InitializeSolutionDictionary() { solutionDictionary = new Dictionary>(); } public virtual void Add(TKey key, TValue value) { if (ContainsKey(key)) { if (!solutionDictionary[key].Contains(value)) { solutionDictionary[key].Add(value); } } else { List lst = new List(); lst.Add(value); solutionDictionary.Add(key, lst); } } public virtual bool ContainsKey(TKey key) { return solutionDictionary.ContainsKey(key); } public virtual int Size() { return solutionDictionary.Count; } public virtual IEnumerable Keys() { return solutionDictionary.Keys; } public virtual IEnumerable> Values() { return solutionDictionary.Values; } public virtual List GetSolutionsFromGeneration(int generation) { return solutionDictionary.Where(x => x.Value.Count(y => y.Iteration == generation) != 0).Select(x => x.Key).ToList(); } } }