Skip to content

Commit 6b62385

Browse files
committed
Version 1
1 parent 332c7b2 commit 6b62385

3 files changed

Lines changed: 125 additions & 31 deletions

File tree

8puzzleProblm.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# Writer of this code:
2+
# Seyyed Ali Shohadaalhosseini
3+
4+
# We'll never forget that:
5+
# < What doesn't kill you makes you STRONGER >
6+
17
import AISearchAlgorithm
28

39

@@ -23,10 +29,42 @@
2329
[7, 6, 5]
2430
]
2531

26-
# print(Search.searchDFS(initialState, GoalState))
27-
# print(Search.searchBFS(initialState, GoalState))
28-
# print(Search.searchDLS(initialState, GoalState, 28), sep=" === > \n\n")
29-
# print(Search.searchIDS(initialState, GoalState), sep=" === > \n\n")
30-
# print(Search.searchUCS(initialState, GoalState), sep=" === > \n\n")
31-
# print(Search.searchGreedy(initialState, GoalState), sep=" === > \n\n")
32-
print(Search.searchHillClimbing(initialState, GoalState), sep=" === > \n\n")
32+
ans = "y"
33+
34+
while ans == "y":
35+
WhichAlgorithm = input("""Enter one of the name below to search with the default parameters: \n
36+
Enter DFS as Depth First Search\n
37+
Enter BFS as Breadth First Search\n
38+
Enter DLS as Depth Limited Search\n
39+
Enter IDS as Iterative Deepening Search\n
40+
Enter UCS as Uniform Cost Search\n
41+
Enter ASTAR as A Star Search\n
42+
Enter GREEDY as Greedy Search\n
43+
Enter HILL as Hill Climbing Search: \n
44+
Enter the name here: """)
45+
46+
if WhichAlgorithm == "DFS":
47+
print(Search.searchDFS(initialState, GoalState))
48+
49+
elif WhichAlgorithm == "BFS":
50+
print(Search.searchBFS(initialState, GoalState))
51+
52+
elif WhichAlgorithm == "DLS":
53+
print(Search.searchDLS(initialState, GoalState, 28), sep=" === > \n\n")
54+
55+
elif WhichAlgorithm == "IDS":
56+
print(Search.searchIDS(initialState, GoalState), sep=" === > \n\n")
57+
58+
elif WhichAlgorithm == "UCS":
59+
print(Search.searchUCS(initialState, GoalState), sep=" === > \n\n")
60+
61+
elif WhichAlgorithm == "ASTAR":
62+
print(Search.searchAstar(initialState, GoalState), sep=" === > \n\n")
63+
64+
elif WhichAlgorithm == "GREEDY":
65+
print(Search.searchGreedy(initialState, GoalState), sep=" === > \n\n")
66+
67+
elif WhichAlgorithm == "HILL":
68+
print(Search.searchHillClimbing(initialState, GoalState), sep=" === > \n\n")
69+
70+
ans = input("Do you want to continue ? y/n ")

AISearchAlgorithm.py

Lines changed: 80 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def searchDFS(self, initialState, GoalState, spaceGraph=[]):
6565

6666
# now we must calculate the child of the currentstate and add them to the frontier
6767
# In this code our default problem is 8 puzzle problem
68-
self.updateFringe(currentState, explored)
68+
self.updateFringe(currentState, explored, GoalState)
6969

7070
# Step 3
7171
currentState = self.fringe[-1]
@@ -122,7 +122,7 @@ def searchBFS(self, initialState, GoalState, spaceGraph=[]):
122122

123123
# now we must calculate the child of the currentstate and add them to the frontier
124124
# In this code our default problem is 8 puzzle problem
125-
self.updateFringe(currentState, explored)
125+
self.updateFringe(currentState, explored, GoalState)
126126

127127
# Step 3
128128
currentState = self.fringe[0]
@@ -243,7 +243,7 @@ def searchIDS(self, initialState, GoalState, spaceGraph=[]):
243243
return answerIs
244244

245245
def searchUCS(self, initialState, GoalState, spaceGraph=[]):
246-
## This algorithms problem is that we can not define the cost for the new nodes
246+
## Temporary Done
247247
"""This algorithm will return the solution path.
248248
249249
Args:
@@ -258,7 +258,7 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
258258
explored = list()
259259
parentStates = list()
260260

261-
currentState = initialState
261+
currentState = deepcopy(initialState)
262262
self.fringe.append(currentState)
263263

264264
else:
@@ -271,9 +271,9 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
271271
if currentState not in explored:
272272
explored.append(currentState)
273273

274-
print("\n\n", "This is currrent state: ", "\n\n")
275-
for i in currentState:
276-
print(i)
274+
# print("\n\n", "This is currrent state: ", "\n\n")
275+
# for i in currentState:
276+
# print(i)
277277

278278
# if counter == 1000:
279279
# break
@@ -291,7 +291,7 @@ def searchUCS(self, initialState, GoalState, spaceGraph=[]):
291291
del self.fringe[currentStateIndex]
292292
self.FatherCost = self.cost[currentStateIndex]
293293
del self.cost[currentStateIndex]
294-
294+
print(self.FatherCost)
295295
# now we must calculate the child of the currentstate and add them to the frontier
296296
# In this code our default problem is 8 puzzle problem
297297
self.updateFringe(currentState, explored, GoalState, algorithmType="UCS")
@@ -363,8 +363,66 @@ def searchGreedy(self, initialState, GoalState, spaceGraph=[]):
363363

364364
counter += 1
365365

366-
def searchAstar(self):
367-
pass
366+
def searchAstar(self, initialState, GoalState, spaceGraph=[]):
367+
## Temporary Done
368+
"""This algorithm will return the solution path.
369+
370+
Args:
371+
initialState (List): Start state.
372+
GoalState (list): End state.
373+
spaceGraph (list): For the time user enter his graph states.
374+
"""
375+
if spaceGraph == []:
376+
currentState = list()
377+
self.fringe = list()
378+
self.cost = [0]
379+
explored = list()
380+
parentStates = list()
381+
382+
currentState = deepcopy(initialState)
383+
self.fringe.append(currentState)
384+
385+
else:
386+
# we don't need to calculate the childs.
387+
pass
388+
counter = 0
389+
while True:
390+
391+
# step 1
392+
if currentState not in explored:
393+
explored.append(currentState)
394+
395+
# print("\n\n", "This is currrent state: ", "\n\n")
396+
# for i in currentState:
397+
# print(i)
398+
399+
# if counter == 1000:
400+
# break
401+
402+
if currentState == GoalState:
403+
print(f"The path has been found at iterate {counter}")
404+
return explored
405+
406+
else:
407+
# current state is in explored list
408+
pass
409+
410+
# step 2 - deleting the currentstate from the fringe and cost
411+
currentStateIndex = self.fringe.index(currentState)
412+
del self.fringe[currentStateIndex]
413+
self.FatherCost = self.cost[currentStateIndex]
414+
del self.cost[currentStateIndex]
415+
print(self.FatherCost)
416+
# now we must calculate the child of the currentstate and add them to the frontier
417+
# In this code our default problem is 8 puzzle problem
418+
self.updateFringe(currentState, explored, GoalState, algorithmType="Astar")
419+
420+
# Step 3 - Choosing the state we minimum cost
421+
minimumCost = min(self.cost)
422+
minimumCostIndex = self.cost.index(minimumCost)
423+
currentState = self.fringe[minimumCostIndex]
424+
425+
counter += 1
368426

369427
def searchHillClimbing(self, initialState, GoalState, spaceGraph=[]):
370428
## Done successfully
@@ -499,7 +557,14 @@ def updateFringe(self, currentState, parents, GoalState, algorithmType="", probl
499557
else:
500558
if algorithmType == "UCS":
501559
self.fringe.append(newstate)
502-
self.cost.append(self.CostCalculator(newstate, GoalState, type="MissedPlaced"))
560+
self.cost.append(self.CostCalculator(newstate, GoalState, type="OnDepthCount"))
561+
elif algorithmType == "Astar":
562+
self.fringe.append(newstate)
563+
gn = self.CostCalculator(newstate, GoalState, type="OnDepthCount")
564+
hn = self.heuristics(newstate, GoalState, type="MissedPlaced")
565+
fn = gn + hn
566+
self.cost.append(hn)
567+
pass
503568

504569
elif algorithmType == "Greedy":
505570
self.fringe.append(newstate)
@@ -529,26 +594,17 @@ def updateFringe(self, currentState, parents, GoalState, algorithmType="", probl
529594

530595
return self.fringe # A list of all the next states
531596

532-
def CostCalculator(self, newState, GoalState, type="MissedPlaced"):
597+
def CostCalculator(self, newState, GoalState, type="OnDepthCount"):
533598
# This function has problem
534599
"""This function calculates the cost of a state
535600
This function calculates the G(n)
536601
537602
Args:
538603
newState (list): The state is a a of three list
539-
type (str, optional): Defaults to "MissedPlaced".
604+
type (str, optional): Defaults to "OnDepthCount".
540605
"""
541-
# -------------------This method is not right--------------------- #
542-
# -------------------This method is not right--------------------- #
543-
if type == "MissedPlaced":
544-
CostOfNewState = 0
545-
for row in range(len(newState)):
546-
for col in range(len(GoalState)):
547-
if newState[row][col] != GoalState[row][col]:
548-
CostOfNewState += 1
549-
return CostOfNewState + self.FatherCost
550-
# -------------------This method is not right--------------------- #
551-
# -------------------This method is not right--------------------- #
606+
# -------------------This must be check more--------------------- #
607+
return 1 + self.FatherCost
552608

553609
def heuristics(self, newState, GoalState, type="MissedPlaced"):
554610
# Done successfully
363 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)