Changeset 18204
- Timestamp:
- 01/18/22 16:35:16 (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/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs
r18193 r18204 37 37 38 38 [StorableType("65B2E870-7EED-4DD0-918E-94A346671784")] 39 p rotectedenum DimensionType {39 public enum DimensionType { 40 40 All, 41 41 Single 42 42 } 43 43 [StorableType("C73064F2-4E2F-473F-8DBA-DD47881726D4")] 44 p rotectedenum SearchRangeType {44 public enum SearchRangeType { 45 45 None, // Fixed on current value 46 46 Full, … … 49 49 } 50 50 [StorableType("A9EBE01E-53DE-4EB7-94B3-26C624034A08")] 51 p rotectedenum SamplingType {51 public enum SamplingType { 52 52 Exhaustive, 53 53 RandomSampling, … … 57 57 [StorableType("4EBF0DFE-90A1-4DFE-8CC7-D358EF8AF96B")] 58 58 [Flags] 59 p rotectedenum SamplingPointsType {59 public enum SamplingPointsType { 60 60 None = 0, 61 61 BeforeCombinations = 1, … … 63 63 } 64 64 65 protected ValueParameter<EnumValue<DimensionType>> Dimension => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"]; 66 protected ValueParameter<EnumValue<SearchRangeType>> SearchRange => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"]; 67 protected ValueParameter<EnumValue<SamplingType>> Sampling => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"]; 68 protected ValueParameter<IntValue> SampleCount => (ValueParameter<IntValue>)Parameters["SampleCount"]; 69 protected ValueParameter<EnumValue<SamplingPointsType>> SamplingPoints => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"]; 70 65 #region Properties 66 public DimensionType Dimension { get { return DimensionParameter.Value.Value; } set { DimensionParameter.Value.Value = value; } } 67 public SearchRangeType SearchRange { get { return SearchRangeParameter.Value.Value; } set { SearchRangeParameter.Value.Value = value; } } 68 public SamplingType Sampling { get { return SamplingParameter.Value.Value; } set { SamplingParameter.Value.Value = value; } } 69 public int SampleCount { get { return SampleCountParameter.Value.Value; } set { SampleCountParameter.Value.Value = value; } } 70 public SamplingPointsType SamplingPoints { get { return SamplingPointsParameter.Value.Value; } set { SamplingPointsParameter.Value.Value = value; } } 71 #endregion 72 73 #region Parameter Properties 74 public ValueParameter<EnumValue<DimensionType>> DimensionParameter => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"]; 75 public ValueParameter<EnumValue<SearchRangeType>> SearchRangeParameter => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"]; 76 public ValueParameter<EnumValue<SamplingType>> SamplingParameter => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"]; 77 public ValueParameter<IntValue> SampleCountParameter => (ValueParameter<IntValue>)Parameters["SampleCount"]; 78 public ValueParameter<EnumValue<SamplingPointsType>> SamplingPointsParameter => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"]; 79 #endregion 71 80 72 81 public SamplingSegmentOptimizationManipulator() { … … 86 95 87 96 protected override void ManipulateBounded(IRandom random, IntegerVector integerVector, IntMatrix bounds) { 88 var indices = CreateIndices(random, integerVector, bounds, Dimension.Value.Value, SearchRange.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();97 var indices = CreateIndices(random, new IntegerVector(new [] { integerVector.Min(), integerVector.Max() }), bounds, Dimension, SearchRange); 98 99 if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations)) 100 indices = indices.Select(i => SampleIndices(i, Sampling, random, SampleCount).ToList()).ToArray(); 92 101 93 102 var solutions = CreateCombinations(indices[0], indices[1]); 103 if (!solutions.Any()) { 104 if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations)) 105 return; // no valid combinations -> no mutation 106 throw new InvalidOperationException("no indices!"); 107 } 94 108 95 if (SamplingPoints. Value.Value.HasFlag(SamplingPointsType.AfterCombinations))96 solutions = SampleIndices(solutions, Sampling .Value.Value, random, SampleCount.Value.Value);109 if (SamplingPoints.HasFlag(SamplingPointsType.AfterCombinations)) 110 solutions = SampleIndices(solutions, Sampling, random, SampleCount); 97 111 98 112 var best = FindBest(solutions); … … 105 119 var indices = new IEnumerable<int>[integerVector.Length]; 106 120 int targetIndex = random.Next(indices.Length); // first or second index 121 integerVector = new IntegerVector(new[] { integerVector.Min(), integerVector.Max() }); 107 122 for (int i = 0; i < indices.Length; i++) { 108 123 var searchRange = dimension == DimensionType.All || targetIndex == i ? indexRange : SearchRangeType.None; … … 122 137 case SearchRangeType.None: 123 138 return new[] { 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); 139 case SearchRangeType.RandomDirection: // including currentIndex 140 return random.Next(2) == 0 141 ? Enumerable.Range(0, currentIndex + 1) // left 142 : Enumerable.Range(currentIndex, length - currentIndex); // right 143 case SearchRangeType.RandomRange: // random range around current index, not completely random range 144 int start = random.Next(0, currentIndex + 1), end = random.Next(currentIndex, length); 145 return Enumerable.Range(start, end - start + 1); 129 146 default: 130 147 throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null); … … 148 165 case SamplingType.LinearSelection: 149 166 var indicesList = indices.ToList(); 150 var selected = MathNet.Numerics.Generate.LinearSpaced(count, 0, indicesList.Count )151 .Select(i => (int)Math. Round(i))167 var selected = MathNet.Numerics.Generate.LinearSpaced(count, 0, indicesList.Count - 1) // LinearSpaced stop is inclusive 168 .Select(i => (int)Math.Floor(i)) 152 169 .Distinct(); 153 170 return selected.Select(i => indicesList[i]); -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs
r18202 r18204 183 183 184 184 var resultValues = new double[data.Rows]; 185 var array = new double[data.Columns];186 185 for (int row = 0; row < data.Rows; row++) { 187 for (int i = 0; i < array.Length; i++) 188 array[i] = data[row, i]; 189 190 //var vector = DoubleVector.Build.DenseOfArray(array); 191 //var segment = vector.SubVector(bounds.Start, bounds.Size); 192 var segment = DoubleVector.Build.DenseOfEnumerable(array.Skip(bounds.Start).Take(bounds.Size)); 193 186 var vector = data.GetRow(row); 187 var segment = vector.Skip(bounds.Start).Take(bounds.Size); 194 188 switch (aggregation) { 195 189 case Aggregation.Sum:
Note: See TracChangeset
for help on using the changeset viewer.