优化Scipy中的模拟双退火序列

0 投票
1 回答
35 浏览
提问于 2025-04-14 15:34

我想优化一组包含两个数值的序列,每个数值都有自己的限制,使用的是来自scipy的模拟双退火方法:

这里有一个序列的例子:

self.sequence_length = 10 
        

self.action_space = [(Bounds(lb=np.pi/12, ub=5*np.pi/9), Bounds(lb=75.0, ub=200.0))] * self.sequence_length

这是一个输出的例子:

[((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0)), ((0.2617993877991494, 1.7453292519943295), (75.0, 200.0))]

所以,每个序列中的元组都有两个数值,并且每个数值都有自己的限制,目标是优化整个序列:

result = dual_annealing(self.func, initial_temp=10000, bounds=self.action_space, maxfun=1000)

但是,出现了以下错误:

Traceback (most recent call last):
  File "main.py", line 98, in <module>
    obj = Sim()
  File "main.py", line 92, in __init__
    result = dual_annealing(self.generate_sequence, initial_temp=10000, bounds=self.action_space, maxfun=1000)
  File "/home/user/miniconda3/envs/sim/lib/python3.8/site-packages/scipy/optimize/_dual_annealing.py", line 639, in dual_annealing
    if (np.any(np.isinf(lower)) or np.any(np.isinf(upper)) or np.any(
TypeError: ufunc 'isinf' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

有没有人能提供一个解决方案呢?

1 个回答

1

这个问题是因为dual_annealing这个函数需要一个包含所有变量的上下限的单一Bounds对象,而不是一个Bounds对象的列表。你可以通过创建一个包含所有变量上下限的单一Bounds对象来解决这个问题。

试试下面这段代码:

self.sequence_length = 10 

lb = [np.pi/12, 75.0] * self.sequence_length
ub = [5*np.pi/9, 200.0] * self.sequence_length

self.action_space = Bounds(lb=lb, ub=ub)

result = dual_annealing(self.func, initial_temp=10000, bounds=self.action_space, maxfun=1000)

撰写回答