1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2018 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 HEAL.Attic;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.Modifiers {
|
---|
33 | [StorableType("39EFBF50-DCC3-4E04-B0C1-C0EF1BC70736")]
|
---|
34 | [Item("MultiObjectiveEvaluationCacheProblemModifier", "A problem modifier that caches old evaluations")]
|
---|
35 | public class EvaluationCacheProblemModifier : ProblemModifier {
|
---|
36 | private readonly object locker = new object();
|
---|
37 | [Storable]
|
---|
38 | private Dictionary<string, Tuple<double[], IScope>> cache;
|
---|
39 |
|
---|
40 | #region Constructors & Cloning
|
---|
41 | [StorableConstructor]
|
---|
42 | protected EvaluationCacheProblemModifier(StorableConstructorFlag _) : base(_) { }
|
---|
43 | protected EvaluationCacheProblemModifier(EvaluationCacheProblemModifier original, Cloner cloner) : base(original, cloner) {
|
---|
44 | cache = original?.cache.ToDictionary(x => x.Key, x => Tuple.Create((double[])x.Value.Item1.Clone(), cloner.Clone(x.Value.Item2)));
|
---|
45 | }
|
---|
46 | public EvaluationCacheProblemModifier() {
|
---|
47 | cache = new Dictionary<string, Tuple<double[], IScope>>();
|
---|
48 | }
|
---|
49 | [StorableHook(HookType.AfterDeserialization)]
|
---|
50 | private void AfterDeserialization() {
|
---|
51 | if (cache == null) cache = new Dictionary<string, Tuple<double[], IScope>>();
|
---|
52 | }
|
---|
53 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
54 | return new EvaluationCacheProblemModifier(this, cloner);
|
---|
55 | }
|
---|
56 | #endregion
|
---|
57 |
|
---|
58 | #region ProblemModifier
|
---|
59 | public override double[] ModifiedEvaluate(Individual individual, IRandom random) {
|
---|
60 | var key = ToKey(individual, Encoding);
|
---|
61 | lock (locker) {
|
---|
62 | if (cache.TryGetValue(key, out var tuple))
|
---|
63 | return tuple.Item1;
|
---|
64 | }
|
---|
65 | var qualities = base.ModifiedEvaluate(individual, random);
|
---|
66 | StoreInCache(key, qualities, individual);
|
---|
67 | return qualities;
|
---|
68 | }
|
---|
69 |
|
---|
70 | public override void Initialize() {
|
---|
71 | cache = new Dictionary<string, Tuple<double[], IScope>>();
|
---|
72 | }
|
---|
73 | #endregion
|
---|
74 |
|
---|
75 | #region Helpers
|
---|
76 | private void StoreInCache(string key, double[] qualities, Individual individual) {
|
---|
77 | IScope individualScope = new Scope();
|
---|
78 | individualScope.Variables.AddRange(individual.Values.Select(v => new Variable(v.Key, v.Value)));
|
---|
79 | individualScope = (IScope)individualScope.Clone();
|
---|
80 | lock (locker) {
|
---|
81 | if (!cache.ContainsKey(key))
|
---|
82 | cache.Add(key, Tuple.Create(qualities, individualScope));
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | private static string ToKey(Individual i, IEncoding enc) {
|
---|
87 | switch (enc) {
|
---|
88 | case IntegerVectorEncoding _:
|
---|
89 | return string.Join(";", i.IntegerVector());
|
---|
90 | case RealVectorEncoding _:
|
---|
91 | return string.Join(";", i.RealVector());
|
---|
92 | default:
|
---|
93 | throw new ArgumentException("Encoding not supported");
|
---|
94 | }
|
---|
95 | }
|
---|
96 | #endregion
|
---|
97 | }
|
---|
98 | }
|
---|