Пример #1
0
int test_training_set_1(void)
{
	training_set_t* tset;
	size_t u, k, i;

	/* Model configuration */
	/* Setup model parameters */
	model_parameters_t params = { 0 };

	printf("Test training set ... \n");

	params.items_number = ITEM_NUMBER_TEST_SIZE;
	params.users_number = USER_NUMBER_TEST_SIZE;
	params.training_set_size = TRAINING_SET_TEST_SIZE;

	/* Initialize a training set */
	tset = init_training_set(params);

	srand ( 47 );

	for (u = 0; u < USER_NUMBER_TEST_SIZE; u++)
	{
		for (i = 0; i < ITEM_NUMBER_TEST_SIZE; i++)
		{
			set_known_rating(u, i, (float) (rand() % 10), tset);
		}
	}

	compile_training_set(tset);

	for (i = 0; i < TRAINING_SET_TEST_SIZE; i++)
	{
		float val;

		u = tset->ratings->entries[i].row_i;
		k = tset->ratings->entries[i].column_j;

		assert(element_exists(u, k, tset->ratings_matrix));

		val = get_element(u, k, tset->ratings_matrix);

		assert(val == tset->ratings->entries[i].value);
	}

	free_training_set(tset);
	
	printf("Test training set [OK] \n");

	return 0;
}
Пример #2
0
static void thread_method (void* arg)
{
  uv_mutex_lock(&factors_mutex);
  free_learned_factors(factors);
  compile_training_set (training_set);
  factors = learn (training_set, server_param->model);
  
  uv_mutex_lock(&factors_backup_mutex);
  printf("factors_backup_mutex locked inside of thread \n");
  free_learned_factors(factors_backup);
  factors_backup = copy_learned_factors (factors);
  uv_mutex_unlock(&factors_backup_mutex);
  uv_mutex_unlock(&factors_mutex);
}
Пример #3
0
int main (int argc, char** argv)
{
	int r;
	server_param = parse_arguments (argc, argv);
	LOG ("Extracting data ...");
	training_set = server_extract_data (server_param);
	LOG ("Learning ...");
	compile_training_set (training_set);
	factors = learn (training_set, server_param->model);
	factors_backup = copy_learned_factors (factors);
	LOG ("Learning completed");
	complete = malloc(20 *sizeof(int));
	memset(complete,0,20 *sizeof(int));
	//parser_settings.on_headers_complete = on_headers_complete;
	parser_settings.on_url = on_url;
	parser_settings.on_header_value = on_value;
	uv_loop = uv_default_loop();

	r = uv_tcp_init (uv_loop, &server);
	CHECK (r, "bind");

	struct sockaddr_in address = uv_ip4_addr ("0.0.0.0", server_param->port);

	r = uv_tcp_bind (&server, address);
	CHECK (r, "bind");
	uv_listen ( (uv_stream_t*) &server, 128, on_connect);

	LOGF ("listening on port %u", server_param->port);
    	uv_timer_t timer;
    	r = uv_timer_init(uv_default_loop(), &timer);
    	assert(r == 0);
   	r = uv_timer_start(&timer, timer_cb, 10000, 10000);
    	assert(r == 0);
	
  	r = uv_mutex_init(&factors_mutex);
  	assert(r == 0);
  	r = uv_mutex_init(&factors_backup_mutex);
  	assert(r == 0);
  	r = uv_mutex_init(&tset_mutex);
  	assert(r == 0);
	uv_run (uv_loop);
}
Пример #4
0
int on_value (http_parser* parser, const char *at, size_t length)
{	
	if(parser->method!=3)
		return 0;
	client_t* client = (client_t*)(parser->data);
	uv_tcp_t* handle = &client->handle;
	uv_buf_t resbuf;
	coo_entry_t* rating;
	if(!complete[client->request_num])
	{
	rating = get_rating_from_http(at,"(user=[[:digit:]]{0,4}&item=[[:digit:]]{0,4}&rating=[[:digit:]])");
	complete[client->request_num]=1;
	if(rating!=NULL)
	{
		LOGF("%u %u %lf \n", rating->row_i,rating->column_j, rating->value);
		uv_mutex_lock(&tset_mutex);
		compile_training_set(training_set);
		add_rating(rating->row_i,rating->column_j,rating->value,training_set);
		uv_mutex_unlock(&tset_mutex);
		resbuf.base = malloc(sizeof(POST_SUCCESS));
		strcpy(resbuf.base,POST_SUCCESS);
		resbuf.len=sizeof(POST_SUCCESS);
		printf("%s",resbuf.base);
		free(rating);
	}else
	{
		LOG("Parameters incorrect");
		resbuf.base=POST_ERROR;
		resbuf.len=sizeof(POST_ERROR);
	}
	
	uv_write(&client->write_req, (uv_stream_t*) handle, &resbuf, 1, after_write);
	

	}else return 1;

	return 0;
}
Пример #5
0
int 
main(void) {

	learned_factors_t* learned;
	training_set_t* tset;
	recommended_items_t* r_items = NULL;

	learning_model_t model = {0};

	//Model configuration
	//Setup model parameters
	model_parameters_t params = { 0 };
	
	params.items_number = 3;
	params.users_number = 2;
	params.training_set_size = 5;

	params.dimensionality = 40;
	params.iteration_number = 60;

	params.lambda = 0.055;
	params.step = 0.0095;

	params.lambda_bias = 0.02;
	params.step_bias = 0.001;
	
	//Use the basic matrix factorization model
	model.learning_algorithm = learn_mf_bias;
	model.rating_estimator   = estimate_rating_mf_bias;
	model.parameters	 = params;

	//Learning
	//Initialize a training set
	tset = init_training_set(params);

	set_known_rating(0, 0, 1, tset);
	set_known_rating(0, 1, 0, tset);
	set_known_rating(0, 2, 6, tset);

	set_known_rating(1, 1, 0, tset);
	set_known_rating(1, 0, 2, tset);

	compile_training_set(tset);

	learned = learn(tset, model);

	//Rating estimation
	printf(	"users [0] item [0], rating = %f \n",
		 estimate_rating_from_factors(0, 0, learned, model));

	printf(	"users [0] item [1], rating = %f \n",
		 estimate_rating_from_factors(0, 1, learned, model));

	printf(	"users [0] item [2], rating = %f \n", 
		estimate_rating_from_factors(0, 2, learned, model));

	printf(	"users [1] item [1], rating = %f \n", 
		estimate_rating_from_factors(1, 1, learned, model));

	printf(	"users [1] item [0], rating = %f \n",
		 estimate_rating_from_factors(1, 0, learned, model));

	r_items = recommend_items(0, 3, learned, tset, model);

	print_recommended_items(r_items->items->head, 0);

	free_recommended_items(r_items);
	free_learned_factors(learned);
	free_training_set(tset);

	return 0;
}