如果目标函数有太多的参数,SciPy差分进化将失败

2024-04-19 13:12:30 发布

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

差分进化算法的SciPyimplementation是否具有最大数量的变量?我的code处理8个变量的问题的玩具版本,但是当我尝试优化4000个变量的实际问题时,目标函数始终返回无穷大的值

代码(有关输入文件,请参见GitHubrepo

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的元素为true。数组freq是一个长度为30000的向量,包含单词频率值

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


Tags: import元素defasnpload数组inf
1条回答
网友
1楼 · 发布于 2024-04-19 13:12:30

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

对于具有differential_evolution的约束极小化,目标函数仅为evaluated if the constraints are feasible。这样,计算时间就不会浪费在试验解决方案上

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

        * 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中创建可行的解决方案

相关问题 更多 >