-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathconstraints.py
More file actions
281 lines (228 loc) · 8.93 KB
/
constraints.py
File metadata and controls
281 lines (228 loc) · 8.93 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import neet
import networkx as nx
import numpy as np
from abc import ABCMeta, abstractmethod
class ConstraintError(Exception):
"""
A constraint was applied to an invalid object.
"""
pass
class AbstractConstraint(object, metaclass=ABCMeta):
"""
An abstract class representing a constraint used for rejection testing.
"""
@abstractmethod
def satisfies(self, net):
"""
Test a provided network against the constraint.
:param net: a network to test
:returns: ``True`` if the constraint is satisfied
"""
return True
class TopologicalConstraint(AbstractConstraint):
"""
An abstract class representing a constraint on the topology of a network.
"""
@abstractmethod
def satisfies(self, graph):
"""
Test a provided graph against the constraint.
:param graph: a graph to test
:type graph: nx.DiGraph
:returns: ``True`` if the constraint is satisfied
:raises TypeError: if the graph is not a networkx DiGraph
"""
if not isinstance(graph, nx.DiGraph):
raise TypeError('only directed graphs are testable with topological constraints')
return super().satisfies(graph)
class DynamicalConstraint(AbstractConstraint):
"""
An abstract class representing a constraint on the dynamics of a network.
"""
@abstractmethod
def satisfies(self, net):
"""
Test a provided net against the constraint.
:param net: a network to test
:type net: neet.Network
:returns: ``True`` if the constraint is satisfied
:raises TypeError: if the network is not a neet.Network
"""
if not isinstance(net, neet.Network):
raise TypeError('only neet networks are testable with dynamical constraints')
return super().satisfies(net)
class HasExternalNodes(TopologicalConstraint):
def __init__(self, target):
"""
An topological constraint requiring a specific number of external
nodes, i.e. a specific number of nodes with no incomming edges.
If ``target`` is a directed graph, this constraint will require
networks to have the same number of external nodes as the ``target``.
Alternativly, ``target`` can be a non-negative integer.
:param target: the target number of external nodes
:type target: nx.DiGraph or integer
"""
if isinstance(target, int):
if target < 0:
raise ValueError('the target number of external nodes must be non-negative')
num_external = target
elif isinstance(target, nx.DiGraph):
num_external = self.__count_external(target)
else:
raise TypeError('target must be either an integer or nx.DiGraph')
self.num_external = num_external
def __count_external(self, g):
"""
Count the number of external nodes in a directed graph.
"""
return np.count_nonzero([d == 0 for _, d in g.in_degree()])
def satisfies(self, graph):
"""
This constraint is only satisfied if the provided graph has
``self.num_external``-many external nodes.
:param graph: a graph to test
:type graph: nx.DiGraph
:returns: ``True`` if the digraph as the desired number of external
nodes
"""
if super().satisfies(graph):
return self.__count_external(graph) == self.num_external
class IsConnected(TopologicalConstraint):
"""
Ensure that the resulting graph is (weakly) connected.
"""
def satisfies(self, graph):
"""
This constraint is only satisfied if the provided graph as is weakly
connected.
:param graph: a graph to test
:type graph: nx.DiGraph
:returns: ``True`` if the digraph as the desired number of external
nodes
"""
if super().satisfies(graph):
try:
return nx.is_weakly_connected(graph)
except nx.exception.NetworkXException as err:
raise ConstraintError() from err
class IsIrreducible(DynamicalConstraint):
"""
Ensure that all dynamical nodes have irreducible functions.
"""
def satisfies(self, network):
"""
This constraint is only satisfied if every node's function logically
depends on each of it's incoming neighbors.
:param network: a network to test
:type network: neet.boolean.LogicNetwork
:returns: ``True`` if every node's function is irredicible
:raises NotImplementedError: if the network is not a
neet.boolean.LogicNetwork
"""
if super().satisfies(network):
if not isinstance(network, neet.boolean.LogicNetwork):
raise NotImplementedError()
for idx in range(network.size):
for neighbor_in in network.neighbors_in(idx):
if not network.is_dependent(idx, neighbor_in):
return False
return True
class HasCanalizingNodes(DynamicalConstraint):
def __init__(self, target):
"""
A dynamical constraint requiring that a specific number of nodes be
canalizing, i.e. a specific number of nodes are canalizing on at least
one input.
If ``target`` is a Neet network, this constraint will require that
networks to have the same number of canalizing nodes as ``target``.
Alternatively, ``target`` can be a non-negative integer.
:param target: the target number of canalizing nodes
:type target: neet.boolean.LogicNetwork or integer
:raises NotImplementedError: if the provied target is nei
"""
if isinstance(target, int):
if target < 0:
raise ValueError('the target number of canalizing nodes must be non-negative')
num_canalizing = target
elif isinstance(target, neet.Network):
num_canalizing = self.__count_canalizing_nodes(target)
else:
raise TypeError('target must be either an integer or a neet.Network')
self.num_canalizing = num_canalizing
def __count_canalizing_nodes(self, network):
"""
Count the number of canalizing nodes in a network.
"""
return len(network.canalizing_nodes())
def satisfies(self, network):
"""
This constraint is only satisfied if the provided network has
``self.num_canalizing``-many canalizing nodes.
"""
if super().satisfies(network):
return self.__count_canalizing_nodes(network) == self.num_canalizing
class GenericTopological(TopologicalConstraint):
def __init__(self, test):
"""
A generic constraint defined in terms of a callable.
:param test: a user-specified test
:type test: callable
"""
if not callable(test):
raise TypeError('test must be callable')
self.test = test
def satisfies(self, net):
"""
Test a provided network against a generic constraint.
:param net: a network to test
:returns: ``True`` if the constraint is satisified
"""
if super().satisfies(net):
return self.test(net)
class GenericDynamical(DynamicalConstraint):
def __init__(self, test):
"""
A generic constraint defined in terms of a callable.
:param test: a user-specified test
:type test: callable
"""
if not callable(test):
raise TypeError('test must be callable')
self.test = test
def satisfies(self, net):
"""
Test a provided network against a generic constraint.
:param net: a network to test
:returns: ``True`` if the constraint is satisified
"""
if super().satisfies(net):
return self.test(net)
class NodeConstraint(DynamicalConstraint):
"""
Constraints which operate on nodes (functions) of dynamical networks.
"""
pass
class GenericNodeConstraint(DynamicalConstraint):
def __init__(self, test):
if not callable(test):
raise TypeError('test must be callable')
self.test = test
def satisfies(self, net):
if super().satisfies(net):
return self.test(net)
class IrreducibleNode(NodeConstraint):
def satisfies(self, conditions):
if len(conditions) == 0:
return True
k = list(map(len, conditions))[0]
for i in range(k):
counter = {} # type: ignore
for state in conditions:
state_sans_source = state[:i] + state[i + 1:]
if int(state[i]) == 1:
counter[state_sans_source] = counter.get(state_sans_source, 0) + 1
else:
counter[state_sans_source] = counter.get(state_sans_source, 0) - 1
if not any(counter.values()):
return False
return True