Skip to content

Commit

Permalink
Prepare first release. (#3)
Browse files Browse the repository at this point in the history
* Move the plots and plotting code.

* Remove a few random old scripts.

* Use native pipe and add package prefixes.

* Add a README and NEWS.

* Re-init renv.
  • Loading branch information
MHenderson authored May 22, 2024
1 parent 2adf56e commit 2c15e68
Show file tree
Hide file tree
Showing 28 changed files with 1,317 additions and 1,132 deletions.
30 changes: 0 additions & 30 deletions 19x19_ryser.R

This file was deleted.

21 changes: 0 additions & 21 deletions 6x6_ryser.R

This file was deleted.

Binary file removed 6x6_ryser.png
Binary file not shown.
20 changes: 0 additions & 20 deletions 76x76_ryser.R

This file was deleted.

7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ryser-in-r (development version)

# ryser-in-r (v0.1.0)

* 6x6, 19x19 and 76x76 latin square examples
* Not using keedwell
* Based on tidygraph and igraph
34 changes: 34 additions & 0 deletions R/19x19_ryser.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
library(dplyr)
library(ggplot2)
library(here)
library(igraph)
library(purrr)
library(tidygraph)
library(tidyr)

list.files("R", full.names = TRUE) |> walk(source)

expand_grid(row = 1:3, column = 1:4) |>
mutate(
symbol = c(1, 3, 4, 2, 5, 6, 1, 3, 4, 2, 3, 5)
) |>
add_cols(5:19, 19) |>
add_rows(4:19) |>
mutate(
original = case_when(
row <= 3 & column <= 4 ~ TRUE,
TRUE ~ FALSE
)
) |>
ggplot(aes(column, row)) +
geom_tile(aes(fill = symbol)) +
geom_text(data = . |> filter(original), aes(label = symbol), size = 3, colour = "red") +
geom_text(data = . |> filter(!original), aes(label = symbol), size = 2, colour = "white") +
scale_y_reverse() +
coord_fixed() +
theme_void() +
theme(
legend.position = "none",
)

ggsave(here("plots", "19x19_ryser.png"), width = 3, height = 3)
25 changes: 25 additions & 0 deletions R/6x6_ryser.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
library(dplyr)
library(ggplot2)
library(here)
library(igraph)
library(purrr)
library(tidygraph)
library(tidyr)

list.files("R", full.names = TRUE) |> walk(source)

expand_grid(row = 1:3, column = 1:4) |>
mutate(symbol = c(1, 3, 4, 2, 5, 6, 1, 3, 4, 2, 3, 5)) |>
add_cols(5:6, 6) |>
add_rows(4:6) |>
ggplot(aes(column, row)) +
geom_tile(aes(fill = symbol)) +
geom_text(aes(label = symbol), size = 5, colour = "white") +
scale_y_reverse() +
coord_fixed() +
theme_void() +
theme(
legend.position = "none",
)

ggsave(here("plots", "6x6_ryser.png"), width = 3, height = 3)
24 changes: 24 additions & 0 deletions R/76x76_ryser.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library(dplyr)
library(ggplot2)
library(here)
library(igraph)
library(purrr)
library(tidygraph)
library(tidyr)

list.files("R", full.names = TRUE) |> walk(source)

expand_grid(row = 1:3, column = 1:4) |>
mutate(symbol = c(1, 3, 4, 2, 5, 6, 1, 3, 4, 2, 3, 5)) |>
add_cols(5:76, 76) |>
add_rows(4:76) |>
ggplot(aes(column, row)) +
geom_tile(aes(fill = symbol)) +
scale_y_reverse() +
coord_fixed() +
theme_void() +
theme(
legend.position = "none",
)

ggsave(here("plots", "76x76_ryser.png"), width = 3, height = 3)
6 changes: 3 additions & 3 deletions R/edge_tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ edge_tbl <- function(R, i, l_order = 3) {
all_symbols <- 1:l_order

# symbols used in column i
used <- R %>% filter(column == i) %>% pull(symbol)
used <- R |> dplyr::filter(column == i) |> dplyr::pull(symbol)

# symbols missing from column i
missing <- setdiff(all_symbols, used)

# edge data frame for column i
edge_df <- tibble(
edge_df <- tibble::tibble(
to = paste0("s", missing)
) %>%
mutate(from = paste0("c", i))
dplyr::mutate(from = paste0("c", i))

return(edge_df)
}
8 changes: 4 additions & 4 deletions R/edge_tbl_2.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ edge_tbl_2 <- function(R, i, l_order = 3) {
all_symbols <- 1:l_order

# symbols used in row i
used <- R %>% filter(row == i) %>% pull(symbol)
used <- R |> dplyr::filter(row == i) |> dplyr::pull(symbol)

# symbols missing from row i
missing <- setdiff(all_symbols, used)

# edge data frame for column i
edge_df <- tibble(
edge_df <- tibble::tibble(
to = paste0("s", missing)
) %>%
mutate(from = paste0("r", i))
) |>
dplyr::mutate(from = paste0("r", i))

return(edge_df)
}
28 changes: 14 additions & 14 deletions R/next_col_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ next_col_matching <- function(R, i, l_order) {

bg <- to_tidygraph_2(R, l_order, n_rows, n_cols)

m <- max_bipartite_match(bg)
m <- igraph::max_bipartite_match(bg)

# names of edges in the matching
matching_names <- match(m$matching, names(m$matching))

# add a matching indicator to the edges
bg <- bg %>%
activate(edges) %>%
mutate(
bg <- bg |>
tidygraph::activate(edges) |>
dplyr::mutate(
matching = to == matching_names[from]
) %>%
filter(from <= n_rows)
) |>
dplyr::filter(from <= n_rows)

# just the matching itself, as a graph
mg <- bg %>%
activate(edges) %>%
filter(matching)
mg <- bg |>
tidygraph::activate(edges) |>
dplyr::filter(matching)

EE <- ends(mg, E(mg))
EE <- igraph::ends(mg, E(mg))

R %>%
bind_rows(
tibble(
R |>
dplyr::bind_rows(
tibble::tibble(
column = rep(i, n_rows),
row = 1:n_rows,
row = 1:n_rows,
symbol = as.numeric(gsub("s", "", EE[, 2]))
)
)
Expand Down
8 changes: 4 additions & 4 deletions R/next_col_random.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ next_col_random <- function(R, i, l_order) {
n_rows <- max(R$row)
n_cols <- max(R$column)

R %>%
bind_rows(
tibble(
R |>
dplyr::bind_rows(
tibble::tibble(
column = rep(i, n_rows),
row = 1:n_rows,
row = 1:n_rows,
symbol = sample(1:l_order, n_rows)
)
)
Expand Down
24 changes: 12 additions & 12 deletions R/next_row_matching.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,29 @@
next_row_matching <- function(R, i, l_order) {
bg <- to_tidygraph(R, l_order)

m <- max_bipartite_match(bg)
m <- igraph::max_bipartite_match(bg)

# names of edges in the matching
matching_names <- match(m$matching, names(m$matching))

# add a matching indicator to the edges
bg <- bg %>%
activate(edges) %>%
mutate(
bg <- bg |>
tidygraph::activate(edges) |>
dplyr::mutate(
matching = to == matching_names[from]
)

# just the matching itself, as a graph
mg <- bg %>%
activate(edges) %>%
filter(matching)
mg <- bg |>
tidygraph::activate(edges) |>
dplyr::filter(matching)

EE <- ends(mg, E(mg))
EE <- igraph::ends(mg, E(mg))

R %>%
bind_rows(
tibble(
row = rep(i, l_order),
R |>
dplyr::bind_rows(
tibble::tibble(
row = rep(i, l_order),
column = 1:l_order,
symbol = as.numeric(gsub("s", "", EE[,2]))
)
Expand Down
8 changes: 4 additions & 4 deletions R/next_row_random.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
next_row_random <- function(R, i, l_order) {

R %>%
bind_rows(
tibble(
row = rep(i, l_order),
R |>
dplyr::bind_rows(
tibble::tibble(
row = rep(i, l_order),
column = 1:l_order,
symbol = sample(1:l_order, l_order)
)
Expand Down
7 changes: 4 additions & 3 deletions R/to_tidygraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ to_tidygraph <- function(R, l_order = 3) {
column_vertices <- paste0("c", 1:l_order)
symbol_vertices <- paste0("s", 1:l_order)

l_nodes <- tibble(
l_nodes <- tibble::tibble(
name = c(column_vertices, symbol_vertices),
type = c(rep(TRUE, l_order), rep(FALSE, l_order))
)

## EDGE DATA FRAME
f <- function(i) return(edge_tbl(R, i, l_order))

l_edges <- map_df(1:l_order, f)
l_edges <- purrr::map_df(1:l_order, f)

tidygraph::tbl_graph(nodes = l_nodes, edges = l_edges)

tbl_graph(nodes = l_nodes, edges = l_edges)
}
12 changes: 6 additions & 6 deletions R/to_tidygraph_2.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@ to_tidygraph_2 <- function(R, l_order, n_rows, n_cols) {
n_dummy_nodes <- l_order - n_rows
dummy_vertices <- paste0("d", 1:n_dummy_nodes)

l_nodes <- tibble(
l_nodes <- tibble::tibble(
name = c(row_vertices, dummy_vertices, symbol_vertices),
type = c(rep(TRUE, n_rows + n_dummy_nodes), rep(FALSE, l_order))
)

## EDGE DATA FRAME
f <- function(i) return(edge_tbl_2(R, i, l_order))

l_edges <- map_df(1:n_rows, f)
l_edges <- purrr::map_df(1:n_rows, f)

G <- tbl_graph(nodes = l_nodes, edges = l_edges)
G <- tidygraph::tbl_graph(nodes = l_nodes, edges = l_edges)

d_edges <- tibble(
to = rep(symbol_vertices, as.numeric(l_order - n_cols - degree(G, symbol_vertices))),
d_edges <- tibble::tibble(
to = rep(symbol_vertices, as.numeric(l_order - n_cols - degree(G, symbol_vertices))),
from = rep(dummy_vertices, each = l_order - n_cols)
)

tbl_graph(nodes = l_nodes, edges = bind_rows(l_edges, d_edges))
tidygraph::tbl_graph(nodes = l_nodes, edges = dplyr::bind_rows(l_edges, d_edges))

}

13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# ryser-in-r

## 6 x 6

![](plots/6x6_ryser.png)

## 19 x 19

![](plots/19x19_ryser.png)

## 76 x 76

![](plots/76x76_ryser.png)
Loading

0 comments on commit 2c15e68

Please # to comment.