python - 如何用循环缩短这个网格移动逻辑(python)

标签 python logic refactoring

我觉得把所有这些都写出来有点愚蠢,因为我知道必须有一种更短的方法来做到这一点。但我就是没有想到。

我想用 for 或 while 循环来缩短它,但我无法想象它会是什么样子。如果您不知道我想要做什么,那就是基本上以随机 9 方向设置更改 2d 列表的值(移动它们)。

def move_agents(agent_location):
 """Randomly generates a movement pattern for each agent and moves them
 accordingly. Agents have 9 possible movements, 8 being directions, and the last
 being not moving"""
 #Generate random directions for each agent
 move_agent_1 = random_gen(2)
 move_agent_2 = random_gen(2)
 move_agent_3 = random_gen(2)
 move_agent_4 = random_gen(2)
 move_agent_5 = random_gen(2)
 move_agent_6 = random_gen(2)

 #All Agent 1 movements
 if (move_agent_1 == 1):
      #Up left
      agent_location[0][0] = int(agent_location[0][0]) - 1 #Row
      agent_location[1][0] = int(agent_location[1][0]) - 1 #Column
 elif (move_agent_1 == 2):
      #Up
      agent_location[0][0] = int(agent_location[0][0]) - 1 #Row
      agent_location[1][0] = int(agent_location[1][0]) #Column

 elif (move_agent_1 == 3):
      #Up Right
      agent_location[0][0] = int(agent_location[0][0]) - 1 #Row
      agent_location[1][0] = int(agent_location[1][0]) + 1 #Column
 elif (move_agent_1 == 4):
      #Left
      agent_location[0][0] = int(agent_location[0][0])  #Row
      agent_location[1][0] = int(agent_location[1][0]) - 1 #Column
 elif (move_agent_1 == 5):
      #No move
      agent_location[0][0] = int(agent_location[0][0]) #Row
      agent_location[1][0] = int(agent_location[1][0]) #Column
 elif (move_agent_1 == 6):
      #Right
      agent_location[0][0] = int(agent_location[0][0]) #Row
      agent_location[1][0] = int(agent_location[1][0]) + 1 #Column
 elif (move_agent_1 == 7):
      #Down left
      agent_location[0][0] = int(agent_location[0][0]) + 1 #Row
      agent_location[1][0] = int(agent_location[1][0]) - 1#Column
 elif (move_agent_1 == 8):
      #Down
      agent_location[0][0] = int(agent_location[0][0]) + 1 #Row
      agent_location[1][0] = int(agent_location[1][0]) #Column
 elif (move_agent_1 == 9):
      #Down right
      agent_location[0][0] = int(agent_location[0][0]) + 1 #Row
      agent_location[1][0] = int(agent_location[1][0]) + 1 #Column

 #All Agent 2 movements
 if (move_agent_2 == 1):
      #Up left
      agent_location[0][1] = int(agent_location[0][1]) - 1 #Row
      agent_location[1][1] = int(agent_location[1][1]) - 1 #Column
 elif (move_agent_2 == 2):
      #Up
      agent_location[0][1] = int(agent_location[0][1]) - 1 #Row
      agent_location[1][1] = int(agent_location[1][1]) #Column
 elif (move_agent_2 == 3):
      #Up Right
      agent_location[0][1] = int(agent_location[0][1]) - 1 #Row
      agent_location[1][1] = int(agent_location[1][1]) + 1#Column
 elif (move_agent_2 == 4):
      #Left
      agent_location[0][1] = int(agent_location[0][1]) #Row
      agent_location[1][1] = int(agent_location[1][1]) -1 #Column
 elif (move_agent_2 == 5):
      #No move
      agent_location[0][1] = int(agent_location[0][1]) #Row
      agent_location[1][1] = int(agent_location[1][1]) #Column
 elif (move_agent_2 == 6):
      #Right
      agent_location[0][1] = int(agent_location[0][1]) #Row
      agent_location[1][1] = int(agent_location[1][1]) + 1 #Column
 elif (move_agent_2 == 7):
      #Down left
      agent_location[0][1] = int(agent_location[0][1]) + 1 #Row
      agent_location[1][1] = int(agent_location[1][1]) - 1#Column
 elif (move_agent_2 == 8):
      #Down
      agent_location[0][1] = int(agent_location[0][1]) #Row
      agent_location[1][1] = int(agent_location[1][1]) + 1 #Column
 elif (move_agent_2 == 9):
      #Down right
      agent_location[0][1] = int(agent_location[0][1]) + 1 #Row
      agent_location[1][1] = int(agent_location[1][1]) + 1 #Column

 #All Agent 3 movements   
 if (move_agent_3 == 1):
      #Up left
      agent_location[0][2] = int(agent_location[0][2]) - 1 #Row
      agent_location[1][2] = int(agent_location[1][2]) - 1 #Column
 elif (move_agent_3 == 2):
      #Up
      agent_location[0][2] = int(agent_location[0][2]) - 1 #Row
      agent_location[1][2] = int(agent_location[1][2]) #Column
 elif (move_agent_3 == 3):
      #Up Right
      agent_location[0][2] = int(agent_location[0][2]) - 1 #Row
      agent_location[1][2] = int(agent_location[1][2]) + 1 #Column
 elif (move_agent_3 == 4):
      #Left
      agent_location[0][2] = int(agent_location[0][2]) #Row
      agent_location[1][2] = int(agent_location[1][2]) - 1 #Column
 elif (move_agent_3 == 5):
      #No move
      agent_location[0][2] = int(agent_location[0][2]) #Row
      agent_location[1][2] = int(agent_location[1][2]) #Column
 elif (move_agent_3 == 6):
      #Right
      agent_location[0][2] = int(agent_location[0][2]) #Row
      agent_location[1][2] = int(agent_location[1][2]) + 1 #Column
 elif (move_agent_3 == 7):
      #Down left
      agent_location[0][2] = int(agent_location[0][2]) + 1 #Row
      agent_location[1][2] = int(agent_location[1][2]) - 1 #Column
 elif (move_agent_3 == 8):
      #Down
      agent_location[0][2] = int(agent_location[0][2]) + 1 #Row
      agent_location[1][2] = int(agent_location[1][2]) #Column
 elif (move_agent_3 == 9):
      #Down right
      agent_location[0][2] = int(agent_location[0][2]) + 1 #Row
      agent_location[1][2] = int(agent_location[1][2]) + 1 #Column

 #All Agent 4 movements
 if (move_agent_4 == 1):
      #Up left
      agent_location[0][3] = int(agent_location[0][3]) - 1 #Row
      agent_location[1][3] = int(agent_location[1][3]) - 1 #Column
 elif (move_agent_4 == 2):
      #Up
      agent_location[0][3] = int(agent_location[0][3]) - 1 #Row
      agent_location[1][3] = int(agent_location[1][3]) #Column
 elif (move_agent_4 == 3):
      #Up Right
      agent_location[0][3] = int(agent_location[0][3]) - 1 #Row
      agent_location[1][3] = int(agent_location[1][3]) - 1 #Column
 elif (move_agent_4 == 4):
      #Left
      agent_location[0][3] = int(agent_location[0][3]) #Row
      agent_location[1][3] = int(agent_location[1][3]) - 1 #Column
 elif (move_agent_4 == 5):
      #No move
      agent_location[0][3] = int(agent_location[0][3]) #Row
      agent_location[1][3] = int(agent_location[1][3]) #Column
 elif (move_agent_4 == 6):
      #Right
      agent_location[0][3] = int(agent_location[0][3]) #Row
      agent_location[1][3] = int(agent_location[1][3]) + 1 #Column
 elif (move_agent_4 == 7):
      #Down left
      agent_location[0][3] = int(agent_location[0][3]) + 1 #Row
      agent_location[1][3] = int(agent_location[1][3]) - 1 #Column
 elif (move_agent_4 == 8):
      #Down
      agent_location[0][3] = int(agent_location[0][3]) + 1 #Row
      agent_location[1][3] = int(agent_location[1][3]) #Column
 elif (move_agent_4 == 9):
      #Down right
      agent_location[0][3] = int(agent_location[0][3]) + 1 #Row
      agent_location[1][3] = int(agent_location[1][3]) + 1 #Column

 #All agent 5 movements
 if (move_agent_5 == 1):
      #Up left
      agent_location[0][4] = int(agent_location[0][4]) - 1 #Row
      agent_location[1][4] = int(agent_location[1][4]) - 1 #Column
 elif (move_agent_5 == 2):
      #Up
      agent_location[0][4] = int(agent_location[0][4]) - 1 #Row
      agent_location[1][4] = int(agent_location[1][4]) #Column
 elif (move_agent_5 == 3):
      #Up Right
      agent_location[0][4] = int(agent_location[0][4]) - 1 #Row
      agent_location[1][4] = int(agent_location[1][4]) + 1 #Column
 elif (move_agent_5 == 4):
      #Left
      agent_location[0][4] = int(agent_location[0][4]) #Row
      agent_location[1][4] = int(agent_location[1][4]) - 1 #Column
 elif (move_agent_5 == 5):
      #No move
      agent_location[0][4] = int(agent_location[0][4]) #Row
      agent_location[1][4] = int(agent_location[1][4]) #Column
 elif (move_agent_5 == 6):
      #Right
      agent_location[0][4] = int(agent_location[0][4]) #Row
      agent_location[1][4] = int(agent_location[1][4]) + 1 #Column
 elif (move_agent_5 == 7):
      #Down left
      agent_location[0][4] = int(agent_location[0][4]) + 1 #Row
      agent_location[1][4] = int(agent_location[1][4]) - 1 #Column
 elif (move_agent_5 == 8):
      #Down
      agent_location[0][4] = int(agent_location[0][4]) + 1 #Row
      agent_location[1][4] = int(agent_location[1][4]) #Column
 elif (move_agent_5 == 9):
      #Down right
      agent_location[0][4] = int(agent_location[0][4]) + 1 #Row
      agent_location[1][4] = int(agent_location[1][4]) + 1 #Column

 #All agent 6 movements    
 if (move_agent_6 == 1):
      #Up left
      agent_location[0][5] = int(agent_location[0][5]) - 1 #Row
      agent_location[1][5] = int(agent_location[1][5]) - 1 #Column
 elif (move_agent_6 == 2):
      #Up
      agent_location[0][5] = int(agent_location[0][5]) - 1 #Row
      agent_location[1][5] = int(agent_location[1][5]) #Column
 elif (move_agent_6 == 3):
      #Up Right
      agent_location[0][5] = int(agent_location[0][5]) - 1 #Row
      agent_location[1][5] = int(agent_location[1][5]) + 1 #Column
 elif (move_agent_6 == 4):
      #Left
      agent_location[0][5] = int(agent_location[0][5]) #Row
      agent_location[1][5] = int(agent_location[1][5]) - 1 #Column
 elif (move_agent_6 == 5):
      #No move
      agent_location[0][5] = int(agent_location[0][5]) #Row
      agent_location[1][5] = int(agent_location[1][5]) #Column
 elif (move_agent_6 == 6):
      #Right
      agent_location[0][5] = int(agent_location[0][5]) #Row
      agent_location[1][5] = int(agent_location[1][5]) + 1 #Column
 elif (move_agent_6 == 7):
      #Down left
      agent_location[0][5] = int(agent_location[0][5]) + 1 #Row
      agent_location[1][5] = int(agent_location[1][5]) - 1 #Column
 elif (move_agent_6 == 8):
      #Down
      agent_location[0][5] = int(agent_location[0][5]) + 1 #Row
      agent_location[1][5] = int(agent_location[1][5]) #Column
 elif (move_agent_6 == 9):
      #Down right
      agent_location[0][5] = int(agent_location[0][5]) + 1 #Row
      agent_location[1][5] = int(agent_location[1][5]) + 1 #Column

最佳答案

好的,让我们进一步改进。

move_agent[x] 范围从 1 到 9 给出以下模式:

123
456
789

因此,如果您的代理移动,就好像它从字段 5 开始,然后转到编号为 move_agent[x] 的字段

所以,这是它移动的方向:

deltaX=(move_agent[x]-1)%3 -1
deltaX=(move_agent[x]-1)/3 -1

它为您提供了以下模式:

   deltaX              deltaY
-1   0   +1         -1   -1   -1
-1   0   +1          0    0    0
-1   0   +1         +1   +1   +1

现在,这是代码:

for x in range(0,6):
    deltaX=(move_agent[x]-1)%3 -1
    deltaX=(move_agent[x]-1)/3 -1
    agent_location[0][x] = int(agent_location[0][x]) + deltaX #Row
    agent_location[1][x] = int(agent_location[1][x]) +deltaY  #Column

所以, Action 被浓缩为5行。您可以用计算本身替换最后几行中的 deltaX/Y 并节省另外两行...

关于python - 如何用循环缩短这个网格移动逻辑(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27033769/

相关文章:

c++ - 如何将 map 和 unordered_map 作为同一个对象来处理

python - 是否可以在从 Jenkins 启动的 vagrant box 中运行测试套件?

Java位比较,bitset?

algorithm - N-Array Tree 有些逻辑看不懂

c# - 处理合并的业务和表示代码的最佳方式?

java - 空抽象类是一种不好的做法吗?为什么?

python - 为什么在使用过滤器时 "return s and s.strip()"有效?

python父类 'wrapping'子类方法

python - 将 pandas DataFrame 或经度和纬度列表转换为 python 中的 gpx 文件

java - 最大余数