Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Problems.ExternalEvaluation/3.3/CachedExternalEvaluator.cs @ 15428

Last change on this file since 15428 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

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