Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HypergraphSubstitutionSystem #643

Draft
wants to merge 27 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
95b9168
Initial commit.
daneelsan May 11, 2021
12d5430
Merge remote-tracking branch 'origin/master' into feature/HypergraphS…
May 11, 2021
27c8830
Generated a Multihistory object (rather hacky). Add type conversion b…
May 12, 2021
19adaf0
Moving the calls to libSetReplace to HypergraphSubstitutionSystem ins…
daneelsan May 14, 2021
7345aed
Merge remote-tracking branch 'origin/master' into feature/HypergraphS…
daneelsan May 19, 2021
f166ce6
Updating libSetReplace function names.
daneelsan May 19, 2021
bc29ae5
Merge remote-tracking branch 'origin/master' into feature/HypergraphS…
daneelsan May 22, 2021
6f8f425
Migrating call to cpp$HypergraphSubstitutionSystem to HypergraphSubst…
daneelsan May 24, 2021
b807755
Fixing parameters for setSubstitutionSystem
daneelsan May 24, 2021
da02508
Adding "EventSeparation" to "EventSelection" for HypergraphSubstituti…
daneelsan Jun 1, 2021
19d6db3
Reworking how rules are handled before calling GenerateMultihistory[H…
daneelsan Jun 1, 2021
f743b80
Merge branch 'master' into feature/HypergraphSubstitutionSystem
daneelsan Jun 1, 2021
acfb4bd
Merge branch 'master' into feature/HypergraphSubstitutionSystem
daneelsan Jun 4, 2021
540eac0
Merge branch 'master' into feature/HypergraphSubstitutionSystem
daneelsan Jun 17, 2021
a14386d
Renaming Message::invalidEventOrdering to Message::invalidEventOrderi…
daneelsan Jun 17, 2021
1e3330b
Improving parameter and message names in GenerateMultihistory. Removi…
daneelsan Jun 17, 2021
fcd9f26
Fixing examples in GenerateMultihistory tests.
daneelsan Jun 17, 2021
b9e27d7
Updating token deduplication unevaluated test for MultisetSusbsitutio…
daneelsan Jun 17, 2021
e3638b0
Adding missing leading $ variable.
daneelsan Jun 19, 2021
4b9264e
Adding unevaluated-related tests for HypergraphSubstitutionSystem
daneelsan Jun 20, 2021
aa61cd4
Renaming $eventDeduplicationCodes to $tokenDeduplicationCodes.
daneelsan Jun 20, 2021
979f6e0
Renaming token deduplication error message in tests for AtomicStateSy…
daneelsan Jun 20, 2021
b96a084
Adding first batch of documentation.
daneelsan Jun 20, 2021
d008532
Fixing linting errors in the documentation.
daneelsan Jun 20, 2021
3f9e1b1
Merge branch 'master' into feature/HypergraphSubstitutionSystem
daneelsan Sep 23, 2021
6666c1d
Merge branch 'master' into feature/HypergraphSubstitutionSystem
daneelsan Sep 23, 2021
9030aec
Removing files.
daneelsan Sep 23, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion Documentation/TypeSystem/$SetReplaceTypes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# $SetReplaceTypes

**`$SetReplaceTypes`** gives the list of all [types](/Documentation/Types/README.md) defined in *SetReplace*:
**`$SetReplaceTypes`** gives the list of all [types](/Documentation/Types/README.md) defined in _SetReplace_:

```wl
In[] := $SetReplaceTypes
Expand Down
44 changes: 44 additions & 0 deletions Documentation/Types/Multihistory/HypergraphSubstitutionSystem0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# {HypergraphSubstitutionSystem, 0}

**`{HypergraphSubstitutionSystem, 0}`** is a [`Multihistory`](README.md) type currently returned by
[generators](/Documentation/Generators/README.md) of the
[`HypergraphSubstitutionSystem`](/Documentation/Systems/HypergraphSubstitutionSystem.md):

```wl
In[] := multi = GenerateMultihistory[HypergraphSubstitutionSystem[{{a_, b_}, {a_, c_}} :> {{b, c}}],
{"MaxDestroyerEvents" -> 2},
None,
{"ReverseSortedInputTokenIndices", "InputTokenIndices", "RuleIndex"},
{}] @ {{1, 2}, {1, 3}}
```

<img src="/Documentation/Images/HypergraphMultihistory.png" width="607.8">

It uses the hypergraph related C++ code found in the [libSetReplace](/libSetReplace/), same as what **WolframModel**
uses when the option
[Method -> "LowLevel"](/Documentation/SymbolsAndFunctions/WolframModelAndWolframModelEvolutionObject/Options/Method.md)
is called.

The information about the multihistory is not entirely stored in the generated object. Instead, a handle to the C++
hypergraph object is kept inside (see
[Managed Library Expressions](https://reference.wolfram.com/language/LibraryLink/tutorial/InteractionWithWolframLanguage#353220453)):

```wl
In[] := multi[[2]]

Out[] = <|"Rules" -> {{a_, b_}, {a_, c_}} :> {{b, c}},
"GlobalAtoms" -> {Hold[1], Hold[2], Hold[3]},
"ObjectHandle" -> HypergraphSubstitutionSystemHandle[1]|>
```

No properties are implemented yet. However, the object can be
[converted](/Documentation/TypeSystem/SetReplaceTypeConvert.md) to a
[WolframModelEvolutionObject](/Documentation/SymbolsAndFunctions/WolframModelAndWolframModelEvolutionObject/WolframModelAndWolframModelEvolutionObject.md):

```wl
In[] := multi //
SetReplaceTypeConvert[{WolframModelEvolutionObject, 2}] //
#["ExpressionsEventsGraph", VertexLabels -> Automatic] &
```

<img src="/Documentation/Images/HypergraphToWolframModelEvolutionObject1.png" width="385.8">
4 changes: 2 additions & 2 deletions Documentation/Types/Multihistory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ In[] := GenerateMultihistory[MultisetSubstitutionSystem[{a_, b_} :> {a + b}], Ma
You will be able to use [`properties`](/Documentation/Properties/README.md) to extract information about multihistories,
but we have not implemented any properties yet.

* [`AtomicStateSystem`](AtomicStateSystem0.md)
* [`MultisetSubstitutionSystem`](MultisetSubstitutionSystem0.md)
- [`AtomicStateSystem`](AtomicStateSystem0.md)
- [`MultisetSubstitutionSystem`](MultisetSubstitutionSystem0.md)
236 changes: 236 additions & 0 deletions Kernel/HypergraphSubstitutionSystem.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
Package["SetReplace`"]

PackageImport["GeneralUtilities`"]

PackageExport["HypergraphSubstitutionSystem"]

PackageScope["generateHypergraphSubstitutionSystem"]

importLibSetReplaceFunction[
"hypergraphSubstitutionSystemInitialize" -> cpp$hypergraphSubstitutionSystemInitialize,
{Integer, (* set ID *)
{Integer, 1, "Constant"}, (* rules *)
{Integer, 1, "Constant"}, (* event selection functions for rules *)
{Integer, 1, "Constant"}, (* initial set *)
Integer, (* event selection function *)
{Integer, 1, "Constant"}, (* ordering function index, forward / reverse, function, forward / reverse, ... *)
Integer, (* event deduplication *)
(* random seed, passed as two numbers because LibraryLink does not support unsigned ints *)
{Integer, 1, "Constant"}},
"Void"];

importLibSetReplaceFunction[
"hypergraphSubstitutionSystemReplace" -> cpp$hypergraphSubstitutionSystemReplace,
{Integer, (* set ID *)
{Integer, 1, "Constant"}, (* {events, generations, atoms, max expressions per atom, expressions} *)
Real}, (* time constraint *)
"Void"];

$unset = -1;
$maxUInt32 = 2^32 - 1;

generateHypergraphSubstitutionSystem[HypergraphSubstitutionSystem[rawRules___],
rawEventSelection_,
rawTokenDeduplication_,
rawEventOrdering_,
rawStoppingCondition_,
rawInit_] :=
Module[{
rules, maxGeneration, maxDestroyerEvents, eventSeparation, eventOrdering,
timeConstraint, maxEvents, maxVertices, maxVertexDegree, maxEdges, init,
objHandle, objID, globalAtoms
},
(* TODO(daniel): Improve checking of $libSetReplaceAvailable *)
If[!$libSetReplaceAvailable, Return @ $Failed];

rules = parseRules[rawRules];
{maxGeneration, maxDestroyerEvents, eventSeparation} = Values @ rawEventSelection;
eventOrdering = parseEventOrdering[rawEventOrdering];
{timeConstraint, maxEvents, maxVertices, maxVertexDegree, maxEdges} = Values @ rawStoppingCondition;
init = parseInit[rawInit];

objHandle = CreateManagedLibraryExpression["SetReplace", SetReplace`HypergraphSubstitutionSystemHandle];
objID = ManagedLibraryExpressionID[objHandle, "SetReplace"];

globalAtoms = hypergraphSubstitutionSystemInit[objID,
rules,
init,
maxDestroyerEvents,
eventOrdering,
rawTokenDeduplication,
eventSeparation];

cpp$hypergraphSubstitutionSystemReplace[
objID,
{maxEvents, maxGeneration, maxVertices, maxVertexDegree, maxEdges} /. {Infinity | (_ ? MissingQ) -> $unset},
timeConstraint /. Infinity -> $unset];

(* NOTE(daniel): We either need to save global index or the initial state to convert it to WMEvolutionObject *)
Multihistory[
{HypergraphSubstitutionSystem, 0},
<|"Rules" -> rawRules,
"GlobalAtoms" -> globalAtoms,
"ObjectHandle" -> objHandle|>]
];

hypergraphSubstitutionSystemInit[objID_,
rules_,
init_,
maxDestroyerEvents_,
eventOrdering_,
tokenDeduplication_,
eventSelection_] :=
Module[{setAtoms, atomsInRules, globalAtoms, globalIndex, mappedSet, localIndices, mappedRules},
setAtoms = Hold /@ Union[Catenate[init]];
atomsInRules = ruleAtoms /@ rules;
globalAtoms = Union @ Join[setAtoms, Catenate[atomsInRules[[All, 1]]]];
globalIndex = AssociationThread[globalAtoms -> Range[Length[globalAtoms]]];
mappedSet = Map[globalIndex, Map[Hold, init, {2}], {2}];
localIndices = AssociationThread[#[[2]] -> - Range[Length[#[[2]]]]] & /@ atomsInRules;
mappedRules = Table[
ruleAtomsToIndices[
rules[[K]],
globalIndex,
localIndices[[K]]],
{K, Length[rules]}];

cpp$hypergraphSubstitutionSystemInitialize[
objID,
encodeNestedLists[List @@@ mappedRules],
ConstantArray[eventSelection /. {"Any" -> 0, "Spacelike" -> 1}, Length @ rules],
encodeNestedLists[mappedSet],
Replace[maxDestroyerEvents, Infinity -> $unset],
Catenate[Replace[eventOrdering, $orderingFunctionCodes, {2}]],
$tokenDeduplicationCodes[tokenDeduplication],
IntegerDigits[RandomInteger[{0, $maxUInt32}], 2^16, 2]
];

globalAtoms
];

(* Parsing *)

(** Rules **)

(* parseRules[anonymousRules_ ? anonymousRulesQ] := ToPatternRules[anonymousRules]; *)
parseRules[rawRules_] /; setReplaceRulesQ[rawRules] :=
Module[{canonicalRules},
canonicalRules = toCanonicalRules[rawRules];
canonicalRules /; MatchQ[canonicalRules, {___ ? simpleRuleQ}]
];
declareMessage[General::invalidHypergraphRules, "Rules `rules` is not a valid hypergraph substitution rule."];
parseRules[rawRules_] := throw[Failure["invalidHypergraphRules", <|"rules" -> rawRules|>]];
parseRules[rawRules___] /; !CheckArguments[HypergraphSubstitutionSystem[rawRules], 1] := throw[Failure[None, <||>]];

(** TokenDeduplication **)

(** EventOrdering **)

$eventOrderingFunctions = {
"SortedInputTokenIndices" -> {$sortedExpressionIDs, $forward},
-"SortedInputTokenIndices" -> {$sortedExpressionIDs, $backward},
"ReverseSortedInputTokenIndices" -> {$reverseSortedExpressionIDs, $forward},
-"ReverseSortedInputTokenIndices" -> {$reverseSortedExpressionIDs, $backward},
"InputTokenIndices" -> {$expressionIDs, $forward},
-"InputTokenIndices" -> {$expressionIDs, $backward},
"RuleIndex" -> {$ruleIndex, $forward},
-"RuleIndex" -> {$ruleIndex, $backward},
"Any" -> {$any, $forward} (* OrderingDirection here doesn't do anything *)
};

parseEventOrdering[ordering : {(Alternatives @@ Keys[$eventOrderingFunctions])...}] /;
!FreeQ[ordering, "Random"] :=
parseEventOrdering[ordering[[1 ;; FirstPosition[ordering, "Random"][[1]] - 1]]];

parseEventOrdering[ordering : {(Alternatives @@ Keys[$eventOrderingFunctions])...}] /;
!FreeQ[ordering, "Any"] && FirstPosition[ordering, "Any"][[1]] != Length[ordering] :=
parseEventOrdering[ordering[[1 ;; FirstPosition[ordering, "Any"][[1]]]]];

parseEventOrdering[ordering : {(Alternatives @@ Keys[$eventOrderingFunctions])...}] /;
FreeQ[ordering, "Random"] :=
Replace[ordering, $eventOrderingFunctions, {1}];

(** Init **)

parseInit[init_ ? hypergraphQ] := init;
declareMessage[General::hypergraphInitNotList,
"Hypergraph Substitution System init `init` is not a valid hypergraph."];
parseInit[init_] := throw[Failure["hypergraphInitNotList", <|"init" -> init|>]];

(* Encoding *)

(* The following code turns a nested list into a single list, prepending sizes of each sublist. I.e., {{a}, {b, c, d}}
becomes {2, 1, a, 3, b, c, d}, where the first 2 is the length of the entire list, and 1 and 3 are the lengths of
sublists. *)
(* This format is used to pass both rules and set data into libSetReplace over LibraryLink *)

encodeNestedLists[list_List] := Flatten @ {Length @ list, encodeNestedLists /@ list};
encodeNestedLists[arg_] := arg;

(* Check if we have simple anonymous rules and use C++ library in that case *)

ruleAtoms[left_ :> right_] := ModuleScope[
leftVertices = Union @ Catenate[left[[1]]];
leftAtoms = Select[leftVertices, AtomQ];
patterns = Complement[leftVertices, leftAtoms];
patternSymbols = Map[Hold, patterns, {2}][[All, 1]];
createdAtoms = Map[Hold, Hold[right], {3}][[1, 1]];
rightAtoms = Complement[
Union @ Catenate @ Map[Hold, Hold[right], {4}][[1, 2]],
Join[patternSymbols, createdAtoms]];
(* {global, local} *)
{Union @ Join[Hold /@ leftAtoms, rightAtoms],
Union @ Join[patternSymbols, createdAtoms]}
];

ruleAtomsToIndices[left_ :> right_, globalIndex_, localIndex_] := ModuleScope[
newLeft = Replace[
left[[1]],
{x_ ? AtomQ :> globalIndex[Hold[x]],
x_Pattern :> localIndex[Map[Hold, x, {1}][[1]]]},
{2}];
newRight = Replace[
Map[Hold, Hold[right], {4}][[1, 2]],
x_ :> Lookup[localIndex, x, globalIndex[x]],
{2}];
newLeft -> newRight
];

$orderingFunctionCodes = <|
$sortedExpressionIDs -> 0,
$reverseSortedExpressionIDs -> 1,
$expressionIDs -> 2,
$ruleIndex -> 3,
$any -> 4,
$forward -> 0,
$backward -> 1
|>;

$tokenDeduplicationCodes = <|
None -> 0,
"SameInputSetIsomorphicOutputs" -> 1
|>;

(* HypergraphSubstitutionSystem *)

SetUsage @ "
HypergraphSubstitutionSystem[$$] is a rewriting system that replaces hyperedges \
with elements matching pattern$1, pattern$2, $$ by a list produced by evaluating output$, where pattern$i can be \
matched in any order.
";

SyntaxInformation[HypergraphSubstitutionSystem] = {"ArgumentsPattern" -> {rules_}};

declareMultihistoryGenerator[
generateHypergraphSubstitutionSystem,
HypergraphSubstitutionSystem,
<|"MaxGeneration" -> {Infinity, "NonNegativeIntegerOrInfinity"},
"MaxDestroyerEvents" -> {Infinity, "NonNegativeIntegerOrInfinity"},
"EventSeparation" -> {"Spacelike", {"Spacelike", "Any"}}|>,
Keys @ $eventOrderingFunctions,
<|"TimeConstraint" -> {Infinity, "PositiveNumberOrInfinity"},
"MaxEvents" -> {Infinity, "NonNegativeIntegerOrInfinity"},
"MaxVertices" -> {Infinity, "NonNegativeIntegerOrInfinity"},
"MaxVertexDegree" -> {Infinity, "NonNegativeIntegerOrInfinity"},
"MaxEdges" -> {Infinity, "NonNegativeIntegerOrInfinity"}|>,
Keys @ $tokenDeduplicationCodes];
3 changes: 3 additions & 0 deletions Kernel/HypergraphSubstitutionSystemProperties.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Package["SetReplace`"]

(* declareRawProperty[evenIntegerDescription, "EvenInteger", description]; *)
Loading