Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.ExternalEvaluation/3.3/CachedExternalEvaluator.cs @ 6172

Last change on this file since 6172 was 6172, checked in by epitzer, 13 years ago

prevent additional uncached execution of base class (#1516)

File size: 2.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28namespace HeuristicLab.Problems.ExternalEvaluation {
29
30  [Item("CachedExternalEvaluationValuesCollector", "Creates a solution message, and communicates it via the driver to receive a quality message, also keeps a cache of previous evaluation results.")]
31  [StorableClass]
32  public class CachedExternalEvaluator : ExternalEvaluator {
33
34    #region Parameters
35    public OptionalValueParameter<EvaluationCache> CacheParameter {
36      get { return (OptionalValueParameter<EvaluationCache>)Parameters["Cache"]; }
37    }
38    #endregion
39
40    #region Parameter Values
41    public EvaluationCache Cache {
42      get { return CacheParameter.Value; }
43      set { CacheParameter.Value = value; }
44    }
45    protected DoubleValue Quality {
46      get { return QualityParameter.ActualValue; }
47      set { QualityParameter.ActualValue = value; }
48    }
49    protected IEvaluationServiceClient Client {
50      get { return ClientParameter.ActualValue; }
51    }
52    #endregion
53
54    #region Construction & Cloning
55    [StorableConstructor]
56    protected CachedExternalEvaluator(bool deserializing) : base(deserializing) { }
57    protected CachedExternalEvaluator(CachedExternalEvaluator original, Cloner cloner) : base(original, cloner) { }
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new CachedExternalEvaluator(this, cloner);
60    }
61    public CachedExternalEvaluator()
62      : base() {
63      Parameters.Add(new OptionalValueParameter<EvaluationCache>("Cache", "Cache of previously evaluated solutions"));
64    }
65    #endregion
66
67    public override IOperation Apply() {
68      if (Cache == null) Cache = new EvaluationCache();
69      if (Quality == null) Quality = new DoubleValue(0);
70
71      Quality.Value = Cache.GetValue(BuildSolutionMessage(), m => Client.Evaluate(m).Quality);
72
73      if (Successor != null)
74        return ExecutionContext.CreateOperation(Successor);
75      else
76        return null;
77    }
78  }
79}
Note: See TracBrowser for help on using the repository browser.