IndexError: invalid index to scalar variable

jyoti: <p>import random<br>
import numpy as np<br>
import math<br>
import matplotlib.pyplot as plt</p>
<p>def fitness_function(position):<br>
s1=0.014109786<em>position[0] + 0.004596846</em>position[1] + 0.01603721<em>position[2] + 0.029275618</em>position[3] + 0.007085358<em>position[4] + 0.013234328</em>position[5] + 0.012554958<em>position[6] + 0.012447232</em>position[7] + 0.007867602<em>position[8] + 0.011312568</em>position[9] + 0.003087858<em>position[10] + 0.016566954</em>position[11] + 0.008428942<em>position[12] + 0.008477444</em>position[13] + 0.004357354<em>position[14]<br>
s2=0.016566954</em>position[0] + 0.00585045<em>position[1] + 0.053172638</em>position[2] + 0.113404042<em>position[3] + 0.028190744</em>position[4] + 0.046330688<em>position[5] + 0.05629084</em>position[6] + 0.047796486<em>position[7] + 0.025793022</em>position[8] + 0.046164518<em>position[9] + 0.026696192</em>position[10] + 0.080422654<em>position[11] + 0.029074508</em>position[12] + 0.039611624<em>position[13] + 0.044835566</em>position[14]<br>
return s1+s2</p>
<p><span class="hashtag">#Some</span> variables to calculate the velocity<br>
W = 0.5<br>
c1 = 0.8<br>
c2 = 0.9<br>
target = 1</p>
<p>n_iterations = int(input("Inform the number of iterations: "))<br>
target_error = float(input("Inform the target error: "))<br>
n_particles = int(input("Inform the number of particles: "))</p>
<p>particle_position_vector=np.array([np.random.uniform(low=0.025, high=0.035) for i in range (n_particles)])<br>
print(particle_position_vector)<br>
pbest_position = particle_position_vector<br>
pbest_fitness_value = float(‘inf’)<br>
gbest_fitness_value = float(‘inf’)<br>
gbest_position = np.array([float(‘inf’)for _ in range(n_particles)])</p>
<p>velocity_vector =np.array([0,0,0,0,0,0,0,0,0,0,0,0,0,0])<br>
print(velocity_vector)</p>
<p>iteration = 0<br>
while iteration &lt; n_iterations:<br>
for i in range(n_particles):<br>
fitness_cadidate = fitness_function(particle_position_vector[i])<br>
print(fitness_cadidate, ’ ', particle_position_vector[i])</p>
<pre><code>    if(pbest_fitness_value[i] &gt; fitness_cadidate):
        pbest_fitness_value[i] = fitness_cadidate
        pbest_position[i] = particle_position_vector[i]

    if(gbest_fitness_value &gt; fitness_cadidate):
        gbest_fitness_value = fitness_cadidate
        gbest_position = particle_position_vector[i]

if(abs(gbest_fitness_value - target) &lt; 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
</code></pre>
<p>print("The best position is ", gbest_position, "in iteration number ", iteration)</p>
<p>while trying to solve one PSO algorithm problem I got the error as:</p>
<p>IndexError                                Traceback (most recent call last)<br>
 in <br>
32 while iteration &lt; n_iterations:<br>
33     for i in range(n_particles):<br>
—&gt; 34         fitness_cadidate = fitness_function(particle_position_vector[i])<br>
35         print(fitness_cadidate, ’ ', particle_position_vector[i])<br>
36</p>
<p> in fitness_function(position)<br>
5<br>
6 def fitness_function(position):<br>
----&gt; 7     s1=0.014109786<em>position[0] + 0.004596846</em>position[1] + 0.01603721<em>position[2] + 0.029275618</em>position[3] + 0.007085358<em>position[4] + 0.013234328</em>position[5] + 0.012554958<em>position[6] + 0.012447232</em>position[7] + 0.007867602<em>position[8] + 0.011312568</em>position[9] + 0.003087858<em>position[10] + 0.016566954</em>position[11] + 0.008428942<em>position[12] + 0.008477444</em>position[13] + 0.004357354<em>position[14]<br>
8     s2=0.016566954</em>position[0] + 0.00585045<em>position[1] + 0.053172638</em>position[2] + 0.113404042<em>position[3] + 0.028190744</em>position[4] + 0.046330688<em>position[5] + 0.05629084</em>position[6] + 0.047796486<em>position[7] + 0.025793022</em>position[8] + 0.046164518<em>position[9] + 0.026696192</em>position[10] + 0.080422654<em>position[11] + 0.029074508</em>position[12] + 0.039611624<em>position[13] + 0.044835566</em>position[14]<br>
9     return s1+s2</p>
<p>IndexError: invalid index to scalar variable.\</p>
<p>Please help me to solve the error.</p>

ptrblck: <p>I guess the indexing of <code>position</code> fails as it seems to be a scalar.<br>
Could you check its shape and make sure you are able to index it with indices in <code>[0, 14]</code>?</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=12" title=":wink:" class="emoji" alt=":wink:" loading="lazy" width="20" height="20"></p>

jyoti: <pre><code class="lang-auto">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 &lt; 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 &gt; fitness_cadidate):
            pbest_fitness_value = fitness_cadidate
            pbest_position[i] = particle_position_vector[i]

        if(gbest_fitness_value &gt; fitness_cadidate):
            gbest_fitness_value = fitness_cadidate
            gbest_position = particle_position_vector[i]

    if(abs(gbest_fitness_value - target) &lt; 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)
</code></pre>
<pre><code class="lang-auto">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.</code></pre>

ptrblck: <aside class="quote no-group" data-username="jyoti" data-post="3" data-topic="146014">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/6f9a4e/48.png" class="avatar"> jyoti:</div>
<blockquote>
<p><code>Only 15 line output should come.</code></p>
</blockquote>
</aside>
<p>I don’t think that’s the case as your code uses:</p>
<pre data-code-wrap="python"><code class="lang-python">print([fitness_cadidate, ' ', particle_position_vector[i]], "in iteration no", iteration)
</code></pre>
<p>in the <code>while iteration &lt; n_iterations</code> (10 iterations) and <code>for i in range(n_particles)</code> (15 iterations) loops, so 150 outputs would be expected unless the <code>break</code> statement is reached.</p>

jyoti: <p>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.</p>
<p>Thanks in advance.</p>

ptrblck: <p>If you want a single print of <code>particle_position_vector</code> per iteration, move</p>
<pre><code class="lang-python">print([fitness_cadidate, ' ', particle_position_vector[i]], "in iteration no", iteration)
</code></pre>
<p>to the <code>while iteration &lt; n_iterations</code> loop and remove the indexing.</p>

jyoti: <p>I modified the code as you have suggested.</p>
<pre><code class="lang-auto">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 &lt; n_iterations:      
    for i in range(n_particles):
        fitness_cadidate = fitness_function(particle_position_vector)
        
        
        if(pbest_fitness_value &gt; fitness_cadidate):
            pbest_fitness_value = fitness_cadidate
            pbest_position[i] = particle_position_vector[i]

        if(gbest_fitness_value &gt; fitness_cadidate):
            gbest_fitness_value = fitness_cadidate
            gbest_position = particle_position_vector[i]

    if(abs(gbest_fitness_value - target) &lt; 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)
</code></pre>
<p>When I run it, I got the output as:</p>
<pre><code class="lang-auto">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
```</code></pre>