2D网格的Python数据结构建议

问题描述 投票:4回答:3

我希望在Python中实现一个蚁群优化算法,虽然它是Python和面向对象编程的新手,所以学习曲线相当陡峭。在这一点上,我被困在如何解决以下情况:

  • 当蚂蚁在2D网格中走动时,它们会遇到障碍物,其他蚂蚁的信息素沉积物,食物等。我用什么数据结构来表示这个2D世界以及每个细胞的上述属性?

我曾尝试过2D阵列,认为array[x-coord][y-coord]可以指向具有适当属性{} (dictionary)(Obstacle: 'Yes / 'No', Pheromone Level: X %, etc.)。不幸的是,尽管NumPy允许我创建一个2D数组,但我无法将字典对象分配给各种坐标。

from numpy import *

myArray = array([[1,2,3,4],
                 [5,6,7,8],
                 [9,10,11,12]])

myArray[2][2]={}

返回:

Traceback (most recent call last):
  File "/Users/amormachine/Desktop/PythonTest.py", line 7, in <module>
    myArray[2][2]={}
TypeError: long() argument must be a string or a number, not 'dict'
[Finished in 0.6s with exit code 1]

我不是致力于字典或这个实施这个项目的范例,并且肯定会欣赏该集团的智慧。

python data-structures
3个回答
1
投票

确定你可以,你只是不知道你的dtype是否为int ...所以使用对象制作你的数组,你可以使用对象......

In [43]: a = [[{},{},{}],[{},{},{}]]

In [44]: a = numpy.array(a)

In [45]: a[1][1] = {'hello':'world','something':5}

In [46]: a
Out[46]:
array([[{}, {}, {}],
       [{}, {'hello': 'world', 'something': 5}, {}]], dtype=object)

虽然不确定使用numpy对象会获得什么,但最好不要将其作为列表列表


0
投票

在普通的Python中,我会选择list-of-dicts方法,但是对于NumPy,我发现使用不同属性的单独数组更自然,而不是试图将事物保存在一个结构中。

import numpy as np

grid_shape = (120,80)

# example of random initialization with this grid shape
pheremone_level = np.random.rand(*grid_shape)
obstacle = np.random.rand(*grid_shape) > 0.8

正如@bitwise所说,它完全取决于你想要执行的操作。通常,NumPy中的“正确”方式将更接近于在Matlab中编写它而非非NumPy Python的方式。不幸的是我不熟悉Ant Colony Optimization的工作方式,所以我不能说什么更合适。


0
投票

我正在寻找与结构化2D网格相关的东西,谷歌引导我到这个页面。

虽然我的解决方案并不完全与问题中提到的网格相关,但我不想重复“结构化二维网格”数据结构的问题,我在这里发布我的解决方案。我希望它对搜索2D结构化网格并在搜索引擎中重定向的观众有用

注意:该方法仅返回单元格顶点和每个单元格的顶点连接。通过添加其他例程,可以轻松生成应用程序所需的其他数量,如单元体积,单元质心,外接圆,外圆等

import numpy as np
import matplotlib.pyplot as plt

def create_structured_grid(corner1=None, corner2=None, nx=5, ny=5, plt_=True, annotate=True):
   """
   creates a structured grid of rectangular lattice

   input:
   ------
       corner1 : [x_start, y_start]
       corner2 : [x_end, y_end]
       nx      : numpts in x
       ny      : numpts in y
       plt_    : boolean whether to plot or not
       annotate: whether to annotate the grid points or not
   output:
   -------
       vertex_array : numpy.array((numpts, dim),dtype=float) of vertices
       connectivity : numpy.array((num_cells, 2**dim), dtyp=int) of
                   vertex connectivity for each cell
       plots   : additionally plots if boolean values are true
   """
   #corner1 = np.array([0.0, 0.0])
   #corner2 = np.array([1.0, 1.0])
   dim = len(corner1) #currently only for 2D,
   x_pts = np.linspace(corner1[0], corner2[0], nx)
   y_pts = np.linspace(corner1[1], corner2[1], ny)

   Xv, Yv = np.meshgrid(x_pts, y_pts)
   numpts = nx*ny
   vertex_array = np.zeros((numpts, 2), dtype=float)

   vertex_array[:,0] = np.reshape(Xv, numpts)
   vertex_array[:,1] = np.reshape(Yv, numpts)

   num_cells = int(nx-1)*(ny-1)
   connectivity = np.zeros((num_cells, int(2**dim)), dtype=int)

   rows = ny-1
   cols = nx-1
   for row in range(rows):
       for col in range(cols):
           num = nx*row + col
           connectivity[cols*row + col] = [num+0, num+1, num+nx, num+nx+1]

   if plt_:
       X,Y = vertex_array.T
       fig = plt.figure()
       ax = fig.add_subplot(111)
       ax.set_aspect('equal')
       plt.scatter(X,Y, marker='o', s=50, color='g', alpha=1.0)
       plt.plot(Xv,Yv, linewidth=2, color='k')
       plt.plot(Yv,Xv, linewidth=2, color='k')
       if annotate:
           for idx, cc in enumerate(vertex_array):
               plt.text(cc[0], cc[1],  str(idx), color='k', verticalalignment='bottom', horizontalalignment='right', fontsize='medium')
       plt.show(block=False)

   return vertex_array, connectivity

对函数的调用可以是这样的:

c1 = np.array([0.0, 0.0])
c2 = np.array([1.0, 1.0])
vertices, connctivity = create_structured_grid(corner1=c1, corner2=c2, nx=4, ny=4)

vertices = array([[ 0.        ,  0.        ],
                  [ 0.33333333,  0.        ],
                  [ 0.66666667,  0.        ],
                  [ 1.        ,  0.        ],
                  [ 0.        ,  0.33333333],
                  [ 0.33333333,  0.33333333],
                  [ 0.66666667,  0.33333333],
                  [ 1.        ,  0.33333333],
                  [ 0.        ,  0.66666667],
                  [ 0.33333333,  0.66666667],
                  [ 0.66666667,  0.66666667],
                  [ 1.        ,  0.66666667],
                  [ 0.        ,  1.        ],
                  [ 0.33333333,  1.        ],
                  [ 0.66666667,  1.        ],
                  [ 1.        ,  1.        ]])
connectivity = array([[ 0,  1,  5,  6],
                      [ 1,  2,  6,  7],
                      [ 2,  3,  7,  8],
                      [ 4,  5,  9, 10],
                      [ 5,  6, 10, 11],
                      [ 6,  7, 11, 12],
                      [ 8,  9, 13, 14],
                      [ 9, 10, 14, 15],
                      [10, 11, 15, 16]])

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.