-
Notifications
You must be signed in to change notification settings - Fork 1
Add test suite that runs anatevka on large graphs with known MWPMs #60
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
*.fasl | ||
.DS_Store | ||
*~ | ||
.idea/ | ||
.ipynb_checkpoints/ | ||
.python-version | ||
__pycache__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
;;;; tests/cases.lisp | ||
;;;; | ||
;;;; Runs the distributed blossom algorithm against a family of graphs with known | ||
;;;; MWPM, and asserts that we find the correct answer. | ||
|
||
(in-package :anatevka-tests) | ||
|
||
(eval-when (:compile-toplevel :load-toplevel :execute) ; needed at compile-time | ||
|
||
(defun test-case-directory-filepath (dirname) | ||
"Return the absolute pathname for `DIRNAME', which is the name of a test case directory in the `tests/py/cases'." | ||
(declare (string dirname)) | ||
(let* ((anatevka-dir (ql:where-is-system "anatevka-tests"))) | ||
(merge-pathnames (concatenate 'string "tests/py/cases/" dirname "/*.*") | ||
anatevka-dir))) | ||
|
||
(defun parse-test-case-file (filename) | ||
"Parses a test case file and extracts the MWPM value and the list of nodes." | ||
(with-open-file (stream filename :direction :input) | ||
(let ((mwpm-value (parse-integer (read-line stream))) ; read first line for mwpm | ||
(nodes '())) | ||
(loop :for line := (read-line stream nil) | ||
:while line | ||
:do (destructuring-bind (x y) | ||
(mapcar #'parse-integer (uiop:split-string line :separator ",")) | ||
(push (list x y) nodes))) | ||
(values mwpm-value (reverse nodes))))) | ||
|
||
(defun process-test-case-directory (dirname) | ||
"Iterates through all files in test case directory named `DIRNAME' and parses them." | ||
(let* ((dirpath (test-case-directory-filepath dirname)) | ||
(files (directory dirpath))) | ||
(loop :for file :in files | ||
:collect (list file (multiple-value-list (parse-test-case-file file))) | ||
:into test-cases | ||
:finally (return test-cases)))) | ||
|
||
(defun sanitize-test-name (dirname test-path) | ||
"Generates a valid Lisp symbol for a test name based on directory and file name." | ||
(let* ((test-name (pathname-name test-path)) | ||
(clean-name (format nil "test-blossom-suite-~A-~A" dirname test-name))) | ||
(intern (string-upcase (substitute #\- #\/ clean-name)) :anatevka-tests)))) | ||
|
||
(defmacro define-blossom-suite (dirname | ||
(&key (border +default-border+) | ||
(debug? nil) | ||
(dryad-clock-rate +default-dryad-clock-rate+) | ||
(iterations +default-iterations+) | ||
(timeout +default-timeout+) | ||
(timestep +default-timestep+) | ||
(dryad-class 'dryad))) | ||
"Creates a suite of `DEFINE-BLOSSOM-TEST's by processing the test case files at `DIRNAME' and asserting that we produce the correct minimum-weight perfect matching (MWPM). The `DIRNAME' is a number n specifying a directory containing test cases of random complete graphs laid out on an nxn grid. The test names produced by `DEFINE-BLOSSOM-SUITE' are of the form `TEST-BLOSSOM-SUITE-n-p-i' where p is the 'node density' (i.e. what proportion of the grid locations have a node at them) and i is the test case number." | ||
`(progn | ||
,@(loop :for (test-path (mwpm-value nodes)) | ||
:in (process-test-case-directory dirname) | ||
:for test-name := (sanitize-test-name dirname test-path) | ||
:collect | ||
`(define-blossom-test ,test-name ,nodes | ||
(:border ,border :debug? ,debug? :dryad-clock-rate ,dryad-clock-rate | ||
:iterations ,iterations :timeout ,timeout :timestep ,timestep | ||
:dryad-class ,dryad-class :solution-weight ,mwpm-value) | ||
())))) | ||
|
||
(define-blossom-suite "10" ()) ; 10x10 grid tests | ||
|
||
(define-blossom-suite "20" ()) ; 20x20 grid tests |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9c4066b9-4681-4652-9339-7546f9f8ab43", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from generate_test_cases import generate_sparse_complete_graph, find_min_weight_perfect_matching, visualize_graph, save_matching_to_file" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8340f011-13ac-4327-9bce-72dc87c7ede6", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"p = 0.4\n", | ||
"n = 20" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "b075b58e-6ca9-45cd-83c6-9377cd7c2a89", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"for i in range(0, 10):\n", | ||
" G, nodes = generate_sparse_complete_graph(n, p)\n", | ||
" matching, mwpm_value = find_min_weight_perfect_matching(G)\n", | ||
" filename = f\"cases/{n}/{p}-{i}.txt\"\n", | ||
" save_matching_to_file(filename, mwpm_value, nodes)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.12.4" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Python code for generating anatevka test cases | ||
|
||
Install with `poetry`, and then open the `GenerateTestCases.ipynb` notebook in | ||
`jupyter lab` to create additional test cases in the `cases` directory. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
15 | ||
2,1 | ||
0,0 | ||
6,8 | ||
7,0 | ||
5,7 | ||
3,9 | ||
7,2 | ||
2,2 | ||
5,3 | ||
1,3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
14 | ||
8,8 | ||
2,4 | ||
9,0 | ||
2,1 | ||
7,7 | ||
4,3 | ||
1,4 | ||
1,7 | ||
5,3 | ||
2,8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
13 | ||
7,4 | ||
2,4 | ||
7,7 | ||
1,5 | ||
3,0 | ||
6,7 | ||
5,6 | ||
9,1 | ||
4,1 | ||
3,5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
9 | ||
2,1 | ||
8,1 | ||
3,1 | ||
1,1 | ||
8,0 | ||
0,2 | ||
3,6 | ||
6,6 | ||
5,9 | ||
3,5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
14 | ||
4,4 | ||
8,8 | ||
2,1 | ||
0,6 | ||
3,0 | ||
9,5 | ||
6,7 | ||
5,6 | ||
1,9 | ||
3,5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
18 | ||
7,4 | ||
9,0 | ||
2,4 | ||
0,4 | ||
1,1 | ||
4,5 | ||
5,0 | ||
3,9 | ||
2,6 | ||
3,5 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
15 | ||
3,8 | ||
4,9 | ||
1,8 | ||
0,9 | ||
0,3 | ||
7,0 | ||
7,3 | ||
1,6 | ||
1,9 | ||
8,2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
16 | ||
0,1 | ||
0,7 | ||
5,5 | ||
5,4 | ||
9,2 | ||
8,0 | ||
1,4 | ||
3,6 | ||
8,5 | ||
5,2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
11 | ||
2,7 | ||
8,7 | ||
5,4 | ||
6,4 | ||
2,9 | ||
0,2 | ||
1,7 | ||
0,8 | ||
7,8 | ||
2,8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
14 | ||
4,4 | ||
2,1 | ||
8,7 | ||
7,0 | ||
4,2 | ||
2,3 | ||
4,5 | ||
8,9 | ||
3,6 | ||
3,2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
18 | ||
4,3 | ||
3,1 | ||
5,7 | ||
8,0 | ||
0,8 | ||
1,9 | ||
7,1 | ||
4,2 | ||
4,8 | ||
3,6 | ||
2,7 | ||
3,2 | ||
4,4 | ||
9,0 | ||
3,8 | ||
0,6 | ||
2,9 | ||
6,0 | ||
7,5 | ||
7,8 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
23 | ||
3,7 | ||
5,1 | ||
8,0 | ||
0,2 | ||
8,6 | ||
2,5 | ||
4,2 | ||
3,6 | ||
0,1 | ||
2,4 | ||
7,9 | ||
6,7 | ||
3,5 | ||
9,0 | ||
8,1 | ||
0,0 | ||
9,6 | ||
2,0 | ||
0,9 | ||
6,9 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly this seems fine, since there aren't a bajillion test cases, but I have a dull memory that it's ill-advised to intern programmatically generated symbols. As an alternative, I guess we could just iterate through the directory at eval time of a single umbrella test, whether than at compile time to generate individuated tests?
Don't worry about it, just a comment.