1 year ago
#264464
Jyoti
IndexError: invalid index to scalar variable.- PSO algorithm
While trying to solve this problem using PSO algorithm I got the error message as "IndexError: invalid index to scalar variable". Since I am new to PSO algorithm, I am unable to figure out the error. Kindly help me to solve the issue.
import random
import numpy as np
import math
import matplotlib.pyplot as plt
def fitness_function(position):
s1=0.014109786*position[0] + 0.004596846*position[1] + 0.01603721*position[2] + 0.029275618*position[3] + 0.007085358*position[4] + 0.013234328*position[5] + 0.012554958*position[6] + 0.012447232*position[7] + 0.007867602*position[8] + 0.011312568*position[9] + 0.003087858*position[10] + 0.016566954*position[11] + 0.008428942*position[12] + 0.008477444*position[13] + 0.004357354*position[14]
s2=0.016566954*position[0] + 0.00585045*position[1] + 0.053172638*position[2] + 0.113404042*position[3] + 0.028190744*position[4] + 0.046330688*position[5] + 0.05629084*position[6] + 0.047796486*position[7] + 0.025793022*position[8] + 0.046164518*position[9] + 0.026696192*position[10] + 0.080422654*position[11] + 0.029074508*position[12] + 0.039611624*position[13] + 0.044835566*position[14]
return s1+s2
#Some variables to calculate the velocity
W = 0.5
c1 = 0.8
c2 = 0.9
target = 1
n_iterations = int(input("Inform the number of iterations: "))
target_error = float(input("Inform the target error: "))
n_particles = int(input("Inform the number of particles: "))
particle_position_vector=np.array([np.random.uniform(low=0.025, high=0.035) for i in range (n_particles)])
print(particle_position_vector)
pbest_position = particle_position_vector
pbest_fitness_value = float('inf')
gbest_fitness_value = float('inf')
gbest_position = np.array([float('inf')for _ in range(n_particles)])
velocity_vector =np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0])
print(velocity_vector)
iteration = 0
while iteration < n_iterations:
for i in range(n_particles):
fitness_cadidate = fitness_function(particle_position_vector[i])
print(fitness_cadidate, ' ', particle_position_vector[i])
if(pbest_fitness_value[i] > fitness_cadidate):
pbest_fitness_value[i] = fitness_cadidate
pbest_position[i] = particle_position_vector[i]
if(gbest_fitness_value > fitness_cadidate):
gbest_fitness_value = fitness_cadidate
gbest_position = particle_position_vector[i]
if(abs(gbest_fitness_value - target) < target_error):
break
for i in range(n_particles):
new_velocity = (W*velocity_vector[i]) + (c1*random.random()) * (pbest_position[i] - particle_position_vector[i]) + (c2*random.random()) * (gbest_position-particle_position_vector[i])
new_position = new_velocity + particle_position_vector[i]
particle_position_vector[i] = new_position
iteration = iteration + 1
print("The best position is ", gbest_position, "in iteration number ", iteration)```
```IndexError Traceback (most recent call last)
<ipython-input-35-5610603d3302> in <module>
32 while iteration < n_iterations:
33 for i in range(n_particles):
---> 34 fitness_cadidate = fitness_function(particle_position_vector[i])
35 print(fitness_cadidate, ' ', particle_position_vector[i])
36
<ipython-input-35-5610603d3302> in fitness_function(position)
5
6 def fitness_function(position):
----> 7 s1=0.014109786*position[0] + 0.004596846*position[1] + 0.01603721*position[2] + 0.029275618*position[3] + 0.007085358*position[4] + 0.013234328*position[5] + 0.012554958*position[6] + 0.012447232*position[7] + 0.007867602*position[8] + 0.011312568*position[9] + 0.003087858*position[10] + 0.016566954*position[11] + 0.008428942*position[12] + 0.008477444*position[13] + 0.004357354*position[14]
8 s2=0.016566954*position[0] + 0.00585045*position[1] + 0.053172638*position[2] + 0.113404042*position[3] + 0.028190744*position[4] + 0.046330688*position[5] + 0.05629084*position[6] + 0.047796486*position[7] + 0.025793022*position[8] + 0.046164518*position[9] + 0.026696192*position[10] + 0.080422654*position[11] + 0.029074508*position[12] + 0.039611624*position[13] + 0.044835566*position[14]
9 return s1+s2
IndexError: invalid index to scalar variable.
Moreover, I want to plot a graph of fitness value with the iteration numbers. Please help me.
python
python-3.x
jupyter-notebook
evolutionary-algorithm
particle-swarm
0 Answers
Your Answer