-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pancake.py
176 lines (138 loc) · 4.33 KB
/
Pancake.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# coding=utf-8
from __future__ import print_function
from simpleai.search import SearchProblem, astar, breadth_first, depth_first, uniform_cost, greedy, limited_depth_first, iterative_limited_depth_first
from simpleai.search.viewers import WebViewer, ConsoleViewer, BaseViewer
import random
N = int(input("Enter number of pancakes: "))
order = input("Do you want to enter ordering?: YES/NO ")
my_list = []
if(order=="yes"):
my_list = list(map(int, input("Enter the top to bottom ordering between [0-N], separeted bu spaces : ").strip().split()))[:N]
else:
for i in range(N):
my_list.append(i)
random.shuffle(my_list)
print("initial state: (",end="")
print(*my_list,sep=",",end="")
print(")")
cost = 0
def spatula(inp_liste, spatula):
global cost
cost = spatula + 1
a = int((spatula/2)+1)
for i in range(a):
temp = inp_liste[i]
inp_liste[i]=inp_liste[spatula-i]
inp_liste[spatula-i]=temp
return inp_liste
def listToString(s):
str1 = ""
for i in range(len(s)):
str1 += str(s[i])
return str1
def stringToList(s):
list1 = []
for i in range(len(s)):
list1.append(int(s[i]))
return list1
def unsorted(state):
for i in range (len(state)):
j=len(state) - 1 - i
if(state[j]!=GOAL[j]):
return int(GOAL[j]) + 1
return 0
GOAL = listToString(sorted(my_list))
my_viewer = WebViewer()
class PancakeProblem(SearchProblem):
def actions(self, state):
if not (state == GOAL):
return listToString(range(1,unsorted(state)))
else:
return []
def result(self, state, action):
return listToString( spatula(stringToList(state), int(action)))
def is_goal(self, state):
return state == GOAL
def heuristic(self, state):
#return heuristic1(self, state) # Our first heuristic funciton
return heuristic2(self, state) #second heuristic function
def cost(self, state, action, state2):
return cost
def heuristic1(self, state):
for i in range(len(state)):
j = len(state) - 1 - i
if (state[j] != GOAL[j]):
reward = 0.5 if GOAL[j] == state[0] else 0
return int(GOAL[j]) + 1 - reward
return 0
def heuristic2(self, state):
groups = 1
reward = 1 if GOAL[len(state) - 1] == state[len(state) - 1] else 0
for i in range(len(state) - 1):
diff = int(state[i]) - int(state[i + 1])
if abs(diff) != 1:
groups += 1
return groups - reward
'''
#v2
def heuristic(self, state):
groups = 1
reward = 1 if GOAL[len(state)-1] == state[len(state)-1] else 0
for i in range(len(state) - 1):
diff = int(state[i]) - int(state[i + 1])
if abs(diff) != 1:
groups += 1
return groups - reward
#v1.3 Our first heuristic func
def heuristic(self, state):
global unsorted
for i in range(len(state)):
j = len(state) - 1 - i
if (state[j] != GOAL[j]):
reward = 0.5 if GOAL[j] == state[0] else 0
unsorted = int(GOAL[j]) + 1
return int(GOAL[j]) + 1 - reward
return 0
#v1.1
def heuristic(self, state):
global unsorted
for i in range(len(state)):
j = len(state) - 1 - i
if(state[j] != GOAL[j]):
unsorted = int(GOAL[j]) + 1
return int(GOAL[j]) + 1
return 0
#v1.0
def heuristic(self, state):
for i in range(len(state)):
j=len(state) - 1 - i
if(state[j] != GOAL[j]):
return int(GOAL[j]) + 1
return 0
'''
problem = PancakeProblem(initial_state=listToString(my_list))
# BFS
#result = breadth_first(problem, viewer=my_viewer)
#print("BFS: ")
# DFS
# result = depth_first(problem, viewer=my_viewer)
# print("DFS: ")
# UCS
# result = uniform_cost(problem, viewer=my_viewer)
# print("UCS: ")
# Greedy S
result = greedy(problem, viewer=my_viewer)
print("GREEDY S: ")
# DLS
# result = limited_depth_first(problem, (N-1)*2-1 , viewer=my_viewer)
# print(" DLS: ")
# iterative DLS
# result = iterative_limited_depth_first(problem, viewer=my_viewer)
# print("iterative DLS: ")
# A*
# result = astar(problem, viewer=my_viewer)
# print("A*:")
print(result.state)
print(result.path())
print('Stats: ')
print(my_viewer.stats)