From 7883b422b043438fa27e32069231f8694947bd09 Mon Sep 17 00:00:00 2001 From: Max Fan Date: Sun, 25 Jul 2021 22:38:14 -0400 Subject: [PATCH] Make recom and flip chains an iterable a la GerryChain Python Although this is a breaking change, this brings GerryChain Julia more in line with GerryChain Python and allows the user to save and export state more easily. This API is different from the one proposed in #94 in that it returns a Partition object and the scores. --- Project.toml | 1 + src/GerryChain.jl | 1 + src/flip.jl | 4 +++- src/recom.jl | 4 +++- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index b9beeee..613db7f 100644 --- a/Project.toml +++ b/Project.toml @@ -17,6 +17,7 @@ Logging = "56ddb016-857b-54e1-b83d-db4d58db5568" ProgressBars = "49802e3a-d2f1-5c88-81d8-b72133a6f568" PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +ResumableFunctions = "c5292f4c-5179-55e1-98c5-05642aab7184" Shapefile = "8e980c4a-a4fe-5da2-b3a7-4b4b0353a2f4" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" diff --git a/src/GerryChain.jl b/src/GerryChain.jl index 8a2d44a..95f9f16 100644 --- a/src/GerryChain.jl +++ b/src/GerryChain.jl @@ -13,6 +13,7 @@ import Shapefile import LibGEOS import LibSpatialIndex using Logging +using ResumableFunctions export AbstractGraph, BaseGraph, diff --git a/src/flip.jl b/src/flip.jl index 3d71f0a..a4359e1 100644 --- a/src/flip.jl +++ b/src/flip.jl @@ -150,7 +150,8 @@ every score at each step of the chain. infinite loops if the acceptance function is never satisfied! """ -function flip_chain( +function flip_chain end # this is a workaround +@resumable function flip_chain( graph::BaseGraph, partition::Partition, pop_constraint::PopulationConstraint, @@ -186,6 +187,7 @@ function flip_chain( end end score_vals = score_partition_from_proposal(graph, partition, proposal, scores) + @yield partition, score_vals push!(chain_scores.step_values, score_vals) step_completed = true end diff --git a/src/recom.jl b/src/recom.jl index 1bfd91d..1d30283 100644 --- a/src/recom.jl +++ b/src/recom.jl @@ -285,7 +285,8 @@ step of the chain. infinite loops if the acceptance function is never satisfied! """ -function recom_chain( +function recom_chain end # this is a workaround +@resumable function recom_chain( graph::BaseGraph, partition::Partition, pop_constraint::PopulationConstraint, @@ -322,6 +323,7 @@ function recom_chain( end end score_vals = score_partition_from_proposal(graph, partition, proposal, scores) + @yield partition, score_vals push!(chain_scores.step_values, score_vals) step_completed = true end