Changeset 18193
- Timestamp:
- 01/13/22 16:32:03 (3 years ago)
- 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 465 465 </ProjectReference> 466 466 </ItemGroup> 467 <ItemGroup>468 <PackageReference Include="OneOf">469 <Version>3.0.205</Version>470 </PackageReference>471 </ItemGroup>472 467 <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> 473 468 <!-- 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 36 36 public class SamplingSegmentOptimizationManipulator : SegmentOptimizationMutator { 37 37 38 [StorableType("65B2E870-7EED-4DD0-918E-94A346671784")] 38 39 protected enum DimensionType { 39 40 All, 40 41 Single 41 42 } 43 [StorableType("C73064F2-4E2F-473F-8DBA-DD47881726D4")] 42 44 protected enum SearchRangeType { 45 None, // Fixed on current value 43 46 Full, 44 None, 45 Left, 46 Right 47 RandomDirection, 48 RandomRange 47 49 } 50 [StorableType("A9EBE01E-53DE-4EB7-94B3-26C624034A08")] 48 51 protected enum SamplingType { 49 52 Exhaustive, … … 52 55 } 53 56 57 [StorableType("4EBF0DFE-90A1-4DFE-8CC7-D358EF8AF96B")] 58 [Flags] 59 protected enum SamplingPointsType { 60 None = 0, 61 BeforeCombinations = 1, 62 AfterCombinations = 2 63 } 64 54 65 protected ValueParameter<EnumValue<DimensionType>> Dimension => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"]; 55 66 protected ValueParameter<EnumValue<SearchRangeType>> SearchRange => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"]; 56 67 protected ValueParameter<EnumValue<SamplingType>> Sampling => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"]; 57 68 protected ValueParameter<IntValue> SampleCount => (ValueParameter<IntValue>)Parameters["SampleCount"]; 69 protected ValueParameter<EnumValue<SamplingPointsType>> SamplingPoints => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"]; 70 58 71 59 72 public SamplingSegmentOptimizationManipulator() { … … 62 75 Parameters.Add(new ValueParameter<EnumValue<SamplingType>>("Sampling", new EnumValue<SamplingType>(SamplingType.Exhaustive))); 63 76 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))); 64 78 } 65 79 public SamplingSegmentOptimizationManipulator(SamplingSegmentOptimizationManipulator original, Cloner cloner) … … 73 87 protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) { 74 88 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 77 98 var best = FindBest(solutions); 78 CopyTo(best.Item1, integerVector); 99 if (best != null) { 100 CopyTo(best.Item1, integerVector); 101 } 79 102 } 80 103 … … 84 107 for (int i = 0; i < indices.Length; i++) { 85 108 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 } 87 114 } 88 115 return indices; 89 116 } 90 117 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) { 92 119 switch (searchRange) { 93 120 case SearchRangeType.Full: … … 95 122 case SearchRangeType.None: 96 123 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); 101 129 default: 102 130 throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null); … … 105 133 106 134 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 }); 111 140 } 112 141 … … 116 145 return indices; 117 146 case SamplingType.RandomSampling: 118 return indices.SampleRandom (random, count);147 return indices.SampleRandomWithoutRepetition(random, count); 119 148 case SamplingType.LinearSelection: 120 149 var indicesList = indices.ToList(); … … 130 159 protected Tuple<IntegerVector, double> FindBest(IEnumerable<IntegerVector> solutions) { 131 160 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(); 133 162 return best; 134 163 }
Note: See TracChangeset
for help on using the changeset viewer.