Free cookie consent management tool by TermsFeed Policy Generator

Changeset 10749


Ignore:
Timestamp:
04/15/14 13:16:51 (11 years ago)
Author:
mkommend
Message:

#2175: Branched DataAnalysis.Symbolic with local changes.

Location:
branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic
Files:
2 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisMultiObjectiveTrainingBestSolutionAnalyzer.cs

    r9456 r10749  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Linq;
     
    9897
    9998      //if the pareto front of best solutions shall be updated regardless of the quality, the list initialized empty to discard old solutions
    100       IList<double[]> trainingBestQualities;
     99      List<double[]> trainingBestQualities;
    101100      if (UpdateAlways.Value) {
    102101        trainingBestQualities = new List<double[]>();
     
    105104      }
    106105
    107       #region find best trees
    108       IList<int> nonDominatedIndexes = new List<int>();
    109       ISymbolicExpressionTree[] tree = SymbolicExpressionTree.ToArray();
     106      ISymbolicExpressionTree[] trees = SymbolicExpressionTree.ToArray();
    110107      List<double[]> qualities = Qualities.Select(x => x.ToArray()).ToList();
    111108      bool[] maximization = Maximization.ToArray();
    112       List<double[]> newNonDominatedQualities = new List<double[]>();
    113       for (int i = 0; i < tree.Length; i++) {
    114         if (IsNonDominated(qualities[i], trainingBestQualities, maximization) &&
    115           IsNonDominated(qualities[i], qualities, maximization)) {
    116           if (!newNonDominatedQualities.Contains(qualities[i], new DoubleArrayComparer())) {
    117             newNonDominatedQualities.Add(qualities[i]);
    118             nonDominatedIndexes.Add(i);
     109
     110      var nonDominatedInvididuals = new[] { new { Tree = default(ISymbolicExpressionTree), Qualities = default(double[]) } }.ToList();
     111      nonDominatedInvididuals.Clear();
     112
     113      // build list of new non-dominated solutions
     114      for (int i = 0; i < trees.Length; i++) {
     115        if (IsNonDominated(qualities[i], nonDominatedInvididuals.Select(ind => ind.Qualities), maximization) &&
     116            IsNonDominated(qualities[i], trainingBestQualities, maximization)) {
     117          for (int j = nonDominatedInvididuals.Count - 1; j >= 0; j--) {
     118            if (IsBetterOrEqual(qualities[i], nonDominatedInvididuals[j].Qualities, maximization)) {
     119              nonDominatedInvididuals.RemoveAt(j);
     120            }
    119121          }
     122          nonDominatedInvididuals.Add(new { Tree = trees[i], Qualities = qualities[i] });
    120123        }
    121124      }
    122       #endregion
     125
     126      var nonDominatedSolutions = nonDominatedInvididuals.Select(x => new { Solution = CreateSolution(x.Tree, x.Qualities), Qualities = x.Qualities }).ToList();
     127
    123128      #region update Pareto-optimal solution archive
    124       if (nonDominatedIndexes.Count > 0) {
    125         ItemList<DoubleArray> nonDominatedQualities = new ItemList<DoubleArray>();
    126         ItemList<T> nonDominatedSolutions = new ItemList<T>();
    127         // add all new non-dominated solutions to the archive
    128         foreach (var index in nonDominatedIndexes) {
    129           T solution = CreateSolution(tree[index], qualities[index]);
    130           nonDominatedSolutions.Add(solution);
    131           nonDominatedQualities.Add(new DoubleArray(qualities[index]));
    132         }
    133         // add old non-dominated solutions only if they are not dominated by one of the new solutions
     129      if (nonDominatedSolutions.Count > 0) {
     130        //add old non-dominated solutions only if they are not dominated by one of the new solutions
    134131        for (int i = 0; i < trainingBestQualities.Count; i++) {
    135           if (IsNonDominated(trainingBestQualities[i], newNonDominatedQualities, maximization)) {
    136             if (!newNonDominatedQualities.Contains(trainingBestQualities[i], new DoubleArrayComparer())) {
    137               nonDominatedSolutions.Add(TrainingBestSolutions[i]);
    138               nonDominatedQualities.Add(TrainingBestSolutionQualities[i]);
    139             }
     132          if (IsNonDominated(trainingBestQualities[i], nonDominatedSolutions.Select(x => x.Qualities), maximization)) {
     133            nonDominatedSolutions.Add(new { Solution = TrainingBestSolutions[i], Qualities = TrainingBestSolutionQualities[i].ToArray() });
    140134          }
    141135        }
    142136
    143         results[TrainingBestSolutionsParameter.Name].Value = nonDominatedSolutions;
    144         results[TrainingBestSolutionQualitiesParameter.Name].Value = nonDominatedQualities;
     137        var sortedNonDominatedSolutions = nonDominatedSolutions.OrderByDescending(x => x.Qualities[0]);
     138        results[TrainingBestSolutionsParameter.Name].Value = new ItemList<T>(sortedNonDominatedSolutions.Select(x => x.Solution));
     139        results[TrainingBestSolutionQualitiesParameter.Name].Value = new ItemList<DoubleArray>(sortedNonDominatedSolutions.Select(x => new DoubleArray(x.Qualities)));
    145140      }
    146141      #endregion
     
    148143    }
    149144
    150     private class DoubleArrayComparer : IEqualityComparer<double[]> {
    151       public bool Equals(double[] x, double[] y) {
    152         if (y.Length != x.Length) throw new ArgumentException();
    153         for (int i = 0; i < x.Length; i++) {
    154           if (!x[i].IsAlmost(y[i])) return false;
    155         }
    156         return true;
    157       }
    158 
    159       public int GetHashCode(double[] obj) {
    160         int c = obj.Length;
    161         for (int i = 0; i < obj.Length; i++)
    162           c ^= obj[i].GetHashCode();
    163         return c;
    164       }
    165     }
    166 
    167145    protected abstract T CreateSolution(ISymbolicExpressionTree bestTree, double[] bestQuality);
    168146
    169     private bool IsNonDominated(double[] point, IList<double[]> points, bool[] maximization) {
     147    private bool IsNonDominated(double[] point, IEnumerable<double[]> points, bool[] maximization) {
    170148      foreach (var refPoint in points) {
    171         bool refPointDominatesPoint = true;
    172         for (int i = 0; i < point.Length; i++) {
    173           refPointDominatesPoint &= IsBetterOrEqual(refPoint[i], point[i], maximization[i]);
    174         }
     149        bool refPointDominatesPoint = IsBetterOrEqual(refPoint, point, maximization);
    175150        if (refPointDominatesPoint) return false;
    176151      }
    177152      return true;
    178153    }
     154
     155    private bool IsBetterOrEqual(double[] lhs, double[] rhs, bool[] maximization) {
     156      for (int i = 0; i < lhs.Length; i++) {
     157        var result = IsBetterOrEqual(lhs[i], rhs[i], maximization[i]);
     158        if (!result) return false;
     159      }
     160      return true;
     161    }
     162
    179163    private bool IsBetterOrEqual(double lhs, double rhs, bool maximization) {
    180       if (maximization) return lhs > rhs;
    181       else return lhs < rhs;
     164      if (maximization) return lhs >= rhs;
     165      else return lhs <= rhs;
    182166    }
    183167  }
  • branches/DataAnalysis.ComplexityAnalyzer/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r10562 r10749  
    112112  </ItemGroup>
    113113  <ItemGroup>
     114    <Compile Include="Analyzers\SymbolicDataAnalysisModelComplexityAnalyzer.cs" />
    114115    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" />
    115116    <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
Note: See TracChangeset for help on using the changeset viewer.