2

I am applying PSO in an optimization problem. I have a cost function c(x), in which x is a n dimensional array. Using pyswarms it is possible to calculate the minimum using

swarm_size = 200
dim = len(X)      # Dimension of X
epsilon = 1.0
options = {'c1': 1.7, 'c2':1.7, 'w':0.729}
bdmin = np.array(bdmin)
bdmax = np.array(bdmax)


constraints = (bdmin,bdmax)
optimizer = ps.single.GlobalBestPSO(n_particles=swarm_size,
                                dimensions=dim,
                                options=options,
                                bounds=constraints)

cost, joint_vars = optimizer.optimize(opt_func, iters=1000)

However, since the convergence is being slow and I have a notion of what should be the vector x whose results in the minimum of c(x), I would like to inform the algorithm where to begin the swarm. I try the parameter init_pos described in pyswarms documentation.

init_pos = np.array(init_pos) #My guess for a initial swarm

In this case, the variable init_pos is an array of size (len(X),). However, when I execute the code

optimizer = ps.single.GlobalBestPSO(n_particles=swarm_size,
                                dimensions=dim,
                                options=options,
                                bounds=constraints,
                                init_pos = init_pos)

I get the following error:

Traceback (most recent call last):

File "<ipython-input-110-f2477b03016c>", line 1, in <module>
runfile('/home/lps/Downloads/pso.py', wdir='/home/lps/Downloads')

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/spyder_kernels/customize/spydercustomize.py", line 668, in 
runfile
execfile(filename, namespace)

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/spyder_kernels/customize/spydercustomize.py", line 108, in 
execfile
exec(compile(f.read(), filename, 'exec'), namespace)

File "/home/lps/Downloads/pso.py", line 234, in <module>
init_pos = init_pos)

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/pyswarms/single/global_best.py", line 132, in __init__
init_pos=init_pos,

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/pyswarms/base/base_single.py", line 110, in __init__
self.reset()

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/pyswarms/base/base_single.py", line 197, in reset
options=self.options,

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/pyswarms/backend/generators.py", line 245, in create_swarm
return Swarm(position, velocity, options=options)

File "<attrs generated init 
73b4f2f5e7b2bdb45fae355f5990e313431ea4d3>", line 11, in __init__
self.dimensions = __attr_factory_dimensions(self)

File "/home/lps/anaconda3/lib/python3.7/site- 
packages/pyswarms/backend/swarms.py", line 117, in dimensions_default
return self.position.shape[1]

IndexError: tuple index out of range

What is the correct way to use the init_pos parameter?

donut
  • 628
  • 2
  • 9
  • 23

1 Answers1

3

Author of Pyswarms here :)

The size of init_pos should be (swarm_size, num_dimensions). However, as you said:

n this case, the variable init_pos is an array of size (len(X),). However, when I execute the code

Since you're passing a 1-dimensional array, it will error out. Again, to resolve this, initialize your position matrix such that it has two-dimensions.

Hope it helps!

Lj Miranda
  • 368
  • 2
  • 10