Example #1
0
int main()
{
    int x,y;
    BSTNode *root=0;
    Interval *interval=0;

    size_t len;
    char buf[4096];
    FILE *fi = fopen("TestOutput.out", "rb");
    FILE *fo=fopen("logs.txt","a");
    len = fread(buf, sizeof(char), sizeof(buf), fi);

    displayTime(fo);
    fprintf(fo,"Before execution:\n");
    fprintf(fo,"The checksum of %s is %#x\n","output.out", checkSum(buf, len, 0));

    fi=fopen("TestInput.in","r+");
    fo=fopen("TestOutput.out","w");

    //generateInput(nodeNr,intervalNr,pointNr,fi);

    fi=fopen("TestInput.in","r");
    for(int i=1; i<=nodeNr; i++)
    {
        interval=createNewInterval(interval,fi);
        root=insert(root,interval);
        free(interval);

    }
    fprintf(fo,"\n");
    for(int i=1; i<=intervalNr; i++) {
        interval=createNewInterval(interval,fi);
        fprintf(fo,"\n\nQuerry interval: [%d,%d] \n",interval->lo,interval->hi);
        fprintf(fo,"Intersected intervals:\n");
        segmentIntersect(root,interval,fo);
        free(interval);
    }
    for(int i=1; i<=pointNr; i++) {
        fprintf(fo,"\n");
        fscanf(fi,"%d",&x);
        fprintf(fo,"\n\nQuerry point:%d \n",x);
        searchPoint(root,x,fo);
        fprintf(fo,"\n");
    }
    fi = fopen("TestOutput.out", "rb");
    fo=fopen("logs.txt","a");
    len = fread(buf, sizeof(char), sizeof(buf), fi);
    fprintf(fo,"After execution:\n");
    fprintf(fo,"The checksum of %s is %#x\n\n","output.out", checkSum(buf, len, 0));
    return 0;
}
Example #2
0
void scChunkManager::genChunks(point p, measure_type radius) {
    std::random_device rd;
    std::mt19937_64 mt(rd()); //use the crappy random generator to make the seed for the good one
    ChunkGenerator cgen {mt, attributes};
    QRect allTileBounds (boundingRect(p, radius));

    //TODO: can totally vectorize this shit.
    for (measure_type x = allTileBounds.topLeft().x(); x < allTileBounds.topRight().x(); x++) {
        for (measure_type y = allTileBounds.bottomLeft().y(); y < allTileBounds.topLeft().y(); y++) {
            point searchPoint (x, y);
            chunk_iterator f (find(chunkList, searchPoint));
            unique_insert(chunkList, f, cgen(searchPoint));
        }
    }
}
//
// Sequential Minimal Optimisation (SMO) algorithm for Reduced Set Density Estimation (RSDE).
//    Finds weights to minimize : 0.5*wts*Q*wts'- wts*D'
//
void SMO(double* Q, double* D, unsigned int N, double* weights)
{
  unsigned int i,j,numChanged=0,I1,I2;
  double wtMax, sD, error1, error2=1e10;
  double *weightsBACKUP;
  bool* examine; 
  bool done = false, loop=false;
  examine = (bool*) mxMalloc(N*sizeof(bool));

  bool firstTime=true;

  weightsBACKUP = (double*) mxMalloc(N*sizeof(double));
  for (i=0,sD=0;i<N;i++) sD += D[i];
  for (i=0;i<N;i++) weights[i] = D[i]/sD;
  for (i=0;i<N;i++) examine[i] = true;

  while (!done) {
    wtMax = -1;
    for (i=0;i<N;i++) {
      if (firstTime) if (weights[i] < weightTolerance) examine[i] = false;
      if (examine[i] && (wtMax < weights[i])) { 
        wtMax = weights[i]; I2 = i; 
      }
    }
    double wI1_old;
    for (i=0;i<N;i++) weightsBACKUP[i] = weights[i];
    if (searchPoint(I1,I2,weights,Q,D,N,wI1_old)) numChanged++;

    loop = true;
    examine[I2] = false;  // don't care about matching I1 & I2 now
    if (weights[I1] == wI1_old) examine[I1] = 0;
    for (i=0;i<N;i++) {                    // check if we're done:
      if (weights[i] == wtMax) examine[i] = false;  // don't care about the maximal weight
      if (weights[i] == weights[I1] && i!=I1) examine[i] = false;
      if (examine[i]) loop = false;        // if still some to look at, not yet done with this set!
    }
    firstTime = false;

    if (loop) {
        error1=0;
        for (i=0;i<N;i++) {
          double tmp = 0;
          for (j=0;j<N;j++)
            tmp += Q[i+N*j]*weights[j];
          error1 += .5*weights[i]*tmp - weights[i]*D[i];
        }
        if (error1 > error2) {
          for (i=0;i<N;i++) weights[i] = weightsBACKUP[i];
//          printf("Error got worse!\n"); //should do: alpha=alpha_bk;
          done=true;
        } else if (fabs(error1-error2) < errorTolerance) done = true;
        
        if (numChanged==0) done = true;   // if nothing changed, we can quit
        if (~done) {
          loop = false; numChanged = 0;   // back to pairwise optimization steps
          for (i=0;i<N;i++) examine[i] = true; // consider everything again
          firstTime = true;
          error2 = error1;                  //  save this as the new error
        }
    }
//    printf("  -- %f\n",error2);
//    mexCallMATLAB(0, NULL, 0, NULL, "pause"); 
  }
  mxFree(examine);
}