static void _nnAddRm(uint8_t type, bor_nn_params_t *params) { bor_rand_mt_t *rand; bor_nn_t *nn; static bor_list_t els_list; static int els_len = ADD_ELS_LEN; static el_t els[ADD_ELS_LEN]; int i, j; rand = borRandMTNewAuto(); params->type = type; nn = borNNNew(params); borListInit(&els_list); for (i = 0; i < els_len; i++){ borVec2Set(&els[i].w, borRandMT(rand, -3, 3), borRandMT(rand, -3, 3)); borNNElInit(nn, &els[i].el, (const bor_vec_t *)&els[i].w); borListAppend(&els_list, &els[i].list); } for (i = 0; i < ADD_ELS_LEN; i++){ //fprintf(stdout, "%02d:\n", i); borNNAdd(nn, &els[i].el); //borNNDump(vp, stdout); } for (i = 0; i < ADD_ELS_LEN; i += 3){ borNNRemove(nn, &els[i].el); borListDel(&els[i].list); } for (i = 0; i < ADD_NUM_TESTS; i++){ for (j = 1; j <= ADD_NUM_NNS; j++){ test2(rand, nn, &els_list, j); } } borNNDel(nn); borRandMTDel(rand); }
svo_gng_eu_t *svoGNGEuNew(const svo_gng_eu_ops_t *ops, const svo_gng_eu_params_t *params) { svo_gng_eu_t *gng_eu; bor_nn_params_t nnp; size_t i; bor_real_t maxbeta; gng_eu = BOR_ALLOC(svo_gng_eu_t); gng_eu->net = borNetNew(); gng_eu->ops = *ops; gng_eu->params = *params; // set up ops data pointers if (!gng_eu->ops.new_node_data) gng_eu->ops.new_node_data = gng_eu->ops.data; if (!gng_eu->ops.del_node_data) gng_eu->ops.del_node_data = gng_eu->ops.data; if (!gng_eu->ops.input_signal_data) gng_eu->ops.input_signal_data = gng_eu->ops.data; if (!gng_eu->ops.terminate_data) gng_eu->ops.terminate_data = gng_eu->ops.data; if (!gng_eu->ops.callback_data) gng_eu->ops.callback_data = gng_eu->ops.data; // initialize error heap gng_eu->err_heap = borPairHeapNew(errHeapLT, (void *)gng_eu); // precompute beta^n gng_eu->beta_n = BOR_ALLOC_ARR(bor_real_t, gng_eu->params.lambda); gng_eu->beta_n[0] = gng_eu->params.beta; for (i = 1; i < gng_eu->params.lambda; i++){ gng_eu->beta_n[i] = gng_eu->beta_n[i - 1] * gng_eu->params.beta; } // precompute beta^(n * lambda) maxbeta = gng_eu->beta_n[gng_eu->params.lambda - 1]; gng_eu->beta_lambda_n_len = 1000; gng_eu->beta_lambda_n = BOR_ALLOC_ARR(bor_real_t, gng_eu->beta_lambda_n_len); gng_eu->beta_lambda_n[0] = maxbeta; for (i = 1; i < gng_eu->beta_lambda_n_len; i++){ gng_eu->beta_lambda_n[i] = gng_eu->beta_lambda_n[i - 1] * maxbeta; } gng_eu->cycle = 1L; gng_eu->step = 1; // initialize nncells nnp = params->nn; nnp.gug.dim = params->dim; nnp.vptree.dim = params->dim; nnp.linear.dim = params->dim; gng_eu->nn = borNNNew(&nnp); // initialize temporary vector if (gng_eu->params.dim == 2){ gng_eu->tmpv = (bor_vec_t *)borVec2New(BOR_ZERO, BOR_ZERO); }else if (gng_eu->params.dim == 3){ gng_eu->tmpv = (bor_vec_t *)borVec3New(BOR_ZERO, BOR_ZERO, BOR_ZERO); }else{ gng_eu->tmpv = borVecNew(gng_eu->params.dim); } return gng_eu; }
static void testCorrect(void) { bor_nn_params_t params; bor_real_t range[4] = { -15., 15., -18., 17. }; el_t *ns, *near[3]; bor_nn_el_t *el_linear[50], *el_gug[50], *el_vp[50]; int i, j, k; int len_linear, len_gug, len_vp; int incorrect; bor_vec2_t v; borNNParamsInit(¶ms); borNNParamsSetDim(¶ms, 2); params.gug.num_cells = 0; params.gug.max_dens = 1; params.gug.expand_rate = 2.; params.gug.aabb = range; params.type = BOR_NN_LINEAR; linear = borNNNew(¶ms); params.type = BOR_NN_GUG; gug = borNNNew(¶ms); params.type = BOR_NN_VPTREE; vp = borNNNew(¶ms); ns = elsNew(arr_len); for (k = 0; k < nearest_len; k++){ incorrect = 0; for (i=0; i < loops; i++){ fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); len_linear = borNNNearest(linear, (const bor_vec_t *)&v, k + 1, el_linear); len_gug = borNNNearest(gug, (const bor_vec_t *)&v, k + 1, el_gug); len_vp = borNNNearest(vp, (const bor_vec_t *)&v, k + 1, el_vp); if (len_linear != len_gug || len_linear != len_vp || len_vp != len_gug || len_linear != k + 1){ incorrect = 1; } for (j = 0; j < k + 1; j++){ near[0] = bor_container_of(el_linear[j], el_t, linear); near[1] = bor_container_of(el_gug[j], el_t, gug); near[2] = bor_container_of(el_vp[j], el_t, vp); if (near[0] != near[1] || near[0] != near[2] || near[1] != near[2]){ incorrect = 1; } } } if (incorrect){ fprintf(stderr, "[%d] %08d / %08d FAIL\n", (int)k, (int)i, (int)loops); }else{ fprintf(stderr, "[%d] %08d / %08d OK\n", (int)k, (int)i, (int)loops); } } BOR_FREE(ns); borNNDel(linear); borNNDel(gug); borNNDel(vp); }
static void bench(void) { bor_nn_params_t params; bor_real_t range[4] = { -15., 15., -18., 17. }; el_t *ns, *near; bor_nn_el_t *el[50]; int i, j, k; bor_vec2_t v; int devnull; bor_timer_t timer; devnull = open("/dev/null", O_WRONLY); if (devnull < 0){ perror("Error: "); return; } borNNParamsInit(¶ms); borNNParamsSetDim(¶ms, 2); params.gug.num_cells = 0; params.gug.max_dens = 1; params.gug.expand_rate = 2.; params.gug.aabb = range; params.type = BOR_NN_LINEAR; linear = borNNNew(¶ms); params.type = BOR_NN_GUG; gug = borNNNew(¶ms); params.type = BOR_NN_VPTREE; vp = borNNNew(¶ms); ns = elsNew(arr_len); for (k = 0; k < nearest_len; k++){ borTimerStart(&timer); for (i=0; i < loops; i++){ //fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); borNNNearest(linear, (const bor_vec_t *)&v, k + 1, el); for (j = 0; j < k + 1; j++){ near = bor_container_of(el[j], el_t, linear); write(devnull, &near->v, 1); } } borTimerStop(&timer); borTimerPrintElapsed(&timer, stderr, " - [%d] - linear - \n", k); borTimerStart(&timer); for (i=0; i < loops; i++){ //fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); borNNNearest(gug, (const bor_vec_t *)&v, k + 1, el); for (j = 0; j < k + 1; j++){ near = bor_container_of(el[j], el_t, gug); write(devnull, &near->v, 1); } } borTimerStop(&timer); borTimerPrintElapsed(&timer, stderr, " - [%d] - gug - \n", k); borTimerStart(&timer); for (i=0; i < loops; i++){ //fprintf(stderr, "[%d] %08d / %08d\r", (int)k, (int)i, (int)loops); borVec2Set(&v, borRand(&r, -10., 10.), borRand(&r, -10, 10)); borNNNearest(vp, (const bor_vec_t *)&v, k + 1, el); for (j = 0; j < k + 1; j++){ near = bor_container_of(el[j], el_t, vp); write(devnull, &near->v, 1); } } borTimerStop(&timer); borTimerPrintElapsed(&timer, stderr, " - [%d] - vptree - \n", k); } BOR_FREE(ns); borNNDel(linear); borNNDel(gug); borNNDel(vp); close(devnull); }