本文整理汇总了C++中Puzzle::GetRandomSuccessor方法的典型用法代码示例。如果您正苦于以下问题:C++ Puzzle::GetRandomSuccessor方法的具体用法?C++ Puzzle::GetRandomSuccessor怎么用?C++ Puzzle::GetRandomSuccessor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Puzzle
的用法示例。
在下文中一共展示了Puzzle::GetRandomSuccessor方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: SimulatedAnnealing_Linear
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;
}
示例2: SimulatedAnnealing_Exp
/**
* 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;
}