Example #1
0
Gene Gene::cross(Gene* father, Gene* mother, bool mutate)
{
	double perfectChild[100];
	double* fatherDist = father->getDist();
	double* motherDist = mother->getDist();

	for(int i = 0; i < 100; i++)
		perfectChild[i] = (fatherDist[i] + motherDist[i]) / 2;

	delete[] fatherDist;
	delete[] motherDist;

	double imaginary[100];
	for(int i = 0; i < 100; imaginary[i++] = 0); 

	bool succeed = DFT(1, 100, perfectChild, imaginary);
	if(!succeed)
		exit(0);
	
	int* sines = selectTop(imaginary);
	int* cosines = selectTop(perfectChild);

	Harmonic waves[WAVECOUNT];
	for(int i = 0; i < WAVECOUNT; i++)
	{
		double alpha = mutate && randFloat() <= MUTATION_RATE ? MAX_OF_SCALED_SIN_PLUS_COS - 2 * randFloat() * MAX_OF_SCALED_SIN_PLUS_COS : imaginary[sines[i]];
		double beta = mutate && randFloat() <= MUTATION_RATE ? MAX_OF_SCALED_SIN_PLUS_COS - 2 * randFloat() * MAX_OF_SCALED_SIN_PLUS_COS : perfectChild[cosines[i]];
		int mu = mutate && randFloat() <= MUTATION_RATE ? (rand() % 48) + 1 : sines[i];
		int omega = mutate && randFloat() <= MUTATION_RATE ? (rand() % 48) + 1 : cosines[i];

		waves[i] = Harmonic(alpha, mu, beta, omega);
	}

	double magnitude = mutate && randFloat() <= MUTATION_RATE ? randFloat() * (father->magnitude + mother->magnitude) : ((father->magnitude + mother->magnitude) / 2);

	return Gene(magnitude, waves);
}
Example #2
0
////////////////////////////////
// compute function for renderer
////////////////////////////////
complex ComputeFunc (int x, int y, int z, int time)
{
	
	complex psi_1={0, 0}, psi_2={0, 0}, psi_3={0, 0}, psi_4={0, 0}, psi_target={0, 0};
	complex psi_1t={0, 0}, psi_2t={0, 0}, psi_3t={0, 0}, psi_4t={0, 0};
	complex psi={0, 0};
	
	// static vars
	static float t=0.0f;		// blending factor
	static int old_time=0;	
	
	if (old_time!=time)
	{
		// evaluate variables not dependent on space here
		// calculate morphing factor
		t=time;
		t=(morphtime==0)?t:t/morphtime;
		if (t>1.0f) t=1.0f;
		if (!morph) t=0;
	}
	
	
	// calculate space-dependent vars here
	if (wave1)
	{
		if (harmonic)
		{	psi_1=Harmonic (x, y, z, time, w1);
		}
		else if (box)
		{		
			psi_1=ParticleinaBox (x, y, z, time, nx1, ny1, nz1);
		}
		else if (oneelectron)
		{
			psi_1=OneElectronAtom (x, y, z, time, n1, l1, m1);
		}
	}	
	if (wave2)
	{
		if (harmonic)
		{	psi_2=Harmonic (x, y, z, time, w2);
		}
		else if (box)
		{		
			psi_2=ParticleinaBox (x, y, z, time, nx2, ny2, nz2);
		}
		else if (oneelectron)
		{
			psi_2=OneElectronAtom (x, y, z, time, n2, l2, m2);
		}
	}	
	if (wave3)
	{
		if (harmonic)
		{	psi_3=Harmonic (x, y, z, time, w3);
		}
		else if (box)
		{		
			psi_3=ParticleinaBox (x, y, z, time, nx3, ny3, nz3);
		}
		else if (oneelectron)
		{
			psi_3=OneElectronAtom (x, y, z, time, n3, l3, m3);
		}
	}	
	if (wave4)
	{
		if (harmonic)
		{	psi_4=Harmonic (x, y, z, time, w4);
		}
		else if (box)
		{		
			psi_4=ParticleinaBox (x, y, z, time, nx4, ny4, nz4);
		}
		else if (oneelectron)
		{
			psi_4=OneElectronAtom (x, y, z, time, n4, l4, m4);
		}
	}	
	
	// multiply with coefficients
	psi_target.real=re21*psi_1.real-im21*psi_1.imag
			+re22*psi_2.real-im22*psi_2.imag
			+re23*psi_3.real-im23*psi_3.imag
			+re24*psi_4.real-im24*psi_4.imag;
	psi_target.imag=im21*psi_1.real+re21*psi_1.imag
			+im22*psi_2.real+re22*psi_2.imag
			+im23*psi_3.real+re23*psi_3.imag
			+im24*psi_4.real+re24*psi_4.imag;
	
	psi_1t.real=re11*psi_1.real-im11*psi_1.imag; psi_1t.imag=im11*psi_1.real+re11*psi_1.imag;
	psi_2t.real=re12*psi_2.real-im12*psi_2.imag; psi_2t.imag=im12*psi_2.real+re12*psi_2.imag;
	psi_3t.real=re13*psi_3.real-im13*psi_3.imag; psi_3t.imag=im13*psi_3.real+re13*psi_3.imag;
	psi_4t.real=re14*psi_4.real-im14*psi_4.imag; psi_4t.imag=im14*psi_4.real+re14*psi_4.imag;
	psi_1=psi_1t; psi_2=psi_2t; psi_3=psi_3t; psi_4=psi_4t;
	
	// target wave function
	if (morph)
	{		
		if (morph12)
		{
			psi.real=(1.0f-t)*(psi_1.real+psi_2.real+psi_3.real+psi_4.real)
					+t*psi_target.real;
			psi.imag=(1.0f-t)*(psi_1.imag+psi_2.imag+psi_3.imag+psi_4.imag)
					+t*psi_target.imag;
		}
		if (morph21)
		{
			psi.real=t*(psi_1.real+psi_2.real+psi_3.real+psi_4.real)
					+(1.0f-t)*psi_target.real;
			psi.imag=t*(psi_1.imag+psi_2.imag+psi_3.imag+psi_4.imag)
					+(1.0f-t)*psi_target.imag;	
		}		
	}
	else
	{
		psi.real=psi_1.real+psi_2.real+psi_3.real+psi_4.real;
		psi.imag=psi_1.imag+psi_2.imag+psi_3.imag+psi_4.imag;
	}	
	
	/**********************************************************************/
	// end function evaluation
	old_time=time;	
	return psi;	
}