Changeset 18230
- Timestamp:
- 03/07/22 15:45:30 (3 years ago)
- Location:
- branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs
r18214 r18230 728 728 } 729 729 case OpCodes.SubVector: { 730 DoubleVector SubVector(DoubleVector v , double start, double end) { 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); 734 if (startIdx < endIdx) { 735 return v.SubVector(startIdx, count: endIdx - startIdx); 736 } else { // wrap around 737 var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx)); 738 v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx] 739 v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx); // copy [0:endIdx] to [size-startIdx:size] 740 return resultVector; 730 DoubleVector SubVector(DoubleVector v , double start, double end) { 731 int size = v.Count; 732 int ToIdx(double x) { 733 int idx = (int)Math.Round(x * (size - 1)); 734 return (idx % size + size) % size; // positive mod 735 } 736 int startIdx = ToIdx(start); 737 int endIdx = ToIdx(end); 738 if (startIdx <= endIdx) { 739 return v.SubVector(startIdx, count: endIdx - startIdx + 1); // incl end 740 } else { // wrap around 741 var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx) + 1); 742 v.CopySubVectorTo(resultVector, sourceIndex: startIdx, targetIndex: 0, count: size - startIdx); // copy [startIdx:size] to [0:size-startIdx] 743 v.CopySubVectorTo(resultVector, sourceIndex: 0, targetIndex: size - startIdx, count: endIdx); // copy [0:endIdx] to [size-startIdx:size] 744 return resultVector; 745 } 741 746 } 742 } 743 var cur = Evaluate(dataset, ref row, state, traceDict); 744 TraceEvaluation(currentInstr, cur); 745 return FunctionApply(cur, 746 s => s, 747 v => { 748 var node = (IWindowedSymbolTreeNode)currentInstr.dynamicNode; 749 return SubVector(v, node.Offset, node.Length); 750 }); 747 var cur = Evaluate(dataset, ref row, state, traceDict); 748 TraceEvaluation(currentInstr, cur); 749 return FunctionApply(cur, 750 s => s, 751 v => { 752 var node = (IWindowedSymbolTreeNode)currentInstr.dynamicNode; 753 return SubVector(v, node.Offset, node.Length); 754 }); 751 755 } 752 756 case OpCodes.SubVectorSubtree: { 753 DoubleVector SubVector(DoubleVector v, double start, double end) { 754 int Mod(int x, int m) => (x % m + m) % m; 755 int startIdx = Mod((int)Math.Round(start * v.Count), v.Count); 756 int endIdx = Mod((int)Math.Round(end * v.Count), v.Count); 757 int size = v.Count; 758 if (startIdx < endIdx) { 759 return v.SubVector(startIdx, count: endIdx - startIdx); 760 } else { // wrap around 761 var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx)); 762 v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx] 763 v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx); // copy [0:endIdx] to [size-startIdx:size] 764 return resultVector; 757 DoubleVector SubVector(DoubleVector v, double start, double end) { 758 int Mod(int x, int m) => (x % m + m) % m; 759 int startIdx = Mod((int)Math.Round(start * v.Count), v.Count); 760 int endIdx = Mod((int)Math.Round(end * v.Count), v.Count); 761 int size = v.Count; 762 if (startIdx < endIdx) { 763 return v.SubVector(startIdx, count: endIdx - startIdx); 764 } else { // wrap around 765 var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx)); 766 v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx] 767 v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx); // copy [0:endIdx] to [size-startIdx:size] 768 return resultVector; 769 } 765 770 } 766 } 767 var cur = Evaluate(dataset, ref row, state, traceDict); 768 var offset = Evaluate(dataset, ref row, state, traceDict); 769 var length = Evaluate(dataset, ref row, state, traceDict); 770 TraceEvaluation(currentInstr, cur); 771 return FunctionApply(cur, 772 s => s, 773 v => SubVector(v, offset.Scalar, length.Scalar) 774 ); 775 } 771 var cur = Evaluate(dataset, ref row, state, traceDict); 772 var offset = Evaluate(dataset, ref row, state, traceDict); 773 var length = Evaluate(dataset, ref row, state, traceDict); 774 TraceEvaluation(currentInstr, cur); 775 return FunctionApply(cur, 776 s => s, 777 v => SubVector(v, offset.Scalar, length.Scalar) 778 ); 779 } 776 780 case OpCodes.Variable: { 777 781 if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN; -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/NestedOptimizerSubVectorImprovementManipulator.cs
r18229 r18230 94 94 95 95 #region Parameter Properties 96 public IConstrainedValueParameter<IAlgorithm> NestedOptimizerParameter {97 get { return ( IConstrainedValueParameter<IAlgorithm>)Parameters["NestedOptimizer"]; }96 public OptionalConstrainedValueParameter<IAlgorithm> NestedOptimizerParameter { 97 get { return (OptionalConstrainedValueParameter<IAlgorithm>)Parameters["NestedOptimizer"]; } 98 98 } 99 99 … … 119 119 var rs = new RandomSearchAlgorithm() { 120 120 Problem = problem, 121 BatchSize = 10 ,122 MaximumEvaluatedSolutions = 100 121 BatchSize = 100, 122 MaximumEvaluatedSolutions = 1000 123 123 }; 124 124 … … 126 126 Problem = problem, 127 127 PlusSelection = new BoolValue(true), 128 PopulationSize = new IntValue(1 ),128 PopulationSize = new IntValue(10), 129 129 Children = new IntValue(10), 130 130 MaximumGenerations = new IntValue(100) … … 158 158 var optimizers = new ItemSet<IAlgorithm>() { rs, es, ga, osga }; 159 159 160 Parameters.Add(new ConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs));160 Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs)); 161 161 Parameters.Add(new FixedValueParameter<PercentValue>("PercentOptimizedSubVectorNodes", new PercentValue(1.0))); 162 162 } … … 170 170 [StorableConstructor] 171 171 private NestedOptimizerSubVectorImprovementManipulator(StorableConstructorFlag _) : base(_) { } 172 [StorableHook(HookType.AfterDeserialization)] 173 private void AfterDeserialization() { 174 if (Parameters.TryGetValue("NestedOptimizer", out var param)) { 175 if (param is ConstrainedValueParameter<IAlgorithm> constrainedParam) { 176 Parameters.Remove("NestedOptimizer"); 177 Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", 178 new ItemSet<IAlgorithm>(constrainedParam.ValidValues), constrainedParam.Value) 179 ); 180 } 181 } 182 } 172 183 173 184 public override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) { 185 if (NestedOptimizer == null) 186 return; 187 174 188 int vectorLengths = GetVectorLengths(ProblemDataParameter.ActualValue); 175 189 … … 183 197 algorithm.Start(CancellationToken); 184 198 185 if (algorithm.ExecutionState != ExecutionState.Stopped) 186 throw new InvalidOperationException("Nested Algorithm did not finish."); 187 199 //if (algorithm.ExecutionState != ExecutionState.Stopped) 200 // return; 201 202 if (!algorithm.Results.ContainsKey(BestSolutionParameterName)) 203 return; 204 205 // use the latest best result 188 206 var solution = (IntegerVector)algorithm.Results[BestSolutionParameterName].Value; 189 207 UpdateFromVector(symbolicExpressionTree, selectedSubVectorNodes, solution, vectorLengths); … … 204 222 } 205 223 206 private static int GetVectorLengths(T problemData) { 224 private static int GetVectorLengths(T problemData) { // ToDo evaluate a tree to get vector length per node 207 225 var vectorLengths = problemData.Dataset.DoubleVectorVariables 208 226 .Select(v => problemData.Dataset.GetDoubleVectorValue(v, row: 0).Count) … … 217 235 foreach (var nodeIdx in selectedNodes) { 218 236 var node = nodes[nodeIdx]; 219 node.Offset = (double)solution[i++] / (vectorLength - 1); 220 node.Length = (double)solution[i++] / (vectorLength - 1); 237 node.Offset = (double)solution[i++] / (vectorLength - 1); // round in case of float 238 node.Length = (double)solution[i++] / (vectorLength - 1); // round in case of float 221 239 } 222 240 } … … 231 249 return tree.Root.GetSubtree(0).GetSubtree(0); 232 250 } 233 234 251 } 235 252 } -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs
r18217 r18230 173 173 } 174 174 175 private static double BoundedAggregation(DoubleArray data, IntRange bounds, Aggregation aggregation) {176 var matrix = new DoubleMatrix(1, data.Length);177 for (int i = 0; i < data.Length; i++) matrix[0, i] = data[i];178 return BoundedAggregation(matrix, bounds, aggregation);179 }175 //private static double BoundedAggregation(DoubleArray data, IntRange bounds, Aggregation aggregation) { 176 // var matrix = new DoubleMatrix(1, data.Length); 177 // for (int i = 0; i < data.Length; i++) matrix[0, i] = data[i]; 178 // return BoundedAggregation(matrix, bounds, aggregation); 179 //} 180 180 181 181 private static double BoundedAggregation(DoubleMatrix data, IntRange bounds, Aggregation aggregation) { 182 if (bounds.Size == 0) {183 return 0;184 }182 //if (bounds.Size == 0) { 183 // return 0; 184 //} 185 185 186 186 var resultValues = new double[data.Rows]; 187 187 for (int row = 0; row < data.Rows; row++) { 188 188 var vector = data.GetRow(row); 189 var segment = vector.Skip(bounds.Start).Take(bounds.Size );189 var segment = vector.Skip(bounds.Start).Take(bounds.Size + 1); // exclusive end 190 190 switch (aggregation) { 191 191 case Aggregation.Sum:
Note: See TracChangeset
for help on using the changeset viewer.