Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Analysis.AlgorithmBehavior/HeuristicLab.Analysis.SolutionCaching/3.3/SolutionCache.cs @ 11193

Last change on this file since 11193 was 10105, checked in by ascheibe, 11 years ago

#1886 renamed some properties in SolutionInformation as suggested by swagner

File size: 3.4 KB
RevLine 
[10023]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.Collections.Generic;
[10026]23using System.Linq;
[10023]24using HeuristicLab.Common;
25using HeuristicLab.Core;
[10090]26using HeuristicLab.Optimization;
[10023]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Analysis.SolutionCaching {
[10104]30  //TODO: should implement a collection interface
[10023]31  [Item("SolutionCache", "Stores solutions generated by an algorithm.")]
32  [StorableClass]
[10090]33  public abstract class SolutionCache<TKey, TValue> : Problem, ISolutionCache
[10023]34    where TKey : Item
[10026]35    where TValue : SolutionInformation<TKey> {
[10023]36
[10090]37    #region IStorableContent Members
38    public string Filename { get; set; }
39    #endregion
40
[10023]41    [Storable]
42    protected Dictionary<TKey, List<TValue>> solutionDictionary;
43
44    public Dictionary<TKey, List<TValue>> SolutionDictionary {
45      get { return solutionDictionary; }
46    }
47
48    public SolutionCache() {
49      InitializeSolutionDictionary();
50    }
51
52    [StorableConstructor]
53    protected SolutionCache(bool deserializing) : base(deserializing) { }
54    protected SolutionCache(SolutionCache<TKey, TValue> original, Cloner cloner)
55      : base(original, cloner) {
56      InitializeSolutionDictionary();
57
[10090]58      foreach (KeyValuePair<TKey, List<TValue>> keyValuePair in original.solutionDictionary) {
[10023]59        List<TValue> lst = new List<TValue>();
60        foreach (TValue value in keyValuePair.Value) {
61          lst.Add((TValue)value.Clone(new Cloner()));
62        }
63        this.solutionDictionary.Add((TKey)keyValuePair.Key.Clone(new Cloner()), lst);
64      }
65    }
66
67    protected virtual void InitializeSolutionDictionary() {
68      solutionDictionary = new Dictionary<TKey, List<TValue>>();
69    }
70
71    public virtual void Add(TKey key, TValue value) {
72      if (ContainsKey(key)) {
73        if (!solutionDictionary[key].Contains(value)) {
74          solutionDictionary[key].Add(value);
75        }
76      } else {
77        List<TValue> lst = new List<TValue>();
78        lst.Add(value);
79        solutionDictionary.Add(key, lst);
80      }
81    }
82
83    public virtual bool ContainsKey(TKey key) {
84      return solutionDictionary.ContainsKey(key);
85    }
86
87    public virtual int Size() {
88      return solutionDictionary.Count;
89    }
[10026]90
[10104]91    public virtual IEnumerable<TKey> Keys() {
92      return solutionDictionary.Keys;
93    }
94
95    public virtual IEnumerable<List<TValue>> Values() {
96      return solutionDictionary.Values;
97    }
98
[10026]99    public virtual List<TKey> GetSolutionsFromGeneration(int generation) {
[10105]100      return solutionDictionary.Where(x => x.Value.Count(y => y.Iteration == generation) != 0).Select(x => x.Key).ToList<TKey>();
[10026]101    }
[10023]102  }
103}
Note: See TracBrowser for help on using the repository browser.