Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling/3.3/FullEvaluatorNode.cs @ 16147

Last change on this file since 16147 was 15896, checked in by jkarder, 7 years ago

#2205: added surrogate modeling network

File size: 3.8 KB
Line 
1using System.Threading;
2using HeuristicLab.Common;
3using HeuristicLab.Core;
4using HeuristicLab.Core.Networks;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.RealVectorEncoding;
7using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
8using HeuristicLab.Problems.DataAnalysis;
9
10namespace HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling {
11  [Item("Full Evaluator", "")]
12  [StorableClass]
13  public sealed class FullEvaluatorNode : OrchestratedAlgorithmNode {
14    public new FullEvaluationAlgorithm Algorithm {
15      get { return (FullEvaluationAlgorithm)base.Algorithm; }
16      set { base.Algorithm = value; }
17    }
18
19    #region Port Names
20    private const string ExploitationFullEvalRequestPortName = "ExploitationFullEvalRequestPort";
21    private const string ExplorationFullEvalRequestPortName = "ExplorationFullEvalRequestPort";
22    private const string UpdateRegressionSolutionPortName = "Update RegressionSolution";
23    #endregion Port Names
24
25    #region Ports
26    public IMessagePort ExploitationFullEvalRequestPort { get { return (IMessagePort)Ports[ExploitationFullEvalRequestPortName]; } }
27    public IMessagePort ExplorationFullEvalRequestPort { get { return (IMessagePort)Ports[ExplorationFullEvalRequestPortName]; } }
28    public IConfigurationPort UpdateRegressionSolutionPort { get { return (IConfigurationPort)Ports[UpdateRegressionSolutionPortName]; } }
29    #endregion Ports
30
31    #region Constructors & Cloning
32    [StorableConstructor]
33    private FullEvaluatorNode(bool deserializing) : base(deserializing) { }
34    private FullEvaluatorNode(FullEvaluatorNode original, Cloner cloner) : base(original, cloner) {
35      RegisterPortEvents();
36    }
37    public FullEvaluatorNode() : this("Full Evaluator") { }
38    public FullEvaluatorNode(string name) : base(name) {
39      var exploitationPort = CreateEvaluationPort<RealVector>(ExploitationFullEvalRequestPortName, "RealVector", "Quality");
40      Ports.Add(exploitationPort);
41
42      var explorationPort = CreateEvaluationPort<RealVector>(ExplorationFullEvalRequestPortName, "RealVector", "Quality");
43      Ports.Add(explorationPort);
44
45      var updatePort = new ConfigurationPort(UpdateRegressionSolutionPortName) {
46        Parameters = {
47          new PortParameter<IRegressionSolution>("RegressionSolution") {
48            Type = PortParameterType.Input
49          }
50        }
51      };
52      Ports.Add(updatePort);
53
54      Algorithm = new FullEvaluationAlgorithm();
55
56      RegisterPortEvents();
57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new FullEvaluatorNode(this, cloner);
61    }
62    #endregion Constructors & Cloning
63
64    [StorableHook(HookType.AfterDeserialization)]
65    private void AfterDeserialization() {
66      RegisterPortEvents();
67    }
68
69    #region Helpers
70    private void RegisterPortEvents() {
71      ExploitationFullEvalRequestPort.MessageReceived += ExploitationFullEvalRequestPort_MessageReceived;
72      ExplorationFullEvalRequestPort.MessageReceived += ExplorationFullEvalRequestPort_MessageReceived;
73    }
74
75    private void ExploitationFullEvalRequestPort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
76      var evalMsg = e.Value;
77      var realVector = (RealVector)evalMsg["RealVector"];
78      var quality = (DoubleValue)evalMsg["Quality"];
79      Algorithm.EnqueueForExploitation(realVector, quality.Value);
80    }
81
82    private void ExplorationFullEvalRequestPort_MessageReceived(object sender, EventArgs<IMessage, CancellationToken> e) {
83      var evalMsg = e.Value;
84      var realVector = (RealVector)evalMsg["RealVector"];
85      var quality = (DoubleValue)evalMsg["Quality"];
86      Algorithm.EnqueueForExploration(realVector, quality.Value);
87    }
88    #endregion Helpers
89  }
90}
Note: See TracBrowser for help on using the repository browser.