GA包调用出现未使用参数错误,TSP与GA结合使用遇阻
Hey there! Let's work through this problem together—this error is super common when working with the GA package, and it's usually not the package itself that's the issue. Here's what's going on and how to fix it:
Core Issue: Your Fitness Function Isn't Accepting the distMatrix Parameter
The error Error in fitness(Pop[i, ], ...) : unused argument (distMatrix = c(0, 1167,...)) means exactly what it says: you're passing a distMatrix parameter to your fitness function, but that function isn't set up to receive it.
When you call ga() and include extra arguments (like distMatrix) in the ... section, the GA package tries to pass those arguments directly to your fitness function. If your fitness function doesn't list distMatrix as one of its inputs, R throws this error.
Fix: Update Your Fitness Function to Include distMatrix
For example, your fitness function should look something like this (adjust based on your TSP logic):
# Correct fitness function that accepts distMatrix tsp_fitness <- function(path, distMatrix) { # Calculate total distance of the path (close the loop by returning to start) total_distance <- sum(distMatrix[cbind(path, c(path[-1], path[1]))]) # GA maximizes fitness, so return inverse of distance (smaller distance = higher fitness) return(1 / total_distance) }
If your current function only has path as an argument (e.g., tsp_fitness <- function(path) { ... }), that's why the error happens—you're trying to pass distMatrix to a function that doesn't expect it.
Why RStudio Doesn't Show the distMatrix Parameter
RStudio's auto-completion only shows parameters that are explicitly defined in your function. If your fitness function doesn't list distMatrix as an input, RStudio has no way to know you plan to pass it. The example you referenced probably has a fitness function that includes distMatrix as a parameter, which is why that user's setup works.
Additional Checks
Verify GA Package Version:
RunpackageVersion("GA")to check your version. If the example you used was written for a much older/newer version of GA, there might be subtle changes in how arguments are passed. You can update GA withinstall.packages("GA")or install a specific version if needed (using thedevtoolspackage).Double-Check Your
ga()Call:
Make sure you're passingdistMatrixcorrectly in thega()function call. It should be included in the extra arguments (not as a named parameter that's part of the corega()function). For example:# Correct ga() call with distMatrix passed to fitness function ga_solution <- ga(type = "permutation", fitness = tsp_fitness, lower = 1, upper = nrow(distMatrix), # Number of cities popSize = 50, maxiter = 100, distMatrix = distMatrix) # This gets passed to your fitness function
Quick Recap
- The error isn't with the GA package itself—it's a mismatch between the arguments you're passing and what your fitness function accepts.
- Update your fitness function to include
distMatrixas a parameter. - Confirm your
ga()call is passing the matrix correctly.
内容的提问来源于stack exchange,提问作者KerJa

