-
Notifications
You must be signed in to change notification settings - Fork 0
/
es_decision tree model.py
247 lines (201 loc) · 6.51 KB
/
es_decision tree model.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import tkinter as tk
from tkinter import messagebox
# Define the Node class for the decision tree
class Node:
def __init__(self, question, options, children=None, result=None):
"""
Initializes a node in the decision tree.
Parameters:
- question (str): The question to display at this node.
- options (list): A list of option strings for the user to choose from.
- children (dict): A mapping from option strings to child nodes.
- result (str): The result to display if this is a leaf node.
"""
self.question = question
self.options = options
self.children = children or {}
self.result = result
# Leaf nodes with recommendations
leave_node = Node(
question=None,
options=[],
result="Recommendation: Leave"
)
wait_node = Node(
question=None,
options=[],
result="Recommendation: Wait"
)
# Build the decision tree by defining nodes and their relationships
# Level 1 (Root node)
root = Node(
question="What is the restaurant's level of Patrons?", # Question 1
options=["Full", "Some", "None"]
)
# Level 2
# For "Full" path from root
node2_full = Node(
question="What is the Wait Time (minutes)?", # Question 2
options=["0", "10", "30", "60"]
)
# For "Some" path from root
node2_some = wait_node # Recommendation: Wait
# For "None" path from root
node2_none = leave_node # Recommendation: Leave
# Level 3
# For node2_full path
node3_full_0 = wait_node # Recommendation: Wait
node3_full_10 = Node(
question="Is it Raining?", # Question 3
options=["Yes", "No"]
)
node3_full_30 = Node(
question="Is there an Alternative?", # Question 3
options=["Yes", "No"]
)
node3_full_60 = leave_node # Recommendation: Leave
# Level 4
# For node3_full_10 path
node4_full_10_yes = wait_node # Recommendation: Wait
node4_full_10_no = Node(
question="Are people being Annoying?", # Question 4
options=["Yes", "No"]
)
# For node3_full_30 path
node4_full_30_yes = Node(
question="Is it Friday or Saturday?", # Question 4
options=["Yes", "No"]
)
node4_full_30_no = Node(
question="Is there a Bar?", # Question 4
options=["Yes", "No"]
)
# Level 5
# For node4_full_10_no path
node5_full_10_no_yes = Node(
question="Are you Hungry?", # Question 5
options=["Yes", "No"]
)
node5_full_10_no_no = wait_node # Recommendation: Wait
# For node4_full_30_yes path
node5_full_30_yes_yes = wait_node # Recommendation: Wait
node5_full_30_yes_no = leave_node # Recommendation: Leave
# For node4_full_30_no path
node5_full_30_no_yes = wait_node # Recommendation: Wait
node5_full_30_no_no = Node(
question="Do you have a Reservation?", # Question 5
options=["Yes", "No"]
)
# Level 6
# For node5_full_10_no_yes path
node6_full_10_no_yes_yes = leave_node # Recommendation: Leave
node6_full_10_no_yes_no = wait_node # Recommendation: Wait
# For node5_full_30_no_no path
node6_full_30_no_no_yes = wait_node # Recommendation: Wait
node6_full_30_no_no_no = leave_node # Recommendation: Leave
# Assign children to nodes
# Level 1
root.children = {
"Full": node2_full,
"Some": node2_some,
"None": node2_none
}
# Level 2
node2_full.children = {
"0": node3_full_0, # Recommendation: Wait
"10": node3_full_10,
"30": node3_full_30,
"60": node3_full_60 # Recommendation: Leave
}
# Level 3
node3_full_10.children = {
"Yes": node4_full_10_yes, # Recommendation: Wait
"No": node4_full_10_no
}
node3_full_30.children = {
"Yes": node4_full_30_yes,
"No": node4_full_30_no
}
# Level 4
node4_full_10_no.children = {
"Yes": node5_full_10_no_yes,
"No": node5_full_10_no_no # Recommendation: Wait
}
node4_full_30_yes.children = {
"Yes": node5_full_30_yes_yes, # Recommendation: Wait
"No": node5_full_30_yes_no # Recommendation: Leave
}
node4_full_30_no.children = {
"Yes": node5_full_30_no_yes, # Recommendation: Wait
"No": node5_full_30_no_no
}
# Level 5
node5_full_10_no_yes.children = {
"Yes": node6_full_10_no_yes_yes, # Recommendation: Leave
"No": node6_full_10_no_yes_no # Recommendation: Wait
}
node5_full_30_no_no.children = {
"Yes": node6_full_30_no_no_yes, # Recommendation: Wait
"No": node6_full_30_no_no_no # Recommendation: Leave
}
# Level 6
node6_full_10_no_yes_yes.children = {}
node6_full_10_no_yes_no.children = {}
node6_full_30_no_no_yes.children = {}
node6_full_30_no_no_no.children = {}
# GUI Application Class
class DecisionTreeApp:
def __init__(self, root_node):
self.root_node = root_node
self.current_node = root_node
# Initialize the main window
self.window = tk.Tk()
self.window.title("Expert System Recommendation")
self.window.geometry("600x200")
# Question label
self.question_label = tk.Label(
self.window,
text=self.current_node.question,
wraplength=550,
font=("Times New Roman", 18)
)
self.question_label.pack(pady=20)
# Frame to hold option buttons
self.buttons_frame = tk.Frame(self.window)
self.buttons_frame.pack()
# Create buttons for the current options
self.create_option_buttons()
def create_option_buttons(self):
# Clear existing buttons
for widget in self.buttons_frame.winfo_children():
widget.destroy()
# Create a button for each option
for option in self.current_node.options:
button = tk.Button(
self.buttons_frame,
text=option,
width=20,
command=lambda opt=option: self.select_option(opt)
)
button.pack(side=tk.LEFT, padx=10, pady=10)
def select_option(self, option):
# Move to the next node based on the selected option
if option in self.current_node.children:
self.current_node = self.current_node.children[option]
if self.current_node.result:
# If it's a leaf node, show the result
messagebox.showinfo("Result", self.current_node.result)
self.window.destroy()
else:
# Update the question and options for the next node
self.question_label.config(text=self.current_node.question)
self.create_option_buttons()
else:
messagebox.showerror("Error", "Invalid option selected.")
def run(self):
# Start the GUI event loop
self.window.mainloop()
# Main execution
if __name__ == "__main__":
app = DecisionTreeApp(root)
app.run()