Beispiel #1
0
prtIcatDNA()
{
  uint64_t readDNA(void);
  /* obtain the unique identifier for iCAT board */
  /* used within the fail files */
  uint64_t tDNA = 0LL;
  tDNA = readDNA();
  printf("iCAT_DNA Parameter: 0x%llx\n",iCAT_DNA);
  printf("Read      ICAT DNA: 0x%llx\n",tDNA);
}
int debug(int argc,char** argv){
	test();
	const int temp[] = {0, 7306, 77, 9859, 8996, 0, 7615, 0, 0, 1641, 8007, 0, 1666, 1098, 3253, 0, 5908, 6549, 7241, 8392, 2533, 8392, 7810, 150, 0, 8248, 1100, 0, 0, 0, 300, 350, 300
		, 2324, 4627, 1393, 457, 4736, 0, 819, 0, 289, 0, 0, 2872, 5034, 1, 539, 2160, 3399, 9902, 1094, 6143, 100, 0, 4660, 5670, 400, 1214, 0, 5202, 0, 6629, 5316, 0,
		8392, 7810, 1519, 4743, 0, 513, 3589, 1790, 25, 1433, 5552, 0, 8695, 3, 9352, 1413, 9324, 1, 25, 3278, 0, 0, 0, 6020, 0, 1071, 2400, 3678, 1641, 8007, 0, 4650,
		1098, 1801, 1211, 2465, 6549, 6435, 1471, 3032, 2598, 100, 3005, 9326, 150, 1,25, 3278, 0, 4710, 9276, 3266, 4386, 0, 0, 7340, 0, 0, 3559, 9407, 0, 4710, 6038
		, 3187, 9961, 7280, 1460, 7224, 7744, 200, 5723, 2289, 0, 3948, 9274, 0, 573, 0, 8815, 3264, 0, 3809, 0, 0, 0};
	int size = sizeof( temp ) / sizeof ( *temp );
	std::vector<int> ancestor (temp, temp+size);
	Physics* WWDPhysics = new Physics();
	//init creature
	readDNA(&ancestor,WWDPhysics);
	//WWDPhysics->testPhysics();
	//default glut doesn't return from mainloop
	WWDPhysics->solveGroundConflicts();
	return glutmain(argc, argv,1024,600,"Walking with dinosaurs",WWDPhysics);
}
Beispiel #3
0
/*
 * setup the RF FPGA registers based on 
 * icat status and console type
 */
int setupRfType()
{
   uint64_t readDNA(void);
   int status;
   int icatFpgaFlag;
   // determine if RF Controller FPGA is icat capable
   icatFpgaFlag = fpgaIcatEnabled();
   if (icatFpgaFlag > 0)
   {
       int icatID;
       // determine if icat RF is attached to the controller
       icatID = getIcatId();
       diagPrint(NULL," ICAT ID: 0x%lx\n",icatID);
       if (icatID != 0xa)
       {
          diagPrint(NULL," iCAT NOT Attached\n");
          set_field(RF,transmitter_mode,0);  // Non-ICAT  RF
          RfType = 0;
       }
       else
       {
          diagPrint(NULL,"iCAT Attached\n");
          RfType = 1;

          /* obtain the unique identifier for iCAT board */
          /* used within the fail files */
          iCAT_DNA = readDNA();
          diagPrint(NULL," ICAT DNA: 0x%llx\n",iCAT_DNA);
       }
       if (ConsoleTypeFlag != 1) {
          diagPrint(NULL," Setting for VNMRS Console\n");
          // printf("Setting for VNMRS Console\n");
          set_field(RF,console_type,0);  // VNMRS = 0, 400-MR = 1
       }
       else {
          diagPrint(NULL," Setting for 400-MR Console\n");
          // printf("Setting for 400-MR Console\n");
          set_field(RF,console_type,1);  // VNMRS = 0, 400-MR = 1
       }

       set_field(RF,p2_disable,0); // This will then connect the interface on the P2 connector
   }
   return RfType;
}
Beispiel #4
0
int main(int argc,char** argv)
{
	/* number of task, task rank */
	int numtasks, rank, sendcnt, recvcnt;

	/* point dimension, number of points*/
	int dimension, lines;
	
	/* file to be read */
	FILE* fp;

	char* filename;
	
	/* content read from file */
	char* dnaSource;
	double* tdSource;

	/* data points for every process */
	char* dnaBuf;
	double* tdBuf;

	/* centroid */
	char* dnaC;
	double* tdC;


	/* labels for each point */
	int* labels;

	/* whether this task is a dna clustering */
	int dna;
	/* how many clusters */
	int cluster;

	/* check the arguments */
	if (argc < 5) {
		printf("Usage: mpiKmeans <dna or 2d> <input file name> <lines> <clusters> <dimension>\n");
		exit(1);
	}

	/* check the arguments */
	if (!strcmp(argv[1],"dna") && argc < 6) {
		printf("Usage: mpiKmeans <dna or 2d> <input file name> <lines> <clusters> <dimension>\n");
		exit(1);
	}
	
	/* file name */
	filename = argv[2];
	/* how many points */
	lines = atoi(argv[3]);
	/* dna clustring or not */
	dna = !strcmp(argv[1], "dna");
	/* how many clusters */
	cluster = atoi(argv[4]);

	MPI_Init(&argc,&argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
	MPI_Comm_size(MPI_COMM_WORLD, &numtasks);

	if (dna) {
		/* initialize dna clustring parameters */
		dimension = atoi(argv[5]);
		sendcnt = (lines + numtasks - 1)/numtasks * dimension;
		recvcnt = sendcnt;
		dnaSource = malloc(sizeof(char) * dimension * lines);
		dnaBuf = malloc(sizeof(char) * recvcnt);
		dnaC = malloc(sizeof(char) * cluster * dimension);
	} else {
		/* initialize 2d clustring parameters */
		dimension = 2;
		sendcnt = (lines + numtasks - 1)/numtasks * dimension;
		recvcnt = sendcnt;
		tdSource = malloc(sizeof(double) * dimension * lines);
		tdBuf = malloc(sizeof(double) * recvcnt);
		tdC = malloc(sizeof(double) * cluster * dimension);
	}

	
	/* if it's master, read the data source and genterate centroids */
	if (rank == MASTER) {
		fp = fopen(filename, "r");	
		if(fp == NULL) {
			printf("Cannot open file %s\n", filename);
			exit(1);
		}
		
		if (dna) {
			readDNA(fp, dnaSource, dimension);
			generateDNACenter(dnaC, dnaSource, lines, dimension, cluster);
		} else {
			read2D(fp, tdSource);
			generate2DCenter(tdC, tdSource, lines, dimension, cluster);
		}	
		fclose(fp);
		
	}

	/* compute the number and offset of data elements to send for each process */
	int* sendcnts = malloc(sizeof(int)*numtasks);
	int* strides = malloc(sizeof(int)*numtasks);
	int i;
	for(i = 0;i<numtasks-1;i++) {
		sendcnts[i] = sendcnt;
	}
	sendcnts[numtasks-1] = lines * dimension - sendcnt * (numtasks - 1);
	int base = 0;
	for(i = 0;i<numtasks;i++) {
		strides[i] = base;
		base += sendcnts[i];
	}
	/* scatter the data and broadcast the center */
	if(dna) {
		MPI_Scatterv (dnaSource,sendcnts,strides,MPI_CHAR,dnaBuf,recvcnt,MPI_CHAR,MASTER,MPI_COMM_WORLD);
		MPI_Bcast (dnaC,cluster * dimension,MPI_CHAR,MASTER,MPI_COMM_WORLD); 
	} else {
		MPI_Scatterv (tdSource,sendcnts,strides,MPI_DOUBLE,tdBuf,recvcnt,MPI_DOUBLE,MASTER,MPI_COMM_WORLD);
		MPI_Bcast (tdC,cluster * dimension,MPI_DOUBLE,MASTER,MPI_COMM_WORLD); 
	}

	/* compute the number of points for each process */
	int num = sendcnts[rank]/dimension;
	/* allocate an array for labeling each point */
	labels = malloc(sizeof(int) * num);
	
	/* do k-means until converging */
	do {
		int j;
		/* dna clustering */
		if(dna) {
			/* the count of each character in every position of each point in every process */
			int* newcnts = malloc(sizeof(int) * cluster * dimension * 4);
			/* the count of each character in every position of each point in sum */
			int* recvcnts = malloc(sizeof(int) * cluster * dimension * 4);
			/* initialize to zero */
			memset(newcnts, 0, sizeof(int) * cluster * dimension * 4);
			/* the new centroids for every cluster computed */
			char* recvctrs = malloc(sizeof(char) * cluster * dimension);
			/* for every point, compute its distance from each centroid, choose the minimum one and label it */
			for(i = 0; i < num; i++) {
				/* initialize label and distance */
				int lb = -1;
				int dDNA = MAX_DIST;
				/* iterate each cluster centroid and update the label and distance */
				for(j = 0; j < cluster;j++) {
					int tmpDist = distDNA(dnaC+j*dimension, dnaBuf+i*dimension, dimension);		
					if (tmpDist < dDNA) {
						dDNA = tmpDist;
						lb = j;
					}
				}	
				/* iterate each position of the point and update the counts of each character */
				for(j = 0; j < dimension; j++) {
					switch(dnaBuf[i*dimension+j]) {
						case 'A':
							newcnts[4 * dimension * lb + 4 * j + 0]++;
							break;
						case 'C':
							newcnts[4 * dimension * lb + 4 * j + 1]++;
							break;
						case 'G':
							newcnts[4 * dimension * lb + 4 * j + 2]++;
							break;
						case 'T':
							newcnts[4 * dimension * lb + 4 * j + 3]++;
							break;
						default:
							break;
					}
				}
				/* update the point's label */
				labels[i] = lb;
			}
			
			/* reduce the character counts in every position of each point on the master node */
			MPI_Reduce(newcnts, recvcnts, cluster * 4 * dimension, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);
			/* set the terminate flag */
			int flag = 0;
			/* on the master node, compute the new centroid for each cluster */
			if(rank == MASTER) {
				/* iterate each cluster */
				for (i = 0; i < cluster; i++) {
					/* iterate each position of a centroid */
					for (j = 0; j < dimension; j++) {
						/* choose the character with most counts */
						int left = 
						recvcnts[i*dimension*4+4*j+0] > recvcnts[i*dimension*4+4*j+1]?0:1;
						int right = 
						recvcnts[i*dimension*4+4*j+2] > recvcnts[i*dimension*4+4*j+3]?2:3;

						int final = recvcnts[i*dimension*4+4*j+left] > 
						recvcnts[i*dimension*4+4*j+right]?left:right;

						/* update the centroid */
						recvctrs[i * dimension + j] = bases[final];
					}
				}
				/* initialize the difference of centroids */
				int dsum = 0;
				/* iterate each cluster centroid and update the difference of centroids */
				for(i =0;i<cluster;i++) {
					dsum += distDNA(recvctrs+dimension*i,dnaC+dimension*i,dimension);
				}
				/* see if it is time to terminate */
				if(dsum <= DNA_MIN)
					flag = 1;
				free(dnaC);
				/* update the centroids */
				dnaC = recvctrs;
			}
			/* broadcast the terminate flag */
			MPI_Bcast (&flag,1,MPI_INT,MASTER,MPI_COMM_WORLD); 
			free(newcnts);
			free(recvcnts);
			if(flag)
				break;
			
			/* broadcast the new centroids */
			MPI_Bcast (dnaC,cluster * dimension,MPI_CHAR,MASTER,MPI_COMM_WORLD); 
			
		} else {
			/* the new centroids on each process */
			double* newctrs = malloc(sizeof(double) * cluster * dimension);
			/* the received centroids on the master */
			double* recvctrs = malloc(sizeof(double) * cluster * dimension);
			/* the number of points on each cluster */
			int* newcnts = malloc(sizeof(int) * cluster);
			/* the received number of points on each cluster */
			int* recvcnts = malloc(sizeof(int) * cluster);
			/* initialization */
			memset(newctrs, 0, sizeof(double) * cluster * dimension);
			memset(newcnts, 0, sizeof(int) * cluster);
			/* iterate each point on one process */
			for(i = 0; i < num; i++) {
				/* initialize the label and distance */
				int lb = -1;
				double d2D = MAX_DIST;
				/* iterate each centroid and update the label to the one with minimum distance */
				for(j = 0; j < cluster;j++) {
					double tmpDist = 
						dist2D(tdC+j*dimension, tdBuf+i*dimension);		
					if (tmpDist < d2D) {
						d2D = tmpDist;
						lb = j;
					}
							
				}	
				/* update the label */
				labels[i] = lb;
				/* update the count */
				newcnts[lb]++;
				/* sum each point */
				for (j = 0; j < dimension; j++) {
					newctrs[lb * dimension + j] += tdBuf[i * dimension + j];
				}
			}
			/* reduce the new centroid sum to the master process */
			MPI_Reduce(newctrs, recvctrs, cluster * dimension, MPI_DOUBLE, MPI_SUM, MASTER, MPI_COMM_WORLD);
			/* reduce the cluster counts to the master process */
			MPI_Reduce(newcnts, recvcnts, cluster, MPI_INT, MPI_SUM, MASTER, MPI_COMM_WORLD);
			/* initilize the termination flag */
			int flag = 0;
			/* on the master process, compute the new centroids */
			if (rank == MASTER){
				/* iterate each cluster */
				for (i = 0; i < cluster; i++) {
					for (j = 0; j < dimension; j++) {
						recvctrs[i * dimension + j] /= recvcnts[i];	
					}
				}
				/* intilize the difference sum */
				double sum = 0;
				/* iterate each cluster and update the sum */
				for (i = 0; i < cluster; i++) {
					sum += dist2D(recvctrs+i*dimension,tdC+i*dimension);
				}
				/* if the difference is less than a threshold, set the termination flag */
				if (sum < TD_MIN)		
					flag = 1;
				free(tdC);
				tdC = recvctrs;
			}
			/* broadcast the temination flag */
			MPI_Bcast (&flag,1,MPI_INT,MASTER,MPI_COMM_WORLD); 

			free(newcnts);
			free(recvcnts);
			free(newctrs);

			if(flag)
				break;

			/* broadcast the new centroids */
			MPI_Bcast (tdC,cluster * dimension,MPI_DOUBLE,MASTER,MPI_COMM_WORLD); 
			
		}
			
	}while(1);
int WWD(int argc,char** argv){
	HANDLE *handles =new HANDLE[numCores];

	const int temp[] = {
		//Body
		295,195,95,	//main box: h,w,d
		1,		//1 box attached
		1,/*Joint type*/		50,50,5,/*preXYS*/		50,50,4,/*postXYS*/	45,45,45,/*DofXYZ*/ //Create joint
		295,195,95,	//attached box: h,w,d
		//NN-Effector0-layer 0
		1,	2,10,0,1,100,100,	//2-in-node: f=interpolate, in0=0,in1=1,w0=100,w1=100
		0,	2,0,0,0,0,100,		//2-in-node: f=sum,in0=0,in1=1,w0=0,w1=100 (just sends in1 through unchanged) - No more nodes
		1,0,1,	//NN-Effector: use outputs O0=1,O1=0,O2=1
		0,		//no attached boxes
		//NN-main-layer 0	(sensors not implemented yet)
		1,	0,30,			//constant node 30
		1,	0,62,			//constant node 62
		2,	0,125,			//constant node 125 - change layer
		//NN-main-layer 1
		1,	2,0,0,1,50,23,	//2-in-node: f=sum in0=0 in1=1 w0=50 w1=23
		2,	1,12,2,5,		//1-in node: f=cos in0=2 w0=5 - change layer
		//NN-main-layer 2
		1,	2,1,0,1,1,200,	//2-in node: f=product in0=0 in1=1, w0=1 w1=200
		0,	2,0,0,1,1,1		//2-in node: f=sum in0=0 in1=1, w0=1 w1=1 - No more nodes
	};
	int size = sizeof( temp ) / sizeof ( *temp );
	std::vector<int> ancestor (temp, temp+size);

	std::vector<creature>* creatures = new std::vector<creature>();

	//Creates one ancestor and the rest is mutation of the ancestor
	creatures->push_back(creature());
	creatures->at(0).dna=ancestor;
	creatures->at(0).fitness=0;
	for(int i=1; i<populationSize;i++){
		creatures->push_back(creature());
		creatures->at(i).dna=mutate(ancestor,2);
		creatures->at(i).fitness=0;
	}

	for(int i=0;i<nrOfGenerations;i++){
		//#pragma omp parallel
		{
			//run simulator
			//#pragma omp for schedule(dynamic)
			for(int j =0; j<populationSize; j++){
				//init world
				Physics* WWDPhysics = new Physics();

				//init creature
				readDNA(&creatures->at(j).dna,WWDPhysics);

				//run sim
				WWDPhysics->runSimulation();
				creatures->at(j).fitness = WWDPhysics->getFitness();
				creatures->at(j).treePointer = getMTree(&creatures->at(j).dna);
				delete WWDPhysics;
			}

			/*
			//threads
			for(int j =0; j<numCores; j++){
			handles[j] = (HANDLE)_beginthreadex(0, 0, &threadSim,(void*) j, 0, 0);
			}

			WaitForMultipleObjects(numCores, handles, true,INFINITE);

			for(int j =0; j<numCores; j++){
			CloseHandle(handles[j]);
			} */

			//print all unsorted
			/*for(int j=0;j< (int) worlds.size();j++){
			printf("nr %d %f\n",j,worlds.at(j)->getFitness());
			}*/

			//mutate
			//#pragma omp single nowait
			evolve(creatures); //Evolve cleans up the MTrees so no need for that here
			//print survivors sorted

			//#pragma omp for ordered
			for(int j=0;j< (int) (creatures->size()/5.f);j++){
#ifdef _DEBUG
				printf("nr %d %f\n",j,creatures->at(j).fitness);
#endif
			}
			//#pragma omp for ordered
#ifdef _DEBUG
			printf("round %d \n",i);
#endif
		}
	}

	for (int i = 0; i < (int) creatures->at(0).dna.size(); i++){
#ifdef _DEBUG
		printf("%d,", creatures->at(0).dna.at(i));
#endif
	}

	//Show end result if we want to...
	Physics* WWDPhysics = new Physics();
	//init creature
	readDNA(&creatures->at(0).dna,WWDPhysics);
	//default glut doesn't return from mainloop

	WWDPhysics->calcSize();
	WWDPhysics->solveGroundConflicts();
#ifdef _DEBUG
	printf("\nPress enter to continue\n");
#endif
	getchar();
	return glutmain(argc, argv,1024,600,"Walking with dinosaurs",WWDPhysics);
}
// WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int cmdShow){
	argv =CommandLineToArgvW(GetCommandLineW(),&argc);

	directory = getDirectory();
	srand(time(0));
#ifdef _DEBUG
	console();
#endif
	loadSaves();
	int height=768,width =1024;
	calcSizes(768-menuHeight,1024-border);

	WNDCLASSEX wc;

	//HDC hDC;
	HGLRC hRC;

	// register window class
	//definere et vindues classe og dens parametre/udsende
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	//alt tab icon + windows linie icon
	wc.hIcon = LoadIcon( GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1) );
	//look of the cursor
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	//background color
	wc.hbrBackground = (HBRUSH)GetSysColorBrush(COLOR_3DFACE);
	//menu bar
	wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
	wc.lpszClassName = "main";
	//icon venstre top hjørne
	//følgende virker også, loader dog default 32x32 icon
	//LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
	wc.hIconSm		 = (HICON)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 16, 16, 0);
	if(!RegisterClassEx(&wc)){
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	// create main window
	hWnd = CreateWindowEx( WS_EX_CLIENTEDGE,
		"main", "Walking With Dinosaurs",
		WS_THICKFRAME | WS_CAPTION | WS_VISIBLE | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX |WS_CLIPCHILDREN |WS_MAXIMIZEBOX,
		GetSystemMetrics(SM_CXMAXIMIZED)/2-SM_CXFIXEDFRAME/2-width/2-listWidth, GetSystemMetrics(SM_CYMAXIMIZED)/2-SM_CYFIXEDFRAME/2-height/2, 1024, 768,
		NULL, NULL, hInstance, NULL );

	//simulations window
	// register window class
	wc.cbSize		 = sizeof(WNDCLASSEX);
	//cs_owndc svaes the cach, used for painting outside normal routine
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
	wc.hCursor = LoadCursor( NULL, IDC_ARROW );
	wc.hbrBackground = (HBRUSH)GetStockObject( BLACK_BRUSH );
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "blank";
	if(!RegisterClassEx(&wc)){
		MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}

	blank = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("blank"), "", WS_CHILD | WS_VISIBLE ,listWidth,bAreaHeight,simWidth,simHeight, hWnd,(HMENU)IDC_SIM, GetModuleHandle(NULL), NULL);

	EnableOpenGL( blank, &hDC, &hRC );

	//selection listbox
	hWndList = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), "", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL|LBS_NOTIFY,0, 0, listWidth, listHeight, hWnd,  (HMENU)IDC_LISTBOX, GetModuleHandle(NULL), NULL);
	for(int i=0;i<(int)saves.size();i++){
		SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)saves.at(i)->name.c_str());
	}
	SendMessage(hWndList,LB_SETCURSEL,0,0);

	//init creature/world
	WWDPhysics = new Physics();
	readDNA(&saves.at(0)->dna,WWDPhysics);
	WWDPhysics->runSimStartUp();
	WWDPhysics->reshape(simWidth,simHeight);

	//settings area
	int row1=10,row2=35, row3 =60;
	int col1 =160,col2=col1+170,col3=500,col4=col3+170, col5=340,col6=col5+170;
	HWND hWndFitS=CreateWindowEx(NULL,TEXT("STATIC"),	"Fitness Test",WS_CHILD|WS_VISIBLE,	col1, row1, 100, 18, hWnd, (HMENU)IDC_TEST_STATIC, GetModuleHandle(NULL),	NULL);
	HWND hwndCombo = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("combobox"), "", WS_CHILD | WS_VISIBLE| CBS_DROPDOWNLIST ,col2, row1-5, 150, 24, hWnd,  (HMENU)IDC_FITNESSTYPE_COMBOBOX, GetModuleHandle(NULL), NULL);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 0, move);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Iterative Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 1, iterateMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Dwarfslayer Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 2, dwarfslayerMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Fat Loving Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 3, fatLovingMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Box Loving Move");
	SendMessage(hwndCombo,CB_SETITEMDATA, 4, boxLovingMove);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Jump");
	SendMessage(hwndCombo,CB_SETITEMDATA, 5, jump);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"Combi");
	SendMessage(hwndCombo,CB_SETITEMDATA, 6, combi);

	SendMessage(hwndCombo,CB_ADDSTRING, 0, (LPARAM)"None");
	SendMessage(hwndCombo,CB_SETITEMDATA, 7, none);

	SendMessage(hwndCombo,CB_SETCURSEL,0,0);
	WWDPhysics->addFitnessFunction(move,1);

	HWND hWndButton=CreateWindowEx(NULL,TEXT("BUTTON"),	"RUN", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		col1, row3, 100, 24,
		hWnd, (HMENU)IDC_RUN_BUTTON, GetModuleHandle(NULL),	NULL);

	HWND hWndResetButton=CreateWindowEx(NULL,TEXT("BUTTON"),	"Reset Simulation", WS_TABSTOP|WS_VISIBLE|WS_CHILD|BS_DEFPUSHBUTTON,
		col3, row3, 150, 24,
		hWnd, (HMENU)IDC_RESET_BUTTON, GetModuleHandle(NULL),	NULL);

	HGDIOBJ hfDefault=GetStockObject(DEFAULT_GUI_FONT);
	SendMessage(hWndButton,WM_SETFONT, (WPARAM)hfDefault, MAKELPARAM(FALSE,0));

	HWND hWndNoG=CreateWindowEx(NULL,TEXT("STATIC"),"Number of Generations",WS_CHILD|WS_VISIBLE,	col1, row2, 150, 18,
		hWnd, (HMENU)IDC_NOG_STATIC, GetModuleHandle(NULL),	NULL);

	HWND hWndNoGS=CreateWindowEx(NULL,TEXT("EDIT"),	"50",WS_CHILD|WS_VISIBLE|ES_NUMBER,	col2, row2, 42, 18,
		hWnd, (HMENU)IDC_NOG_EDIT, GetModuleHandle(NULL),	NULL);

	HWND hWndPopS=CreateWindowEx(NULL,TEXT("STATIC"),	"Population Size",WS_CHILD|WS_VISIBLE,	col3, row1, 150, 18,
		hWnd, (HMENU)IDC_POP_STATIC, GetModuleHandle(NULL),	NULL);
	HWND hWndPop=CreateWindowEx(NULL,TEXT("EDIT"),	"100",WS_CHILD|WS_VISIBLE |ES_NUMBER,	col4, row1, 42, 18,
		hWnd, (HMENU)IDC_POP_EDIT, GetModuleHandle(NULL),	NULL);

	HWND hWndViewRate=CreateWindowEx(NULL,TEXT("STATIC"),	"Simulation view precision",WS_CHILD|WS_VISIBLE,	col3, row2, 200, 18,
		hWnd, (HMENU)IDC_VIEW_STATIC, GetModuleHandle(NULL),	NULL);

	HWND hWndViewRateB=CreateWindowEx(NULL,TEXT("BUTTON"),	"",WS_CHILD|WS_VISIBLE | BS_CHECKBOX,	col4, row2, 100, 18,
		hWnd, (HMENU)IDC_VIEW_CHECKBOX, GetModuleHandle(NULL),	NULL);
	SendMessage(hWndViewRateB, BM_SETCHECK, BST_CHECKED,0);
	fixedSteps=true;
	EnableWindow(hWndViewRateB,false);


	MSG msg = messageLoop(hDC, hRC);
	return msg.wParam;
}
//Main Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch (message)
	{
	case WM_SYSKEYDOWN:
		{
			if (lParam & 1<<29)
			{
				WWDPhysics->m_modifierKeys = VK_LMENU;
			}
			break;
		}
	case WM_SYSKEYUP:
		{
			if (lParam & 1<<29)
			{
				WWDPhysics->m_modifierKeys = VK_LMENU;
			} else
			{
				WWDPhysics->m_modifierKeys = 0;
			}

			break;
		}

	case WM_SIZE:													// Size Action Has Taken Place

		switch (wParam)											// Evaluate Size Action
		{
		case SIZE_MINIMIZED:									// Was Window Minimized?
			return 0;												// Return

		case SIZE_MAXIMIZED:
			{									// Was Window Maximized?
				int Width = LOWORD (lParam);
				int Height = HIWORD (lParam);
				calcSizes(HIWORD (lParam),LOWORD (lParam));

				MoveWindow(hWndList,0,0,listWidth,listHeight,true);
				MoveWindow(blank,listWidth,bAreaHeight,simWidth,simHeight,true);

				if (sOpenGLInitialized)
				{
					WWDPhysics->reshape(simWidth,simHeight);
				}
			}
			return 0;												// Return

			//resize
		case SIZE_RESTORED:// Was Window Restored?
			if(hwnd == hWnd)
			{
				int Width = LOWORD (lParam);
				int Height = HIWORD (lParam);
				calcSizes(HIWORD (lParam),LOWORD (lParam));

				MoveWindow(hWndList,0,0,listWidth,listHeight,true);
				MoveWindow(blank,listWidth,bAreaHeight,simWidth,simHeight,true);

				if (sOpenGLInitialized){
					WWDPhysics->reshape(simWidth,simHeight);
				}
			}
			return 0;												// Return
		}
		break;

	case WM_CREATE:
		{
		}

		return 0;

	case WM_MBUTTONUP:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseFunc(1,1,xPos,yPos);
			POINT p = POINT();
			p.x=xPos; p.y=yPos;
			SetFocus(ChildWindowFromPoint(hwnd,p));
			break;
		}
	case WM_MBUTTONDOWN:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseFunc(1,0,xPos,yPos);
			POINT p = POINT();
			p.x=xPos; p.y=yPos;
			SetFocus(ChildWindowFromPoint(hwnd,p));
			break;
		}

	case WM_LBUTTONUP:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseFunc(0,1,xPos,yPos);
			POINT p = POINT();
			p.x=xPos; p.y=yPos;
			SetFocus(ChildWindowFromPoint(hwnd,p));
			break;
		}
	case 0x020A://WM_MOUSEWHEEL:
		{
			int zDelta = (short)HIWORD(wParam);
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			if (zDelta>0)
				WWDPhysics->zoomIn();
			else
				WWDPhysics->zoomOut();
			break;
		}

	case WM_MOUSEMOVE:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseMotionFunc(xPos,yPos);
			break;
		}
	case WM_RBUTTONUP:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseFunc(2,1,xPos,yPos);
			POINT p = POINT();
			p.x=xPos; p.y=yPos;
			SetFocus(ChildWindowFromPoint(hwnd,p));
			break;
		}
	case WM_RBUTTONDOWN:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseFunc(2,0,xPos,yPos);
			POINT p = POINT();
			p.x=xPos; p.y=yPos;
			SetFocus(ChildWindowFromPoint(hwnd,p));
			break;
		}
	case WM_LBUTTONDOWN:
		{
			int xPos = LOWORD(lParam);
			int yPos = HIWORD(lParam);
			WWDPhysics->mouseFunc(0,0,xPos,yPos);
			POINT p = POINT();
			p.x=xPos; p.y=yPos;
			SetFocus(ChildWindowFromPoint(hwnd,p));
			break;
		}

	case WM_CONTEXTMENU:
		{
			POINT p;
			GetCursorPos(&p);
			ScreenToClient(hWndList,&p);
			int index = SendMessage(hWndList,LB_ITEMFROMPOINT,0, MAKELPARAM(p.x,p.y));

			int noElements = SendMessage(hWndList,LB_GETCOUNT,0,0);
			if(0<=index && index <noElements){
				popupMenuSel=index;

				HMENU popupMenu = CreatePopupMenu();
				InsertMenu(popupMenu,0,MF_BYPOSITION|MF_STRING,IDC_RUN_MBUTTON,"Run");
				InsertMenu(popupMenu,0,MF_BYPOSITION|MF_STRING,IDC_RENAME_MBUTTON,"Rename");
				InsertMenu(popupMenu,0,MF_BYPOSITION|MF_STRING,IDC_DELETE_MBUTTON,"Delete");
#ifdef _DEBUG
				InsertMenu(popupMenu,0,MF_BYPOSITION|MF_STRING,IDC_SHOWDNA_MBUTTON,"Show DNA");
#endif
				GetCursorPos(&p);
				TrackPopupMenu(popupMenu,TPM_TOPALIGN|TPM_LEFTALIGN,p.x,p.y,0,hwnd,NULL);
			}

			break;}

	case WM_CLOSE:
		PostQuitMessage( 0 );
		return 0;

	case WM_DESTROY:
		return 0;

	case WM_KEYUP:
		switch ( wParam )
		{
		case VK_PRIOR:
		case VK_NEXT:
		case VK_END:
		case VK_HOME:
		case VK_LEFT:
		case VK_UP:
		case VK_RIGHT:
		case VK_DOWN:
			{
				if (WWDPhysics)
					WWDPhysics->specialKeyboardUp(wParam,0,0);
				return 0;
			}
		default:
			{
				WWDPhysics->keyboardUpCallback(tolower(wParam),0,0);
			}
			return DefWindowProc( hwnd, message, wParam, lParam );
		}

	case WM_KEYDOWN:
		switch ( wParam )
		{
		case VK_CONTROL:
		case VK_PRIOR:
		case VK_NEXT:
		case VK_END:
		case VK_HOME:
		case VK_LEFT:
		case VK_UP:
		case VK_RIGHT:
		case VK_DOWN:
			{
				if (WWDPhysics)
					WWDPhysics->specialKeyboard(wParam,0,0);
				break;
			}

		case ' ':
			{
				if (WWDPhysics)
					//WWDPhysics->clientResetScene();
						if(saves.size()>0){
							SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_LISTBOX,LBN_SELCHANGE),0);
						}
						break;
			}
		case 'Q':
		case VK_ESCAPE:
			{
				quitRequest = 1;
				PostQuitMessage(0);
			}
			return 0;
		}
		return 0;

	case WM_CHAR:
		if (!quitRequest)
			WWDPhysics->keyboardCallback(wParam,0,0);
		break;

	case WM_COMMAND:
		switch(LOWORD(wParam)){
		case IDC_VIEW_CHECKBOX:
			{
				HWND check = GetDlgItem(hwnd, IDC_VIEW_CHECKBOX);
				switch (SendMessage(check, BM_GETCHECK, 0,0))
				{
				case BST_CHECKED:
					SendMessage(check, BM_SETCHECK, BST_UNCHECKED,0);
					fixedSteps=false;
#ifdef _DEBUG
					printf("off\n");
#endif
					break;

				case BST_UNCHECKED:
					SendMessage(check, BM_SETCHECK, BST_CHECKED,0);
					fixedSteps=true;
#ifdef _DEBUG
					printf("on\n");
#endif
					break;
				}
			}
			break;

		case IDC_FITNESSTYPE_COMBOBOX:
			switch (HIWORD(wParam)){
			case CBN_SELCHANGE:
				HWND hwndfit = GetDlgItem(hwnd, IDC_FITNESSTYPE_COMBOBOX);
				int index = SendMessage(hwndfit, CB_GETCURSEL,0,0);
				fitnessTest tmptest=(fitnessTest) SendMessage(hwndfit,CB_GETITEMDATA, index,0);
				WWDPhysics->clearFitnessFunctions();
				WWDPhysics->addFitnessFunction(tmptest,1);
				break;
			}

			break;

		case IDC_LISTBOX:
			{
				switch (HIWORD(wParam)){
				case LBN_SELCHANGE:
					{
						HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX);

						// Get selected index.
						int index = (int)SendMessage(hwndList, LB_GETCURSEL, 0, 0);

						delete WWDPhysics;

						WWDPhysics = new Physics();
						SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_FITNESSTYPE_COMBOBOX,LBN_SELCHANGE),0);
						readDNA(&saves.at(index)->dna,WWDPhysics);
						WWDPhysics->runSimStartUp();
						WWDPhysics->reshape(simWidth,simHeight);
					}
				}
			}
			break;
		case IDC_RESET_BUTTON:
			{
				if(saves.size()>0){
					SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_LISTBOX,LBN_SELCHANGE),0);
				}
			}
			break;
		case IDC_RUN_BUTTON:
			{
				HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX);

				_ASSERTE(hwndList != NULL);

				// Get current selection index in listbox
				int itemIndex = (int) SendMessage(hwndList, LB_GETCURSEL, (WPARAM)0, (LPARAM) 0);
				if (itemIndex == LB_ERR)
				{
					// No selection
					return 0;
				}

				//threads
				SYSTEM_INFO sysinfo;
				GetSystemInfo( &sysinfo );

				int numCores= sysinfo.dwNumberOfProcessors;

				int pop =0;
				HWND hwndPop = GetDlgItem(hwnd, IDC_POP_EDIT);

				_ASSERTE(hwndPop != NULL);

				int length=0;

				length = GetWindowTextLength(hwndPop);

				if(length > 0){
					TCHAR * text = new TCHAR[length + 1];
					GetWindowText(hwndPop,text,length+1);
					for(int i =0; i<length;i++){
						int value = ((int)text[i])-48;
						int res =  (value* pow(10.,length-1-i)+0.5);
						pop+=res;
					}
					delete text;
				}else{
					MessageBox(NULL, "No populastion size selected", TEXT("ERROR"), MB_OK | MB_ICONERROR);
					return 0;
				}
				if(pop<10){
					MessageBox(NULL, "the Populasion size is to small", TEXT("ERROR"), MB_OK | MB_ICONERROR);
					return 0;
				}

				int noG = 0;
				HWND hwndNoG = GetDlgItem(hwnd, IDC_NOG_EDIT);

				_ASSERTE(hwndNoG != NULL);

				length=0;

				length = GetWindowTextLength(hwndNoG);
				if(length > 0){
					TCHAR * text = new TCHAR[length + 1];
					GetWindowText(hwndNoG,text,length+1);
					for(int i =0; i<length;i++){
						int value = ((int)text[i])-48;
						int res =  (value* pow(10.,length-1-i)+0.5);
						noG+=res;
					}
					delete text;
				}else{
					MessageBox(NULL, "No number of generations selected", TEXT("ERROR"), MB_OK | MB_ICONERROR);
					return 0;
				}

				HWND hwndfit = GetDlgItem(hwnd, IDC_FITNESSTYPE_COMBOBOX);
				int index = SendMessage(hwndfit, CB_GETCURSEL,0,0);
				fitnessTest tmptest=(fitnessTest) SendMessage(hwndfit,CB_GETITEMDATA, index,0);

				noGenerations=noG;
				argumentList* aList = new argumentList();
				aList->nC=numCores;
				aList->p=pop;
				aList->nG=noG;
				aList->iI=itemIndex;
				aList->stopSim=&stopSim;
				aList->type=tmptest;
				aList->theResult = new creature();

				proInfo = new progressInfo();

				HANDLE threadHandle = (HANDLE) _beginthreadex(0,0,&runServer,(void*)aList,0,0);

				UINT_PTR time = SetTimer(0,0,10,(TIMERPROC)&update);
				DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_PROGRESS), hwnd, progressControll,(LPARAM)&noG);
				KillTimer(0,time);

				save* tmpCreature =new save();
				tmpCreature->dna= aList->theResult->dna;
				tmpCreature->fitness=aList->theResult->fitness;

				delete proInfo;
				delete aList->theResult;
				delete aList;

				DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_NAMING), hwnd, namingControl,(LPARAM)&tmpCreature->name);

				saves.push_back(tmpCreature);
				SendMessage(hWndList, LB_ADDSTRING, 0, (LPARAM)saves.at(saves.size()-1)->name.c_str());
				SendMessage(hWndList,LB_SETCURSEL,saves.size()-1,0);
				delete WWDPhysics;

				WWDPhysics = new Physics();
				WWDPhysics->addFitnessFunction(tmptest,1);
				readDNA(&saves.at(saves.size()-1)->dna,WWDPhysics);
				WWDPhysics->runSimStartUp();
				WWDPhysics->reshape(simWidth,simHeight);
			}
			break;

		case IDC_RUN_MBUTTON:
			{
				HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX);
				SendMessage(hwndList,LB_SETCURSEL,popupMenuSel,0);

				SendMessage(hWnd,WM_COMMAND, MAKEWPARAM(IDC_RUN_BUTTON,0),0);
			}

			break;
		case IDC_RENAME_MBUTTON:
			{
				DialogBoxParam(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_NAMING), hwnd, namingControl,(LPARAM)&saves.at(popupMenuSel)->name);
				HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX);
				int itemIndex = (int) SendMessage(hwndList, LB_GETCURSEL, (WPARAM)0, (LPARAM) 0);
				for(int i =0; i<saves.size();i++){
					SendMessage(hwndList,LB_DELETESTRING,0,0);
				}
				for(int i =0; i<saves.size();i++){
					SendMessage(hwndList, LB_ADDSTRING, 0, (LPARAM)saves.at(i)->name.c_str());
				}

				SendMessage(hwndList,LB_SETCURSEL,itemIndex,0);
			}
			break;
		case IDC_DELETE_MBUTTON:
			{
				HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX);
				int itemIndex = (int) SendMessage(hwndList, LB_GETCURSEL, (WPARAM)0, (LPARAM) 0);

				SendMessage(hwndList,LB_DELETESTRING,popupMenuSel,0);
				delete saves.at(popupMenuSel);
				saves.erase(saves.begin()+popupMenuSel,saves.begin()+popupMenuSel+1);

				if(itemIndex==popupMenuSel){
					if(saves.size() > 0){
						if(itemIndex==saves.size()){
							itemIndex--;
						}
						SendMessage(hwndList,LB_SETCURSEL,itemIndex,0);
						SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_LISTBOX,LBN_SELCHANGE),0);
					}else{
						delete WWDPhysics;
						WWDPhysics=new Physics();
						WWDPhysics->reshape(simWidth,simHeight);
					}
				}
			}
			break;
		case IDC_SHOWDNA_MBUTTON:
			{
#ifdef _DEBUG
				HWND hwndList = GetDlgItem(hwnd, IDC_LISTBOX);

				printf("DNA of %s\n",saves.at(popupMenuSel)->name.c_str());
				for(int i=0; i<saves.at(popupMenuSel)->dna.size();i++){
					printf("%d, ", saves.at(popupMenuSel)->dna.at(i));
				}
				printf("\n");
#endif
			}
			break;
		case ID_FILE_NEW40002:

			randomCreature();
			while( (WWDPhysics->noBoxes<2) || (!WWDPhysics->runSimStartUp())  ){
				{//deleting
					int itemIndex = (int) SendMessage(hWndList, LB_GETCURSEL, (WPARAM)0, (LPARAM) 0);
					SendMessage(hWndList,LB_DELETESTRING,itemIndex,0);
					delete saves.at(itemIndex);
					saves.erase(saves.begin()+itemIndex,saves.begin()+itemIndex+1);
				}
				randomCreature();
			}
			break;

		case ID_FILE_SAVE40003:

			saveSaves(saves);
			break;

		case ID_FILE_EXIT:
			PostQuitMessage( 0 );

			break;
		case ID_VIDEO_CAPTURE:
			if(VFWInit()){
				captureVideo(hDC);
				MessageBox(hwnd, "Video Captured", TEXT("DONE"), MB_OK | MB_ICONINFORMATION);
			}else{
				MessageBox(hwnd, "Video Capture failed to start\n Try another codec", TEXT("ERROR"), MB_OK | MB_ICONERROR);
			}
			SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_LISTBOX,LBN_SELCHANGE),0);
			break;
#ifdef _DEBUG
		default:

			printf("");
#endif
		}
		break;

	default:
		return DefWindowProc( hwnd, message, wParam, lParam );
	}
	return 0;
}