Skip to content

Commit

Permalink
comments and timer added
Browse files Browse the repository at this point in the history
  • Loading branch information
nikashahabi committed Apr 3, 2021
1 parent b2abdf1 commit 0bb02cd
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 17 deletions.
8 changes: 8 additions & 0 deletions .idea/8Queen.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 50 additions & 17 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import random
import copy
import math
import time

def C2n(n):
'returns C(2,n)'
return n * (n-1) / 2


class CheckeredPageState:
'defines a state. each column has only one queen in each state'

def __init__(self, checkeredPage):
self.checkeredPage = checkeredPage
Expand All @@ -15,6 +18,7 @@ def __init__(self, checkeredPage):
self.setHeuristic()

def setDic(self):
'sets 3 dictionaries. for example: dicRows[i] = k means that the ith row has k queens in it. O(dimension^2)'
dicRows = {}
dicDiagonal1 = {}
dicDiagonal2 = {}
Expand All @@ -34,6 +38,7 @@ def setDic(self):
self.dicDiagonal2 = dicDiagonal2

def setHeuristic(self):
'sets heuristic of a state.heuristic of a state is the number of pairs of queens that are attacking each other. O(dimension) '
h = 0
for key in self.dicRows:
if self.dicRows[key] > 1:
Expand All @@ -47,6 +52,7 @@ def setHeuristic(self):
self.h = h

def getRandomSteepestAscent(self):
'between successors of a state which have the lowest heuristic, returns a random one. O(dimension^4)'
neighbors = []
huristic = float("inf")
for j in range(self.dimension):
Expand All @@ -68,6 +74,9 @@ def getRandomSteepestAscent(self):
return(random.choice(neighbors))

def getFirstChoice(self):
'randomly generates successors of a state until it finds a successor with heuristic lower than the hueristic of the current state'
'otherwise it returns None'
'O(dimension^4)'
test = [[False for i in range(self.dimension)] for j in range(self.dimension)]
while 1:
i = random.randrange(0, self.dimension)
Expand All @@ -85,7 +94,7 @@ def getFirstChoice(self):
if neighbor.h < self.h:
return neighbor
flag = True
'checks if we have randomly generated all the successors'
'checks if we have randomly generated all the successors. returns None if so'
for x in test:
for y in x:
if y is False:
Expand All @@ -97,10 +106,12 @@ def getFirstChoice(self):
return None

def printPage(self):
'prints the checkered page of the current state O(n^2)'
for xs in self.checkeredPage:
print(" ".join(map(str, xs)))

def getMove(self, neighbor):
'prints the move from the current state to the given neighbor state O(dimension^2)'
test = False
for j in range(self.dimension):
for i in range(self.dimension):
Expand All @@ -116,6 +127,7 @@ def getMove(self, neighbor):
break

def randomSuccessor(self):
'returns a random successor of the current state O(dimension ^2)'
j = random.randrange(0, self.dimension)
while 1:
i = random.randrange(0, self.dimension)
Expand All @@ -131,7 +143,9 @@ def randomSuccessor(self):


def HillCLimbingSteepestAscent(checkeredPageInitial):
'gets the initial checkered page and performs the hill climbing algorithm steepest ascent variant'
current = CheckeredPageState(checkeredPageInitial)
print("start of hill climbing algorithm steepest ascent")
while 1:
print("current state checkered page:")
current.printPage()
Expand All @@ -148,7 +162,9 @@ def HillCLimbingSteepestAscent(checkeredPageInitial):
current = neighbor

def HillCLimbingFirstChoice(checkeredPageInitial):
'gets the initial checkered page and performs the hill climbing algorithm first choice variant'
current = CheckeredPageState(checkeredPageInitial)
print("start of hill climbing algorithm first choice variant")
while 1:
print("current state checkered page:")
current.printPage()
Expand All @@ -166,13 +182,16 @@ def HillCLimbingFirstChoice(checkeredPageInitial):


def getRandomCheckeredPage(dimension):
'returns a random checkered page in which each column has exactly one queen in it'
checkeredPage = [[0 for i in range(dimension)] for j in range(dimension)]
randNumbers = random.sample(range(0, dimension), dimension)
for j in range(dimension):
checkeredPage[randNumbers[j]][j] = 1
return checkeredPage

def HillClimbingRandomRestart(dimension):
'gets the dimension of the page and performs the hill climbing algorithm with random restart'
print("start of hill climbing algorithm with random restart")
while 1:
print("-----------------------------------")
print("new start of hill climbing algorithm with random restart")
Expand All @@ -182,12 +201,16 @@ def HillClimbingRandomRestart(dimension):
print("the hill climbing algorithm with random restart ended")
return state

def SimulatedAnnealing(checkeredPageInitial):
def SimulatedAnnealing(checkeredPageInitial, T=4000, tChange=0.8):
'gets the initial checkered page and performs the simulated annealing algorithm'
current = CheckeredPageState(checkeredPageInitial)
T = 4000
print("start of simulated annealing algorithm")
while 1:
T *= 0.99
if T < 0.0001:
print("current state checkered page:")
current.printPage()
print("current state h:", current.h)
T *= tChange
if T < 1:
print("final state checkered page:")
current.printPage()
print("final state h:", current.h)
Expand All @@ -200,24 +223,34 @@ def SimulatedAnnealing(checkeredPageInitial):
next = current.randomSuccessor()
deltaE = current.h - next.h
if deltaE > 0:
current.getMove(next)
current = next
else:
rand = random.uniform(0, 1)
probability = math.exp(deltaE / T)
if rand <= probability:
current.getMove(next)
current = next


check = [[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,1,0,0,0,0],
[1,0,0,0,1,0,0,0],
[0,1,0,0,0,1,0,1],
[0,0,1,0,0,0,1,0],
[0,0,0,0,0,0,0,0]]

SimulatedAnnealing(check)

for i in range(1):
print("------------------")
randomCheck = getRandomCheckeredPage(8)
print("new random check generated")
startHillFirst = time.time()
HillCLimbingFirstChoice(randomCheck)
endHillFirst = time.time()
print("------------------")
HillCLimbingSteepestAscent(randomCheck)
endHillSteep = time.time()
print("------------------")
HillClimbingRandomRestart(8)
endHillRandom = time.time()
print("------------------")
SimulatedAnnealing(randomCheck)
endSim = time.time()
print("run time of hill climbing first choice", endHillFirst - startHillFirst)
print("run time of hill climbing steepest ascent", endHillSteep - endHillFirst)
print("run time of hill climbing random restart", endHillRandom - endHillSteep)
print("run time of simulated annealing", endSim - endHillSteep)


0 comments on commit 0bb02cd

Please sign in to comment.