如何使用 simanneal 包

How to use simanneal package

我正在尝试通过 simanneal 包 https://github.com/perrygeo/simanneal 使用模拟退火来优化我的 function/object 的参数。 我的代码如下所示:

from simanneal import Annealer

class ReservoirAnnealer(Annealer):

    def __init__(self, state, res):
        self.reservoir = res
        self.resSize = np.size(self.reservoir.W_top)
        super(ReservoirAnnealer, self).__init__(state) 


    def move(self):
        a = random.randint(0, self.resSize)
        b = random.randint(0, self.resSize)
        self.state[a,b] ^= 1

    def energy(self):
        reservoir.W = self.state
        return reservoir.fit(data, 1500, penalty=5e-7, errorEvaluationLength=750)

reservoir = Reservoir(1,1023,spectralRadius=1.25,inputScaling=1,leakingRate=0.3, transientTime=100)
initialState = reservoir.W_top.ravel()
annealer = ReservoirAnnealer(initialState, reservoir)

W_top 是一个内部有 0 或 1 的矩阵,所以像这样:

[ 1.  1.  1. ...,  0.  0.  1.]

当我执行上面的代码时,出现错误:

ValueError                                Traceback (most recent call last)
<ipython-input-87-5a5b9eb8c324> in <module>()
     20 reservoir = Reservoir(1,1023,spectralRadius=1.25,inputScaling=1,leakingRate=0.3, transientTime=100)
     21 initialState = reservoir.W_top.ravel()
---> 22 annealer = ReservoirAnnealer(initialState, reservoir)
     23 #itinearay, cost = annealer.anneal()

<ipython-input-87-5a5b9eb8c324> in __init__(self, state, res)
      6         self.reservoir = res
      7         #self.resSize = np.size(self.reservoir.W)
----> 8         super(ReservoirAnnealer, self).__init__(state)
      9 
     10 

C:\Users\Luca\Anaconda3\lib\site-packages\simanneal\anneal.py in __init__(self, initial_state, load_state)
     45 
     46     def __init__(self, initial_state=None, load_state=None):
---> 47         if initial_state:
     48             self.state = self.copy_state(initial_state)
     49         elif load_state:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

虽然我真的不知道那是什么意思。原始示例使用字典,但我的矩阵非常大,我不想将其放入字典中。 有人知道如何正确使用该库吗?

将列表传递给它。由于 numpy 数组 @ line anneal: 47

上的 if 语句,它失败了