-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathsetSubstitutionSystem.m
357 lines (286 loc) · 13 KB
/
setSubstitutionSystem.m
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
Package["SetReplace`"]
PackageImport["GeneralUtilities`"]
PackageExport["$SetReplaceMethods"]
(* This is a main function of the package. This function calls either C++ or Wolfram Language implementation, and can
produce a WolframModelEvolutionObject that contains information about evolution of the network step-by-step.
All SetReplace* and WolframModel functions use argument checks and implementation done here. *)
PackageScope["$eventOrderingFunctions"]
PackageScope["setReplaceRulesQ"]
PackageScope["stepCountQ"]
PackageScope["multiwayEventSelectionFunctionQ"]
PackageScope["setSubstitutionSystem"]
PackageScope["$stepSpecKeys"]
PackageScope["$maxEvents"]
PackageScope["$maxGenerationsLocal"]
PackageScope["$maxDestroyerEvents"]
PackageScope["$maxFinalVertices"]
PackageScope["$maxFinalVertexDegree"]
PackageScope["$maxFinalExpressions"]
PackageScope["$fixedPoint"]
PackageScope["$timeConstraint"]
PackageScope["$sortedExpressionIDs"]
PackageScope["$reverseSortedExpressionIDs"]
PackageScope["$expressionIDs"]
PackageScope["$ruleIndex"]
PackageScope["$any"]
PackageScope["$forward"]
PackageScope["$backward"]
PackageScope["$globalSpacelike"]
PackageScope["$spacelike"]
PackageScope["$sameInputSetIsomorphicOutputs"]
(* Termination reason values *)
$maxEvents = "MaxEvents";
$maxGenerationsLocal = "MaxGenerationsLocal";
$maxFinalVertices = "MaxFinalVertices";
$maxFinalVertexDegree = "MaxFinalVertexDegree";
$maxFinalExpressions = "MaxFinalExpressions";
$fixedPoint = "FixedPoint";
$timeConstraint = "TimeConstraint";
SetUsage @ "
$SetReplaceMethods gives the list of available values for Method option of SetReplace and related functions.
";
(* Argument Checks *)
(* Argument checks here produce messages for the caller which is specified as an argument. That is because
setSubstitutionSystem is used by all SetReplace* and WolframModel functions, which need to produce their own
messages.*)
(* Set is a list *)
General::setNotList =
"The set specification `1` should be a List.";
setSubstitutionSystem[
rules_, set_, stepSpec_, caller_, returnOnAbortQ_, o : OptionsPattern[]] := 0 /;
!ListQ[set] &&
Message[caller::setNotList, set];
(* Rules are valid *)
setReplaceRulesQ[rules_] :=
MatchQ[rules, {(_Rule | _RuleDelayed)..} | _Rule | _RuleDelayed];
declareMessage[
General::invalidRules, "The rule specification `rules` should be either a Rule, RuleDelayed, or a List of them."];
setSubstitutionSystem[
rules_, set_, stepSpec_, caller_, returnOnAbortQ_, o : OptionsPattern[]] := 0 /;
!setReplaceRulesQ[rules] &&
Message[caller::invalidRules, rules];
(* Step count is valid *)
$stepSpecKeys = <|
$maxEvents -> "MaxEvents",
(* local means the evolution will keep running until no further matches can be made exceeding the max generation.
This might result in a different evolution order. *)
$maxGenerationsLocal -> "MaxGenerations",
$maxDestroyerEvents -> "MaxDestroyerEvents",
(* these are any level-2 expressions in the set, not just atoms. *)
$maxFinalVertices -> "MaxVertices",
$maxFinalVertexDegree -> "MaxVertexDegree",
$maxFinalExpressions -> "MaxEdges"|>;
$stepSpecNamesInErrorMessage = <|
$maxEvents -> "number of replacements",
$maxGenerationsLocal -> "number of generations",
$maxDestroyerEvents -> "number of destroyer events",
$maxFinalVertices -> "number of vertices",
$maxFinalVertexDegree -> "vertex degree",
$maxFinalExpressions -> "number of edges"|>;
stepCountQ[n_] := IntegerQ[n] && n >= 0 || n == \[Infinity];
multiwayEventSelectionFunctionQ[None | $spacelike] = True;
multiwayEventSelectionFunctionQ[_] = False;
General::nonIntegerIterations =
"The `1` `2` should be a non-negative integer or infinity.";
General::nonListExpressions =
"Encountered expression `1` which is not a list, even though a constraint on vertices is specified.";
General::tooSmallStepLimit =
"The maximum `1` `2` is smaller than that in initial condition `3`.";
General::multiwayFinalStepLimit =
"The limit for the `2` is not supported for multiway systems.";
stepSpecQ[caller_, set_, spec_, eventSelectionFunction_] :=
(* Check everything is a non-negative integer. *)
And @@ KeyValueMap[
If[stepCountQ[#2],
True
,
Message[caller::nonIntegerIterations, $stepSpecNamesInErrorMessage[#1], #2];
False
] &,
spec] &&
(* Check vertices make sense if vertex constraints are specified. *)
If[(MissingQ[spec[$maxFinalVertices]] && MissingQ[spec[$maxFinalVertexDegree]]) || AllTrue[set, ListQ],
True
,
Message[caller::nonListExpressions, SelectFirst[set, Not @* ListQ]];
False
] &&
(* Check initial condition does not violate the limits already. *)
And @@ (
If[Lookup[spec, #1, Infinity] >= #2,
True
,
Message[caller::tooSmallStepLimit, $stepSpecNamesInErrorMessage[#1], spec[#1], #2];
False
] & @@@ {
{$maxFinalVertices, If[MissingQ[spec[$maxFinalVertices]], 0, Length[Union[Catenate[set]]]]},
{$maxFinalVertexDegree, If[MissingQ[spec[$maxFinalVertexDegree]], 0, Max[Counts[Catenate[Union /@ set]]]]},
{$maxFinalExpressions, Length[set]}}) &&
(* Check final step constraints are not requested for a multiway system *)
(!multiwayEventSelectionFunctionQ[eventSelectionFunction] ||
AllTrue[
{$maxFinalVertices, $maxFinalExpressions, $maxFinalVertexDegree},
If[spec[#] === Infinity || MissingQ[spec[#]],
True
,
Message[caller::multiwayFinalStepLimit, $stepSpecNamesInErrorMessage[#]];
False
] &]);
(* Method is valid *)
$cppMethod = "LowLevel";
$wlMethod = "Symbolic";
$SetReplaceMethods = {Automatic, $cppMethod, $wlMethod};
General::invalidMethod =
"Method should be one of " <> listToSentence[$SetReplaceMethods] <> ".";
setSubstitutionSystem[
rules_, set_, stepSpec_, caller_, returnOnAbortQ_, o : OptionsPattern[]] := 0 /;
!MatchQ[OptionValue[Method], Alternatives @@ $SetReplaceMethods] &&
Message[caller::invalidMethod];
(* TimeConstraint is valid *)
setSubstitutionSystem[
rules_, set_, stepSpec_, caller_, returnOnAbortQ_, o : OptionsPattern[]] := 0 /;
!MatchQ[OptionValue[TimeConstraint], _ ? (# > 0 &)] &&
Message[caller::timc, OptionValue[TimeConstraint]];
(* EventOrderingFunction is valid *)
$eventOrderingFunctions = <|
"OldestEdge" -> {$sortedExpressionIDs, $forward},
"LeastOldEdge" -> {$sortedExpressionIDs, $backward},
"LeastRecentEdge" -> {$reverseSortedExpressionIDs, $forward},
"NewestEdge" -> {$reverseSortedExpressionIDs, $backward},
"RuleOrdering" -> {$expressionIDs, $forward},
"ReverseRuleOrdering" -> {$expressionIDs, $backward},
"RuleIndex" -> {$ruleIndex, $forward},
"ReverseRuleIndex" -> {$ruleIndex, $backward},
"Random" -> Nothing, (* Random is done automatically in C++ if no more sorting is available *)
"Any" -> {$any, $forward} (* OrderingDirection here doesn't do anything *)
|>;
(* This applies only to C++ due to #158, WL code uses similar order but does not apply "LeastRecentEdge" correctly. *)
$eventOrderingFunctionDefault = $eventOrderingFunctions /@ {"LeastRecentEdge", "RuleOrdering", "RuleIndex"};
parseEventOrderingFunction[caller_, Automatic] := $eventOrderingFunctionDefault;
parseEventOrderingFunction[caller_, s_String] := parseEventOrderingFunction[caller, {s}];
parseEventOrderingFunction[caller_, func : {(Alternatives @@ Keys[$eventOrderingFunctions])...}] /;
!FreeQ[func, "Random"] :=
parseEventOrderingFunction[caller, func[[1 ;; FirstPosition[func, "Random"][[1]] - 1]]];
parseEventOrderingFunction[caller_, func : {(Alternatives @@ Keys[$eventOrderingFunctions])...}] /;
!FreeQ[func, "Any"] && FirstPosition[func, "Any"][[1]] != Length[func] :=
parseEventOrderingFunction[caller, func[[1 ;; FirstPosition[func, "Any"][[1]]]]];
parseEventOrderingFunction[caller_, func : {(Alternatives @@ Keys[$eventOrderingFunctions])...}] /;
FreeQ[func, "Random"] :=
$eventOrderingFunctions /@ func;
General::invalidEventOrdering = "EventOrderingFunction `1` should be one of `2`, or a list of them by priority.";
parseEventOrderingFunction[caller_, func_] := (
Message[caller::invalidEventOrdering, func, Keys[$eventOrderingFunctions]];
$Failed
);
(* String-valued parameter is valid *)
$eventSelectionFunctions = <|
"GlobalSpacelike" -> $globalSpacelike,
None -> None, (* match-all local multiway *)
"MultiwaySpacelike" -> $spacelike (* enumerates all possible "GlobalSpacelike" evolutions *)
|>;
$eventDeduplications = <|
None -> None,
"SameInputSetIsomorphicOutputs" -> $sameInputSetIsomorphicOutputs
|>;
parseParameterValue[caller_, name_, value_, association_] /; KeyMemberQ[association, value] := association[value];
General::invalidParameterValue = "`1` `2` should be one of `3`.";
parseParameterValue[caller_, name_, value_, association_] := (
Message[caller::invalidParameterValue, name, value, Keys[association]];
$Failed
);
(* Checks if a rule that can be understood by C++ code. Will be generalized in the future until simply returns True. *)
SetAttributes[inertCondition, HoldAll];
simpleRuleQ[rule_] := inertConditionSimpleRuleQ[rule /. Condition -> inertCondition];
(* Left-hand side of the rule must refer either to specific atoms, *)
atomPatternQ[pattern_ ? AtomQ] := True;
(* or to patterns referring to one atom at-a-time. *)
atomPatternQ[pattern_Pattern ? (AtomQ[#[[1]]] && #[[2]] === Blank[] &)] := True;
atomPatternQ[_] := False;
inertConditionSimpleRuleQ[
(* empty expressions/subsets are not supported in the input, conditions are not supported *)
inertCondition[left : {{__ ? atomPatternQ}..}, True]
:> right : Module[{___ ? AtomQ} (* newly created atoms *), {{___ ? AtomQ}...}]] := Module[{p},
ConnectedGraphQ @ Graph[
Flatten[Apply[
UndirectedEdge,
(Partition[#, 2, 1] & /@ (Append[#, #[[1]]] &) /@ left),
{2}]]
/. x_Pattern :> p[x[[1]]]]
];
inertConditionSimpleRuleQ[___] := False;
(* This function accepts both the number of generations and the number of steps as an input, and runs until the first
of the two is reached. it also takes a caller function as an argument, which is used for message generation. *)
Options[setSubstitutionSystem] = {
Method -> Automatic,
TimeConstraint -> Infinity,
"EventOrderingFunction" -> Automatic,
"EventSelectionFunction" -> "GlobalSpacelike",
"EventDeduplication" -> None
};
(* It automatically switches between WL and C++ implementations *)
General::symbOrdering = "Custom event ordering, selection and deduplication are not supported for symbolic method.";
General::noLowLevel =
"Low level implementation was not compiled for your system type.";
General::lowLevelNotImplemented =
"Low level implementation is only available for local rules, " <>
"and only for sets of lists (hypergraphs).";
General::symbNotImplemented =
"Custom event ordering, selection and deduplication are only available for local rules, " <>
"and only for sets of lists (hypergraphs).";
setSubstitutionSystem[
rules_ ? setReplaceRulesQ,
set_List,
stepSpec_,
caller_,
returnOnAbortQ_,
o : OptionsPattern[]] /;
stepSpecQ[
caller, set, stepSpec, OptionValue[setSubstitutionSystem, {o}, "EventSelectionFunction"]] := ModuleScope[
method = OptionValue[Method];
timeConstraint = OptionValue[TimeConstraint];
eventOrderingFunction = parseEventOrderingFunction[caller, OptionValue["EventOrderingFunction"]];
eventSelectionFunction = parseParameterValue[
caller, "EventSelectionFunction", OptionValue["EventSelectionFunction"], $eventSelectionFunctions];
eventDeduplication = parseParameterValue[
caller, "EventDeduplication", OptionValue["EventDeduplication"], $eventDeduplications];
symbolicEvaluationSupportedQ = OptionValue["EventOrderingFunction"] === Automatic &&
OptionValue["EventSelectionFunction"] === "GlobalSpacelike" &&
OptionValue["EventDeduplication"] === None;
failedQ = False;
If[eventOrderingFunction === $Failed || eventSelectionFunction === $Failed || eventDeduplication === $Failed,
Return[$Failed]
];
If[!symbolicEvaluationSupportedQ && method === "Symbolic",
Message[caller::symbOrdering];
Return[$Failed]
];
If[(timeConstraint > 0) =!= True, Return[$Failed]];
canonicalRules = toCanonicalRules[rules];
If[MatchQ[method, Automatic | $cppMethod]
&& MatchQ[set, {{___}...}]
&& MatchQ[canonicalRules, {___ ? simpleRuleQ}],
If[$libSetReplaceAvailable,
Return[
setSubstitutionSystem$cpp[
rules, set, stepSpec, returnOnAbortQ, timeConstraint, eventOrderingFunction, eventSelectionFunction,
eventDeduplication]]
]
];
If[MatchQ[method, $cppMethod],
failedQ = True;
If[!$libSetReplaceAvailable,
Message[caller::noLowLevel]
,
Message[caller::lowLevelNotImplemented]
]
];
If[failedQ || !MatchQ[OptionValue[Method], Alternatives @@ $SetReplaceMethods],
$Failed
,
If[!symbolicEvaluationSupportedQ,
Message[caller::symbNotImplemented];
Return[$Failed]
];
setSubstitutionSystem$wl[caller, rules, set, stepSpec, returnOnAbortQ, timeConstraint]
]
];