Created
November 19, 2022 19:09
-
-
Save grosscol/de4b91bdff2492d56f022e15c8c8f562 to your computer and use it in GitHub Desktop.
Compare attributes of candidate against all pathways requirements.
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
Pathways-attributes matrix | |
A B C | |
p0 0 0 0 | |
p1 0 0 1 | |
p2 0 1 0 | |
p3 1 0 0 | |
p4 0 1 1 | |
p5 1 0 1 | |
p6 1 1 0 | |
p7 1 1 1 | |
Pathways row sums | |
p0 p1 p2 p3 p4 p5 p6 p7 | |
0 1 1 1 2 2 2 3 | |
candidate 1: | |
A B C | |
0 0 1 | |
acceptable by paths: | |
p0 p1 | |
candidate 2: | |
A B C | |
1 0 1 | |
acceptable by paths: | |
p0 p1 p3 p5 | |
candidate 3: | |
A B C | |
1 1 1 | |
acceptable by paths: | |
p0 p1 p2 p3 p4 p5 p6 p7 |
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
# Each pathway described by attributes, A,B,C | |
# Create pathway for every combination of attributes | |
attr_names <- LETTERS[1:3] | |
paths <- c(0,0,0, #p0 (no requirements pathway) | |
0,0,1, #p1 | |
0,1,0, #p2 | |
1,0,0, #p3 | |
0,1,1, #p4 | |
1,0,1, #p5 | |
1,1,0, #p6 | |
1,1,1) #p7 (full requirements pathway) | |
# Create matrix and label nicely. | |
paths_matrix <- matrix(paths, ncol=3, byrow=T) | |
colnames(paths_matrix) = attr_names | |
rownames(paths_matrix) = paste('p',0:7,sep='') | |
# Calculate row sums of the paths matrix | |
paths_row_sums <- rowSums(paths_matrix) | |
# Show matrix and sums | |
cat("Pathways-attributes matrix\n") | |
print(paths_matrix) | |
cat('\n') | |
cat("Pathways row sums\n") | |
print(paths_row_sums) | |
cat('\n') | |
# Check a candidate against a matrix of pathways | |
# This is essentially performing bitwise AND of the candidate attributes | |
# against each pathway's attributes. If all a pathway's requirements are met | |
# then the result will have the same number of 1's as the pathway. | |
# Params: | |
# cand, a vector of attributes in the same order as p_mat columns | |
# p_mat, a the matrix of pathways where attributes are columns. | |
# row_sums, the row sums of p_mat to avoid recomputing them each call | |
candidate_accepting_paths <- function(cand, p_mat, row_sums){ | |
candidate_sums <- p_mat %*% cand | |
matching_sums <- candidate_sums == paths_row_sums | |
# return names of paths that have all requirements satisified | |
rownames(p_mat)[matching_sums] | |
} | |
# Encode a candidate as a vector of attributes | |
candidate_1 <- c(0,0,1) | |
candidate_2 <- c(1,0,1) | |
candidate_3 <- c(1,1,1) | |
# Make candidate's named vectors for pretty printing | |
names(candidate_1) <- attr_names | |
names(candidate_2) <- attr_names | |
names(candidate_3) <- attr_names | |
# Check which paths are the candidates meets the requirements of | |
c1_accepted <- candidate_accepting_paths(candidate_1, paths_matrix, paths_row_sums) | |
c2_accepted <- candidate_accepting_paths(candidate_2, paths_matrix, paths_row_sums) | |
c3_accepted <- candidate_accepting_paths(candidate_3, paths_matrix, paths_row_sums) | |
# print results | |
cat("candidate 1:\n") | |
print(candidate_1) | |
cat("acceptable by paths:\n") | |
cat(paste(c1_accepted, collapse = ' ')) | |
cat('\n\n') | |
cat("candidate 2:\n") | |
print(candidate_2) | |
cat("acceptable by paths:\n") | |
cat(paste(c2_accepted, collapse = ' ')) | |
cat('\n\n') | |
cat("candidate 3:\n") | |
print(candidate_3) | |
cat("acceptable by paths:\n") | |
cat(paste(c3_accepted, collapse = ' ')) | |
cat('\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment