#include "PopulationContainer.h" extern HWND g_hWnd; // global window handle extern HBITMAP MakeCompatibleBitmap(HDC hdc, HBITMAP hBmp); extern PBITMAPINFO CreateBitmapInfoStruct(HWND hwnd, HBITMAP hBmp); /* returns a randomly generated Val*/ Val c_rand(void) { if(rand()%2 > 0) return Val(((double)(rand()%8192))/(2048.0)-2.0, ((double)(rand()%8192))/(2048.0)-2.0, ((double)(rand()%8192))/(2048.0)-2.0); double a = ((double)(rand()%8192))/(2048.0)-2.0; return Val(a,a,a); } /* default constructor*/ PopulationContainer::PopulationContainer() { } /* initializes the population in the container*/ void PopulationContainer::init() { /* generate the population*/ P = new Population(64,b_func_list,num_b_funcs,u_func_list, num_u_funcs,v_list,num_vs,c_rand); // check for error if (!P->members()) { fprintf(stderr,"error creating population!\n"); exit(-1); } for(int i=0 ; isize;i++) { /* set every member to 0 */ P->trees[i]->fitness=0; } /* create all the bitmaps */ UpdateMaps(); } /* create a new population based upon mutations from a * chosen image with index m*/ void PopulationContainer::mutatePop(int m) { P->trees[m]->fitness=1; /* set chosen image's index to 1, which is higher than all others */ P->sort(); /* sort so that the chosen image is at index 0 */ /*iterate through all the trees */ for(int i =0;i<((64)-1);i++) { delete P->trees[1+i]; /* delete all trees but the chosen one */ /* copy the chosen tree in as the new node */ P->trees[1+i] = (Binary_Node *)tree_copy( P->trees[0] ,NULL); /* then mutate the new node */ P->mutate(P->trees[1+i]); } for( i=0 ; i<64;i++) { P->trees[i]->fitness=0; } UpdateMaps(); } /* resets the population, same as init*/ void PopulationContainer::reset() { P = new Population(64,b_func_list,num_b_funcs,u_func_list, num_u_funcs,v_list,num_vs,c_rand); if (!P->members()) { fprintf(stderr,"error creating population!\n"); exit(-1); } for(int i=0 ; isize;i++) { P->trees[i]->fitness=0; } UpdateMaps(); } /* returns the node with index i */ Node* PopulationContainer::getNode(int i) { return P->trees[i]; } /* updates the vector of bitmaps */ void PopulationContainer::UpdateMaps() { /* if there are currently bitmaps, destroy them */ if(!bitmaps.empty()) { for(vector::iterator itr = bitmaps.begin() ; itr != bitmaps.end() ; itr++) { DeleteObject((*itr)); } bitmaps.clear(); } /* iterate through all trees and get a bitmap out of them*/ for(int i=0; i< P->members() ; i++) { bitmaps.push_back(getBitmapFromNode( P->trees[i],80,80,0.0f,0.0f,1.0f,1.0f )); } } /* returns a handle to a bitmap using the given node of * the dimensions given. * c_x, c_y are usually set to 0 (center of bitmap) * scales are usually set to 1 */ HBITMAP PopulationContainer::getBitmapFromNode(Node* node,int width, int height,double c_x, double c_y, double xscale , double yscale) { vector bm;/* vector to hold color info */ bm.resize(width*height*4); /* resized to the size of the bitmap*/ short r,g,b; /* temp variables */ Val value; Val* x = v_list[0]->var; Val* y = v_list[1]->var; for(int j=0;jr = (((double)j)/((width)/ (xscale*2) )-(xscale-c_x)); x->g = (((double)j)/((width)/ (xscale*2) )-(xscale-c_x)); x->b = (((double)j)/((width)/ (xscale*2) )-(xscale-c_x)); y->r = (((double)k)/((height)/ (yscale*2) )-(yscale-c_y)); y->g = (((double)k)/((height)/ (yscale*2) )-(yscale-c_y)); y->b = (((double)k)/((height)/ (yscale*2) )-(yscale-c_y)); /* get the color value */ Val value = node->value(); /* scale the color val to be between 0 and 256 */ r =(short)((value.r)*256); g =(short)((value.g)*256); b =(short)((value.b)*256); /* Replace the previous 3 lines with these three lines to have the range be -1 - 1 istead of 0 - 1. r =(short)(((value.r+1)/2.0)*256); g =(short)(((value.g+1)/2.0)*256); b =(short)(((value.b+1)/2.0)*256); */ /* make sure that any values out of bounds are set to * 255 or 0 */ if(r > 255) r = 255; if(r < 0) r = 0; if(g > 255)g = 255; if(g < 0) g = 0; if(b > 255) b = 255; if(b < 0) b = 0; /* put the values in the color vector */ bm[(k*width+j)*4+0] = b; bm[(k*width+j)*4+1] = g; bm[(k*width+j)*4+2] = r;//RGB(b,g,r); } } /* create a temporary device context to convert bitmap */ HDC hdc = GetDC(g_hWnd); /* create a bitmap from the color information */ HBITMAP bmp = CreateBitmap(width,height,1,32,&bm[0]); PBITMAPINFO pBitInfo = CreateBitmapInfoStruct(g_hWnd, bmp); PBITMAPINFOHEADER pbih; pbih = (PBITMAPINFOHEADER) pBitInfo; /* convert bitmap to a Device Independant Bitmap */ HBITMAP dib = CreateDIBitmap(hdc,pbih,CBM_INIT,&bm[0],pBitInfo,0); /* delete temporary device context */ DeleteDC(hdc); /* delete device dependent bitmap */ DeleteObject(bmp); return dib; } /* draws the bitmap to the screen */ void PopulationContainer::DrawBitmap(HDC hdc, HBITMAP hBitmap, int xStart, int yStart) { BITMAP bm; HDC hdcMem; POINT ptSize, ptOrg; hdcMem = CreateCompatibleDC(hdc); /* select bitmap into a memory context*/ SelectObject(hdcMem, hBitmap); SetMapMode (hdcMem, GetMapMode(hdc)); GetObject(hBitmap, sizeof(BITMAP), (LPVOID) &bm); ptSize.x = bm.bmWidth; ptSize.y = bm.bmHeight; DPtoLP(hdc,&ptSize,1); ptOrg.x =0; ptOrg.y = 0; DPtoLP(hdcMem,&ptOrg,1); /* blit from bitmat memory context to screen memory context */ BitBlt(hdc, xStart, yStart,ptSize.x,ptSize.y, hdcMem, ptOrg.x,ptOrg.y,SRCCOPY); DeleteDC(hdcMem); }