Error in sample.int(length(x), size, replace, prob) :
too few positive probabilities
The code is below:
M = 100000;
costMatrix = as.matrix(rbind(
c( 0,12,10, M, M, M,12),
c(12, 0, 8,12, M, M, M),
c(10, 8, 0,11, 3, M, 9),
c( M,12,11, 0,11,10, M),
c( M, M, 3,11, 0, 6, 7),
c( M, M, M,10, 6, 0, 9),
c(12, M, 9, M, 7, 9, 0)));
numcities = 7;
I get this error in 3.0.3, but not in 3.0.2:
Error in sample.int(length(x), size, replace, prob) : too few positive probabilities
The code is below:
M = 100000; costMatrix = as.matrix(rbind( c( 0,12,10, M, M, M,12), c(12, 0, 8,12, M, M, M), c(10, 8, 0,11, 3, M, 9), c( M,12,11, 0,11,10, M), c( M, M, 3,11, 0, 6, 7), c( M, M, M,10, 6, 0, 9), c(12, M, 9, M, 7, 9, 0))); numcities = 7;
given a tour, calculate the total cost
tourCost <- function(tour, costMatrix) { tour <- c(tour, tour[1]) route <- embed(tour, 2)[, 2:1] sum(costMatrix[route]) }
inverse of the total distance is the fitness
tspFitness <- function(tour, ...) 1/tourCost(tour, ...) require(GA) result <- ga(type = "permutation", fitness = tspFitness, costMatrix = costMatrix, min = 1, max = numcities, popSize = 10, maxiter = 500, run = 100, pmutation = 0.2 , monitor = NULL) soln <- as.vector(result@solution[1,]) # use first soln tourCost(soln,costMatrix) tour <- c(soln,result@solution[1]); tour # approx best tour