keurfonluu / stochopy

Python library for stochastic numerical optimization
https://github.com/keurfonluu/stochopy
BSD 3-Clause "New" or "Revised" License
92 stars 16 forks source link

IndexError: boolean index did not match indexed array along dimension 1 #3

Closed esiwgnahz closed 3 years ago

esiwgnahz commented 4 years ago

Thank you. I encountered the following problem. Traceback (most recent call last): File " in ea.optimize(solver = "cpso", sync = True) File "/site-packages/stochopy/evolutionary_algorithm.py", line 293, in optimize xstart = xstart, sync = sync) File "/stochopy/evolutionary_algorithm.py", line 755, in _cpso pbest[idx] = np.array(X[idx]) IndexError: boolean index did not match indexed array along dimension 1; dimension is 80 but corresponding boolean dimension is 1

The execution is as follows

 func = "rastrigin"
# mpi_comm = MPI.COMM_WORLD
# mpi_rank = mpi_comm.Get_rank()    
n_dim = 80 
popsize = 4 + np.floor(3.*np.log(n_dim))

lower = np.full(n_dim, -40)
upper = np.full(n_dim, -20)

# Initialize solver; calc_cost calculates the residual after running a FEM problem.
ea = Evolutionary(calc_cost, lower = lower, upper = upper, eps1 = 1e-2, popsize = popsize,  max_iter = 2000, mpi = False, random_state = -1)

# Solve
starttime = time()
ea.optimize(solver = "cpso", sync = True)    
print(ea)
print("Elapsed time: %.2f seconds" % (time() - starttime))
keurfonluu commented 4 years ago

Does any example script work?

My initial guess would be that you did not define correctly the cost function calc_cost (input parameter vector and additional arguments, output scalar). I cannot help more without any reproducible script.