0

差分进化算法的 SciPy实现是否有最大变量数?我的代码适用于具有 8 个变量的问题的玩具版本,但是当我尝试使用 4000 个变量优化实际问题时,目标函数始终返回无穷大值。

代码(有关输入文件,请参阅 GitHub存储库)

import numpy as np
from scipy.optimize import differential_evolution as de
from scipy.optimize import NonlinearConstraint as nlc

def kf(x, w, freq):
    kc = x>0
    kw = ~np.any(w[~kc,:], axis=0)
    return -freq[kw].sum()

def cons_fun(x):
    return np.sum(x>0)

def optimize(w, freq):
    cons = nlc(cons_fun, -np.inf, 1000)
    bnds = [np.array([-1,1]),]*w.shape[0]
    res = de(kf, args=(w, freq), maxiter=1000, bounds=bnds, popsize=2, polish=False,
             constraints=cons, disp=True, workers=-1, updating='deferred')
    output = res.x>0
    np.save('output.npy', output)

if __name__ == '__main__':
    # try optimizing toy version of problem
    small_w = np.load('small_w.npy')
    small_freq = np.load('small_freq.npy')
    optimize(small_w, small_freq)
    
    # try optimizing actual problem
    w = np.load('w.npy')
    freq = np.load('freq.npy')
    optimize(w, freq)

实际问题的程序输出

differential_evolution step 1: f(x)= inf
differential_evolution step 2: f(x)= inf
differential_evolution step 3: f(x)= inf

...等等数百个步骤

有关优化问题的更多信息

我正在尝试确定一组 1000 个汉字,以最大限度地提高你写常用单词的能力。该数组w是一个稀疏布尔矩阵,形状为 4000(潜在字符数)乘以 30000(字数)。w如果对应于该行的字符出现在对应于该列的单词中,则元素 of为真。该数组freq是一个长度为 30000 的向量,其中包含词频值。

目标函数kf将 4000 个元素的数组x作为其参数。该数组x包含介于 -1 和 1 之间的值。试用字符集由 中的正元素确定x。非线性约束将正元素的数量限制x为 1000。

4

1 回答 1

0

中可以使用的变量数量没有限制differential_evolution

对于differential_evolution目标函数的约束最小化,仅在约束可行时才进行评估。这样就不会将计算时间浪费在试验解决方案上。

如果满足以下条件,则接受试用解决方案:

        * it satisfies all constraints and provides a lower or equal objective
          function value, while both the compared solutions are feasible
        - or -
        * it is feasible while the original solution is infeasible,
        - or -
        * it is infeasible, but provides a lower or equal constraint violation
          for all constraint functions.

您是否调查过您的约束函数以检查是否有可能在bounds?

于 2021-01-05T04:13:47.103 回答