IndexError:标量变量的索引无效。- PSO 算法

发布于 2025-01-12 10:10:19 字数 4242 浏览 0 评论 0原文

在尝试使用 PSO 算法解决此问题时,我收到错误消息“IndexError:标量变量的索引无效”。由于我是 PSO 算法的新手,我无法找出错误。请帮助我解决这个问题。

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.

此外,我想用迭代次数绘制适应度值图。请帮我。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文