以矢量化的方式将整数数组映射到唯一标识符的另一种方法

2024-10-01 17:31:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在一些排队和库存问题中,系统的状态可以用一维整数数组(长度恒定)来表示。现在我想将状态映射到一个唯一的数字/字符串,以便快速识别它们。在

目前,我正在使用以下方法:

import numpy as np

#Example: state space consists of all integer arrays with 3 elements ranging from 0 to 4.
statespace      = np.indices([5]*3).reshape(3,-1).T 

#Obtain unique integers by converting to a number system with the smallest possible base. 
minvalues       = np.amin(statespace,axis=0)
maxvalues       = np.amax(statespace,axis=0)
base            = 1+np.max(maxvalues-minvalues)        
statecode       = np.power(base, np.arange(statespace.shape[1]))

def getStateCode(state):
    #Convert states to a unique integer by taking the dot product with statecode.
    return np.dot(state-minvalues,statecode)

#Obtain codes and sort them.    
codes = np.sort(getStateCode(statespace))  

def getStateIndex(state):
    #Searches for the state in the sorted vector with codes.
    statecodes  = getStateCode(state)
    return np.searchsorted(codes,statecodes).astype(int)  

现在

^{pr2}$

返回状态索引0。在

对于中小型问题,这种方法效果很好,而且是矢量化的。但是,对于较大的问题,它会遭受整数溢出的影响,特别是当base很大时。在

当状态空间中的某些元素的范围比其他元素大时,尤其会发生这种情况(例如,第一个元素的范围可能是0到100,而所有其他元素的范围都在0到3之间,从而形成一个基101系统)。即使对于容易存储在内存中的状态空间,这也可能导致statecode的整数溢出。将statecode设为64位整数只会延迟问题。在

有人有其他选择吗(矢量化?)如何将一维整数数组转换为唯一标识符而不出现此问题?由于这些比较是在数以百万计的国家中反复进行的,因此必须采取快速的方法。我已经读过散列函数,但是我在使用这些函数时有点犹豫,因为不能保证唯一性。在


Tags: theto方法元素base状态withnp

热门问题