Skip to content

Commit

Permalink
Website prepared and final changes to the package.
Browse files Browse the repository at this point in the history
  • Loading branch information
melinavidoni committed May 29, 2018
1 parent 6a9e072 commit 1824baf
Show file tree
Hide file tree
Showing 25 changed files with 861 additions and 134 deletions.
14 changes: 12 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
exportPattern("^[^\\.]")
# Generated by roxygen2: do not edit by hand

export(direct_graph)
export(get_all_nodes)
export(get_shortest_path)
export(modify_graph_hsu)
export(modify_graph_vd)
export(parse_vpath)
importFrom(foreach,"%dopar%")
importFrom(magrittr,"%>%")
importFrom(igraph,E)
importFrom(igraph,graph_from_data_frame)
importFrom(igraph,shortest_paths)
importFrom(magrittr,"%>%")
7 changes: 6 additions & 1 deletion R/hsu.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @export
#' @title Hsu et al. (2009) Algorithm
#'
#' @description It is an implementation of Hsu et al. algorithm to transform a digraph and a known set of
Expand Down Expand Up @@ -26,7 +27,11 @@
#' original graph, nodes on the new graph are of type
#' character. The new nodes names are generated by incrementally concatenating the nodes on a forbidden
#' path, but split by a pipe character (\code{|}).

#'
#' @importFrom foreach %dopar%
#' @importFrom magrittr %>%
#'
#'
#' @examples
#' # Obtain a graph and its forbidden subpaths
#' graph <- structure(list(from = c("c", "c", "u", "u", "t", "a", "a", "r", "e", "e", "e", "p", "i", "i", "n", "o"),
Expand Down
55 changes: 55 additions & 0 deletions R/igraph.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#' @export
#' @title iGraph Shortest Path
#'
#' @description A original node N_i can appear on a transformed gStar as different N_i* equivalent nodes. Therefore,
#' this becomes a limitation when searching for a shortest path inside gStar. As a result: all N_i* need to be
#' considered as possible destination nodes when looking for the shortest path. This function is a wrapper for this
#' behavior, providing a straightforward implementation using iGraph capabilities. However, it aims to provide
#' guidance on how to build a similar algorithm for different path-finding algorithms.
#'
#' It is important to mention that new nodes are only considered as destination nodes, and they are not search
#' for origin nodes. This is because N* nodes can only be reached after traveling through gStar nodes. For example,
#' a node \code{"f|e|r"} is actually indicating that \code{"r"} has been reached after traveling through the nodes
#' \code{"f"} and \code{"e"}.
#'
#' @family iGraph Integration
#'
#' @param g A gStar digraph in data frame format, translated using one of the available functions. The weight or cost attribute
#' of each arc of the graph must be stored in a specific column named \code{weight}.
#' @param origin The name of the starting node from G for the path. It must be written as it appears in G, and it is
#' preferable to use a character format, but this can also be of any simple type. No lists or vectors are allowed.
#' @param dest The name of the destination node from G for the path. It must be written as it appears in G, and it is
#' preferable to use a character format, but this can also be of any simple type. No lists or vectors are allowed.
#'
#' @return The shortest path from \code{origin} node to \code{dest} node, calculated in G*, to include the forbidden paths.
#' It uses iGraph's functionalities.
#'
#' @importFrom igraph graph_from_data_frame
#' @importFrom igraph shortest_paths
#' @importFrom igraph E
#'
#' @examples
#' # Given a specific gStar graph:
#' gStar <- structure(list(from = c("u|v", "s|u|v", "s|u", "s", "s", "u", "w", "w", "x", "x",
#' "v", "v", "y", "y", "s", "s|u", "u", "u|v"),
#' to = c("t", "u|v|y", "w", "w", "x", "w", "v", "y", "w", "y", "y", "t",
#' "t", "u", "s|u", "s|u|v", "u|v", "u|v|y"),
#' weight = c(12L, 3L, 5L, 9L, 7L, 5L, 11L, 10L, 1L, 2L, 3L, 12L, 13L, 0L, 8L, 4L, 4L, 3L)),
#' class = "data.frame", row.names = c(NA, -18L), .Names = c("from", "to", "weight"))
#' gStar
#'
#' # Obtain the shortest path
#' get_shortest_path(gStar, "s", "v")
#'
#'
get_shortest_path <- function(g, origin, dest) {
# Convert the graph
g.i <- graph_from_data_frame(g)

# Get all the shortest paths to each node
sp <- shortest_paths(g.i, from = origin, to = get_all_nodes(gStar, dest), weights = E(g.i)$weight, output = "both")

# Return the shortest path
sp$vpath[which.min( lapply(sp$epath, function(y)
ifelse(length(y) != 0, sum(E(g.i)$weight[ y[[1]] ]), 999999999)) )] [[1]]
}
51 changes: 51 additions & 0 deletions R/parsers.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @export
#' @title Parser for G* nodes paths.
#'
#' @description Translates a sequence of nodes from a G* graph, generated with any of the available
Expand Down Expand Up @@ -37,6 +38,7 @@ parse_vpath <- function(vpath) {



#' @export
#' @title Undirected Graph Translator
#'
#' @description The SPPFP transformation functions only work with digraphs -i.e. directed graphs. Because in a digraph
Expand All @@ -56,6 +58,9 @@ parse_vpath <- function(vpath) {
#' @return A new graph, with the same columns and data types of the original graph. This new graph is twice as
#' big as the original, as new arcs are added to represent that each arc can be traveled in both directions.
#'
#' @importFrom foreach %dopar%
#' @importFrom magrittr %>%
#'
#' @examples
#' # Obtain the graph from any way
#' graph <- structure(list(from = c("s", "s", "s", "u", "u", "w", "w", "x", "x", "v", "v", "y", "y"),
Expand Down Expand Up @@ -100,3 +105,49 @@ direct_graph <- function(graph, cores = 1L) {
# Return the value
rbind(graph, directedGraph)
}







#' @export
#' @title Parser for G* nodes.
#'
#' @description A original node N_i can appear on a transformed G* as different nodes. This is the result of
#' the creation of nodes in the transformation processes. Therefore, it is possible that the original node N
#' does not exists on G*, or that multiple N_i* exist. Hence, as all new nodes are generated using a specific
#' structure for the name -compiling all previous nodes names, split by pipe-, this function allows searching
#' for all the N_i* nodes that are equivalente to N_i. This can be used to find shortest paths to all of them.
#'
#' @family Parsers
#'
#' @param gStar A graph in data frame format, translated using one of the available functions.
#' @param originalNode The name of the original node from G, that needs to be searched within G*. It is preferable
#' to use a character format, but this can also be of any simple type. No lists or vectors are allowed.
#'
#' @return A new vector of character type, whose elements are all the N_i* equivalent to the original N node. This
#' also includes the original node.
#'
#' @examples
#' # Given a specific gStar graph:
#' gStar <- structure(list(from = c("u|v", "s|u|v", "s|u", "s", "s", "u", "w", "w", "x", "x", "v",
#' "v", "y", "y", "s", "s|u", "u", "u|v"),
#' to = c("t", "u|v|y", "w", "w", "x", "w", "v", "y", "w", "y", "y", "t",
#' "t", "u", "s|u", "s|u|v", "u|v", "u|v|y"),
#' weight = c(12L, 3L, 5L, 9L, 7L, 5L, 11L, 10L, 1L, 2L, 3L, 12L, 13L, 0L, 8L, 4L, 4L, 3L)),
#' class = "data.frame", row.names = c(NA, -18L), .Names = c("from", "to", "weight"))
#' gStar
#'
#' # Obtain all the nodes equivalent to N_i = "v"
#' get_all_nodes(gStar, "v")
#'
#'
get_all_nodes <- function(gStar, originalNode) {
# Get the list of nodes
nodes <- unique(c(gStar$from, gStar$to))

# Get all the nodes that end on that original node
nodes[sapply(paste0("*\\s|", originalNode, "$"), function(y) grepl(y, nodes))]
}
10 changes: 7 additions & 3 deletions R/villeneuve.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#' @export
#' @title Villeneuve and Desaulniers (2005) Algorithm
#'
#' @description It is an implementation of Villeneuve and Desaulniers' algorithm to transform a digraph
Expand Down Expand Up @@ -27,7 +28,10 @@
#' character. The new nodes names are generated by incrementally concatenating the nodes on a forbidden
#' path, but split by a pipe character (\code{|}). The new graph includes all of the additional attributes
#' that the original graph had.
#'
#'
#' @importFrom foreach %dopar%
#' @importFrom magrittr %>%
#'
#' @examples
#' # Obtain a graph and its forbidden subpaths
#' graph <- structure(list(from = c("s", "s", "s", "u", "u", "w", "w", "x", "x", "v", "v", "y", "y"),
Expand Down Expand Up @@ -121,7 +125,7 @@ modify_graph_vd <- function(g, f, cores = 1L) {
if(!identical(newTo, character(0))) {
# ...add it using the new node, if it needs attributes
if(ncol > 2) {
tempNewArcs[nrow(tempNewArcs) + 1,] <- list(nn, newTo, .get_arc_attributes(g, nn, toNode))
tempNewArcs[nrow(tempNewArcs) + 1,] <- list(nn, newTo, .get_arc_attributes(g, nsName, toNode))
}
# Or without the attributes
else tempNewArcs[nrow(tempNewArcs) + 1,] <- list(nn, newTo)
Expand All @@ -133,7 +137,7 @@ modify_graph_vd <- function(g, f, cores = 1L) {

# Add the new link, but with the original node
if(ncol > 2) {
tempNewArcs[nrow(tempNewArcs) + 1,] <- list(nn, toNode, .get_arc_attributes(g, nn, toNode))
tempNewArcs[nrow(tempNewArcs) + 1,] <- list(nn, toNode, .get_arc_attributes(g, nsName, toNode))
}
# Or without the attributes
else tempNewArcs[nrow(tempNewArcs) + 1,] <- list(nn, toNode)
Expand Down
14 changes: 0 additions & 14 deletions README.Rmd
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```

# R-SPPFP
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rsppfp)](https://cran.r-project.org/package=rsppfp)
[![Travis-CI Build Status](https://travis-ci.org/melvidoni/rsppfp.svg?branch=master)](https://travis-ci.org/melvidoni/rsppfp)
Expand Down
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
---
output: github_document
---

<!-- README.md is generated from README.Rmd. Please edit that file -->

```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```

# R-SPPFP
[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/rsppfp)](https://cran.r-project.org/package=rsppfp)
[![Travis-CI Build Status](https://travis-ci.org/melvidoni/rsppfp.svg?branch=master)](https://travis-ci.org/melvidoni/rsppfp)
Expand Down
6 changes: 6 additions & 0 deletions _pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,19 @@ reference:
contents:
- '`parse_vpath`'
- '`direct_graph`'
- '`get_all_nodes`'

- title: Digraphs Transformations
desc: Implementations of algorithms to transform a graph and its set of forbidden paths.
contents:
- '`modify_graph_vd`'
- '`modify_graph_hsu`'

- title: Integrations
desc: Functions that simplify the integration with other R-Packages.
contents:
- '`get_shortest_path`'


navbar:
type: inverse
Expand Down
Loading

0 comments on commit 1824baf

Please sign in to comment.