[14741] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2016 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 | using System;
|
---|
[14818] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[14741] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 32 |
|
---|
| 33 | // ReSharper disable once CheckNamespace
|
---|
| 34 | namespace HeuristicLab.Algorithms.EGO {
|
---|
| 35 |
|
---|
| 36 | [StorableClass]
|
---|
[14818] | 37 | [Item("RobustImprovementMeassure", "Adding or Subtracting the variance * factor to the model estimation")]
|
---|
| 38 | public class RobustImprovement : InfillCriterionBase {
|
---|
[14741] | 39 |
|
---|
| 40 | #region ParameterNames
|
---|
[14818] | 41 | private const string KParameterName = "NearestNeighbours";
|
---|
[14741] | 42 | #endregion
|
---|
| 43 |
|
---|
| 44 | #region ParameterProperties
|
---|
[14818] | 45 | public IFixedValueParameter<IntValue> KParameter => Parameters[KParameterName] as IFixedValueParameter<IntValue>;
|
---|
| 46 |
|
---|
[14741] | 47 | #endregion
|
---|
| 48 |
|
---|
| 49 | #region Properties
|
---|
[14818] | 50 | private int K => KParameter.Value.Value;
|
---|
| 51 |
|
---|
| 52 | [Storable]
|
---|
| 53 | private double MaxSolutionDist;
|
---|
| 54 |
|
---|
| 55 | [Storable]
|
---|
| 56 | //TODO use VP-Tree instead of array
|
---|
| 57 | private RealVector[] Data;
|
---|
[14741] | 58 | #endregion
|
---|
| 59 |
|
---|
| 60 | #region HL-Constructors, Serialization and Cloning
|
---|
| 61 | [StorableConstructor]
|
---|
[14818] | 62 | private RobustImprovement(bool deserializing) : base(deserializing) { }
|
---|
| 63 |
|
---|
| 64 | private RobustImprovement(RobustImprovement original, Cloner cloner) : base(original, cloner) {
|
---|
| 65 | MaxSolutionDist = original.MaxSolutionDist;
|
---|
| 66 | Data = original.Data != null ? original.Data.Select(cloner.Clone).ToArray() : null;
|
---|
[14741] | 67 | }
|
---|
[14818] | 68 | public RobustImprovement() {
|
---|
| 69 | Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "A value larger than 0 indicating how many nearestNeighbours shall be used to determine the RI meassure", new IntValue(3)));
|
---|
| 70 | }
|
---|
[14741] | 71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[14818] | 72 | return new RobustImprovement(this, cloner);
|
---|
[14741] | 73 | }
|
---|
| 74 | #endregion
|
---|
| 75 |
|
---|
[14818] | 76 |
|
---|
| 77 | public override double Evaluate(RealVector vector) {
|
---|
| 78 | List<RealVector> nearestNeighbours;
|
---|
| 79 | List<double> distances;
|
---|
| 80 | Search(vector, K, out nearestNeighbours, out distances);
|
---|
| 81 | var distVectors = nearestNeighbours.Select(x => Minus(x, vector)).ToList();
|
---|
| 82 | var sum = 0.0;
|
---|
| 83 | var wsum = 1.0; //weights for angular distance
|
---|
| 84 | var used = new HashSet<RealVector>();
|
---|
| 85 | foreach (var distVector in distVectors) {
|
---|
| 86 | var d = Math.Pow(distances[used.Count], 0.5);
|
---|
| 87 | if (used.Count == 0) {
|
---|
| 88 | sum += d;
|
---|
| 89 | } else {
|
---|
| 90 | var w = used.Select(x => Angular(distVector, x)).Min();
|
---|
| 91 | sum += w * d;
|
---|
| 92 | wsum += w;
|
---|
| 93 | }
|
---|
| 94 | used.Add(distVector);
|
---|
| 95 | }
|
---|
| 96 | sum /= wsum * MaxSolutionDist; //normalize
|
---|
| 97 | return sum;
|
---|
| 98 | }
|
---|
| 99 | public override bool Maximization() {
|
---|
| 100 | return ExpensiveMaximization;
|
---|
| 101 | }
|
---|
| 102 | protected override void Initialize() {
|
---|
| 103 | var model = RegressionSolution.Model as IConfidenceRegressionModel;
|
---|
[14741] | 104 | if (model == null) throw new ArgumentException("can not calculate EI without confidence measure");
|
---|
[14818] | 105 | Data = new RealVector[RegressionSolution.ProblemData.Dataset.Rows];
|
---|
| 106 | for (var i = 0; i < Data.Length; i++) {
|
---|
| 107 | Data[i] = new RealVector(Encoding.Length);
|
---|
| 108 | for (var j = 0; j < Encoding.Length; j++)
|
---|
| 109 | Data[i][j] = RegressionSolution.ProblemData.Dataset.GetDoubleValue(i, j);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | var maxSolution = new double[Encoding.Length];
|
---|
| 113 | var minSolution = new double[Encoding.Length];
|
---|
| 114 | for (var i = 0; i < Encoding.Length; i++) {
|
---|
| 115 | var j = i % Encoding.Bounds.Rows;
|
---|
| 116 | maxSolution[i] = Encoding.Bounds[j, 1];
|
---|
| 117 | minSolution[i] = Encoding.Bounds[j, 0];
|
---|
| 118 | }
|
---|
| 119 | MaxSolutionDist = Euclidian(maxSolution, minSolution) / Data.Length;
|
---|
[14741] | 120 | }
|
---|
| 121 |
|
---|
[14818] | 122 | #region Helpers
|
---|
| 123 | private static double Euclidian(IEnumerable<double> a, IEnumerable<double> b) {
|
---|
| 124 | return Math.Sqrt(a.Zip(b, (d, d1) => d - d1).Sum(d => d * d));
|
---|
| 125 | }
|
---|
| 126 | private static double Angular(RealVector a, RealVector b) {
|
---|
| 127 | var innerProduct = a.Zip(b, (x, y) => x * y).Sum();
|
---|
| 128 | var res = Math.Acos(innerProduct / (Norm(a) * Norm(b))) / Math.PI;
|
---|
| 129 | return double.IsNaN(res) ? 0 : res;
|
---|
| 130 | }
|
---|
| 131 | private static double Norm(IEnumerable<double> a) {
|
---|
| 132 | return Math.Sqrt(a.Sum(d => d * d));
|
---|
| 133 | }
|
---|
| 134 | private static RealVector Minus(RealVector a, RealVector b) {
|
---|
| 135 | return new RealVector(a.Zip(b, (d, d1) => d - d1).ToArray());
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | private void Search(RealVector vector, int k, out List<RealVector> nearestNeighbours, out List<double> distances) {
|
---|
| 139 | var neighbours = new SortedList<double, RealVector>(new DuplicateKeyComparer<double>());
|
---|
| 140 | foreach (var n in Data) neighbours.Add(Euclidian(n, vector), n);
|
---|
| 141 | nearestNeighbours = new List<RealVector>();
|
---|
| 142 |
|
---|
| 143 | distances = new List<double>();
|
---|
| 144 | foreach (var entry in neighbours) {
|
---|
| 145 | nearestNeighbours.Add(entry.Value);
|
---|
| 146 | distances.Add(entry.Key);
|
---|
| 147 | if (distances.Count == k) break;
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | #endregion
|
---|
| 151 |
|
---|
| 152 | public class DuplicateKeyComparer<TKey> : IComparer<TKey> where TKey : IComparable {
|
---|
| 153 | public int Compare(TKey x, TKey y) {
|
---|
| 154 | var result = x.CompareTo(y);
|
---|
| 155 | return result == 0 ? 1 : result;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
[14741] | 158 | }
|
---|
| 159 | }
|
---|