Beispiel #1
0
TEST(Regression, Sort)
{
    std::uniform_int_distribution<int> element_dist(std::numeric_limits<int>::min(),
            std::numeric_limits<int>::max());
    std::mt19937 engine(time(0));

    std::vector<int> vs(100000);
    auto f = [&]() {
        return element_dist(engine);
    };
    std::generate(std::begin(vs), std::end(vs), f);
    selection_sort(std::begin(vs), std::end(vs));
    ASSERT_TRUE(std::is_sorted(std::begin(vs), std::end(vs)));
}
Beispiel #2
0
int main(int argc, const char *argv[])
{
    std::numeric_limits<int> limits;

    std::mt19937 engine(time(0)); // Fixed seed of 0
    std::uniform_int_distribution<int> range_dist(0,150);
    std::uniform_int_distribution<int> element_dist(0, limits.max());
   
    size_t n = 100000000;
    auto f = [&](){return element_dist(engine);};
    for(size_t i = 0; i < n; ++i)
    {
        std::vector<int> vs;
        std::generate_n(std::back_inserter(vs), range_dist(engine), f);
        selection_sort(std::begin(vs), std::end(vs));
        assert(std::is_sorted(std::begin(vs), std::end(vs)));
        if(i %100000 == 0)
            std::cout << static_cast<double>(i)/n*100 << "%% complete\n";
    }
    return 0;
}
Beispiel #3
0
std::vector<std::vector<int>> GenerateTestCases()
{
    std::vector<TestInput> test_cases;
    std::numeric_limits<int> limits;

    std::mt19937 engine(time(0));
    std::uniform_int_distribution<int> range_dist(0,1500);
    std::uniform_int_distribution<int> element_dist(
            limits.min(), 
            limits.max());

    size_t n = 10000;
    auto f = [&](){return element_dist(engine);};
    std::generate_n(std::back_inserter(test_cases), n, 
            [&](){ 
            TestInput vs;
            std::generate_n(std::back_inserter(vs), 
                range_dist(engine), 
                f);
            return vs;
            });
    return test_cases;
}
void simpair_detection (double *stream, double *stream2)
{

  long int	i, j;
  int		l;
  long int	band_sp, band_ep;
  int		lx, ly;	
  int		max_p;
  double	L;
  double	dist;
  double	s_best, s_diag, s_vert, s_hori;
  double	Bv, Bh, Bd;
  double	diagpt;
  struct MatrixElement	*swap;


  /*
   * Initialize each parameter
   */
  listnum = 0;
  lx = ly = 0;
  L = 0;
  dist = 0;
  s_best = 0;
  s_diag = s_vert = s_hori = 0;
  Bv = (double)Ty / 2;
  Bh = (double)Tx / 2;
  Bd = (double)(Tx + Ty) / 2;

  if (Tx >= Ty) max_p = Tx;
  else max_p = Ty;


  /*
   * Compute scores and starting positions
   */

  for (i = 1; i <= sqd[0]->length; i++) {

    // Update Sacoe-Chiba band
    diagpt = i * (double)sqd[1]->length/sqd[0]->length;
    band_sp = (int)floor(diagpt - scwidth);
    band_ep = (int)ceil(diagpt + scwidth);
    if (band_sp < 1) {
      band_sp = 1;
    }
    if (band_ep > sqd[1]->length) {
      band_ep = sqd[1]->length;
    }

    for (j = band_sp; j <= band_ep; j++) {

      dist = element_dist (&stream[i-1], &stream2[j-1]);
      s_diag = Bd*epsilon - max_p*dist + pre_column[j-1].score;
      s_vert = Bv*epsilon - Ty*dist + cur_column[j-1].score;
      s_hori = Bh*epsilon - Tx*dist + pre_column[j].score;
      s_best = max (&s_diag, &s_vert, &s_hori);

      cur_column[j].score = s_best;

      // Reset score
      if (cur_column[j].score <= 0) {
        cur_column[j].score = 0;
    	cur_column[j].x_sp = i;
	cur_column[j].y_sp = j;
    	cur_column[j].x_sp_org = i * Tx;
	cur_column[j].y_sp_org = j * Ty;
      // Keep score of neighboring cells
      } else {
        if (s_best == s_diag && pre_column[j-1].score != 0) {  // Select diagonal cell
    	  cur_column[j].x_sp = pre_column[j-1].x_sp;
	  cur_column[j].y_sp = pre_column[j-1].y_sp;
    	  cur_column[j].x_sp_org = pre_column[j-1].x_sp_org;
	  cur_column[j].y_sp_org = pre_column[j-1].y_sp_org;
        } else if (s_best == s_vert && cur_column[j-1].score != 0) {  // Select vertical cell
	  cur_column[j].x_sp = cur_column[j-1].x_sp;
	  cur_column[j].y_sp = cur_column[j-1].y_sp;
	  cur_column[j].x_sp_org = cur_column[j-1].x_sp_org;
	  cur_column[j].y_sp_org = cur_column[j-1].y_sp_org;
        } else if (s_best == s_hori && pre_column[j].score != 0) {  // Select horizontal cell
	  cur_column[j].x_sp = pre_column[j].x_sp;
	  cur_column[j].y_sp = pre_column[j].y_sp;
	  cur_column[j].x_sp_org = pre_column[j].x_sp_org;
	  cur_column[j].y_sp_org = pre_column[j].y_sp_org;
        } else {  // Set current cell as starting position
    	  cur_column[j].x_sp = i;
	  cur_column[j].y_sp = j;
    	  cur_column[j].x_sp_org = (i-1) * Tx + 1;
	  cur_column[j].y_sp_org = (j-1) * Ty + 1;
	}
      }

      // Compute DTW distance
      lx = i*Tx - cur_column[j].x_sp_org + 1;
      ly = j*Ty - cur_column[j].y_sp_org + 1;
      L = (double)(lx + ly) / 2;
      cur_column[j].dist = epsilon*L - cur_column[j].score;


      /*
       * Update candidate pairs in list arrays
       */

      if (cur_column[j].score >= epsilon*Lmin) {

	// Add subsequence pair as a new candidate
	if (listnum == 0) {
	  list[listnum].flag = 0;
	  list[listnum].x_sp = cur_column[j].x_sp;
	  list[listnum].y_sp = cur_column[j].y_sp;
	  list[listnum].x_sp_org = cur_column[j].x_sp_org;
	  list[listnum].y_sp_org = cur_column[j].y_sp_org;
	  list[listnum].x_ep = i;
	  list[listnum].y_ep = j;
	  list[listnum].x_ep_org = i * Tx;
	  list[listnum].y_ep_org = j * Ty;
	  lx = list[listnum].x_ep - list[listnum].x_sp + Tx;
	  ly = list[listnum].y_ep - list[listnum].y_sp + Ty;
	  list[listnum].length = (double)(lx + ly)/2;
  	  list[listnum].score = cur_column[j].score;
	  list[listnum].dist = cur_column[j].dist;
	  listnum++;
	} else {
  	  for (l = 0; l < listnum; l++) {
	    if (list[l].x_sp == cur_column[j].x_sp
		&& list[l].y_sp == cur_column[j].y_sp) { 
 	      list[l].flag = 0;
	      // Overwrite maximum score
     	      if (cur_column[j].score-epsilon*Lmin >= list[l].score-epsilon*Lmin) {
		list[l].score = cur_column[j].score;
		list[l].dist = cur_column[j].dist;
	        list[l].x_ep = i;
	        list[l].y_ep = j;
	        list[l].x_ep_org = i * Tx;
	        list[l].y_ep_org = j * Ty;
		lx = list[l].x_ep_org - list[l].x_sp_org + 1;
		ly = list[l].y_ep_org - list[l].y_sp_org + 1;
		list[l].length = (double)(lx + ly)/2;
	      } 
	      break;
	    // Add subsequence pair as a new candidate
	    } else if (l == listnum-1) {
	      list[listnum].flag = 0;
	      list[listnum].x_sp = cur_column[j].x_sp;
	      list[listnum].y_sp = cur_column[j].y_sp;
	      list[listnum].x_sp_org = cur_column[j].x_sp_org;
	      list[listnum].y_sp_org = cur_column[j].y_sp_org;
	      list[listnum].x_ep = i;
	      list[listnum].y_ep = j;
	      list[listnum].x_ep_org = i * Tx;
	      list[listnum].y_ep_org = j * Ty;
	      lx = list[listnum].x_ep_org - list[listnum].x_sp_org + 1;
      	      ly = list[listnum].y_ep_org - list[listnum].y_sp_org + 1;
	      list[listnum].length = (double)(lx + ly)/2;
	      list[listnum].score = cur_column[j].score;
	      list[listnum].dist = cur_column[j].dist;
	      listnum++;
	      if (listnum >= LIST) {
	        printf ("The number of list is over %d\n", LIST);
	        exit(1);
	      }
	      break;
	    }
  	  }
        }

      // Processing for report
      } else if (listnum > 0) {
        // Check whether starting positions in current column correspond to the starting positions of candidate subsequences
    	for (l = 0; l < listnum; l++) {
	  if (list[l].flag == 1) {
	    if (list[l].x_sp == cur_column[j].x_sp
		&& list[l].y_sp == cur_column[j].y_sp) {
	      list[l].flag = 0;
	    }
	  }
  	}
      }

      /* Initialization for next computation */
      L = 0;
      lx = ly = 0;
      dist = 0;
      s_best = 0;
      s_diag = s_vert = s_hori = 0;

    }


    /*
     * Report optimal subsequence pair 
     */
    if (listnum > 0) {
      for (l = 0; l < listnum; l++) {
	if (list[l].flag == 1) {
  	  printf ("X[%5d:%5d] \t Y[%5d:%5d] \t DTWdist: %.1f \t score: %.2f\n",
		  list[l].x_sp, list[l].x_ep, list[l].y_sp, list[l].y_ep, list[l].dist, list[l].score);
	  // Delete the reported pair in list arrays 
	  list[l].flag = list[listnum-1].flag;
	  list[l].x_sp = list[listnum-1].x_sp;
	  list[l].y_sp = list[listnum-1].y_sp;
	  list[l].x_sp_org = list[listnum-1].x_sp_org;
	  list[l].y_sp_org = list[listnum-1].y_sp_org;
	  list[l].x_ep = list[listnum-1].x_ep;
	  list[l].y_ep = list[listnum-1].y_ep;
	  list[l].x_ep_org = list[listnum-1].x_ep_org;
	  list[l].y_ep_org = list[listnum-1].y_ep_org;
	  list[l].length = list[listnum-1].length;
	  list[l].score = list[listnum-1].score;
	  list[l].dist = list[listnum-1].dist;
	  list[listnum-1].flag = 1;
	  list[listnum-1].x_sp = list[listnum-1].y_sp = 0;
	  list[listnum-1].x_ep = list[listnum-1].y_ep = 0;
	  list[listnum-1].x_sp_org = list[listnum-1].y_sp_org = 0;
	  list[listnum-1].x_ep_org = list[listnum-1].y_ep_org = 0;
	  list[listnum-1].length = 0;
	  list[listnum-1].score = 0;
	  list[listnum-1].dist = 0;
	  listnum--;
	  l--;
	} else {
	  list[l].flag = 1;
	}
      }
    }


    /*
     * Initialization score matrix
     */
    diagpt = (i+1) * (double)sqd[1]->length/sqd[0]->length;
    band_sp = (int)floor(diagpt - scwidth);
    if (band_sp < 1)  band_sp = 1;

    for (j = band_sp-1; j >= band_sp-5; j--) {
      if (j == 0) {
	break;
      }
      pre_column[j].score = 0;
    }

    /*
     * Update matrix pointers
     */
    swap = cur_column;
    cur_column = pre_column;
    pre_column = swap;

  }


  /*
   * Report optimal subsequence pair in list arrays
   */
  for (l = 0; l < listnum; l++) {
    printf ("X[%5d:%5d] \t Y[%5d:%5d] \t DTWdist: %.1f \t score: %.2f\n",
 	    list[l].x_sp, list[l].x_ep, list[l].y_sp, list[l].y_ep, list[l].dist, list[l].score);
  }

}