Skip to content

Commit

Permalink
Connect C++ code to SetReplace function (#17)
Browse files Browse the repository at this point in the history
## Changes

* Attaches C++ code from #13 to `SetReplace` function in WL package, so that it is automatically used over LibraryLink for supported rule types.
* Adds `Method` option to `SetReplace` that allows one to choose between C++ and Wolfram Language implementations.
* Build script now builds the C++ library with `xcodebuild` and puts it into the paclet (only available on macOS).
* Implements unit tests for C++ code.

## Tests

* Build with `./build.wls` and install the created paclet file `SetReplace-0.1.paclet` by evaluating `PacletInstall["path_to_paclet"]` in Wolfram Language, and then import by ``<< SetReplace` ``.
* Run the unit tests in `SetReplace.wlt`: evaluate `TestReport["path_to_wlt_file"]`.
* Check that C++ implementation is significantly faster:
```
ListPlot[Table[{i, 
     AbsoluteTiming[
       SetReplace[{{0, 0}, {0, 0}, {0, 0}}, 
        FromAnonymousRules[{{{1, 2}, {1, 3}, {1, 4}} -> {{5, 6}, {6, 
             7}, {7, 5}, {5, 7}, {7, 6}, {6, 5}, {5, 2}, {6, 3}, {7, 
             4}, {2, 7}, {4, 5}}}], i, Method -> #]][[1]]}, {i, 10, 
     100, 10}] & /@ {"WolframLanguage", "C++"}, 
 PlotLegends -> {"WL", "C++"}, AxesLabel -> {"Iterations", "Time, s"}]
```
![image](https://user-images.githubusercontent.com/1479325/59168046-b0403b00-8af9-11e9-91ee-87ce3827462a.png)

* C++ time scales linearly with the number of steps, unlike previous WL implementation:
```
ListPlot[Table[{i, 
   AbsoluteTiming[
     SetReplace[{{0, 0}, {0, 0}, {0, 0}}, 
      FromAnonymousRules[{{{1, 2}, {1, 3}, {1, 4}} -> {{5, 6}, {6, 
           7}, {7, 5}, {5, 7}, {7, 6}, {6, 5}, {5, 2}, {6, 3}, {7, 
           4}, {2, 7}, {4, 5}}}], i, Method -> "C++"]][[1]]}, {i, 100,
    1000, 100}], AxesLabel -> {"Iterations", "Time, s"}]
```
![image](https://user-images.githubusercontent.com/1479325/59168191-4f653280-8afa-11e9-9520-673bfc63e3fd.png)

* This allows one to get some massive graphs, for example, this takes ~3.5 minutes and produces a graph with 1 million edges:
```
Graph[DirectedEdge @@@ 
  SetReplace[{{0, 0}, {0, 0}, {0, 0}}, 
   FromAnonymousRules[{{{1, 2}, {1, 3}, {1, 4}} -> {{5, 6}, {6, 
        7}, {7, 5}, {5, 7}, {7, 6}, {6, 5}, {5, 2}, {6, 3}, {7, 
        4}, {2, 7}, {4, 5}}}], 125000]]
```
![image](https://user-images.githubusercontent.com/1479325/59168208-660b8980-8afa-11e9-89db-35b5c25c8722.png)

* A branching tree after `5000` steps:
```
HypergraphPlot[
 SetReplace[{{1}}, FromAnonymousRules[{{1}} -> {{2}, {2}, {1, 2}}], 
  5000]]
```
![image](https://user-images.githubusercontent.com/1479325/59168340-03ff5400-8afb-11e9-8bac-6ead142e8633.png)
  • Loading branch information
maxitg committed Jun 10, 2019
1 parent dcab1c9 commit b8157e1
Show file tree
Hide file tree
Showing 10 changed files with 699 additions and 189 deletions.
7 changes: 6 additions & 1 deletion PacletInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@
MathematicaVersion -> "12.0+",
Description -> "Implementation of a set substitution system, which in particular, has graph rewrites as a special case.",
Creator -> "Maksim Piskunov",
Extensions -> {{"Application", Context -> "SetReplace`"}}
URL -> "https://github.com/maxitg/SetReplace",
SystemID -> {"MacOSX-x86-64"},
Extensions -> {
{"Application", Context -> "SetReplace`"},
{"LibraryLink"}
}
]

0 comments on commit b8157e1

Please sign in to comment.