Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2205_OptimizationNetworks/HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling/3.3/PointGeneratorProblem.cs @ 15896

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

#2205: added surrogate modeling network

File size: 2.5 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Encodings.RealVectorEncoding;
7using HeuristicLab.Optimization;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9
10namespace HeuristicLab.Networks.IntegratedOptimization.SurrogateModeling {
11  [Item("Point Generator Problem", "")]
12  [StorableClass]
13  public sealed class PointGeneratorProblem : SingleObjectiveBasicProblem<RealVectorEncoding> {
14    public Action<RealVector, double> NotifyEvaluation { get; set; }
15
16    public override bool Maximization {
17      get { return false; }
18    }
19
20    #region Constructors & Cloning
21    [StorableConstructor]
22    private PointGeneratorProblem(bool deserializing) : base(deserializing) { }
23    private PointGeneratorProblem(PointGeneratorProblem original, Cloner cloner) : base(original, cloner) { }
24    public PointGeneratorProblem() { }
25
26    public override IDeepCloneable Clone(Cloner cloner) {
27
28      return new PointGeneratorProblem(this, cloner);
29    }
30    #endregion
31
32    public override double Evaluate(Individual individual, IRandom random) {
33      var point = individual.RealVector();
34      var quality = 0.0;
35
36      NotifyEvaluation((RealVector)point.Clone(), quality);
37
38      return quality;
39    }
40
41    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
42      base.Analyze(individuals, qualities, results, random);
43
44      var bestIndividuals = individuals.Zip(qualities, (i, q) => new { Individual = i, Quality = q }).MinItems(x => x.Quality);
45      var best = bestIndividuals.First();
46
47      IResult bestSolutionResult;
48      if (!results.TryGetValue("BestSolution", out bestSolutionResult)) {
49        results.Add(bestSolutionResult = new Result("BestSolution", typeof(RealVector)));
50      } else bestSolutionResult = results["BestSolution"];
51
52      IResult bestQualityResult;
53      if (!results.TryGetValue("BestQuality", out bestQualityResult)) {
54        results.Add(bestQualityResult = new Result("BestQuality", typeof(DoubleValue)));
55      } else bestQualityResult = results["BestQuality"];
56
57      var bestQuality = (DoubleValue)bestQualityResult.Value;
58      if (bestQuality == null || best.Quality <= bestQuality.Value) {
59        bestSolutionResult.Value = (RealVector)best.Individual.RealVector().Clone();
60        bestQualityResult.Value = new DoubleValue(best.Quality);
61      }
62    }
63  }
64}
Note: See TracBrowser for help on using the repository browser.