pythonfunctionparticle-swarm

How to pass a variable to pyswarm's functions in Python


I wonder how can I pass a specific variable to a function using Pyswarm in Python.

Check the example bellow

from pyswarm import pso

def model_obj(x):
    return x[0]

def cons(x):
    return x[0] - MY_VAR

lb = [0]
ub = [100]
x0 = [0]
MY_VAR = 10

xopt, fopt = pso(model_obj,lb,ub,x0,f_ieqcons=cons)

My question is how do I pass MY_VAR to the function cons.

Thank you!


Solution

  • I found the solution using *args https://pythonhosted.org/pyswarm/