1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#######################################################################
# TSP - Traveling Salesperson Problem
# Copyrigth (C) 2011 Michael Hahsler and Kurt Hornik
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
## insert a dummy city
##generic
insert_dummy <- function(x, n = 1, const = 0, inf = Inf, label = "dummy")
UseMethod("insert_dummy")
## use insert dummy from ATSP
insert_dummy.TSP <- function(x, n = 1, const = 0, inf = Inf, label = "dummy") {
x <- insert_dummy(ATSP(x), n, const, inf, label)
TSP(x)
}
insert_dummy.ATSP <- function(x, n = 1, const = 0, inf = Inf, label = "dummy") {
method <- attr(x, "method")
n <- as.integer(n)
p <- n_of_cities(x)
if(length(label) == 1 && n > 1) label = rep(label, n)
## add dummy rows/columns
x <- cbind(x, matrix(const, ncol = n, nrow = p,
dimnames = list(NULL, label)))
x <- rbind(x, matrix(const, ncol = p+n, nrow = n,
dimnames = list(label, NULL)))
## place inf between dummies
if(n>1) {
x[(p+1):(p+n), (p+1):(p+n)] <- inf
diag(x[(p+1):(p+n), (p+1):(p+n)]) <- 0
}
attr(x, "method") <- method
ATSP(x)
}
insert_dummy.ETSP <- function(x, n = 1, const = 0, inf = Inf, label = "dummy")
stop("Dummy cities cannot be used with ETSP!")
|