You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GA包调用出现未使用参数错误,TSP与GA结合使用遇阻

Troubleshooting the "unused argument" Error in GA + TSP Implementation

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

  1. Verify GA Package Version:
    Run packageVersion("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 with install.packages("GA") or install a specific version if needed (using the devtools package).

  2. Double-Check Your ga() Call:
    Make sure you're passing distMatrix correctly in the ga() function call. It should be included in the extra arguments (not as a named parameter that's part of the core ga() 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 distMatrix as a parameter.
  • Confirm your ga() call is passing the matrix correctly.

内容的提问来源于stack exchange,提问作者KerJa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:56:28