-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstraint.py
More file actions
204 lines (144 loc) · 4.94 KB
/
Constraint.py
File metadata and controls
204 lines (144 loc) · 4.94 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
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
import networkx as nx
# I added a comment
class Constraint(object):
"""
Abstract Base Class for constraint objects
"""
def __init__(self):
"""
Constructor
"""
self.graph = None
self.violatorsSet = None
# End __init__
def constraintDataCheck(self):
"""
Iterates over vertices and/or edges checking for the required data.
If the required data is not present, it throws an exception
"""
# TO DO: Determine if and how data checking would be required
pass
#raise NotImplementedError("constraintDataCheck should have been implmented")
# End def constraintDataCheck
def checkViolations(self):
"""
Check for specific violations of the constraint
"""
raise NotImplementedError("checkViolations should have been implmented")
# End def checkViolations
def checkPriorViolations(self):
"""
Checks for violations of the constraint that are inherent to the data
(i.e. are there before any optimization is performed). If there are any
then add them to a list to be ignored later.
"""
self.violatorsSet = self.checkViolations()
# End def checkPriorViolations
def checkConstraint(self):
"""
Trivariate valued logic function returns
True if constraint is violated
False if constraint is not violated
None if constraint is not violated and there were no pre-exisiting violation
"""
raise NotImplementedError("checkConstraint should have been implmented")
# End def checkConstraint
class cycleConstraint(Constraint):
"""
Constraint ensures that all vertices are in a cycle
"""
def __init__(self):
# Store a reference to the graph for constaint chcking
self.graph = None
# A set of vertices that violate the cycle constraint
# prior to any optimization
self.violatorsSet = None
# End def __init__
def constraintDataCheck(self):
"""
There is no data that is needed for general cycle checking
"""
pass
# End def constraintCheck
def checkViolations(self):
"""
Finds all vertices that are not in cycles
"""
# Get the set of vertices in the graph
vertexSet = set( self.graph.nodes() )
# Get the list of basis cycles
cycleBasis = nx.cycle_basis( self.graph )
# Convert the list of basis cycles into a set of vertices in the cycles
cycleVertexSet = set( [vertex for sublist in cycleBasis for vertex in sublist] )
# Get the difference between the set of vertices in the graph and those in cycles
nonCycleVertexSet = vertexSet.difference(cycleVertexSet)
return nonCycleVertexSet
def checkPriorViolations(self):
"""
Determine if any vertices are not in cycles from the initial graph.
If there are any, add them to a list of vertices to ignore when checking
constraint violation.
"""
# Initialize the list of violations of the constraint that exist
# prior to any optimization
self.violatorsSet = self.checkViolations()
# End def checkPriorViolations
def checkConstraint(self):
"""
Check to see if any vertices that were in cycles are no longer in cycles.
"""
# Default return true that a constraint was violated
constraintViolated = True
# Check for current violations of the constraint
violationsSet = self.checkViolations()
print "ViolationsSet: ", violationsSet
# Get the difference between current and prior violations,
# if the difference is the empty set, then no new violations
newViolationsSet = violationsSet.difference(self.violatorsSet)
# Return None if there are no prior and no new violations of the constraint
if not newViolationsSet and not self.violatorsSet: constraintViolated = None
# Return False if there are prior violations but no new violations
elif not newViolationsSet: constraintViolated = False
return constraintViolated
# End def checkConstraint
class connectedConstraint(object):
"""
Abstract Base Class for constraint objects
"""
def __init__(self):
"""
Constructor
"""
self.graph = None
self.violatorsSet = None
# End __init__
def constraintDataCheck(self):
"""
Iterates over vertices and/or edges checking for the required data.
If the required data is not present, it throws an exception
"""
# TO DO: Determine if and how data checking would be required
pass
#raise NotImplementedError("constraintDataCheck should have been implmented")
# End def constraintDataCheck
def checkViolations(self):
"""
"""
# End def checkViolations
def checkPriorViolations(self):
"""
Checks for violations of the constraint that are inherent to the data
(i.e. are there before any optimization is performed). If there are any
then add them to a list to be ignored later.
"""
self.violatorsSet = self.checkViolations()
# End def checkPriorViolations
def checkConstraint(self):
"""
Trivariate valued logic function returns
True if constraint is violated
False if constraint is not violated
None if constraint is not violated and there were no pre-exisiting violation
"""
raise NotImplementedError("checkConstraint should have been implmented")
# End def checkConstraint