-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulatedAnnealing.java
52 lines (42 loc) · 1.48 KB
/
SimulatedAnnealing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package pacman.entries.pacman.algo;
import pacman.controllers.Controller;
import pacman.controllers.examples.StarterGhosts;
import pacman.game.Constants;
import pacman.game.Constants.*;
import pacman.game.Game;
import java.util.Random;
/**
* Simulated Annealing algorithm.
*/
public class SimulatedAnnealing extends Controller<MOVE> {
private StarterGhosts ghosts;
public SimulatedAnnealing(){
ghosts = new StarterGhosts();
}
@Override
public MOVE getMove(Game game, long timeDue) {
//get the set of moves
MOVE[] moves = game.getPossibleMoves(game.getPacmanCurrentNodeIndex());
//generate index at random (random move) and a probability
Random random = new Random();
int index = random.nextInt(moves.length);
MOVE move = moves[index];
double proabability = random.nextDouble();
//make copy and advance
Game copy = game.copy();
copy.advanceGame(move,ghosts.getMove());
//setup scheduler
int scheduler = copy.getNumberOfActivePills() + copy.getPacmanNumberOfLivesRemaining()+copy.getNumberOfActivePowerPills();
double difference = Heuristic.h(copy) - Heuristic.h(game);
//if difference is greater make the move
if(difference > 0){
return move;
}else{
//check the probability
if(proabability > Math.exp(difference/scheduler)){
return move;
}
}
return MOVE.NEUTRAL;
}
}