如何在搜索"算法"中编写"If only one variable exist"代码?



我使用了一种DIY网格搜索"算法",从左上角开始,到右下角结束。我的"算法"只能向右和向下移动,它观察邻居,看看哪个最小,然后移动到最小。如果两个邻居都相等,那么优先考虑下降。以下是我的代码:

import numpy as np
import random
# Create grid with random integers from 0 to 9
width, height = 10, 10
np.random.seed(0)
grid = np.random.randint(0, 10, size=(width, height))
# Agent actions
goal = grid[-1][-1] # Row, Column
# Check mechanism
print(grid)
print(goal)
def rightdown():
x, y = 0, 0
current_pos = grid[x][y]
steps = [grid[0][0]]
print("Initial pos: ", current_pos)


for i in range(16): 
if grid[x + 1][y] < grid[x][y + 1]: # Go Right
current_pos = grid[x + 1][y]

x += 1

elif grid[x + 1][y] > grid[x][y + 1]: # Go Down
current_pos =  grid[x][y + 1]

y += 1

elif grid[x + 1][y] == grid[x][y + 1]: # Go Down if same
current_pos = grid[x][y + 1]

x += 1

elif grid[0][9] or grid[1][9] or grid[2][9] or grid[3][9] or grid [4][9] or grid [5][9] or grid [6][9] or grid [7][9] or grid [8][9] or grid [9][9]:
print('right hit')

elif grid[9][0] or grid[9][1] or grid[9][2] or grid[9][3] or grid [9][4] or grid [9][5] or grid [9][6] or grid [9][7] or grid [9][8] or grid [9][9]:
print('bott hit')

print('Current position: ', current_pos)
print('This is x, y: ', x, ',', y, 'n')    

steps.append(current_pos)

print('Total steps taken: ', sum(steps))


rightdown()

我的问题是:我想让它一旦到达数组的末尾(例如右(,它只会下降。但是,我该如何对此进行编码?在英语中,这意味着如果只有右数组存在,向下移动,而不是同时存在右数组和下数组。

EDIT_1:我编辑了代码,看看计数器的位置是到达最下面的一行还是最右边的一列,但是下面的代码现在给出了一个错误:

索引10超出大小为10 的轴0的界限

要绕过此项,请将范围从16更改为15或更小。使用了16,因为它正在迭代到下一个数字。

对于范围(15(中的i:

当以下代码位于以下位置[6][9]时,为什么不打印"right hit"?

您的代码中有几个特殊之处:

  • 您需要检查索引,这样它们就不会试图访问给定数组之外的内容
  • 你需要检查你是否可以提前索引或在移动时越界
  • 比较索引下的值的唯一时间是在决定下一步移动到哪里时:elif grid[0][9] or grid[1][9] .... or grid [9][9]: print('right hit')访问值并检查是否有不真实的值-它不检查索引位置
  • sum of steps()将汇总所有访问的值,而不是计算步数(可能是有意的(

你可以这样修复:

def rightdown(g):
row, column, (max_row, max_column) = 0, 0, g.shape
print(g)
last_pos = -1,-1
current_pos = row,column
current_value = g[row][column]
steps = [current_value]
def p(): #  printer function called twice later: dry - don't repeat yourself
msg = "hit bottom" if row+1 == max_row else (
"hit right" if column+1 == max_column else "")
print("Pos:", current_pos, "  Value:", g[row][column], msg)

while current_pos != (max_row - 1, max_column - 1):
p()
if row+1 < max_row and column+1 < max_column:
vd = g[row + 1][column]
vr = g[row][column + 1]
if vd <= vr:
row += 1
else:
column += 1
elif row+1 == max_row:
column += 1
elif column+1 == max_column:
row += 1
steps.append(g[row][column])
last_pos, current_pos = current_pos, (row, column)
p()
print('Steps', steps, 'taken: ', len(steps), "sum:", sum(steps))

称之为:

import numpy as np
import random
# Create grid with random integers from 0 to 9
width, height = 10, 10
np.random.seed(0)
grid = np.random.randint(0, 10, size=(width, height))
print(grid)
rightdown(grid)

输出:

[[5 0 3 3 7 9 3 5 2 4]
[7 6 8 8 1 6 7 7 8 1]
[5 9 8 9 4 3 0 3 5 0]
[2 3 8 1 3 3 3 7 0 1]
[9 9 0 4 7 3 2 7 2 0]
[0 4 5 5 6 8 4 1 4 9]
[8 1 1 7 9 9 3 6 7 2]
[0 3 5 9 4 4 6 4 4 3]
[4 4 8 4 3 7 5 5 0 1]
[5 9 3 0 5 0 1 2 4 2]]
Pos: (0, 0)   Value: 5
Pos: (0, 1)   Value: 0
Pos: (0, 2)   Value: 3
Pos: (0, 3)   Value: 3
Pos: (0, 4)   Value: 7
Pos: (1, 4)   Value: 1
Pos: (2, 4)   Value: 4
Pos: (3, 4)   Value: 3
Pos: (3, 5)   Value: 3
Pos: (4, 5)   Value: 3
Pos: (4, 6)   Value: 2
Pos: (5, 6)   Value: 4
Pos: (5, 7)   Value: 1
Pos: (5, 8)   Value: 4
Pos: (6, 8)   Value: 7
Pos: (6, 9)   Value: 2 hit right
Pos: (7, 9)   Value: 3 hit right
Pos: (8, 9)   Value: 1 hit right
Pos: (9, 9)   Value: 2 hit bottom
Steps [5, 0, 3, 3, 7, 1, 4, 3, 3, 3, 2, 4, 1, 4, 7, 2, 3, 1, 2] taken: 19 sum: 58

相关内容

最新更新