-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_solver.py
More file actions
135 lines (121 loc) · 5.29 KB
/
run_solver.py
File metadata and controls
135 lines (121 loc) · 5.29 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
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
import solver_helpers as sh
import rule_helpers as rh
from queue_search import *
import a_star_heuristic as astar
import baseline_ai as baseline
import sys
# Helper method to read input from user to set intital state
def read_arg_input() -> tuple:
print("\n------ Welcome to Logical Inference Solver ------\n")
print("Follow the prompts to begin.\n")
print("Enter the number corresponding to the AI you would like to use: \n")
print(" 1. Human (You select actions) 2. Baseline AI (Random) \n" +
" 3. Breadth First Search 4. A* Search \n" +
" 5. A* Search + Neural Network \n")
method = input()
print("\n")
# Net must be trained to use option 5
if method == '5': print("Note: Neural network must already be trained to use this method.\n")
next_arg = ""
arg_list = []
while not next_arg == "DONE":
next_arg = input('Enter the next logical assumption (or type \'DONE\' if complete): ')
arg_list.append(next_arg)
print("\n")
arg_list.pop() # Remove 'DONE' from list
claim = input('Enter the claim to prove: ')
print("\n-----------------------------------------------\n")
return(arg_list, claim, method)
# Helper method to format proof output correctly
def print_padding(size: int) -> str:
if size >= 14: (" ")
elif size == 13: return (" ")
elif size == 12: return (" ")
elif size == 11: return (" ")
elif size == 10: return (" ")
elif size == 9: return (" ")
elif size == 8: return (" ")
elif size == 7: return (" ")
elif size == 6: return (" ")
elif size == 5: return (" ")
elif size == 4: return (" ")
elif size == 3: return (" ")
elif size == 2: return (" ")
else: return (" ")
# Main method when script is executed
if __name__ == "__main__":
(args, claim, method) = read_arg_input()
state = sh.initial_state(args, claim)
problem = SearchProblem(state, sh.proof_complete)
if method == '1':
for i in range(len(args)):
rule = "Assumption"
print(str(i+1) + ". " + args[i] + print_padding(len(args[i]) + len(str(i+1))) + rule +"\n")
print("\n-----------------------------------------------\n")
while not sh.proof_complete(state):
actions = sh.valid_actions(state)
i = 1
for x in actions:
print(str(i) + ". " + str(x))
i += 1
print("\n")
act_idx = input("Select rule number to apply: ")
state = sh.apply_rule(actions[int(act_idx) - 1], state)
print("\n-----------------------------------------------\n")
(args, claim, hist) = sh.unpack(state)
start_rule = hist[0][2]
hist_index = 0
for i in range(len(args)):
rule = "Assumption"
if i >= start_rule:
rule = str(hist[hist_index][0])
rule += " "
rule += str(tuple(map(lambda i: i + 1, hist[hist_index][1])))
hist_index += 1
print(str(i+1) + ". " + args[i] + print_padding(len(args[i]) + len(str(i+1))) + rule +"\n")
elif method == '2':
(state, success, steps) = baseline.apply_random_actions(state)
# Display final proof
(args, claim, hist) = sh.unpack(state)
start_rule = hist[0][2]
hist_index = 0
for i in range(len(args)):
rule = "Assumption"
if i >= start_rule:
input()
rule = str(hist[hist_index][0])
rule += " "
rule += str(tuple(map(lambda i: i + 1, hist[hist_index][1])))
hist_index += 1
print(str(i+1) + ". " + args[i] + print_padding(len(args[i]) + len(str(i+1))) + rule +"\n")
else:
if method == '3': plan, node_count = breadth_first_search(problem)
elif method == '4': plan, node_count = a_star_search(problem, astar.simple_heuristic)
elif method == '5':
Net = astar.NeuralNetwork()
adv = Net.nn_heuristic
plan, node_count = a_star_search(problem, adv)
else:
print("Invalid solving method!\n")
sys.exit()
states = [problem.initial_state]
for a in range(len(plan)):
states.append(sh.apply_rule(plan[a], states[-1]))
# Display final proof
final_state = states[len(states)-1]
(args, claim, hist) = sh.unpack(final_state)
if len(hist) == 0:
if sh.proof_complete(final_state): print("\n------- Claim contained in initial args -------\n")
else: print("\n---------------- Cannot Prove ----------------\n")
sys.exit()
start_rule = hist[0][2]
hist_index = 0
for i in range(len(args)):
rule = "Assumption"
if i >= start_rule:
input()
rule = str(hist[hist_index][0])
rule += " "
rule += str(tuple(map(lambda i: i + 1, hist[hist_index][1])))
hist_index += 1
print(str(i+1) + ". " + args[i] + print_padding(len(args[i]) + len(str(i+1))) + rule +"\n")