ljvmiranda921 / pyswarms

A research toolkit for particle swarm optimization in Python
https://pyswarms.readthedocs.io/en/latest/
MIT License
1.28k stars 332 forks source link

Value operand error when computing best cost for arrays #446

Closed mtartila closed 3 years ago

mtartila commented 3 years ago

Description

im doing a two 5x5 covariance (P estimate and Q process noice) tuning for my Unscented Kalman Filter system. im having an error of

`  208             # fmt: off
    209             self.swarm.current_cost = compute_objective_function(self.swarm, objective_func, pool=pool, **kwargs)
--> 210             self.swarm.pbest_pos, self.swarm.pbest_cost = compute_pbest(self.swarm)
    211             # Set best_cost_yet_found for ftol
    212             best_cost_yet_found = self.swarm.best_cost

~/anaconda3/lib/python3.8/site-packages/pyswarms/backend/operators.py in compute_pbest(swarm)
     67         mask_pos = np.repeat(mask_cost[:, np.newaxis], dimensions, axis=1)
     68         # Apply masks
---> 69         new_pbest_pos = np.where(~mask_pos, swarm.pbest_pos, swarm.position)
     70         new_pbest_cost = np.where(
     71             ~mask_cost, swarm.pbest_cost, swarm.current_cost

<__array_function__ internals> in where(*args, **kwargs)

ValueError: operands could not be broadcast together with shapes (1,10,2) (1,10) (1,10) `

What I Did

I first initialize the parameter with 10 dimension of X, 5 for P and 5 for Q with a testing swarm of 1 for testing : swarm_size = 1 dim = 10 # Dimension of X epsilon = 1.0 options = {'c1': 1.5, 'c2':1.5, 'w':0.5} Next, i create an error function to calculate the RMSE based by the covariance P and Q which changed by X as PSO input particles that i dump to yaml, then to be read by my ROS and C++ script to calculate positions of absolute x and y and predicted x and y position. After that, i calculate the error rmse :

`def error1(X):
    P = np.array([X[0],0,0,0,0,0,X[1],0,0,0,0,0,X[2],0,0,0,0,0,X[3],0,0,0,0,0,X[4]])
    P = P.flatten().tolist()
    Q = np.array([X[5],0,0,0,0,0,X[6],0,0,0,0,0,X[7],0,0,0,0,0,X[8],0,0,0,0,0,X[9]])
    Q = Q.flatten().tolist()
    tes_yaml(P,Q)
    launch()
    hasil_ukf, utm, t_state, t_utm = ambil_bag()
    plot(hasil_ukf,utm)

    # Get the overall rmse
    pos = np.array(err2(hasil_ukf, utm, t_state, t_utm))

    return pos`

later, i create an opt function to reduce the distance between my rmse to the target of 0 as to minimize the rmse `

def opt_func(X):
    n_particles = X.shape[0]  # number of particles
    target = np.array([[0]])
    rmse_min = [(error1(X[i]),target ) for i in range(n_particles)]
    return np.array(rmse_min)
`

lastly, i call the optimizer to optimize the covariance with the dimension of X to minimize the RMSE with 1 iteration for testing if it works or not by writing : %%time

Call an instance of PSO

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

Perform optimization

cost, covariance = optimizer.optimize(opt_func, iters=2)

and thats the error i got, i try to use swarm particle to optimize 10 dimension array for 1 output of rmse. any idea how to fix the error of shape?

stale[bot] commented 3 years ago

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.