コード例 #1
0
int main(int argc,char* args[])
{
 SDL_Init(SDL_INIT_EVERYTHING);
 screen=SDL_SetVideoMode(0,0,32,SDL_FULLSCREEN);
 background=SDL_LoadBMP("wooden_background.bmp");
 apply_surface(0,0,background,screen);
 //print_main_layer();
 load_prices();
 print_player_info(true);
 print_shop(4,5);
 SDL_Flip(screen);
 shop(5,5);
 save_player1("player1");
 //SDL_Delay(1000);
return 0;
}
コード例 #2
0
ファイル: pfsimul.c プロジェクト: jadm1/ieor4739-hw4
int main(int argc, char **argv) {
	/**
	 * utility variables
	 */
	int retcode = 0;
	int delta_t;
	int i, j;
	char *x_filename, *p_filename;
	char *pfv_filename, *pfr_filename;
	FILE *pfv_f, *pfr_f;
	int ov, or;
	double s;

	/**
	 * Program variables
	 */
	int verbose;
	double B;
	int num_sim, num_workers, max_t;
	WorkerBag *wbag = NULL;
	pthread_t *pthread = NULL;
	pthread_mutex_t outputmutex;
	double *x = NULL;
	int *indices = NULL;
	double *p = NULL;
	double *delta = NULL;
	double *sigma = NULL;
	/**
	 * arrays for results
	 */
	double *pf_values = NULL;
	double *pf_returns = NULL;
	double avg_pf_value;
	double avg_pf_return;

	int n; /** number of assets **/
	int t; /** number of periods **/
	Portfolio **ppf = NULL;


	/**
	 * Default parameter values
	 */
	verbose = 0;
	num_sim = 1;
	num_workers = 1;
	max_t = 10000;
	B = 1000000000.0;
	ov = 0;
	or = 0;


	/**
	 * Collect parameters from command line
	 */
	if(argc < 3) {
		printf("usage: %s <portfolio positions file> <prices history file> [ -ov portfolio values output file] [ -or portfolio returns output file] [-q simulations number] [-w workers] [-p max periods] [-v verbose] [-b initial value]\n", argv[0]);
		retcode = 1; goto BACK;
	}
	for(j = 3; j < argc; j++){
		if (0 == strcmp(argv[j], "-v")){
			j += 0;
			verbose = 1;
		}
		else if (0 == strcmp(argv[j],"-q")){
			j += 1;
			num_sim = atoi(argv[j]);
		}
		else if (0 == strcmp(argv[j],"-w")){
			j += 1;
			num_workers = atoi(argv[j]);
		}
		else if (0 == strcmp(argv[j],"-p")){
			j += 1;
			max_t = atoi(argv[j]);
		}
		else if (0 == strcmp(argv[j],"-b")){
			j += 1;
			B = atof(argv[j]);
		}
		else if (0 == strcmp(argv[j],"-ov")){
			j += 1;
			pfv_filename = argv[j];
			ov = 1;
		}
		else if (0 == strcmp(argv[j],"-or")){
			j += 1;
			pfr_filename = argv[j];
			or = 1;
		}
		else{
			printf("bad option %s\n", argv[j]); retcode = 1; goto BACK;
		}
	}

	if (num_workers > num_sim) {
		num_workers = num_sim;
		printf(" --> reset workers to %d\n", num_workers);
	}

	x_filename = argv[1];
	p_filename = argv[2];


	printf("loading positions from %s\n", x_filename);
	retcode = load_initial_positions(x_filename, &x, &n, &indices, 1e-10);
	if (retcode != 0) {
		printf("positions could not be loaded !\n"); goto BACK;
	}

	printf("Portfolio assets: %d\n", n);

	if (verbose) {
		printf("x:");
		UTLShowVector(n, x);
		s = 0;
		for (j = 0; j < n; j++)
			s += x[j];
		printf("sum of positions : %g %%\n", s*100.0);
	}

	printf("loading prices from %s\n", p_filename);
	retcode = load_prices(p_filename, &p, n, indices, &t, max_t);
	if (retcode != 0) {
		printf("prices could not be loaded !\n"); goto BACK;
	}

	printf("periods: %d\n", t);

	if (verbose) {
		printf("prices loaded\n");
	}

	printf("computing vector of averages of changes...\n");
	retcode = compute_avg_changes(p, n, t, &delta);
	if (retcode != 0)
		goto BACK;
	if (verbose) {
		printf("delta:");
		UTLShowVector(n, delta);
	}

	printf("computing vector std's of changes...\n");
	retcode = compute_std_changes(p, n, t, delta, &sigma);
	if (retcode != 0)
		goto BACK;
	if (verbose) {
		printf("sigma:");
		UTLShowVector(n, sigma);
	}



	/** allocating memory for result arrays **/
	pf_values = (double*)calloc(num_sim, sizeof(double));
	pf_returns = (double*)calloc(num_sim, sizeof(double));
	if (pf_values == NULL || pf_returns == NULL) {
		retcode = NOMEMORY; goto BACK;
	}



	/**
	 * Creating portfolio array for every worker (no shared memory so we need to make numworkers copies of 1 portfolio)
	 */
	retcode = portfolio_create_array(num_workers, &ppf, n, t, x, p, delta, sigma, B, pf_values, pf_returns);
	if (retcode != 0)
		goto BACK;


	/** check initial portfolio value**/
	s = 0.0;
	for (i = 0; i < n; i++) {
		s += p[i*t + 0] * ppf[0]->q[i];
	}
	printf("Initial portfolio value: %g\n", s);


	/** prepare simple multithreading **/
	wbag = (WorkerBag*)calloc(num_workers, sizeof(WorkerBag));
	if (wbag == NULL) {
		retcode = NOMEMORY; goto BACK;
	}
	pthread = (pthread_t *)calloc(num_workers, sizeof(pthread_t));
	if (pthread == NULL) {
		printf("could not create thread array\n");
		retcode = NOMEMORY; goto BACK;
	}

	delta_t = UTLTicks_ms();
	pthread_mutex_init(&outputmutex, NULL);

	for(j = 0; j < num_workers; j++) {
		wbag[j].ID = j;
		wbag[j].num_sim = num_sim;
		wbag[j].pf = ppf[j];
		wbag[j].poutputmutex = &outputmutex;
		wbag[j].num_workers = num_workers;
		pthread_mutex_lock(&outputmutex);
		printf("Launching thread for worker %d\n", j);
		pthread_mutex_unlock(&outputmutex);
		pthread_create(&pthread[j], NULL, &pfworker, (void *)&wbag[j]);
	}

	pthread_mutex_lock(&outputmutex);
	printf("Waiting for threads...\n");
	pthread_mutex_unlock(&outputmutex);

	for(j = 0; j < num_workers; j++) {
		pthread_join(pthread[j], NULL);

		pthread_mutex_lock(&outputmutex);
		printf("Thread %d joined ...\n", j);
		pthread_mutex_unlock(&outputmutex);
	}

	pthread_mutex_destroy(&outputmutex);
	delta_t = UTLTicks_ms() - delta_t;

	printf("P&L simulations done in %.1f seconds\n", delta_t/1000.0);

	printf("Averaging over all simulations\n");
	avg_pf_value = average(num_sim, pf_values);
	avg_pf_return = average(num_sim, pf_returns);
	printf("Average final value: %g\n", avg_pf_value);
	printf("Average daily return: %g %%\n", 100.0*avg_pf_return);


	if (ov) {
		printf("saving values...\n");
		pfv_f = fopen(pfv_filename, "w");
		fprintf(pfv_f, "nsim: %d\n", num_sim);
		for (j = 0; j < num_sim; j++) {
			fprintf(pfv_f, "%g\n", pf_values[j]);
		}
		fclose(pfv_f);
	}
	if (or) {
		printf("saving returns...\n");
		pfr_f = fopen(pfr_filename, "w");
		fprintf(pfr_f, "nsim: %d\n", num_sim);
		for (j = 0; j < num_sim; j++) {
			fprintf(pfr_f, "%g\n", pf_returns[j]);
		}
		fclose(pfr_f);
	}
	if (verbose) {
		printf("freeing memory ...\n");
	}
	BACK:

	portfolio_delete_array(num_workers, &ppf);

	UTLFree((void**)&pf_values);
	UTLFree((void**)&pf_returns);
	UTLFree((void**)&wbag);
	UTLFree((void**)&pthread);
	UTLFree((void**)&sigma);
	UTLFree((void**)&delta);
	UTLFree((void**)&p);
	UTLFree((void**)&indices);
	UTLFree((void**)&x);

	return retcode;
}