0

Whilst undergoing a university project using R to calculate the accurateness of a data mining technique algorithm, I and my supervisor agreed in implementing the DP algorithm shown here http://www.iwaponline.com/jh/012/0318/0120318.pdf , that for time series of river height taken at 15 minute intervals a function to calculate all segmentation paths is required.

Whilst writing this function 'Minimize', --not completed by the way but does still work. I encountered the following error 'Error: C stack usage is too close to the limit' and i don't want to carry on until this part works first below is my R code followed by the java code for the function i am trying to copy further followed the example test series is: 1,2,3,4,5,4,3,2,1,2,3,4,5 so T=13 currently i have been trying to get the example to run with K=3 but the Minimize function will eventually be used within a loop of K=1 to K=20. The calerror function in the java calculates the lms error of each segment in my R code i haven't implemented this function yet so is currently commented out. The goal of the Minimize function is to calculate the error for each possible segment and put in an array called cost, which is used to create a matrix which is used to create a matrix of errors.

If anybody has any idea why I get and to possibly correct this error your help is much needed and would be greatly appreciated as well if anybody has any advice in converting JAVA code into R code.

#Recursive method for exploring all paths
library(zoo)
fmt = '%d-%b-%Y %H:%M'
data = read.zoo("desktop/test.csv",header=TRUE,sep=',',tz='',format=fmt,index=0:1)
Values = coredata(data) #strips date/time of values for computation 
Number=plain #array of indexed values

T=length(data)
r=1
K=3

Compare = function (START,END){
  if (START<END){
    boo = -1
  }
  if (START==END){
    boo = 0
  }
  if (START>END){
    boo = 1
  }
  return(boo)
}



Minimize = function(T,K,r){
  if(r<K){
  H=T-K+r
    for(r in 1:H){
      START = Number[1]
       END = Number[r]
      boo1=Compare(START,END)
      if (boo1 !=0){
       cost[r]=cost[r-1]#+calerror(START,END)
      }else{
        cost[r]=0
        r=r+1
        Minimize(T,K,r)
      }
    }
}else{
}
  return(cost)
} 
tester=Minimize(T,K,r)
tester

Finished Java code that works below:

private void minimize(int T, int K, int k) {
        // recursive method for exploring all the paths
        if(k<K){
            for(t[k]=t[k-1];t[k]<T-K+k-1;t[k]++){
                Map.Entry<Date, V> e = inData.getEntryAtIndex(t[k-1]);
                Map.Entry<Date, V> f = inData.getEntryAtIndex(t[k]+1);
                Interval interval = new Interval(e.getKey(),f.getKey());
                //System.out.println(interval);
                if(interval.getStart().compareTo(interval.getEnd())!=0)
                    cost[k]=cost[k-1]+calError(interval);
                else cost[k] = 0;
                minimize(T,K,k+1);
            }
        }
        else{
            for(t[k]=t[k-1];t[k]<T-K+k-1;t[k]++){
                Map.Entry<Date, V> g = inData.getEntryAtIndex(t[k-1]);
                Map.Entry<Date, V> h = inData.getEntryAtIndex(t[k]+1);
                Interval interval1 = new Interval(g.getKey(),h.getKey());
                //System.out.println(interval1);
                if(interval1.getStart().compareTo(interval1.getEnd())!=0)
                    cost[k]=cost[k-1]+calError(interval1);
                else cost[k] = 0;
                Map.Entry<Date, V> i = inData.getEntryAtIndex(t[k]);
                Map.Entry<Date, V> j = inData.getEntryAtIndex(T-1);
                Interval interval2 = new Interval(i.getKey(),j.getKey());
                if(interval2.getStart().compareTo(interval2.getEnd())!=0)
                    cost[k+1]=cost[k]+calError(interval2);
                else cost[k+1]=0;

                if(ckt[K]>cost[k+1]){
                    /*for(int l=0;l<K+1;l++)
                        System.out.println(ckt[l]);*/
                    ckt[K] = cost[k+1];
                    /*for(int l=0;l<K+1;l++)
                        System.out.println(ckt[l]);*/
                    for(int l=0;l<K+1;l++)
                        tOpti[K][l]=t[l];
                    /*for(int l = 0;l<K+1;l++)
                        System.out.print(tOpti[K][l]+",");
                    System.out.println(ckt[K]);*/
                }
            }
        }
    }
IRTFM
  • 258,963
  • 21
  • 364
  • 487
blane clorley
  • 109
  • 1
  • 2
  • 10
  • 1
    Had you searched for the error message, you would have found it's related to recursion ([see here](http://stackoverflow.com/q/14719349/271616)). Also, `Compare` could be greatly simplified to `Compare <- function(x,y) sign(x-y)` – Joshua Ulrich Apr 22 '13 at 18:29
  • Yes I've read that article and I have increased ulimit to 16384. Yes true thanks. Any other ideas? – blane clorley Apr 22 '13 at 18:52

0 Answers0