1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022012 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;


23  using System.Collections.Generic;


24  using System.Linq;


25  using HeuristicLab.Common;


26  using HeuristicLab.Core;


27  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


28 


29  namespace HeuristicLab.Problems.DataAnalysis {


30  /// <summary>


31  ///


32  /// </summary>


33  [StorableClass]


34  [Item("NeighbourhoodWeightCalculator", "")]


35  public class NeighbourhoodWeightCalculator : WeightCalculator {


36 


37  public NeighbourhoodWeightCalculator()


38  : base() {


39  }


40 


41  [StorableConstructor]


42  protected NeighbourhoodWeightCalculator(bool deserializing) : base(deserializing) { }


43  protected NeighbourhoodWeightCalculator(NeighbourhoodWeightCalculator original, Cloner cloner)


44  : base(original, cloner) {


45  }


46 


47  public override IDeepCloneable Clone(Cloner cloner) {


48  return new NeighbourhoodWeightCalculator(this, cloner);


49  }


50 


51  protected override IEnumerable<double> CalculateWeights(ItemCollection<IClassificationSolution> classificationSolutions) {


52  if (classificationSolutions.Count <= 0)


53  return new List<double>();


54 


55  if (!classificationSolutions.All(x => x is IDiscriminantFunctionClassificationSolution))


56  return Enumerable.Repeat<double>(1, classificationSolutions.Count);


57 


58  List<List<double>> estimatedTrainingValEnumerators = new List<List<double>>();


59  List<List<double>> estimatedTrainingClassValEnumerators = new List<List<double>>();


60  IDiscriminantFunctionClassificationSolution discriminantSolution;


61  foreach (var solution in classificationSolutions) {


62  discriminantSolution = (IDiscriminantFunctionClassificationSolution)solution;


63  estimatedTrainingValEnumerators.Add(discriminantSolution.EstimatedTrainingValues.ToList());


64  estimatedTrainingClassValEnumerators.Add(discriminantSolution.EstimatedTrainingClassValues.ToList());


65  }


66 


67  List<double> weights = Enumerable.Repeat<double>(0, classificationSolutions.Count()).ToList<double>();


68 


69  IClassificationProblemData problemData = classificationSolutions.ElementAt(0).ProblemData;


70  List<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable).ToList();


71  List<double> trainingVal = GetValues(targetValues, problemData.TrainingIndizes).ToList();


72 


73  double pointAvg, help;


74  int count;


75  for (int point = 0; point < estimatedTrainingClassValEnumerators.First().Count; point++) {


76  pointAvg = 0.0;


77  count = 0;


78  for (int solution = 0; solution < estimatedTrainingClassValEnumerators.Count; solution++) {


79  if (estimatedTrainingClassValEnumerators[solution][point].Equals(targetValues[point])) {


80  pointAvg += estimatedTrainingValEnumerators[solution][point];


81  count++;


82  }


83  }


84  pointAvg /= (double)count;


85  for (int solution = 0; solution < estimatedTrainingClassValEnumerators.Count; solution++) {


86  if (estimatedTrainingClassValEnumerators[solution][point].Equals(targetValues[point])) {


87  weights[solution] += 0.5;


88  help = Math.Abs(estimatedTrainingValEnumerators[solution][point]  0.5);


89  weights[solution] += help < 0.5 ? 0.5  help : 0.0;


90  }


91  }


92  }


93  return weights;


94  }


95 


96  private IEnumerable<double> GetValues(IList<double> targetValues, IEnumerable<int> indizes) {


97  return from i in indizes


98  select targetValues[i];


99  }


100  }


101  }

