-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
101 lines (85 loc) · 4.04 KB
/
main.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
import argparse
from pathlib import Path
from algorithms import Search_Algorithms as sa
'''
Each variable read in from *.var will be put into a dictionary.
The domain, constraint relationships, and number of constraints can be accessed by provided the letter as the key in any dict.
'''
def main():
# Command Line Parser
parser = argparse.ArgumentParser()
# Positional Arguments
parser.add_argument("variables",
help="Path to a .var file that holds variable info",
type=Path
)
parser.add_argument("constraints",
help="Path to a .con file that holds constraint info",
type=Path
)
parser.add_argument("consistency_enforcing",
help="none|fc are the choices. none is for backtracking. fc is for forward checking."
)
# Parse input
args = parser.parse_args()
# Getting variable data from .var file if path exists
if args.variables.exists() and args.variables.is_file():
# Looping thru file
domains = {} # Holds domain of each variable
letters = {} # Holds letter for each, to be used with constraints. Value is a number to be used like an index
with args.variables.open() as f:
for idx, line in enumerate(f):
v = line.split()
l = v.pop(0).split(":").pop(0) # Removing first element, then getting the letter
letters[l] = idx
v = [int(x) for x in v] # Converting all ints from string to int TODO optimize
domains[l] = v
else:
print("Please provide a .var file that exists. Make sure you are not just providing a directory.")
exit()
# Getting constraint data from .var file if path exists
if args.constraints.exists() and args.constraints.is_file():
# Looping thru file
constraints = {} # Holds constraint relationships
for l in letters:
t = []
for i in letters:
t.append(0)
constraints[l] = t # put in a list with len equal to amount of letters into dict value
with args.constraints.open() as f:
for line in f:
cons = line.split()
if cons[0] in letters and cons[2] in letters:
left_key = cons[0] # Key for dict
right_key = cons[2]
left_index = letters[cons[0]]
right_index = letters[cons[2]]
op = cons[1]
constraints[left_key][right_index] = op
if op == "<":
constraints[right_key][left_index] = ">"
elif op == ">":
constraints[right_key][left_index] = "<"
else:
constraints[right_key][left_index] = op
else:
print("Constraint file contains variable names that are not in the variable file. Please make sure you are inputing the correct and matching paths.")
exit()
else:
print("Please provide a .con file that exists. Make sure you are not just providing a directory.")
exit()
# Counting number of constraints for each letter
num_con = {}
for key, list in constraints.items():
num_con[key] = len(list) - list.count(0)
# Creating dictionaries for domains, constraints and num of constraints. Letters array will hold the keys for each.
# Init Search Algorithms
solver = sa(letters, domains, constraints, num_con)
# Getting enforcement setting from command line
if args.consistency_enforcing == "none": solver.backtracking() # Call backtracking solver
elif args.consistency_enforcing == "fc": solver.forward_checking() # Call forward checking solver
else:
print("Please provide an option for the consistency enforcement. The choices are none and fc.")
exit()
if __name__ == '__main__':
main()