Free cookie consent management tool by TermsFeed Policy Generator

Changeset 18230


Ignore:
Timestamp:
03/07/22 15:45:30 (2 years ago)
Author:
pfleck
Message:

#3040 Changed SubVector symbol to include end-index in result.

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  
    728728          }
    729729        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              }
    741746            }
    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              });
    751755          }
    752756        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              }
    765770            }
    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          }
    776780        case OpCodes.Variable: {
    777781            if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Mutators/NestedOptimizerSubVectorImprovementManipulator.cs

    r18229 r18230  
    9494
    9595    #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"]; }
    9898    }
    9999
     
    119119      var rs = new RandomSearchAlgorithm() {
    120120        Problem = problem,
    121         BatchSize = 10,
    122         MaximumEvaluatedSolutions = 100
     121        BatchSize = 100,
     122        MaximumEvaluatedSolutions = 1000
    123123      };
    124124
     
    126126        Problem = problem,
    127127        PlusSelection = new BoolValue(true),
    128         PopulationSize = new IntValue(1),
     128        PopulationSize = new IntValue(10),
    129129        Children = new IntValue(10),
    130130        MaximumGenerations = new IntValue(100)
     
    158158      var optimizers = new ItemSet<IAlgorithm>() { rs, es, ga, osga };
    159159
    160       Parameters.Add(new ConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs));
     160      Parameters.Add(new OptionalConstrainedValueParameter<IAlgorithm>("NestedOptimizer", optimizers, rs));
    161161      Parameters.Add(new FixedValueParameter<PercentValue>("PercentOptimizedSubVectorNodes", new PercentValue(1.0)));
    162162    }
     
    170170    [StorableConstructor]
    171171    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    }
    172183
    173184    public override void Manipulate(IRandom random, ISymbolicExpressionTree symbolicExpressionTree) {
     185      if (NestedOptimizer == null)
     186        return;
     187     
    174188      int vectorLengths = GetVectorLengths(ProblemDataParameter.ActualValue);
    175189     
     
    183197      algorithm.Start(CancellationToken);
    184198
    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
    188206      var solution = (IntegerVector)algorithm.Results[BestSolutionParameterName].Value;
    189207      UpdateFromVector(symbolicExpressionTree, selectedSubVectorNodes, solution, vectorLengths);
     
    204222    }
    205223
    206     private static int GetVectorLengths(T problemData) {  // ToDo evaluate a tree to get vector length per node
     224    private static int GetVectorLengths(T problemData) { // ToDo evaluate a tree to get vector length per node
    207225      var vectorLengths = problemData.Dataset.DoubleVectorVariables
    208226        .Select(v => problemData.Dataset.GetDoubleVectorValue(v, row: 0).Count)
     
    217235      foreach (var nodeIdx in selectedNodes) {
    218236        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
    221239      }
    222240    }
     
    231249      return tree.Root.GetSubtree(0).GetSubtree(0);
    232250    }
    233 
    234251  }
    235252}
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SegmentOptimization/SegmentOptimizationProblem.cs

    r18217 r18230  
    173173    }
    174174
    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    //}
    180180
    181181    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      //}
    185185
    186186      var resultValues = new double[data.Rows];
    187187      for (int row = 0; row < data.Rows; row++) {
    188188        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
    190190        switch (aggregation) {
    191191          case Aggregation.Sum:
Note: See TracChangeset for help on using the changeset viewer.