Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18193 for branches


Ignore:
Timestamp:
01/13/22 16:32:03 (2 years ago)
Author:
pfleck
Message:

#3040 Added additional parameters and fixed index generation for random search ranges.

Location:
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
Files:
2 edited

Legend:

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

    r18186 r18193  
    465465    </ProjectReference>
    466466  </ItemGroup>
    467   <ItemGroup>
    468     <PackageReference Include="OneOf">
    469       <Version>3.0.205</Version>
    470     </PackageReference>
    471   </ItemGroup>
    472467  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    473468  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs

    r18186 r18193  
    3636  public class SamplingSegmentOptimizationManipulator : SegmentOptimizationMutator {
    3737
     38    [StorableType("65B2E870-7EED-4DD0-918E-94A346671784")]
    3839    protected enum DimensionType {
    3940      All,
    4041      Single
    4142    }
     43    [StorableType("C73064F2-4E2F-473F-8DBA-DD47881726D4")]
    4244    protected enum SearchRangeType {
     45      None, // Fixed on current value
    4346      Full,
    44       None,
    45       Left,
    46       Right
     47      RandomDirection,
     48      RandomRange
    4749    }
     50    [StorableType("A9EBE01E-53DE-4EB7-94B3-26C624034A08")]
    4851    protected enum SamplingType {
    4952      Exhaustive,
     
    5255    }
    5356
     57    [StorableType("4EBF0DFE-90A1-4DFE-8CC7-D358EF8AF96B")]
     58    [Flags]
     59    protected enum SamplingPointsType {
     60      None = 0,
     61      BeforeCombinations = 1,
     62      AfterCombinations = 2
     63    }
     64
    5465    protected ValueParameter<EnumValue<DimensionType>> Dimension => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"];
    5566    protected ValueParameter<EnumValue<SearchRangeType>> SearchRange => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"];
    5667    protected ValueParameter<EnumValue<SamplingType>> Sampling => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"];
    5768    protected ValueParameter<IntValue> SampleCount => (ValueParameter<IntValue>)Parameters["SampleCount"];
     69    protected ValueParameter<EnumValue<SamplingPointsType>> SamplingPoints => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"];
     70   
    5871
    5972    public SamplingSegmentOptimizationManipulator() {
     
    6275      Parameters.Add(new ValueParameter<EnumValue<SamplingType>>("Sampling", new EnumValue<SamplingType>(SamplingType.Exhaustive)));
    6376      Parameters.Add(new ValueParameter<IntValue>("SampleCount")); // only used when sampling != Exhaustive
     77      Parameters.Add(new ValueParameter<EnumValue<SamplingPointsType>>("SamplingPoints", new EnumValue<SamplingPointsType>(SamplingPointsType.BeforeCombinations | SamplingPointsType.AfterCombinations)));
    6478    }
    6579    public SamplingSegmentOptimizationManipulator(SamplingSegmentOptimizationManipulator original, Cloner cloner)
     
    7387    protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) {
    7488      var indices = CreateIndices(random, integerVector, bounds, Dimension.Value.Value, SearchRange.Value.Value);
    75       var solutions = CreateCombinations(indices);
    76       solutions = SampleIndices(solutions, Sampling.Value.Value, random, SampleCount.Value.Value);
     89
     90      if (SamplingPoints.Value.Value.HasFlag(SamplingPointsType.BeforeCombinations))
     91        indices = indices.Select(i => SampleIndices(i, Sampling.Value.Value, random, SampleCount.Value.Value).ToList()).ToArray();
     92
     93      var solutions = CreateCombinations(indices[0], indices[1]);
     94
     95      if (SamplingPoints.Value.Value.HasFlag(SamplingPointsType.AfterCombinations))
     96        solutions = SampleIndices(solutions, Sampling.Value.Value, random, SampleCount.Value.Value);
     97
    7798      var best = FindBest(solutions);
    78       CopyTo(best.Item1, integerVector);
     99      if (best != null) {
     100        CopyTo(best.Item1, integerVector);
     101      }
    79102    }
    80103   
     
    84107      for (int i = 0; i < indices.Length; i++) {
    85108        var searchRange = dimension == DimensionType.All || targetIndex == i ? indexRange : SearchRangeType.None;
    86         indices[i] = CreateSingleIndices(bounds[i % bounds.Rows, 1], integerVector[i], searchRange);
     109        indices[i] = CreateSingleIndices(bounds[i % bounds.Rows, 1], integerVector[i], searchRange, random).ToList();
     110        if (!indices[i].Any()) {
     111          throw new InvalidOperationException("no indices!");
     112          //indices[i] = new[] { integerVector[i] };
     113        }
    87114      }
    88115      return indices;
    89116    }
    90117
    91     protected static IEnumerable<int> CreateSingleIndices(int length, int currentIndex, SearchRangeType searchRange) {
     118    protected static IEnumerable<int> CreateSingleIndices(int length, int currentIndex, SearchRangeType searchRange, IRandom random) {
    92119      switch (searchRange) {
    93120        case SearchRangeType.Full:
     
    95122        case SearchRangeType.None:
    96123          return new[] { currentIndex };
    97         case SearchRangeType.Left:
    98           return Enumerable.Range(0, currentIndex);
    99         case SearchRangeType.Right:
    100           return Enumerable.Range(currentIndex, length - currentIndex);
     124        case SearchRangeType.RandomDirection:
     125          return random.Next(2) == 0 ? Enumerable.Range(0, currentIndex) : Enumerable.Range(currentIndex, length - currentIndex);
     126        case SearchRangeType.RandomRange:
     127          int start = random.Next(0, length - 1), end = random.Next(start + 1, length);
     128          return Enumerable.Range(start, end - start);
    101129        default:
    102130          throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null);
     
    105133
    106134    protected static IEnumerable<IntegerVector> CreateCombinations(IEnumerable<int> startIndices, IEnumerable<int> endIndices) {
    107       return CreateCombinations(new[] { startIndices, endIndices });
    108     }
    109     protected static IEnumerable<IntegerVector> CreateCombinations(IEnumerable<IEnumerable<int>> indices) {
    110       return indices.CartesianProduct().Select(seq => new IntegerVector(seq.ToArray()));
     135      return
     136        from s in startIndices
     137        from e in endIndices
     138        where s < e
     139        select new IntegerVector(new [] { s, e });
    111140    }
    112141
     
    116145          return indices;
    117146        case SamplingType.RandomSampling:
    118           return indices.SampleRandom(random, count);
     147          return indices.SampleRandomWithoutRepetition(random, count);
    119148        case SamplingType.LinearSelection:
    120149          var indicesList = indices.ToList();
     
    130159    protected Tuple<IntegerVector, double> FindBest(IEnumerable<IntegerVector> solutions) {
    131160      var evaluatedSolutions = solutions.Select(s => Tuple.Create(s, Evaluate(s)));
    132       var best = evaluatedSolutions.OrderBy(t => t.Item2).First();
     161      var best = evaluatedSolutions.OrderBy(t => t.Item2).FirstOrDefault();
    133162      return best;
    134163    }
Note: See TracChangeset for help on using the changeset viewer.