Python Forum
IndexError: invalid index to scalar variable.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
IndexError: invalid index to scalar variable.
#1
I got the error as "IndexError: invalid index to scalar variable.", while trying this PSO algorithm problem. I also want to plot a graph between iteration number and fitness value. Kindly help me with this.

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)
Yoriz write Mar-10-2022, 06:23 AM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Here, I have posted the code with an error. Please help me.

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)
Error:
IndexError Traceback (most recent call last) <ipython-input-16-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-16-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.
Reply
#3
"fitness_function(position)" expects an array (or list or any iterable) of exactly 15 elements. When you do:
fitness_function(particle_position_vector[i])
... you are only passing one element. So I guess you have to pass the whole array, like this:
fitness_function(particle_position_vector)
Note that there have to be 15 elements, so the only correct answer to:
input("Inform the number of particles: ")
... is 15.
Reply
#4
(Mar-10-2022, 09:36 AM)ibreeden Wrote: "fitness_function(position)" expects an array (or list or any iterable) of exactly 15 elements. When you do:
fitness_function(particle_position_vector[i])
... you are only passing one element. So I guess you have to pass the whole array, like this:
fitness_function(particle_position_vector)
Note that there have to be 15 elements, so the only correct answer to:
input("Inform the number of particles: ")
... is 15.

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]])
        
        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)
After modifying the code, I got the output like:
Output:
Inform the number of iterations: 10 Inform the target error: 0 Inform the number of particles: 15 [0.02731175 0.02615455 0.0316539 0.03217349 0.03422525 0.03060605 0.0303541 0.03037021 0.02762708 0.03179528 0.03378483 0.02605968 0.03378637 0.02818577 0.02678802] [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] [0.025073619876763563, ' ', 0.027311746421561187] [0.025073619876763563, ' ', 0.026154548512811418] [0.025073619876763563, ' ', 0.031653900892799076] [0.025073619876763563, ' ', 0.03217348852973725] [0.025073619876763563, ' ', 0.03422524835447013] [0.025073619876763563, ' ', 0.030606050682032628] [0.025073619876763563, ' ', 0.030354099198015336] [0.025073619876763563, ' ', 0.03037021067585577] [0.025073619876763563, ' ', 0.02762708294702282] [0.025073619876763563, ' ', 0.0317952794785016] [0.025073619876763563, ' ', 0.03378483403043713] [0.025073619876763563, ' ', 0.026059682094216857] [0.025073619876763563, ' ', 0.03378636800677858] [0.025073619876763563, ' ', 0.02818576945560601] [0.025073619876763563, ' ', 0.02678802479348601] [0.024236544801469147, ' ', 0.027311746421561187] [0.024236544801469147, ' ', 0.026415891253887314] [0.024236544801469147, ' ', 0.028604008990511874] [0.024236544801469147, ' ', 0.03074774274512092] [0.024236544801469147, ' ', 0.03414078591895475] [0.024236544801469147, ' ', 0.030306985044460125] [0.024236544801469147, ' ', 0.030119641891818583] [0.024236544801469147, ' ', 0.028793472393863595] [0.024236544801469147, ' ', 0.027449975183880045] [0.024236544801469147, ' ', 0.03111075257312244] [0.024236544801469147, ' ', 0.030622561175236245] [0.024236544801469147, ' ', 0.02672282825332497] [0.024236544801469147, ' ', 0.028461712812135026] [0.024236544801469147, ' ', 0.02777940411844139] [0.024236544801469147, ' ', 0.026809028972117] [0.02346529762638839, ' ', 0.027311746421561187] [0.02346529762638839, ' ', 0.026981469671633156] [0.02346529762638839, ' ', 0.02813767141334174] [0.02346529762638839, ' ', 0.029592069925483532] [0.02346529762638839, ' ', 0.02837790911491178] [0.02346529762638839, ' ', 0.028364146371212394] [0.02346529762638839, ' ', 0.028994207307403735] [0.02346529762638839, ' ', 0.028521130354018574] [0.02346529762638839, ' ', 0.02733382284896558] [0.02346529762638839, ' ', 0.02857768157227375] [0.02346529762638839, ' ', 0.02989461808249676] [0.02346529762638839, ' ', 0.026756018496818988] [0.02346529762638839, ' ', 0.028287748253333814] [0.02346529762638839, ' ', 0.02757727416516129] [0.02346529762638839, ' ', 0.02715715630639833] [0.023079901618592028, ' ', 0.027311746421561187] [0.023079901618592028, ' ', 0.027119083111800918] [0.023079901618592028, ' ', 0.027535232430653558] [0.023079901618592028, ' ', 0.029197730613825836] [0.023079901618592028, ' ', 0.028117580842869583] [0.023079901618592028, ' ', 0.027493561827534218] [0.023079901618592028, ' ', 0.027535533291056267] [0.023079901618592028, ' ', 0.02757013872393907] [0.023079901618592028, ' ', 0.027327942535210065] [0.023079901618592028, ' ', 0.02767694012644384] [0.023079901618592028, ' ', 0.029576419946264126] [0.023079901618592028, ' ', 0.0269177244497189] [0.023079901618592028, ' ', 0.027621399707694026] [0.023079901618592028, ' ', 0.027497623272480922] [0.023079901618592028, ' ', 0.02724789858712283] [0.022857406338736855, ' ', 0.027311746421561187] [0.022857406338736855, ' ', 0.02727615412256947] [0.022857406338736855, ' ', 0.027532918917815962] [0.022857406338736855, ' ', 0.027888958853220198] [0.022857406338736855, ' ', 0.027753784828660758] [0.022857406338736855, ' ', 0.027483166222550988] [0.022857406338736855, ' ', 0.027368350958994935] [0.022857406338736855, ' ', 0.027543632073428474] [0.022857406338736855, ' ', 0.027324741812849527] [0.022857406338736855, ' ', 0.027405121812830897] [0.022857406338736855, ' ', 0.029140860085802158] [0.022857406338736855, ' ', 0.027148195061810553] [0.022857406338736855, ' ', 0.027540830701325278] [0.022857406338736855, ' ', 0.02746259693956002] [0.022857406338736855, ' ', 0.027255515639827303] [0.022791972609377237, ' ', 0.027311746421561187] [0.022791972609377237, ' ', 0.027284161356972335] [0.022791972609377237, ' ', 0.02752916345003667] [0.022791972609377237, ' ', 0.02774892029312538] [0.022791972609377237, ' ', 0.02754264634622429] [0.022791972609377237, ' ', 0.02746954413807446] [0.022791972609377237, ' ', 0.027326306883516054] [0.022791972609377237, ' ', 0.027538228090200418] [0.022791972609377237, ' ', 0.027323073432060686] [0.022791972609377237, ' ', 0.027400338838344276] [0.022791972609377237, ' ', 0.02803318847454111] [0.022791972609377237, ' ', 0.027214939542158915] [0.022791972609377237, ' ', 0.027339732821101005] [0.022791972609377237, ' ', 0.027460151433373124] [0.022791972609377237, ' ', 0.027269964969757813] [0.022728270076583682, ' ', 0.027311746421561187] [0.022728270076583682, ' ', 0.027299489741849824] [0.022728270076583682, ' ', 0.02733994351843582] [0.022728270076583682, ' ', 0.027503216790345134] [0.022728270076583682, ' ', 0.02748338015769847] [0.022728270076583682, ' ', 0.02742669524665184] [0.022728270076583682, ' ', 0.027313607986463772] [0.022728270076583682, ' ', 0.027468219036881996] [0.022728270076583682, ' ', 0.027320444909955617] [0.022728270076583682, ' ', 0.02736316010077298] [0.022728270076583682, ' ', 0.027688456643096663] [0.022728270076583682, ' ', 0.027292324372632293] [0.022728270076583682, ' ', 0.027337835510298385] [0.022728270076583682, ' ', 0.027415978007609972] [0.022728270076583682, ' ', 0.02729451630468392] [0.022708601467485302, ' ', 0.027311746421561187] [0.022708601467485302, ' ', 0.02730789193497009] [0.022708601467485302, ' ', 0.027335580402316054] [0.022708601467485302, ' ', 0.027498882543126997] [0.022708601467485302, ' ', 0.027429306170710276] [0.022708601467485302, ' ', 0.027324422214565162] [0.022708601467485302, ' ', 0.02731317681215081] [0.022708601467485302, ' ', 0.027461395509372235] [0.022708601467485302, ' ', 0.027314259146785744] [0.022708601467485302, ' ', 0.02733988779244826] [0.022708601467485302, ' ', 0.02735648264439329] [0.022708601467485302, ' ', 0.027299907896279316] [0.022708601467485302, ' ', 0.027329487629375076] [0.022708601467485302, ' ', 0.02741527015294955] [0.022708601467485302, ' ', 0.027307711829914684] [0.02267308392919699, ' ', 0.027311746421561187] [0.02267308392919699, ' ', 0.027308091939796143] [0.02267308392919699, ' ', 0.02732240619587771] [0.02267308392919699, ' ', 0.027337748933687067] [0.02267308392919699, ' ', 0.027330397694597778] [0.02267308392919699, ' ', 0.02731869166187432] [0.02267308392919699, ' ', 0.02731277323114734] [0.02267308392919699, ' ', 0.02733616107745396] [0.02267308392919699, ' ', 0.027313559924095284] [0.02267308392919699, ' ', 0.02733288517115566] [0.02267308392919699, ' ', 0.027337302826067517] [0.02267308392919699, ' ', 0.027308300580075086] [0.02267308392919699, ' ', 0.02732900163476884] [0.02267308392919699, ' ', 0.027413729378012393] [0.02267308392919699, ' ', 0.027308953090812722] [0.022667700579661755, ' ', 0.027311746421561187] [0.022667700579661755, ' ', 0.027309231186214207] [0.022667700579661755, ' ', 0.027319489184052095] [0.022667700579661755, ' ', 0.02733174822598569] [0.022667700579661755, ' ', 0.027324770024226848] [0.022667700579661755, ' ', 0.027316748243450862] [0.022667700579661755, ' ', 0.027312610526234418] [0.022667700579661755, ' ', 0.02731601220083983] [0.022667700579661755, ' ', 0.027313197244750904] [0.022667700579661755, ' ', 0.02731928310142291] [0.022667700579661755, ' ', 0.027325061997480537] [0.022667700579661755, ' ', 0.027308877051798842] [0.022667700579661755, ' ', 0.02732898826003717] [0.022667700579661755, ' ', 0.02737632007547092] [0.022667700579661755, ' ', 0.02731107342103635] The best position is 0.027311746421561187 in iteration number 10 ​
But the problem is, it should be coming like (fitness value, [array of 15 particle's position]) for each iteration, which didn't come. Please help....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  IndexError: index 0 is out of bounds for axis 0 with size 0 atomxkai 2 5,478 Mar-03-2021, 08:26 AM
Last Post: atomxkai
  [split] Getting Index Error - list index out of range krishna 2 2,615 Jan-09-2021, 08:29 AM
Last Post: buran
  IndexError: index 0 is out of bounds for axis 0 with size 0 tmhsa 0 5,315 Apr-24-2020, 10:00 AM
Last Post: tmhsa
  Getting Index Error - list index out of range RahulSingh 2 6,159 Feb-03-2020, 07:17 AM
Last Post: RahulSingh
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 2,589 Jan-28-2020, 09:30 AM
Last Post: axelle
  IndexError: index out of bounds LeoGER 3 10,664 Sep-05-2019, 02:05 PM
Last Post: LeoGER
  Can you help me with this error? IndexError: invalid index of a 0-dim tensor. DerBerliner 1 4,184 Feb-28-2019, 05:47 PM
Last Post: Larz60+
  Unable to understand reason for error IndexError: tuple index out of range rajat2504 4 54,129 Dec-09-2016, 11:04 AM
Last Post: Kebap

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020