Exemplo n.º 1
0
Puzzle PuzzleGenerator::SimulatedAnnealing_Linear(Puzzle p, double temp_start, double delta, double epsilon)
{
	// Function that uses simulated annealing algorithm to find best value puzzle.
	// TEMP_START = initial temperature
	// DELTA = amount that temperature decreases per iteration
	// returns the best puzzle found 

	Puzzle current = p;

	// Keep track of the time so we don't exceed it.
	Timer t;
	t.StartTimer();
	
	int i = 0; // iteration counter (for debugging)
	double temperature = temp_start;

	while (temperature > epsilon)
	{
		//printf ("\nSIMULATED ANNEALING ITERATION %i\n", i);


		// get random successor
		Puzzle successor = current.GetRandomSuccessor();
		//printf ("current value = %i\n", current.GetValue());
		//printf ("successor value = %i\n", successor.GetValue());
		//printf ("temperature = %f\n", temperature);

		// if successor is a "good" move accept it
		if (successor.GetValue() > current.GetValue())
		{
			//printf("successor value > current value...ACCEPTED!\n");
			current = successor;
		}
		else // otherwise accept the move with probability of e^(deltaE/temperature)
		{
			int deltaE = successor.GetValue() - current.GetValue(); // calculate delta E
			//printf("deltaE = %i\n", deltaE);
			double randVal = (rand() % 1000) / 1000.0; // generate random value between (0,1)
			//printf("randVal = %f\n", randVal);
			double thresh = exp(deltaE/temperature);
			//printf("thresh = %f\n", thresh);
			if (randVal < thresh)
			{
				//printf("successor value randomly ACCEPTED\n");
				current = successor;
			}
		}
		

		// cool temperature each iteration
		temperature -= delta;

		// increment iteration counter
		i++;

	}
	
	return current;
}
Exemplo n.º 2
0
/** 
* 	Function that uses simulated annealing algorithm to find best value puzzle.
* 	TEMP_START = initial temperature
* 	ALPHA = how much the temperature is 'cooled' each iteration
* 	EPSILON = when temperature reaches the epsilon we're done
* 	returns the best puzzle found  
**/
Puzzle PuzzleGenerator::SimulatedAnnealing_Exp(Puzzle p, double temp_start, double alpha, double epsilon)
{

	Puzzle current = p;

	// Keep track of the time so we don't exceed it.
	Timer t;
	t.StartTimer();
	
	int i = 0; // iteration counter (for debugging)
	double temperature = temp_start;

	while (temperature > epsilon)
	{
		// get random successor
		Puzzle successor = current.GetRandomSuccessor();

		// if successor is a "good" move accept it
		if (successor.GetValue() > current.GetValue())
		{
			current = successor;
		}
		else // otherwise accept the move with probability of e^(deltaE/temperature)
		{
			int deltaE = successor.GetValue() - current.GetValue(); // calculate delta E
			double randVal = (rand() % 1000) / 1000.0; // generate random value between (0,1)
			double thresh = exp(deltaE/temperature);
			if (randVal < thresh)
			{
				//printf("successor value randomly ACCEPTED\n");
				current = successor;
			}
		}
		

		// cool temperature each iteration
		temperature *= alpha;

		// increment iteration counter
		i++;

	}
	
	return current;
}