home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / APR94_1.ZIP / GA.ZIP / SOURCE.ZIP / GRAPHGA.CPP < prev    next >
C/C++ Source or Header  |  1994-01-10  |  3KB  |  108 lines

  1. //Copyright (C) Man Machine Interfaces 1994. All rights reserved.
  2.  
  3. //graphga.h
  4.  
  5. #include "stdafx.h"
  6.  
  7. //Headers needed for EOS programs
  8. //You need EOS v1.1 to compile this code
  9. #include "eos.h"
  10. #include "geno.h"
  11. #include "basicga.h"
  12. #include "nptxgeno.h"
  13. #include "genrepop.h"
  14. #include "gaenviro.h"
  15.  
  16. //headers specific to graph GA
  17. #include "gdriver.h"
  18. #include "graphga.h"
  19. #include "graphind.h"
  20. #include "grphphen.h"
  21. #include "wmatrix.h"
  22.  
  23. CGraphDrawerGA::CGraphDrawerGA(CGAGraphDriver &driver) 
  24.     : m_Driver(driver)
  25. {
  26. }
  27.  
  28. //print out GA generation stats when testing
  29. void CGraphDrawerGA::Report(uint gen) 
  30. {
  31. #ifdef STANDALONE_TEST
  32.  
  33.     printf("================================ GENERATION %d ==============================\n",gen) ;
  34.     ulong size = m_pPopulation->GetSize() ;
  35.  
  36.     for (uint i = 0; i <size; i++) {
  37.         PTBinaryIndividual pIndiv ;
  38.         pIndiv =  (PTBinaryIndividual) m_pPopulation->GetParent(i) ;
  39.         printf("%g\n",pIndiv->GetFitness()) ;
  40.         }
  41.         
  42.     printf("Sum: %6.4g, Min: %6.4g, Max: %6.4g, Avg: %6.4g\n\n",
  43.         m_pEnvironment->SumFitness,
  44.         m_pEnvironment->MinFitness,
  45.         m_pEnvironment->MaxFitness,
  46.         m_pEnvironment->SumFitness/size) ;
  47.         
  48. #endif //STANDALONE_TEST
  49. }
  50.  
  51. //Create the population of individuals
  52. //We use 2 Point Crossover and Elitism
  53. void CGraphDrawerGA::CreatePopulation(long size, PTIndividual prototype) 
  54. {
  55.     PTNPtCrossGenotype pGeno = new TNPtCrossGenotype(m_Driver.CalcChromosomeLength(),1,2) ;
  56.     CGraphDrawingPheno * pPheno = new CGraphDrawingPheno(m_Driver,m_Driver.GetWidth(),m_Driver.GetHeight()) ;
  57.     CGraphDrawingInd indiv(pGeno,pPheno);
  58.     m_pPopulation = new TGenReplacePopulation(size,&indiv) ;
  59.     m_pPopulation->SetElitism(2) ;
  60. }
  61.  
  62. //print out GA exit stats when testing
  63. void CGraphDrawerGA::ExitReport() 
  64. {   
  65.     m_Driver.m_pBest = m_pEnvironment->GlobalFittestIndivid ;
  66.     m_Driver.m_pWorst = m_pEnvironment->GlobalWorstIndivid ;
  67.     
  68. #ifdef STANDALONE_TEST
  69.  
  70.     double fitness ;
  71.     PTIndividual pIndiv = FittestIndividual(fitness) ;
  72.     pIndiv->Decode() ; //because TFinancialStrategy::Copy() does not build pheno info for performance
  73.     CWordMatrix *pGrid ;
  74.     pIndiv->GetPhenoInfo(&pGrid) ;           
  75.     int numRows = pGrid->GetNumRows() ;
  76.     int numCols = pGrid->GetNumCols() ;
  77.     for (int row=0; row<numRows; row++) {
  78.         for (int col=0; col<numCols; col++) {
  79.             printf("%u ",pGrid->GetAt(row,col)) ;
  80.             }
  81.         printf("\n") ;
  82.         }
  83.     printf("%g\n",fitness) ;
  84.     WORD numNodes = m_Driver.GetNumNodes() ;
  85.     for (WORD node1=1;node1<=numNodes;node1++) 
  86.         for (WORD node2=1;node2<=numNodes;node2++) 
  87.             if (node1 != node2 && m_Driver.Connected(node1,node2))
  88.                 printf("Node(%u) --> Node(%u)\n",node1,node2) ;
  89.         
  90. #endif //STANDALONE_TEST
  91. }
  92.  
  93. //allow for windows processing!
  94. void CGraphDrawerGA::InterGeneration(ulong, PTIndividual, PTIndividual, PTIndividual, PTIndividual) 
  95. {
  96.     MSG msg ;            //MSW message struct
  97.     while (PeekMessage(&msg,AfxGetApp()->m_pMainWnd->m_hWnd,0,0,PM_REMOVE)) {   //while there are msgs for status window
  98.         TranslateMessage(&msg) ;
  99.         DispatchMessage(&msg) ;
  100.         }
  101. //    SetCursor(LoadCursor(NULL, IDC_WAIT));
  102. }
  103.     
  104. BOOL CGraphDrawerGA::Stop() 
  105. {
  106.     return m_Driver.m_Stop ;
  107. }
  108.