IndexError: invalid index to scalar variable

import random
import numpy as np
import math
import matplotlib.pyplot as plt

def fitness_function(position):
s1=0.014109786position[0] + 0.004596846position[1] + 0.01603721position[2] + 0.029275618position[3] + 0.007085358position[4] + 0.013234328position[5] + 0.012554958position[6] + 0.012447232position[7] + 0.007867602position[8] + 0.011312568position[9] + 0.003087858position[10] + 0.016566954position[11] + 0.008428942position[12] + 0.008477444position[13] + 0.004357354position[14]
s2=0.016566954
position[0] + 0.00585045position[1] + 0.053172638position[2] + 0.113404042position[3] + 0.028190744position[4] + 0.046330688position[5] + 0.05629084position[6] + 0.047796486position[7] + 0.025793022position[8] + 0.046164518position[9] + 0.026696192position[10] + 0.080422654position[11] + 0.029074508position[12] + 0.039611624position[13] + 0.044835566position[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)

while trying to solve one PSO algorithm problem I got the error as:

IndexError Traceback (most recent call last)
in
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

in fitness_function(position)
5
6 def fitness_function(position):
----> 7 s1=0.014109786position[0] + 0.004596846position[1] + 0.01603721position[2] + 0.029275618position[3] + 0.007085358position[4] + 0.013234328position[5] + 0.012554958position[6] + 0.012447232position[7] + 0.007867602position[8] + 0.011312568position[9] + 0.003087858position[10] + 0.016566954position[11] + 0.008428942position[12] + 0.008477444position[13] + 0.004357354position[14]
8 s2=0.016566954
position[0] + 0.00585045position[1] + 0.053172638position[2] + 0.113404042position[3] + 0.028190744position[4] + 0.046330688position[5] + 0.05629084position[6] + 0.047796486position[7] + 0.025793022position[8] + 0.046164518position[9] + 0.026696192position[10] + 0.080422654position[11] + 0.029074508position[12] + 0.039611624position[13] + 0.044835566position[14]
9 return s1+s2

IndexError: invalid index to scalar variable.\

Please help me to solve the error.

I guess the indexing of position fails as it seems to be a scalar.
Could you check its shape and make sure you are able to index it with indices in [0, 14]?

PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier :wink:

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,0])
print(velocity_vector)

iteration = 0
while iteration < n_iterations:
    for i in range(n_particles):
        fitness_cadidate = fitness_function(particle_position_vector)
        print([fitness_cadidate, ' ', particle_position_vector[i]], "in iteration no", iteration)
        
        if(pbest_fitness_value > fitness_cadidate):
            pbest_fitness_value = 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)
Inform the number of iterations: 10
Inform the target error: 0
Inform the number of particles: 15
[0.02610731 0.03397208 0.03155405 0.03021921 0.03042221 0.03032528
 0.02715625 0.02886593 0.0324619  0.03491303 0.03156493 0.03372314
 0.03035151 0.03118576 0.03300664]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0.025711393906019356, ' ', 0.026107305119526646]
[0.025711393906019356, ' ', 0.033972083151388305]
[0.025711393906019356, ' ', 0.03155405105746002]
[0.025711393906019356, ' ', 0.030219210681574078]
[0.025711393906019356, ' ', 0.03042220548321309]
[0.025711393906019356, ' ', 0.030325280751438956]
[0.025711393906019356, ' ', 0.027156250359312045]
[0.025711393906019356, ' ', 0.028865926141292526]
[0.025711393906019356, ' ', 0.03246189741806181]
[0.025711393906019356, ' ', 0.03491303477936334]
[0.025711393906019356, ' ', 0.03156492551619854]
[0.025711393906019356, ' ', 0.03372313520902316]
[0.025711393906019356, ' ', 0.030351513541586418]
[0.025711393906019356, ' ', 0.0311857576545056]
[0.025711393906019356, ' ', 0.03300663803717355]
[0.024320132491838665, ' ', 0.026107305119526646]
[0.024320132491838665, ' ', 0.031488022249348865]
[0.024320132491838665, ' ', 0.03017569431126403]
[0.024320132491838665, ' ', 0.029908725315736042]
[0.024320132491838665, ' ', 0.027432097920800463]
[0.024320132491838665, ' ', 0.028802065577261053]
[0.024320132491838665, ' ', 0.02653071811719933]
[0.024320132491838665, ' ', 0.028216384764268295]
[0.024320132491838665, ' ', 0.03134007758402327]
[0.024320132491838665, ' ', 0.03159557476121445]
[0.024320132491838665, ' ', 0.03083455803270586]
[0.024320132491838665, ' ', 0.029565188623312196]
[0.024320132491838665, ' ', 0.029647968869561937]
[0.024320132491838665, ' ', 0.027951648046890806]
[0.024320132491838665, ' ', 0.03073339450800908]
[0.02296460605656049, ' ', 0.026107305119526646]
[0.02296460605656049, ' ', 0.02997534861963597]
[0.02296460605656049, ' ', 0.029374392599910697]
[0.02296460605656049, ' ', 0.02865269141147226]
[0.02296460605656049, ' ', 0.027368508195035494]
[0.02296460605656049, ' ', 0.027034319306849835]
[0.02296460605656049, ' ', 0.02618940270690127]
[0.02296460605656049, ' ', 0.02711178249047202]
[0.02296460605656049, ' ', 0.02772046343679006]
[0.02296460605656049, ' ', 0.030184363563214914]
[0.02296460605656049, ' ', 0.02828877200150204]
[0.02296460605656049, ' ', 0.02647244974812662]
[0.02296460605656049, ' ', 0.02721198535820505]
[0.02296460605656049, ' ', 0.0263129469853678]
[0.02296460605656049, ' ', 0.02750991044901061]
[0.02259136789416094, ' ', 0.026107305119526646]
[0.02259136789416094, ' ', 0.029679023062897925]
[0.02259136789416094, ' ', 0.0271248047972687]
[0.02259136789416094, ' ', 0.028427350589188147]
[0.02259136789416094, ' ', 0.026684762498666414]
[0.02259136789416094, ' ', 0.026658401224146137]
[0.02259136789416094, ' ', 0.026174546388074364]
[0.02259136789416094, ' ', 0.027022107771347087]
[0.02259136789416094, ' ', 0.026903099693249742]
[0.02259136789416094, ' ', 0.029673102930836453]
[0.02259136789416094, ' ', 0.02694369089531378]
[0.02259136789416094, ' ', 0.02640349169797398]
[0.02259136789416094, ' ', 0.02694455909776925]
[0.02259136789416094, ' ', 0.02629397089889013]
[0.02259136789416094, ' ', 0.027209669585093472]
[0.022149953769352096, ' ', 0.026107305119526646]
[0.022149953769352096, ' ', 0.02682242469037271]
[0.022149953769352096, ' ', 0.026772583093873734]
[0.022149953769352096, ' ', 0.027304601595496764]
[0.022149953769352096, ' ', 0.026297457467256653]
[0.022149953769352096, ' ', 0.026444549018921983]
[0.022149953769352096, ' ', 0.026119895819054842]
[0.022149953769352096, ' ', 0.02700911096159259]
[0.022149953769352096, ' ', 0.0263657358661671]
[0.022149953769352096, ' ', 0.027993843172827517]
[0.022149953769352096, ' ', 0.026365623929576348]
[0.022149953769352096, ' ', 0.026395041045615943]
[0.022149953769352096, ' ', 0.02656168200959222]
[0.022149953769352096, ' ', 0.026152881567773605]
[0.022149953769352096, ' ', 0.026350485895260303]
[0.02196536011213198, ' ', 0.026107305119526646]
[0.02196536011213198, ' ', 0.026251771483805367]
[0.02196536011213198, ' ', 0.026639244596274154]
[0.02196536011213198, ' ', 0.026977404274747138]
[0.02196536011213198, ' ', 0.026255377832076328]
[0.02196536011213198, ' ', 0.02631229241030227]
[0.02196536011213198, ' ', 0.02611501371834422]
[0.02196536011213198, ' ', 0.026226620719786483]
[0.02196536011213198, ' ', 0.0262774753418182]
[0.02196536011213198, ' ', 0.02739266893143547]
[0.02196536011213198, ' ', 0.026218430948373485]
[0.02196536011213198, ' ', 0.026304927667818778]
[0.02196536011213198, ' ', 0.026203389133480824]
[0.02196536011213198, ' ', 0.026122136541912408]
[0.02196536011213198, ' ', 0.02634405818713259]
[0.021882729697556242, ' ', 0.026107305119526646]
[0.021882729697556242, ' ', 0.02618123525069074]
[0.021882729697556242, ' ', 0.0265351142140617]
[0.021882729697556242, ' ', 0.026837841262750236]
[0.021882729697556242, ' ', 0.026170214360813824]
[0.021882729697556242, ' ', 0.026243549371857255]
[0.021882729697556242, ' ', 0.026108356079944446]
[0.021882729697556242, ' ', 0.026151910296383522]
[0.021882729697556242, ' ', 0.026212261228528225]
[0.021882729697556242, ' ', 0.02690022381909742]
[0.021882729697556242, ' ', 0.02620027986429856]
[0.021882729697556242, ' ', 0.02628749798690316]
[0.021882729697556242, ' ', 0.026190765785669305]
[0.021882729697556242, ' ', 0.026117284703855]
[0.021882729697556242, ' ', 0.0261554448042546]
[0.02177057859845221, ' ', 0.026107305119526646]
[0.02177057859845221, ' ', 0.026137452259190643]
[0.02177057859845221, ' ', 0.026354344087112323]
[0.02177057859845221, ' ', 0.02655602292137756]
[0.02177057859845221, ' ', 0.026161766803093407]
[0.02177057859845221, ' ', 0.026210888571057963]
[0.02177057859845221, ' ', 0.026107730151894763]
[0.02177057859845221, ' ', 0.026130139893106735]
[0.02177057859845221, ' ', 0.026132435406647104]
[0.02177057859845221, ' ', 0.026256949968816713]
[0.02177057859845221, ' ', 0.02611981673909813]
[0.02177057859845221, ' ', 0.026159853300237]
[0.02177057859845221, ' ', 0.026190467578175024]
[0.02177057859845221, ' ', 0.02611361820553696]
[0.02177057859845221, ' ', 0.026140196980819368]
[0.02174056846037247, ' ', 0.026107305119526646]
[0.02174056846037247, ' ', 0.026134963856525934]
[0.02174056846037247, ' ', 0.02629550889726636]
[0.02174056846037247, ' ', 0.02649961465256457]
[0.02174056846037247, ' ', 0.026114591626350474]
[0.02174056846037247, ' ', 0.02612128464847708]
[0.02174056846037247, ' ', 0.026107639557958993]
[0.02174056846037247, ' ', 0.026120777415775617]
[0.02174056846037247, ' ', 0.026113951106271755]
[0.02174056846037247, ' ', 0.02618017887994501]
[0.02174056846037247, ' ', 0.026111137911880248]
[0.02174056846037247, ' ', 0.026140747268146847]
[0.02174056846037247, ' ', 0.026142988577840906]
[0.02174056846037247, ' ', 0.02610985132077023]
[0.02174056846037247, ' ', 0.026116127462027]
[0.02168917842135052, ' ', 0.026107305119526646]
[0.02168917842135052, ' ', 0.026131785633486043]
[0.02168917842135052, ' ', 0.026159478858256133]
[0.02168917842135052, ' ', 0.026236148573643554]
[0.02168917842135052, ' ', 0.026114245362496704]
[0.02168917842135052, ' ', 0.026113182706214058]
[0.02168917842135052, ' ', 0.026107357940299628]
[0.02168917842135052, ' ', 0.02611543946584295]
[0.02168917842135052, ' ', 0.026113452908013732]
[0.02168917842135052, ' ', 0.02615616908115009]
[0.02168917842135052, ' ', 0.0261102975252415]
[0.02168917842135052, ' ', 0.026125090687745937]
[0.02168917842135052, ' ', 0.026138844063009207]
[0.02168917842135052, ' ', 0.026108116254807235]
[0.02168917842135052, ' ', 0.026109297555616428]
The best position is  0.026107305119526646 in iteration number  10```

But the output should be like (fitness value, [array of 15 particle's position])in each iteration.
Only 15 line output should come.

I don’t think that’s the case as your code uses:

print([fitness_cadidate, ' ', particle_position_vector[i]], "in iteration no", iteration)

in the while iteration < n_iterations (10 iterations) and for i in range(n_particles) (15 iterations) loops, so 150 outputs would be expected unless the break statement is reached.

I got this code from the internet and then modified it according to my problem. However, I want the output as mentioned previously. I would be grateful if you could help.

Thanks in advance.

If you want a single print of particle_position_vector per iteration, move

print([fitness_cadidate, ' ', particle_position_vector[i]], "in iteration no", iteration)

to the while iteration < n_iterations loop and remove the indexing.

I modified the code as you have suggested.

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,0])
print(velocity_vector)

iteration = 0
while iteration < n_iterations:      
    for i in range(n_particles):
        fitness_cadidate = fitness_function(particle_position_vector)
        
        
        if(pbest_fitness_value > fitness_cadidate):
            pbest_fitness_value = 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([fitness_cadidate, ' ', particle_position_vector], "in iteration no", iteration)  
print("The best position is ", gbest_position, "in iteration number ", iteration)

When I run it, I got the output as:

Inform the number of iterations: 10
Inform the target error: 0
Inform the number of particles: 15
[0.0265728  0.03297489 0.03160219 0.02998697 0.02588665 0.02612961
 0.02789183 0.03458498 0.03091031 0.02829033 0.02974824 0.03098218
 0.02523714 0.03122786 0.03175598]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0.022067955290026243, ' ', array([0.0265728 , 0.02657408, 0.02677375, 0.0265752 , 0.02657173,
       0.02657268, 0.02657466, 0.02659285, 0.02666034, 0.02657614,
       0.02658038, 0.02657845, 0.02657131, 0.02657742, 0.02657331])] in iteration no 10
The best position is  0.026572801650095946 in iteration number  10
```