TYPED_TEST_P(recommender_random_test, random) {
  pfi::math::random::mtrand rand(0);
  TypeParam r;

  // Generate random data from two norma distributions, N1 and N2.
  vector<float> mu1;
  mu1.push_back(1.0);
  mu1.push_back(1.0);
  mu1.push_back(1.0);
  for (size_t i = 0; i < 100; ++i) {
    vector<double> v;
    make_random(rand, mu1, 1.0, 3, v);
    string row_name = "r1_" + lexical_cast<string>(i);
    r.update_row(row_name, make_vec(v[0], v[1], v[2]));
  }

  vector<float> mu2;
  mu2.push_back(-1.0);
  mu2.push_back(-1.0);
  mu2.push_back(-1.0);
  for (size_t i = 0; i < 100; ++i) {
    vector<double> v;
    make_random(rand, mu2, 1.0, 3, v);
    string row_name = "r2_" + lexical_cast<string>(i);
    r.update_row(row_name, make_vec(v[0], v[1], v[2]));
  }

  // Then, recommend to mean of N1
  vector<pair<string, float> > ids;
  r.similar_row(make_vec(1.0, 1.0, 1.0), ids, 10);
  ASSERT_EQ(10u, ids.size());
  size_t correct = 0;
  for (size_t i = 0; i < ids.size(); ++i) {
    if (ids[i].first[1] == '1')
    ++correct;
  }
  EXPECT_GT(correct, 5u);

  // save the recommender
  stringstream oss;
  r.save(oss);
  TypeParam r2;
  r2.load(oss);

  // Run the same test
  ids.clear();
  r2.similar_row(make_vec(1.0, 1.0, 1.0), ids, 10);
  ASSERT_EQ(10u, ids.size());
  correct = 0;
  for (size_t i = 0; i < ids.size(); ++i) {
    if (ids[i].first[1] == '1')
    ++correct;
  }
  EXPECT_GT(correct, 5u);
}
Example #2
0
// test for large number of elements
static char *test_operations() {
    size_t N = 200;
    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "failed to make the map");
    mu_assert(make_random(map), "did not make a random fake radix map");

    RadixMap_sort(map);
    mu_assert(check_order(map), "failed to properly sort the RadixMap");
    mu_assert(test_search(map), "failed the search test");
    mu_assert(check_order(map), "RadixMap not sorted after seach");

    while (map->end > 0) {
        RMElement *el = RadixMap_find(map, map->contents[map->end / 2].data.key);
        mu_assert(el != NULL, "should get a result");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "did not delete element");
        mu_assert(old_end - 1 == map->end, "wrong size after delete");
        mu_assert(check_order(map), "did not stay sorted after delete");
    }
    RadixMap_destroy(map);

    return NULL;
}
TYPED_TEST_P(recommender_random_test, mix) {
  pfi::math::random::mtrand rand(0);
  TypeParam r1, r2, expect;
  vector<float> mu(10);
  for (size_t i = 0; i < 100; ++i) {
    vector<double> v;
    make_random(rand, mu, 1.0, 3, v);
    common::sfv_t vec = make_vec(v[0], v[1], v[2]);

    string row = "r_" + lexical_cast<string>(i);
    (i < 50 ? r1 : r2).update_row(row, vec);
    expect.update_row(row, vec);
  }

  string diff1, diff2;
  r1.get_storage()->get_diff(diff1);
  r2.get_storage()->get_diff(diff2);

  r1.get_storage()->mix(diff1, diff2);

  TypeParam mixed;
  mixed.get_storage()->set_mixed_and_clear_diff(diff2);

  compare_recommenders(expect, mixed, false);
}
Example #4
0
static char *test_operations()
{
    size_t N = 200;

    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make map");
    mu_assert(make_random(map), "Didn't make a random fake radix map");

    RadixMap_sort(map);
    mu_assert(check_order(map),
            "Failed to properly sort the RadixMap");

    mu_assert(test_search(map), "Failed the search test");
    mu_assert(check_order(map), "RadixMap didn't stay sorted after search");

    while (map->end > 0) {
        RMElement *el = RadixMap_find(map,
                map->contents[map->end/2].data.key);
        mu_assert(el != NULL, "Should get a result");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "Didn't delete it");
        mu_assert(old_end - 1 == map->end, "Wrong size after delete");

        // test that the end is now the old value
        // but uint32 max so it trails off
        mu_assert(check_order(map),
                "RadixMap didn't stay sorted after delete");
    }
    RadixMap_destroy(map);

    return NULL;
}
Example #5
0
static char *test_operations()
{
    size_t N = 200;

    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make the map.");

    mu_assert(make_random(map), "Didn't make a random fake radix map.");
    //In Zed's code was:
    //RadixMap_sort(map, 0);
    //I think we should sort it again because we do it every time we add a new value
    mu_assert(check_order(map), "Failed to properly sort the RadixMap.");

    mu_assert(test_search(map), "Failed to search test.");
    mu_assert(check_order(map), "RadixMap didn't stay sorted after search.");

    while(map->end > 0) {

        RMElement *el = RadixMap_find(map, map->contents[map->end / 2].data.key);
        mu_assert(el != NULL, "Should get a result.");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "Didn't delete it.");
        mu_assert(old_end - 1 == map->end, "Wrong size after delete.");

        //test taht the end is now the old value, but uint32 max so it trails off
        mu_assert(check_order(map), "RadixMap didn't stay sorted after delete.");
    }

    RadixMap_destroy(map);

    return NULL;
}
Example #6
0
void rm_lifecycle()
{
    size_t N = 500;
    RadixMap *perf = RadixMap_create(N);
    make_random(perf);
    RadixMap_destroy(perf);
}
Example #7
0
char *test_speed() {
    size_t N = 100000;
    int i = 0;
    clock_t fastest = LONG_MAX;

    RadixMap *source_map = RadixMap_create(N);
    mu_assert(source_map != NULL, "Failed to make the map.");
    mu_assert(make_random(source_map), "Didn't make a random fake radix map.");

    for(i = 0; i < 100; i++) {
        RadixMap *map = RadixMap_create(N);
        mu_assert(map != NULL, "Failed to make the map.");

        mu_assert(RadixMap_copy(source_map, map) == 0,
                "Copying unsorted RadixMap failed.");

        clock_t elapsed = -clock();
        RadixMap_sort(map, 0, map->end);
        elapsed += clock();

        if(elapsed < fastest) fastest = elapsed;
        mu_assert(check_order(map), "Failed to sort the RadixMap.");

        RadixMap_destroy(map);
    }

    RadixMap_destroy(source_map);
    debug("Fastest time for size %zu: %f", N, ((float)fastest)/CLOCKS_PER_SEC);

    return NULL;
}
Example #8
0
pos_t *build_pos_list(void)
{
  pos_list = (pos_t*)malloc(sizeof(pos_t) * num_nodes);
  
  if(!strcmp(shape_name, "random")) return make_random();
  if(!strcmp(shape_name, "wave")) return make_wave();
  if(!strcmp(shape_name, "grid")) return make_grid();
  printf("Unknown shape: %s!\n", shape_name);
  exit(3);
}
void update_random(recommender_base& r) {
  pfi::math::random::mtrand rand(0);
  vector<float> mu(3);
  for (size_t i = 0; i < 100; ++i) {
    vector<double> v;
    make_random(rand, mu, 1.0, 3, v);
    string row_name = "r1_" + lexical_cast<string>(i);
    r.update_row(row_name, make_vec(v[0], v[1], v[2]));
  }
}
Example #10
0
// test for big number of elements
static void test_radix (ulong N)
{
        rec *data = (rec *) malloc (N * sizeof (rec));
        assert (data != NULL);

        make_random (data, N);
        radix_sort (data, N);
        check_order (data, N);

        free (data);
}
Example #11
0
int main(int argc, char const *argv[])
{
	RadixMap *map = RadixMap_create(N);
	assert(map != NULL);
	make_random(map);
	RadixMap_printer(map);
	RadixMap_sort(map);
	printf("\n\n");
	RadixMap_printer(map);



	return 0;
}
Example #12
0
int main() {
  struct compressor *cc;
  struct background *cc_bgd;
  unsigned char *rnd1,*rnd2;

  log_set_level("",LOG_DEBUG);
  logging_fd(2);
  cc = compressor_create();
  cc_bgd = compressor_background(cc);

  rnd1 = malloc(SIZE);
  rnd2 = malloc(SIZE);

  make_random(rnd1,SIZE);
  make_random(rnd2,SIZE);

  if(unlink("ct-a.gz")) { end(); }
  if(unlink("ct-b.gz")) { end(); }
  if(file_put_contents("ct-a",rnd1,SIZE)) { end(); }
  if(file_put_contents("ct-b",rnd2,SIZE)) { end(); }

  compressor_add(cc,"ct-a");
  background_start(cc_bgd);
  compressor_add(cc,"ct-b");

  background_finish(cc_bgd);
  compressor_release(cc);

  if(unlink("ct-a")) { end(); }
  if(unlink("ct-b")) { end(); }
  free(rnd1);
  free(rnd2);

  logging_done();
  return 0;
}
std::pair<std::string, std::vector<double> > gen_random_data3() {
  const char *labels[] = { "1", "2", "3" };
  std::vector<float> mus;
  mus.push_back(3);
  mus.push_back(0);
  mus.push_back(-3);

  const float sigma = 1.0;
  const size_t dim = 10;

  std::pair<std::string, std::vector<double> > p;
  size_t l = rand() % 3;
  p.first = labels[l];
  std::rotate(mus.begin(), mus.begin() + l, mus.end());
  make_random(mus, sigma, dim, p.second);
  return p;
}
std::pair<std::string, std::vector<double> > gen_random_data() {
  const float mu_pos = 1.0;
  const float mu_neg = -1.0;
  const float sigma = 1.0;
  const size_t dim = 10;

  float mu;
  std::pair<std::string, std::vector<double> > p;
  if (rand() % 2 == 0) {
    p.first = "OK";
    mu = mu_pos;
  } else {
    p.first = "NG";
    mu = mu_neg;
  }
  make_random(mu, sigma, dim, p.second);
  return p;
}
Example #15
0
// test for big number of elements
static char *test_RadixMap_operations()
{
    size_t N = 200;

    RadixMap *map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make the map.");
    mu_assert(make_random(map), "Didn't make a random fake radix map.");

    RadixMap_sort(map);
    mu_assert(check_order(map), "Failed to properly sort the RadixMap.");

    mu_assert(test_search(map), "Failed the search test.");
    mu_assert(check_order(map), "RadixMap didn't stay sorted after search.");

    RadixMap_destroy(map);

    map = RadixMap_create(N);
    mu_assert(map != NULL, "Failed to make the map.");

    debug("PUSHING VALUES");
    mu_assert(push_random_values(map), "Didn't push random values.");
    debug("VALUES PUSHED!");

    mu_assert(check_order(map), "Map wasn't sorted after pushes.");

    debug("DOING DELETES");
    while(map->end > 0) {
        RMElement *el = RadixMap_find(map, map->contents[map->end / 2].data.key);
        mu_assert(el != NULL, "Should get a result.");

        size_t old_end = map->end;

        mu_assert(RadixMap_delete(map, el) == 0, "Didn't delete it.");
        mu_assert(old_end - 1 == map->end, "Wrong size after delete.");

        // test that the end is now the old value, but uint32 max so it trails off
        mu_assert(check_order(map), "RadixMap didn't stay sorted after delete.");
    }

    RadixMap_destroy(map);

    return NULL;
}
Example #16
0
struct polynomial get_f(void )
{
	struct polynomial uit;
#ifdef OUTPUT_LIST
	uit = make_full(d);
	list_print(uit);
	exit(0);
#else
 #ifdef INPUT_F
	uit = make_full(d);
	printf("\n");
	printf("Please input coefficients below.\n");
	list_print(uit);
	clean_pol(&uit);
	return(uit);
 #else
	uit = make_random(d);
	list_print(uit);
	clean_pol(&uit);
	return(uit);
 #endif
#endif
}
Example #17
0
int main()
{
    mscalar ma, mb, mc, md, me;
    struct polynomial A, B, C, D, E, F, G, H;
    make_scalar(ma);
    make_scalar(mb);
    make_scalar(mc);
    make_scalar(md);
    make_scalar(me);
    A.leading = NULL;
    B.leading = NULL;
    C.leading = NULL;
    D.leading = NULL;
    E.leading = NULL;
    F.leading = NULL;
    G.leading = NULL;
    H.leading = NULL;

    printf("The prime is: %d.\n", p);
    printf("The power is: %d.\n", r);
    setup_scalars();
    set_seed(0);

    sc_zero(ma);
    printf("The number 0 is: ");
    printmscalar(ma);
    printf(".\n");
    sc_one(mb);
    printf("The number 1 is: ");
    printmscalar(mb);
    printf(".\n");
    ito_sc(541, mc);
    printf("The number 541 is: ");
    printmscalar(mc);
    printf(".\n");
    sc_inv(mc, md);
    printf("The inverse of 541 is: ");
    printmscalar(md);
    printf(".\n");
    sc_mult(md, mc, me);
    printf("The number 1 is: ");
    printmscalar(me);
    printf(".\n");

    A = make_random(10);
    F = copy_pol(A);
    /*	print_pol(A);
    	printf("\n"); */
    B = make_random(11);
    H = copy_pol(B);
    /*	print_pol(B);
    	printf("\n"); */
    C = pol_mult(A, B);
    /*	print_pol(C);
    	printf("\n"); */
    D = pol_mult(B, A);
    /*	print_pol(D);
    	printf("\n"); */
    times_int(-1, &D);
    E = pol_add(C, D);
    print_pol(E);
    printf("\n");
    times_int(-1, &F);
    G = pol_add(A, F);
    print_pol(G);
    printf("\n");
    times_int(-1, &H);
    G = pol_add(B, H);
    print_pol(G);
    exit(0);
}
Example #18
0
/**Function********************************************************************

  Synopsis    [Genetic algorithm for DD reordering.]

  Description [Genetic algorithm for DD reordering.
  The two children of a crossover will be stored in
  storedd[popsize] and storedd[popsize+1] --- the last two slots in the
  storedd array.  (This will make comparisons and replacement easy.)
  Returns 1 in case of success; 0 otherwise.]

  SideEffects [None]

  SeeAlso     []

******************************************************************************/
int
cuddGa(
  DdManager * table /* manager */,
  int  lower /* lowest level to be reordered */,
  int  upper /* highest level to be reorderded */)
{
    int 	i,n,m;		/* dummy/loop vars */
    int		index;
#ifdef DD_STATS
    double	average_fitness;
#endif
    int		small;		/* index of smallest DD in population */

    /* Do an initial sifting to produce at least one reasonable individual. */
    if (!cuddSifting(table,lower,upper)) return(0);

    /* Get the initial values. */
    numvars = upper - lower + 1; /* number of variables to be reordered */
    if (table->populationSize == 0) {
	popsize = 3 * numvars;  /* population size is 3 times # of vars */
	if (popsize > 120) {
	    popsize = 120;	/* Maximum population size is 120 */
	}
    } else {
	popsize = table->populationSize;  /* user specified value */
    }
    if (popsize < 4) popsize = 4;	/* enforce minimum population size */

    /* Allocate population table. */
    storedd = ALLOC(int,(popsize+2)*(numvars+1));
    if (storedd == NULL) {
	table->errorCode = CUDD_MEMORY_OUT;
	return(0);
    }

    /* Initialize the computed table. This table is made up of two data
    ** structures: A hash table with the key given by the order, which says
    ** if a given order is present in the population; and the repeat
    ** vector, which says how many copies of a given order are stored in
    ** the population table. If there are multiple copies of an order, only
    ** one has a repeat count greater than 1. This copy is the one pointed
    ** by the computed table.
    */
    repeat = ALLOC(int,popsize);
    if (repeat == NULL) {
	table->errorCode = CUDD_MEMORY_OUT;
	FREE(storedd);
	return(0);
    }
    for (i = 0; i < popsize; i++) {
	repeat[i] = 0;
    }
    computed = st_init_table(array_compare,array_hash);
    if (computed == NULL) {
	table->errorCode = CUDD_MEMORY_OUT;
	FREE(storedd);
	FREE(repeat);
	return(0);
    }

    /* Copy the current DD and its size to the population table. */
    for (i = 0; i < numvars; i++) {
	STOREDD(0,i) = table->invperm[i+lower]; /* order of initial DD */
    }
    STOREDD(0,numvars) = table->keys - table->isolated; /* size of initial DD */

    /* Store the initial order in the computed table. */
    if (st_insert(computed,(char *)storedd,(char *) 0) == ST_OUT_OF_MEM) {
	FREE(storedd);
	FREE(repeat);
	st_free_table(computed);
	return(0);
    }
    repeat[0]++;

    /* Insert the reverse order as second element of the population. */
    for (i = 0; i < numvars; i++) {
	STOREDD(1,numvars-1-i) = table->invperm[i+lower]; /* reverse order */
    }

    /* Now create the random orders. make_random fills the population
    ** table with random permutations. The successive loop builds and sifts
    ** the DDs for the reverse order and each random permutation, and stores
    ** the results in the computed table.
    */
    if (!make_random(table,lower)) {
	table->errorCode = CUDD_MEMORY_OUT;
	FREE(storedd);
	FREE(repeat);
	st_free_table(computed);
	return(0);
    }
    for (i = 1; i < popsize; i++) {
	result = build_dd(table,i,lower,upper);	/* build and sift order */
	if (!result) {
	    FREE(storedd);
	    FREE(repeat);
	    st_free_table(computed);
	    return(0);
	}
	if (st_lookup_int(computed,(char *)&STOREDD(i,0),&index)) {
	    repeat[index]++;
	} else {
	    if (st_insert(computed,(char *)&STOREDD(i,0),(char *)(long)i) ==
	    ST_OUT_OF_MEM) {
		FREE(storedd);
		FREE(repeat);
		st_free_table(computed);
		return(0);
	    }
	    repeat[i]++;
	}
    }

#if 0
#ifdef DD_STATS
    /* Print the initial population. */
    (void) fprintf(table->out,"Initial population after sifting\n");
    for (m = 0; m < popsize; m++) {
	for (i = 0; i < numvars; i++) {
	    (void) fprintf(table->out," %2d",STOREDD(m,i));
	}
	(void) fprintf(table->out," : %3d (%d)\n",
		       STOREDD(m,numvars),repeat[m]);
    }
#endif
#endif

    small = find_best();
#ifdef DD_STATS
    average_fitness = find_average_fitness();
    (void) fprintf(table->out,"\nInitial population: best fitness = %d, average fitness %8.3f",STOREDD(small,numvars),average_fitness);
#endif

    /* Decide how many crossovers should be tried. */
    if (table->numberXovers == 0) {
	cross = 3*numvars;
	if (cross > 60) {	/* do a maximum of 50 crossovers */
	    cross = 60;
	}
    } else {
	cross = table->numberXovers;      /* use user specified value */
    }

    /* Perform the crossovers to get the best order. */
    for (m = 0; m < cross; m++) {
	if (!PMX(table->size)) {	/* perform one crossover */
	    table->errorCode = CUDD_MEMORY_OUT;
	    FREE(storedd);
	    FREE(repeat);
	    st_free_table(computed);
	    return(0);
	}
	/* The offsprings are left in the last two entries of the
	** population table. These are now considered in turn.
	*/
	for (i = popsize; i <= popsize+1; i++) {
	    result = build_dd(table,i,lower,upper); /* build and sift child */
	    if (!result) {
		FREE(storedd);
		FREE(repeat);
		st_free_table(computed);
		return(0);
	    }
	    large = largest();	/* find the largest DD in population */

	    /* If the new child is smaller than the largest DD in the current
	    ** population, enter it into the population in place of the
	    ** largest DD.
	    */
	    if (STOREDD(i,numvars) < STOREDD(large,numvars)) {
		/* Look up the largest DD in the computed table.
		** Decrease its repetition count. If the repetition count
		** goes to 0, remove the largest DD from the computed table.
		*/
		result = st_lookup_int(computed,(char *)&STOREDD(large,0),
				       &index);
		if (!result) {
		    FREE(storedd);
		    FREE(repeat);
		    st_free_table(computed);
		    return(0);
		}
		repeat[index]--;
		if (repeat[index] == 0) {
		    int *pointer = &STOREDD(index,0);
		    result = st_delete(computed, (char **)&pointer,NULL);
		    if (!result) {
			FREE(storedd);
			FREE(repeat);
			st_free_table(computed);
			return(0);
		    }
		}
		/* Copy the new individual to the entry of the
		** population table just made available and update the
		** computed table.
		*/
		for (n = 0; n <= numvars; n++) {
		    STOREDD(large,n) = STOREDD(i,n);
		}
		if (st_lookup_int(computed,(char *)&STOREDD(large,0),
				  &index)) {
		    repeat[index]++;
		} else {
		    if (st_insert(computed,(char *)&STOREDD(large,0),
		    (char *)(long)large) == ST_OUT_OF_MEM) {
			FREE(storedd);
			FREE(repeat);
			st_free_table(computed);
			return(0);
		    }
		    repeat[large]++;
		}
	    }
	}
    }

    /* Find the smallest DD in the population and build it;
    ** that will be the result.
    */
    small = find_best();

    /* Print stats on the final population. */
#ifdef DD_STATS
    average_fitness = find_average_fitness();
    (void) fprintf(table->out,"\nFinal population: best fitness = %d, average fitness %8.3f",STOREDD(small,numvars),average_fitness);
#endif

    /* Clean up, build the result DD, and return. */
    st_free_table(computed);
    computed = NULL;
    result = build_dd(table,small,lower,upper);
    FREE(storedd);
    FREE(repeat);
    return(result);

} /* end of cuddGa */