-
Notifications
You must be signed in to change notification settings - Fork 0
/
rcc8.py
executable file
·187 lines (158 loc) · 7.03 KB
/
rcc8.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
#!/usr/bin/env python
#############################################################################
#### ####
#### RCC8 Reasoner ( v1.2.2 - July 2012 ) ####
#### ####
#### Michael Sioutis ####
#### e-mail: sioutis@di.uoa.gr ####
#### ####
#### http://www.di.uoa.gr/ ####
#### ####
#### National and Kapodistrian University of Athens ####
#### Department of Informatics and Telecommunications ####
#### Panepistimiopolis, Ilisia ####
#### Athens, 157 84 ####
#### ####
#############################################################################
from rcc8glob import setGlobals
from bitcoding import EQ, DALL
from iconsistency import iconsistency
from consistency import consistency
from assignment import staticUnassignedVars
from parsecsp import parsecsp
from optparse import OptionParser
from printm import printMatrix
import sys
from array import array
from counters import conCount, arcCount, nodeCount
from graphUtil.plotdepth import plot as pld
import os
import thread
from graphUtil.visualprocess import init as initializeVisualProcess
def init():
# remove all the files in the folder graph
folder = './graph'
for f in os.listdir(folder):
fpath = os.path.join(folder, f)
try:
os.unlink(fpath)
except Exception, e:
print e
# create spatial variables of specified number
temp = sys.stdin.readline()
Vars = int(temp.split()[0].strip())+1
TypeId = temp.split()[1].strip()
# initialize constraints
ConMatrix = tuple([array('B',[DALL if i != j else EQ for i in xrange(Vars)]) for j in xrange(Vars)])
# parse spatial CSP
parsecsp(ConMatrix)
return TypeId, ConMatrix
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
# main function
def main(argv=None):
if argv is None:
argv = sys.argv
# set some sys useful stuff
sys.setrecursionlimit(1<<30)
sys.setcheckinterval(10000)
# initialize parser for command line arguments
parser = OptionParser()
# set parsing options
parser.add_option("-m", "--method", type="string", dest="method", default="recursive", help="interaction mode: recursive, or iterative [default: %default]")
parser.add_option("-v", "--varheuristic", type="string", dest="varheuristic", default="cardinality", help="interaction mode: none, cardinality, or random [default: %default]")
parser.add_option("-p", "--pcheuristic", type="string", dest="pcheuristic", default="weighted", help="interaction mode: none, weighted, or smart [default: %default]")
parser.add_option("-s", "--split", type="string", dest="split", default="horn", help="interaction mode: base, or horn [default: %default]")
parser.add_option("-l", "--valheuristic", type="string", dest="valheuristic", default="lcv", help="interaction mode: none, or lcv [default: %default]")
parser.add_option("-r", "--process", type="string", dest="process", default="dynamic", help="interaction mode: static, or dynamic [default: %default]")
parser.add_option("-o", "--scope", type="string", dest="scope", default="local", help="interaction mode: local, or global [default: %default]")
parser.add_option("-d", "--print", action="store_true", dest="printsol", help="print solution to output")
# parse command line arguments
options, args = parser.parse_args()
#initialize depth plotting tool
#try:
# thread.start_new_thread(pld, ())
#except:
# print "Error: unable to start thread"
try:
TypeId, ConMatrix = init()
# check and set globals for spliting information
if options.split == "base":
setGlobals('split',0)
elif options.split == "horn":
setGlobals('split',1)
else:
raise Usage("Invalid option: " + options.split)
# check and set globals for patch consistency heuristic information
if options.pcheuristic == "none":
setGlobals('pcheuristic',0)
elif options.pcheuristic == "weighted":
setGlobals('pcheuristic',1)
elif options.pcheuristic == "smart":
setGlobals('pcheuristic',2)
else:
raise Usage("Invalid option: " + options.pcheuristic)
# check and set globals for value decision heuristic information
if options.valheuristic == "none":
setGlobals('valheuristic',0)
elif options.valheuristic == "lcv":
setGlobals('valheuristic',1)
else:
raise Usage("Invalid option: " + options.valheuristic)
# check and set globals for variable decision information
if options.varheuristic == "none":
setGlobals('varheuristic',0)
elif options.varheuristic == "cardinality":
setGlobals('varheuristic',1)
elif options.varheuristic == "random":
setGlobals('varheuristic',2)
else:
raise Usage("Invalid option: " + options.varheuristic)
# check and set globals for scope information
if options.scope == "local":
setGlobals('scope',0)
elif options.scope == "global":
setGlobals('scope',1)
else:
raise Usage("Invalid option: " + options.scope)
# check and set globals for processing information
if options.process == "static":
setGlobals('process',0)
staticUnassignedVars(ConMatrix)
elif options.process == "dynamic":
setGlobals('process',1)
else:
raise Usage("Invalid option: " + options.process)
# check and set globals for method decision information
# exeuctes consistency checker
iniConMatrix = tuple([ic[:] for ic in ConMatrix])
initializeVisualProcess(iniConMatrix)
if options.method == "recursive":
solution = consistency(ConMatrix)
elif options.method == "iterative":
solution = iconsistency(ConMatrix)
else:
raise Usage("Invalid option: " + options.method)
except Usage, err:
print err.msg
parser.print_help()
return 1
# print values of complexity counters
print '--------------------------------------'
print 'Type ID: ', TypeId
print '--------------------------------------'
print 'Visited nodes: ', next(nodeCount)
print 'Visited arcs: ', next(arcCount)
print 'Checked constraints: ', next(conCount)
print '--------------------------------------'
# print solution
if solution != None:
print 'Consistent'
if options.printsol == True:
printMatrix(solution)
else:
print 'Inconsistent'
print '--------------------------------------'
if __name__ == '__main__':
sys.exit(main())