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

2D增强随机游走代码索引错误排查及顶点访问次数可视化实现求助

Fixing Your 2D Reinforced Random Walk Code & Visualizing Visit Counts

Let's break down your issues step by step and get your code working as intended.

1. Resolving Index & Reinforcement Logic Errors

You're running into index errors because of two key mistakes:

a. Incorrect Matrix Dimension Checks

In your boundary condition checks, you're using length(Grille) which returns the total number of elements in the matrix (e.g., 25 for a 5x5 grid), not the number of rows or columns. This means your boundary conditions never trigger correctly, leading to attempts to access indices like Grille[i+1,j] when i is already the last row.

Replace all instances of:

  • length(Grille) with nrow(Grille) when checking row boundaries (i values)
  • length(Grille) with ncol(Grille) when checking column boundaries (j values)

b. Missing Grille State Persistence

In R, function arguments are passed by value, so when you modify Grille inside Next_step, those changes don't carry over to the RW_2d function. This means your walk never actually uses the reinforced history—you're always using the initial all-1s grid. To fix this, have Next_step return both the new position and the updated Grille.

Here's the corrected Next_step function:

Next_step = function(i,j,Grille){ 
  l_rows = nrow(Grille) 
  l_cols = ncol(Grille)
  prob = matrix(NA, nrow = l_rows, ncol = l_cols) 
  
  if ((i==1)&&(1<j)&&(j<l_cols)) { 
    denom = Grille[i,j-1]+Grille[i,j+1]+Grille[i+1,j]
    prob[i+1,j] = Grille[i+1,j]/denom 
    prob[i,j+1] = Grille[i,j+1]/denom 
    prob[i,j-1] = Grille[i,j-1]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(2,3,4), 1, replace = TRUE, prob = c(prob[i,j-1],prob[i+1,j],prob[i,j+1])) 
  } else if ((j==1)&&(1<i)&&(i<l_rows)){ 
    denom = Grille[i,j+1]+Grille[i-1,j]+Grille[i+1,j]
    prob[i+1,j] = Grille[i+1,j]/denom 
    prob[i,j+1] = Grille[i,j+1]/denom 
    prob[i-1,j] = Grille[i-1,j]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(1,3,4), 1, replace = TRUE, prob = c(prob[i-1,j],prob[i+1,j],prob[i,j+1])) 
  } else if ((i==1)&&(j==1)){ 
    denom = Grille[i,j+1]+Grille[i+1,j]
    prob[i+1,j] = Grille[i+1,j]/denom 
    prob[i,j+1] = Grille[i,j+1]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(3,4), 1, replace = TRUE, prob = c(prob[i+1,j],prob[i,j+1])) 
  } else if ((i==l_rows)&&(1<j)&&(j<l_cols)){ 
    denom = Grille[i,j-1]+Grille[i,j+1]+Grille[i-1,j]
    prob[i,j+1] = Grille[i,j+1]/denom 
    prob[i,j-1] = Grille[i,j-1]/denom 
    prob[i-1,j] = Grille[i-1,j]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(1,2,4), 1, replace = TRUE, prob = c(prob[i-1,j],prob[i,j-1],prob[i,j+1])) 
  } else if ((j==l_cols)&&(1<i)&&(i<l_rows)) { 
    denom = Grille[i,j-1]+Grille[i-1,j]+Grille[i+1,j]
    prob[i+1,j] = Grille[i+1,j]/denom 
    prob[i,j-1] = Grille[i,j-1]/denom 
    prob[i-1,j] = Grille[i-1,j]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(1,2,3), 1, replace = TRUE, prob = c(prob[i-1,j],prob[i,j-1],prob[i+1,j])) 
  } else if ((i==l_rows)&&(j==l_cols)){ ## dernière case en bas à droite 
    denom = Grille[i,j-1]+Grille[i-1,j]
    prob[i,j-1] = Grille[i,j-1]/denom 
    prob[i-1,j] = Grille[i-1,j]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(1,2), 1, replace = TRUE, prob = c(prob[i-1,j],prob[i,j-1])) 
  } else if ((i==1)&&(j==l_cols)) { 
    denom = Grille[i,j-1]+Grille[i+1,j]
    prob[i+1,j] = Grille[i+1,j]/denom 
    prob[i,j-1] = Grille[i,j-1]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(2,3), 1, replace = TRUE, prob = c(prob[i,j-1],prob[i+1,j])) 
  } else if ((i==l_rows)&&(j==1)) { 
    denom = Grille[i,j+1]+Grille[i-1,j]
    prob[i,j+1] = Grille[i,j+1]/denom 
    prob[i-1,j] = Grille[i-1,j]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(1,4), 1, replace = TRUE, prob = c(prob[i-1,j],prob[i,j+1])) 
  } else { 
    denom = Grille[i,j-1]+Grille[i,j+1]+Grille[i-1,j]+Grille[i+1,j]
    prob[i+1,j] = Grille[i+1,j]/denom 
    prob[i,j+1] = Grille[i,j+1]/denom 
    prob[i,j-1] = Grille[i,j-1]/denom 
    prob[i-1,j] = Grille[i-1,j]/denom 
    Grille[i,j] = Grille[i,j]+1 
    next_step = sample(c(1,2,3,4), 1, replace = TRUE, prob = c(prob[i-1,j],prob[i,j-1],prob[i+1,j],prob[i,j+1])) 
  } 
  
  # Update position based on next_step
  if (next_step == 1) { # up 
    i = i-1 
  } else if (next_step ==2) { # left 
    j = j-1 
  } else if (next_step == 3) { # down 
    i = i+1 
  } else if (next_step == 4) { # right 
    j = j+1 
  } 
  
  # Return both new position and updated Grille
  return(list(pos = c(i,j), grille = Grille)) 
} 

2. Tracking & Visualizing Vertex Visit Counts

Your original attempt to track visits with sommets had issues with matrix initialization and dimension consistency. Instead, we'll use a dedicated count matrix to track how many times each vertex is visited, then visualize it with a heatmap.

Here's the corrected RW_2d function that tracks visits and returns the count matrix:

RW_2d = function(nRow,nCol,pas){ 
  # Initialize reinforcement grid and visit count matrix
  Grille = matrix(1, nrow = nRow, ncol = nCol) 
  visit_counts = matrix(0, nrow = nRow, ncol = nCol)
  
  # Initialize starting position
  current_i = sample(1:nRow,1) 
  current_j = sample(1:nCol,1) 
  visit_counts[current_i, current_j] = 1 # Count starting position
  
  # Store path for plotting
  axe_x = rep(NA, pas)
  axe_y = rep(NA, pas)
  axe_x[1] = current_i
  axe_y[1] = current_j
  
  # Run the walk
  for (step in 2:pas){ 
    # Get next position and updated Grille
    result = Next_step(current_i, current_j, Grille)
    current_i = result$pos[1]
    current_j = result$pos[2]
    Grille = result$grille
    
    # Update visit counts and path
    visit_counts[current_i, current_j] = visit_counts[current_i, current_j] + 1
    axe_x[step] = current_i
    axe_y[step] = current_j
  } 
  
  # Plot the path
  plot(axe_x, axe_y, type = 'l', main = paste("2D Reinforced Random Walk (", pas, " steps)"),
       xlab = "Row Index", ylab = "Column Index", xlim = c(1, nRow), ylim = c(1, nCol))
  
  # Return visit counts for visualization
  return(visit_counts)
} 

Visualizing Visit Counts

After running RW_2d, you can use the returned visit count matrix to create a heatmap:

# Run the walk and get visit counts
counts_10x10_1000 = RW_2d(10,10,1000)

# Create a heatmap of visit counts
heatmap(counts_10x10_1000, Rowv = NA, Colv = NA, scale = "none",
        main = "Visit Frequency of Each Vertex",
        xlab = "Column", ylab = "Row",
        col = viridis::viridis(20)) # Using viridis for better color contrast (install if needed)

# Alternatively, use image() for a simpler plot
image(t(counts_10x10_1000), axes = TRUE, main = "Visit Frequency",
      xlab = "Column", ylab = "Row", col = viridis::viridis(20))

Testing the Fixed Code

You can now run your original test cases without errors:

RW_2d(5,5,6) 
RW_2d(10,10,100) 
RW_2d(10,10,500) 
RW_2d(10,10,1000) 
RW_2d(100,100,1000)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 19:12:46