1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using HeuristicLab.Common;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Data;
|
---|
6 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
7 | using HeuristicLab.Optimization;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 |
|
---|
10 | namespace 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 | }
|
---|