void MutateIndividual(Individual& individualToBeMutated, double mutationProbability) {
  int numberOfUnits = individualToBeMutated.size();
  for(int i=0;i<numberOfUnits;i++) {
    UnitGene tempUnit = individualToBeMutated[i]; // Choose one unit
    int numberOfGeneSets = tempUnit.size();
    for(int j=0;j<numberOfGeneSets;j++) {
      Genes geneSetForMutation = tempUnit[j];  // Choose one gene set
      if(j<numberOfGeneSets-1) { // MUTATION FOR WHOLE GENE SETS - MACHINE GENE SET MUTATION
        if(i!=0 && j!=1) { // To avoid mutation for the dummy buffer gene set
          if(GetRandomDouble()<mutationProbability) {
            MutateMachineGeneSet(geneSetForMutation);
            tempUnit[j] = geneSetForMutation; // Current Unit is replaced with new mutated machine gene set
          } // Mutation complete. If no mutation, current unit maintains old machine gene set
        }
      }
      else { // NORMAL MUTATION FOR INDIVIDUAL GENES - AXLE GENE MUTATION
        if(i!=0) { // To avoid mutation for the tractor axles gene set
          MutateAxleGeneSet(geneSetForMutation, mutationProbability);
          tempUnit[j] = geneSetForMutation; // Current Unit is replaced with new mutated axle gene set
        }
      } // Mutation complete. If no mutation, current unit maintains old axle gene set
    } // All gene sets in current unit have undergone mutation
    individualToBeMutated[i] = tempUnit; // Mutated unit is now placed in the individual
  } // All units in current individual have undergone mutation
}
int TournamentSelection(Fitnesses& fitnessForSelection, float tournamentSelectionParameter, int tournamentSize) {
  std::vector<int> randomIndividualIndex;
  std::vector<double> randomIndividualFitness;
  int populationSize = fitnessForSelection.size();

  // Select two individuals to participate in the tournament
  for(int i=0;i<tournamentSize;i++) {
    // Contains the indices of two individuals participating in the tournament
    int temporaryIndex = GetRandomIndex(populationSize);
    randomIndividualIndex.push_back(temporaryIndex); // Index needed to pass to main()
    double temporaryFitness = fitnessForSelection[temporaryIndex];
    randomIndividualFitness.push_back(temporaryFitness);
  }

  // Generate random floating point number (double) for tournament selection
  double randomDouble = GetRandomDouble();
  int fittestIndividualIndex, weakestIndividualIndex;

  // Choose fitter individual if random double is lesser than tournament selection parameter
  if(randomDouble < tournamentSelectionParameter) {
    // Return fittest individual 
    fittestIndividualIndex = GetFittestIndividual(randomIndividualIndex, randomIndividualFitness);
    return fittestIndividualIndex;
  }
  else {
    // Return weakest individual
    weakestIndividualIndex = GetWeakestIndividual(randomIndividualIndex, randomIndividualFitness);
    return weakestIndividualIndex;
  }
}
예제 #3
0
double RandTools::GetGaussianDouble(double sigma, double mu) {
	generate = !generate;

	if (!generate)
		return (z1 * sigma + mu);

	double u1, u2;

	do {
		u1 = GetRandomDouble();
		u2 = GetRandomDouble();
	}while (u1 <= epsilon);

	z0 = sqrt(-2.0 * log(u1)) * cos(two_pi * u2);
	z1 = sqrt(-2.0 * log(u1)) * sin(two_pi * u2);
	return (z0 * sigma + mu);
} // Generates two gaussian distributed random numbers and stores the second one untill called again, then repeats.
void MutateAxleGeneSet(Genes& geneSetForMutation, double mutationProbability) {
  int numberOfGenes = geneSetForMutation.size();
  for(int i=0;i<numberOfGenes;i++) {
    if(GetRandomDouble()<mutationProbability){
      //std::cout<<"Inside axle mutation"<<std::endl;
      geneSetForMutation[i]=1-geneSetForMutation[i];
    }
  }
}
예제 #5
0
//************************************************************************
// CLCDGfx::StartTransition
//************************************************************************
void CLCDGfx::Cache()
{
	DWORD dwStart = GetTickCount();

	// Initialize pixels
	if(m_eTransition == TRANSITION_MORPH)
	{
		SLCDPixel *pPixel = NULL;
		SLCDPixel *pSource = NULL;

		int iIndex = 0;
		bool bBreak = false;
		bool bSearch = true;

		int iTransitionPixels = 0;

		POINT pt;
		for(int j=0;j<2;j++)
		{
			iIndex = 0;
			for(int i=0;i<m_nHeight * m_nWidth;i++)
			{
				pt.y = i/m_nWidth;
				pt.x = i - (i/m_nWidth)*m_nWidth;
				if((j==0 && !PtInRect(&m_rTransitionRegion,pt)) || (m_pSavedBitmapBits[i] != 0x00 && (j==1 || m_pBitmapBits[i] != 0x00)))
				{
					iIndex = i;

					pPixel = new SLCDPixel();
					pPixel->dSpeed = GetRandomDouble()*0.5 + 1;
					if(!PtInRect(&m_rTransitionRegion,pt)) {
						pPixel->cValue = m_pBitmapBits[i];
					} else {
						pPixel->cValue = m_pSavedBitmapBits[i];
					}
					pPixel->Start.y = pt.y;
					pPixel->Start.x = pt.x;
					pPixel->Position = pPixel->Start;

					bBreak = false;
					if(j==1 && bSearch)
					{
						// search for a pixel in circles with increasing radius around the location
						iIndex = findNearestMatch(m_pBitmapBits,i);
						if(iIndex < 0) {
							iIndex = i;
						} else {
							bBreak = true;
						}
					}

					if(j==0 || bBreak)
					{
						pPixel->Destination.y = iIndex/m_nWidth;
						pPixel->Destination.x = iIndex - (iIndex/m_nWidth)*m_nWidth;
						m_pBitmapBits[iIndex] = 0;
						m_pSavedBitmapBits[i] = 0;

						if(bBreak)
							iTransitionPixels++;
					}
					else
					{
						if(m_LMovingPixels.size() > 0 && iTransitionPixels > 0)
						{
							pSource = m_LMovingPixels[GetRandomInt(0, (int)m_LMovingPixels.size()-1)];		
							pPixel->Destination = pSource->Destination;
						}
						else
						{
							pPixel->Destination.x = GetRandomInt(0,m_nWidth-1);
							pPixel->Destination.y = GetRandomInt(0,1)==1?-1:m_nHeight+1;
						}
						bSearch = false;
					}

					if(j == 0)
						m_LStaticPixels.push_back(pPixel);
					else {
						m_LMovingPixels.push_back(pPixel);
					}
				}
			}
		}
		bool bRandom = false;
		if(m_LMovingPixels.size() <= 0)
			bRandom = true;

		for(iIndex=0;iIndex<m_nHeight * m_nWidth;iIndex++)
		{
			if(m_pBitmapBits[iIndex] == 0)
				continue;

			pPixel = new SLCDPixel();
			pPixel->dSpeed = GetRandomDouble()*0.5 + 1;
			pPixel->cValue = m_pBitmapBits[iIndex];

			if(!bRandom)
			{
				pSource = m_LMovingPixels[GetRandomInt(0, (int)m_LMovingPixels.size()-1)];	
				pPixel->Start = pSource->Start;
			}
			else
			{
				pPixel->Start.x = GetRandomInt(0,m_nWidth-1);
				pPixel->Start.y = GetRandomInt(0,1)==1?-1:m_nHeight+1;
			}

			pPixel->Position = pPixel->Start;

			pPixel->Destination.y = iIndex/m_nWidth;
			pPixel->Destination.x = iIndex - (iIndex/m_nWidth)*m_nWidth;
			m_LMovingPixels.push_back(pPixel);
		}

	}


	m_dwTransitionStart = GetTickCount();
	TRACE(_T("Textmorphing: time consumed: %0.2f\n"),(double)(m_dwTransitionStart-dwStart)/(double)1000);
}