This repository was archived by the owner on Aug 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulatedAnnealingAI.py
More file actions
41 lines (37 loc) · 1.52 KB
/
SimulatedAnnealingAI.py
File metadata and controls
41 lines (37 loc) · 1.52 KB
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
from copy import deepcopy
from random import random, choice
from math import exp
import MoveUtil
class SimulatedAnnealingAI:
def __init__(self, color):
self.color = color
self.opponentColor = 'W' if self.color == 'B' else 'B'
self.pointMatrix = MoveUtil.getPointMatrix()
self.temperature = 1000
def evaluationFunction(self, board):
score = 0
opponentScore = 0
for i in range(len(board)):
for j in range(len(board[i])):
if board[i][j] == self.color:
score += self.pointMatrix[i][j]
return score
def move(self, board, possibleMoves):
moveCoords = list(possibleMoves.keys())
currentScore = self.evaluationFunction(board)
moveToScore = {}
bestMove = None
for moveCoord in moveCoords:
tempBoard = deepcopy(board)
MoveUtil.updateBoard(tempBoard, possibleMoves, moveCoord, self.color)
tempScore = self.evaluationFunction(tempBoard)
moveToScore[moveCoord] = tempScore - currentScore
if bestMove == None or moveToScore[bestMove] < moveToScore[moveCoord]:
bestMove = moveCoord
chosenMove = choice(moveCoords)
if moveToScore[chosenMove] <= 0:
prob = exp(moveToScore[chosenMove] / self.temperature)
if random() > prob:
chosenMove = bestMove
MoveUtil.updateBoard(board, possibleMoves, chosenMove, self.color)
self.temperature *= 0.5