Changeset 18214
- Timestamp:
- 02/08/22 17:36:18 (3 years ago)
- 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 177 177 <Compile Include="Interfaces\ISymbolicDataAnalysisExpressionManipulator.cs" /> 178 178 <Compile Include="Mutators\SegmentOptimization\SamplingSegmentOptimizationManipulator.cs" /> 179 <Compile Include="Mutators\SamplingSubVectorImprovementManipulator.cs" /> 179 180 <Compile Include="Mutators\SymbolicDataAnalysisExpressionManipulator.cs" /> 180 181 <Compile Include="Crossovers\SymbolicDataAnalysisExpressionDiversityPreservingCrossover.cs" /> -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs
r18060 r18214 729 729 case OpCodes.SubVector: { 730 730 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); 735 734 if (startIdx < endIdx) { 736 735 return v.SubVector(startIdx, count: endIdx - startIdx); -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/MultiSymbolicDataAnalysisExpressionManipulator.cs
r18082 r18214 124 124 125 125 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(); 127 129 var dataAnalysisManipulators = from type in ApplicationManager.Manager.GetTypes(typeof(ISymbolicDataAnalysisExpressionManipulator<T>)) 128 130 where this.GetType().Assembly == type.Assembly -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/SegmentOptimization/SamplingSegmentOptimizationManipulator.cs
r18212 r18214 46 46 Full, 47 47 RandomDirection, 48 RandomRange 48 RandomRange, 49 DirectedDirection, 50 DirectedRange 49 51 } 50 52 [StorableType("A9EBE01E-53DE-4EB7-94B3-26C624034A08")] … … 66 68 public DimensionType Dimension { get { return DimensionParameter.Value.Value; } set { DimensionParameter.Value.Value = value; } } 67 69 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; } } 68 71 public SamplingType Sampling { get { return SamplingParameter.Value.Value; } set { SamplingParameter.Value.Value = value; } } 69 72 public int SampleCount { get { return SampleCountParameter.Value.Value; } set { SampleCountParameter.Value.Value = value; } } … … 74 77 public ValueParameter<EnumValue<DimensionType>> DimensionParameter => (ValueParameter<EnumValue<DimensionType>>)Parameters["Dimension"]; 75 78 public ValueParameter<EnumValue<SearchRangeType>> SearchRangeParameter => (ValueParameter<EnumValue<SearchRangeType>>)Parameters["SearchRange"]; 79 public ValueParameter<IntValue> SlopeCalculationRangeParameter => (ValueParameter<IntValue>)Parameters["SlopeCalculationRange"]; 76 80 public ValueParameter<EnumValue<SamplingType>> SamplingParameter => (ValueParameter<EnumValue<SamplingType>>)Parameters["Sampling"]; 77 81 public ValueParameter<IntValue> SampleCountParameter => (ValueParameter<IntValue>)Parameters["SampleCount"]; 78 82 public ValueParameter<EnumValue<SamplingPointsType>> SamplingPointsParameter => (ValueParameter<EnumValue<SamplingPointsType>>)Parameters["SamplingPoints"]; 79 83 public ValueParameter<BoolValue> CountSamplesAsEvaluationsParameter => (ValueParameter<BoolValue>)Parameters["CountSamplesAsEvaluations"]; 84 80 85 public LookupParameter<IntValue> EvaluatedSolutionsParameter => (LookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; 81 86 #endregion … … 84 89 Parameters.Add(new ValueParameter<EnumValue<DimensionType>>("Dimension", new EnumValue<DimensionType>(DimensionType.All))); 85 90 Parameters.Add(new ValueParameter<EnumValue<SearchRangeType>>("SearchRange", new EnumValue<SearchRangeType>(SearchRangeType.Full))); 91 Parameters.Add(new ValueParameter<IntValue>("SlopeCalculationRange", new IntValue(5))); 86 92 Parameters.Add(new ValueParameter<EnumValue<SamplingType>>("Sampling", new EnumValue<SamplingType>(SamplingType.Exhaustive))); 87 93 Parameters.Add(new ValueParameter<IntValue>("SampleCount")); // only used when sampling != Exhaustive … … 95 101 return new SamplingSegmentOptimizationManipulator(this, cloner); 96 102 } 103 97 104 [StorableConstructor] 98 105 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 100 112 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); 102 114 103 115 if (SamplingPoints.HasFlag(SamplingPointsType.BeforeCombinations)) … … 119 131 } 120 132 121 var best = FindBest(solutions );133 var best = FindBest(solutions, Evaluate); 122 134 if (best != null) { 123 135 CopyTo(best.Item1, integerVector); … … 125 137 } 126 138 127 p rivate 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) { 128 140 var indices = new IEnumerable<int>[integerVector.Length]; 129 141 int targetIndex = random.Next(indices.Length); // first or second index 130 142 for (int i = 0; i < indices.Length; i++) { 131 143 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(); 133 145 if (!indices[i].Any()) { 134 146 throw new InvalidOperationException("no indices!"); … … 139 151 } 140 152 141 p rotected 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) { 142 154 switch (searchRange) { 143 155 case SearchRangeType.Full: … … 152 164 int start = random.Next(0, currentIndex + 1), end = random.Next(currentIndex, length); 153 165 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); 154 173 default: 155 174 throw new ArgumentOutOfRangeException(nameof(searchRange), searchRange, null); … … 157 176 } 158 177 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) { 160 201 return 161 202 from s in startIndices … … 165 206 } 166 207 167 p rotectedstatic 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) { 168 209 switch (sampling) { 169 210 case SamplingType.Exhaustive: … … 173 214 case SamplingType.LinearSelection: 174 215 var indicesList = indices.ToList(); 216 // if single sample, the last is always returned 175 217 var selected = MathNet.Numerics.Generate.LinearSpaced(count, 0, indicesList.Count - 1) // LinearSpaced stop is inclusive 176 218 .Select(i => (int)Math.Floor(i)) … … 182 224 } 183 225 184 p rotected 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(); 188 230 } 189 231
Note: See TracChangeset
for help on using the changeset viewer.