Skip to content

Commit d32ea82

Browse files
committed
first commit
0 parents  commit d32ea82

19 files changed

+919
-0
lines changed

.Rbuildignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
.Rhistory
4+
*.o
5+
*.dll

DESCRIPTION

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Package: salesmanRcpp
2+
Type: Package
3+
Title: What the Package Does (Title Case)
4+
Version: 0.1.0
5+
Author: Mohamed NIANG
6+
Maintainer: Mohamed NIANG <niango777@gmail.com>
7+
Description: This package is an example for the use of Rcpp functions with the classic probem of the travelling salesman problem. Our aim is mainly to compare the time efficiency between R and C++ code of the problem.
8+
License: GPL-3
9+
Encoding: UTF-8
10+
LazyData: true
11+
Imports: Rcpp (>= 1.0.0), combinat
12+
LinkingTo: Rcpp
13+
RoxygenNote: 6.0.1

NAMESPACE

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
useDynLib(salesmanRcpp, .registration = TRUE)
2+
3+
export(Towns, distance, monVoyageR, monVoyageClosest, monVoyageRcpp)
4+
S3method(plot, salesmanRcpp)
5+
6+
importFrom(Rcpp, evalCpp)
7+
importFrom("graphics", "plot", "segments", "text")
8+
importFrom("stats", "rnorm")
9+
importFrom("combinat", "permn")
10+

R/RcppExports.R

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
2+
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393
3+
4+
#' @title monVoyageRcpp
5+
#' @description
6+
#' This algorithms returns the indices of towns in the shortest order for the Euclidean norm
7+
#' @name monVoyageRcpp
8+
#' @param towns dataframe of towns generated by the `Towns` function
9+
#'
10+
#' @export
11+
monVoyageRcpp <- function(towns) {
12+
.Call(`_salesmanRcpp_monVoyageRcpp`, towns)
13+
}
14+
15+
#' @title monVoyageRcpp
16+
#' @description
17+
#' This algorithms returns the indices of towns in the shortest order for the Euclidean norm using the Held-Karp algorithm
18+
#' @name monVoyageRcpp
19+
#' @param towns dataframe of towns generated by the `Towns` function
20+
#'
21+
#' @export
22+
monVoyageRcppHK <- function(towns) {
23+
.Call(`_salesmanRcpp_monVoyageRcppHK`, towns)
24+
}
25+

R/plot.R

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#' plot.salesmanRcpp
3+
#' @description Plot the result of the gfpop function with the data-points
4+
#' @param x a salesman class object
5+
#' @param ... Other parameters
6+
#' @param towns the dataframe with the towns (X,Y) coordinate
7+
#' @param col the color of the path
8+
#' @return plot the towns and the shortest path
9+
plot.salesmanRcpp <- function(x, ..., towns, col = "blue")
10+
{
11+
n <- dim(towns)[1]
12+
plot(towns[,1], towns[,2], pch = '.', lwd = 0, asp = 1, xlab = "", ylab = "")
13+
text(towns[,1], towns[,2], labels = 1:n)
14+
for(s in 1:(length(x)-1))
15+
{segments(towns[x[s],1], towns[x[s],2], towns[x[s+1],1], towns[x[s+1],2], col = col, lwd = 2)}
16+
}

R/salesman.R

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
## GPL-3 License
2+
## Copyright (c) 2019 Mohamed NIANG
3+
4+
#' Towns coordinate generator
5+
#'
6+
#' @description Genearting towns coordinate using a standard normal distribution
7+
#' @param n number of towns to generate
8+
#' @return a dataframe with 2 columns X et Y with the town coordinates
9+
Towns <- function(n = 6)
10+
{
11+
return(data.frame(X=rnorm(n), Y = rnorm(n)))
12+
}
13+
14+
15+
#' Euclidean distance between two 2d points
16+
#'
17+
#' @description Computing L2 distance between 2 points
18+
#' @param v1 first (X1,Y1) point
19+
#' @param v2 second (X1,Y1) point
20+
#' @return a number = sqrt((X1-X2)^2 + (Y1-Y2)^2)
21+
distance <- function(v1, v2)
22+
{
23+
D <- sqrt(sum((v1-v2)^2))
24+
return(D)
25+
}
26+
27+
28+
#' The shortest route in the travelling salesman problem
29+
#'
30+
#' @description This algorithms returns the indices of towns in the shortest order for the Euclidean norm
31+
#' @param towns dataframe of towns generated by the `Towns` function
32+
#' @return vector of town indices of the shortest path
33+
monVoyageR <- function(towns)
34+
{
35+
n <- dim(towns)[1]
36+
distance <- Inf
37+
res <- NULL
38+
voyages <- permn(2:n)
39+
40+
nbLoops <- factorial(n-1)
41+
for(i in 1:nbLoops)
42+
{
43+
Voy <- c(1,unlist(voyages[i]),1)
44+
D <- 0
45+
for(i in 1:n){D <- D + distance(towns[Voy[i],],towns[Voy[i+1],])}
46+
if(D < distance)
47+
{
48+
res <- Voy
49+
distance <- D
50+
}
51+
}
52+
attr(res, "class") <- "salesman"
53+
return(res)
54+
}
55+
56+
57+
#' A naive algorithm in the travelling salesman problem
58+
#'
59+
#' @description This algorithms returns the indices of towns with a simple closest-town strategy
60+
#' @param towns dataframe of towns generated by the `Towns` function
61+
#' @return vector of town indices of the naive strategy
62+
monVoyageClosest <- function(towns)
63+
{
64+
n <- dim(towns)[1]
65+
indices <- 2:n
66+
res <- 1
67+
68+
for(i in 1:(n-2))
69+
{
70+
D <- Inf
71+
ind <- NULL
72+
for(j in indices)
73+
{
74+
if(distance(towns[res[1],],towns[j,]) < D)
75+
{
76+
D <- distance(towns[res[1],],towns[j,])
77+
ind <- j
78+
}
79+
}
80+
res <- c(ind,res)
81+
indices <- indices[indices!= ind]
82+
}
83+
res <- rev(c(1,indices,res))
84+
attr(res, "class") <- "salesman"
85+
return(res)
86+
}

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# The Salesman Problem
2+
3+
> Master 2 Project. Paris-Saclay University. Simple functions R versus Rcpp.
4+
5+
> Rmd file and its htlm in project folder.

man/Towns.Rd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/distance.Rd

+19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/monVoyageClosest.Rd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/monVoyageR.Rd

+17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/monVoyageRcpp.Rd

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot.salesmanRcpp.Rd

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)