Function 将函数传递给pyswarm

Function 将函数传递给pyswarm,function,Function,我想知道如何传递从f3()函数获得的变量,而不用在PySarm中多次调用它。基本上,我需要在目标函数中调用f3()来获得斜率,我必须在约束函数中再次调用它来获得最小值 from pyswarm import pso def f1(b): # replace the new value of b def f2(t, s): slope = s[10]-s[0]/t[10]-t[0] min_s = min(s[]) return slope, min_s def f

我想知道如何传递从f3()函数获得的变量,而不用在PySarm中多次调用它。基本上,我需要在目标函数中调用f3()来获得斜率,我必须在约束函数中再次调用它来获得最小值

from pyswarm import pso
def f1(b):
    # replace the new value of b
def f2(t, s):
    slope = s[10]-s[0]/t[10]-t[0]
    min_s = min(s[])
    return slope, min_s
def f3(B):
    t = []
    s = []
    f1(B)       
    Slope, Min_S = f2(t, s) 
    return slope, Min_S 
# ------------------- pyswarm ----------------------
def objective_function(x):
    B = x
    slope, min = F3(B)      # call f3()
    return slope
def con(x):
    B = x
    Slope, Min_S = f2(t, s) # call f3() again !!!!!
    return Min_S
lb = [-3, -1]
ub = [2, 6]
xopt, fopt = pso(objective_function, lb, ub, f_ieqcons=con)

我想问题是你没有定义你的变量,它应该是x[1],x[2]等的形式。 检查此链接上的文档 这个链接呢

我想问题在于您没有定义变量,它应该是x[1],x[2]等形式。 检查此链接上的文档 这个链接呢