Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18214 for branches


Ignore:
Timestamp:
02/08/22 17:36:18 (3 years ago)
Author:
pfleck
Message:

#3040 Added first version of sampling segment optimization manipulator for vectorial sym reg.

Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj

    r18193 r18214  
    177177    <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionManipulator.cs" />
    178178    <Compile Include="Mutators\SegmentOptimization\SamplingSegmentOptimizationManipulator.cs" />
     179    <Compile Include="Mutators\SamplingSubVectorImprovementManipulator.cs" />
    179180    <Compile Include="Mutators\SymbolicDataAnalysisExpressionManipulator.cs" />
    180181    <Compile Include="Crossovers\SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs" />
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs

    r18060 r18214  
    729729        case OpCodes.SubVector: {
    730730          DoubleVector SubVector(DoubleVector v , double start, double end) {
    731             int Mod(int x, int m) => (x % m + m) % m;
    732             int startIdx = Mod((int)Math.Round(start * v.Count), v.Count);
    733             int endIdx = Mod((int)Math.Round(end * v.Count), v.Count);
    734               int size = v.Count;
     731            int size = v.Count;
     732            int startIdx = Math.Abs((int)Math.Round(start * (size - 1)) % size);
     733            int endIdx = Math.Abs((int)Math.Round(end * (size - 1)) % size);
    735734            if (startIdx < endIdx) {
    736735              return v.SubVector(startIdx, count: endIdx - startIdx);
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/MultiSymbolicDataAnalysisExpressionManipulator.cs

    r18082 r18214  
    124124
    125125    private void InitializeOperators() {
    126       var list = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeManipulator>().ToList();
     126      var list = ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeManipulator>()
     127        .Where(c => !(c is IMultiOperator<ISymbolicExpressionTreeManipulator>))
     128        .ToList();
    127129      var dataAnalysisManipulators = from type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisExpressionManipulator<T>))
    128130                                   where this.GetType().Assembly == type.Assembly
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs

    r18212 r18214  
    4646      Full,
    4747      RandomDirection,
    48       RandomRange
     48      RandomRange,
     49      DirectedDirection,
     50      DirectedRange
    4951    }
    5052    [StorableType("A9EBE01E-53DE-4EB7-94B3-26C624034A08")]
     
    6668    public DimensionType Dimension { get { return DimensionParameter.Value.Value; } set { DimensionParameter.Value.Value = value; } }
    6769    public SearchRangeType SearchRange { get { return SearchRangeParameter.Value.Value; } set { SearchRangeParameter.Value.Value = value; } }
     70    public int SlopeCalculationRange { get { return SlopeCalculationRangeParameter.Value.Value; } set { SlopeCalculationRangeParameter.Value.Value = value; } }
    6871    public SamplingType Sampling { get { return SamplingParameter.Value.Value; } set { SamplingParameter.Value.Value = value; } }
    6972    public int SampleCount { get { return SampleCountParameter.Value.Value; } set { SampleCountParameter.Value.Value = value; } }
     
    7477    public ValueParameter<EnumValue<DimensionType>> DimensionParameter => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"];
    7578    public ValueParameter<EnumValue<SearchRangeType>> SearchRangeParameter => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"];
     79    public ValueParameter<IntValue> SlopeCalculationRangeParameter => (ValueParameter<IntValue>)Parameters["SlopeCalculationRange"];
    7680    public ValueParameter<EnumValue<SamplingType>> SamplingParameter => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"];
    7781    public ValueParameter<IntValue> SampleCountParameter => (ValueParameter<IntValue>)Parameters["SampleCount"];
    7882    public ValueParameter<EnumValue<SamplingPointsType>> SamplingPointsParameter => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"];
    7983    public ValueParameter<BoolValue> CountSamplesAsEvaluationsParameter => (ValueParameter<BoolValue>)Parameters["CountSamplesAsEvaluations"];
     84
    8085    public LookupParameter<IntValue> EvaluatedSolutionsParameter => (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"];
    8186    #endregion
     
    8489      Parameters.Add(new ValueParameter<EnumValue<DimensionType>>("Dimension", new EnumValue<DimensionType>(DimensionType.All)));
    8590      Parameters.Add(new ValueParameter<EnumValue<SearchRangeType>>("SearchRange", new EnumValue<SearchRangeType>(SearchRangeType.Full)));
     91      Parameters.Add(new ValueParameter<IntValue>("SlopeCalculationRange", new IntValue(5)));
    8692      Parameters.Add(new ValueParameter<EnumValue<SamplingType>>("Sampling", new EnumValue<SamplingType>(SamplingType.Exhaustive)));
    8793      Parameters.Add(new ValueParameter<IntValue>("SampleCount")); // only used when sampling != Exhaustive
     
    95101      return new SamplingSegmentOptimizationManipulator(this, cloner);
    96102    }
     103
    97104    [StorableConstructor]
    98105    public SamplingSegmentOptimizationManipulator(StorableConstructorFlag _) : base(_) { }
    99    
     106    [StorableHook(HookType.AfterDeserialization)]
     107    private void AfterDeserialization() {
     108      if (!Parameters.ContainsKey("SlopeCalculationRange"))
     109        Parameters.Add(new ValueParameter<IntValue>("SlopeCalculationRange", new IntValue(5)));
     110    }
     111
    100112    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
    101       var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange);
     113      var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange, SlopeCalculationRange, DataParameter.ActualValue);
    102114     
    103115      if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations))
     
    119131      }
    120132
    121       var best = FindBest(solutions);
     133      var best = FindBest(solutions, Evaluate);
    122134      if (best != null) {
    123135        CopyTo(best.Item1, integerVector);
     
    125137    }
    126138   
    127     private static IEnumerable<int>[] CreateIndices(IRandom random, IntegerVector integerVector, IntMatrix bounds, DimensionType dimension, SearchRangeType indexRange) {
     139    public static IEnumerable<int>[] CreateIndices(IRandom random, IntegerVector integerVector, IntMatrix bounds, DimensionType dimension, SearchRangeType indexRange, int gradientRange, DoubleMatrix data) {
    128140      var indices = new IEnumerable<int>[integerVector.Length];
    129141      int targetIndex = random.Next(indices.Length); // first or second index
    130142      for (int i = 0; i < indices.Length; i++) {
    131143        var searchRange = dimension == DimensionType.All || targetIndex == i ? indexRange : SearchRangeType.None;
    132         indices[i] = CreateSingleIndices(bounds[i % bounds.Rows, 1], integerVector[i], searchRange, random).ToList();
     144        indices[i] = CreateSingleIndices(bounds[i % bounds.Rows, 1], integerVector[i], searchRange, gradientRange, data, random).ToList();
    133145        if (!indices[i].Any()) {
    134146          throw new InvalidOperationException("no indices!");
     
    139151    }
    140152
    141     protected static IEnumerable<int> CreateSingleIndices(int length, int currentIndex, SearchRangeType searchRange, IRandom random) {
     153    public static IEnumerable<int> CreateSingleIndices(int length, int currentIndex, SearchRangeType searchRange, int gradientRange, DoubleMatrix data, IRandom random) {
    142154      switch (searchRange) {
    143155        case SearchRangeType.Full:
     
    152164          int start = random.Next(0, currentIndex + 1), end = random.Next(currentIndex, length);
    153165          return Enumerable.Range(start, end - start + 1);
     166        case SearchRangeType.DirectedDirection:
     167          var slope = CalculateSlope(currentIndex, gradientRange, data); // slope of original data, non-aggregated
     168          return slope < 0
     169            ? Enumerable.Range(0, currentIndex + 1) // left
     170            : Enumerable.Range(currentIndex, length - currentIndex); // right
     171        //case SearchRangeType.DirectedRange:
     172        //  var slope = CalculateSlope(currentIndex, gradientRange, data);
    154173        default:
    155174          throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null);
     
    157176    }
    158177
    159     protected static IEnumerable<IntegerVector> CreateCombinations(IEnumerable<int> startIndices, IEnumerable<int> endIndices) {
     178    private static double CalculateSlope(int position, int range, DoubleMatrix data) {
     179      int begin = Math.Max((int)(position - range / 2.0), 0);
     180      int remaining = data.Columns - begin;
     181      var indices = Enumerable.Range(begin, Math.Min(range, remaining)).ToArray();
     182
     183      int num = data.Rows * indices.Length;
     184      double[] xs = new double[num], ys = new double[num];
     185
     186      for (int i = 0; i < data.Rows; i++) {
     187        for (int j = 0; j < indices.Length; j++) {
     188          int idx = i * indices.Length + j;
     189          xs[idx] = indices[j];
     190          ys[idx] = data[i, j];
     191        }
     192      }
     193     
     194      var  coefficients = MathNet.Numerics.LinearRegression.SimpleRegression.Fit(xs, ys);
     195      var slope = coefficients.Item2;
     196     
     197      return slope;
     198    }
     199
     200    public static IEnumerable<IntegerVector> CreateCombinations(IEnumerable<int> startIndices, IEnumerable<int> endIndices) {
    160201      return
    161202        from s in startIndices
     
    165206    }
    166207
    167     protected static IEnumerable<T> SampleIndices<T>(IEnumerable<T> indices, SamplingType sampling, IRandom random, int count) {
     208    public static IEnumerable<T> SampleIndices<T>(IEnumerable<T> indices, SamplingType sampling, IRandom random, int count) {
    168209      switch (sampling) {
    169210        case SamplingType.Exhaustive:
     
    173214        case SamplingType.LinearSelection:
    174215          var indicesList = indices.ToList();
     216          // if single sample, the last is always returned
    175217          var selected = MathNet.Numerics.Generate.LinearSpaced(count, 0, indicesList.Count - 1) // LinearSpaced stop is inclusive
    176218            .Select(i => (int)Math.Floor(i))
     
    182224    }
    183225
    184     protected Tuple<IntegerVector, double> FindBest(IEnumerable<IntegerVector> solutions) {
    185       var evaluatedSolutions = solutions.Select(s => Tuple.Create(s, Evaluate(s)));
    186       var best = evaluatedSolutions.OrderBy(t => t.Item2).FirstOrDefault();
    187       return best;
     226    public static Tuple<T, double> FindBest<T>(IEnumerable<T> solutions, Func<T, double> evaluate, bool minimize = true) {
     227      var evaluatedSolutions = solutions.Select(s => Tuple.Create(s, evaluate(s)));
     228      var sorted = evaluatedSolutions.OrderBy(t => t.Item2);
     229      return minimize ? sorted.FirstOrDefault() : sorted.LastOrDefault();
    188230    }
    189231
Note: See TracChangeset for help on using the changeset viewer.