python函数参数为列表_python 类函数更改列表( 参数)_python-2.7_酷徒编程知识库

我正在编写粒子群算法,这里有一个最佳全局位置的列表。 我创建的类中的函数正在改变主函数中的参数( 这里列表),所以它只是在破坏我的结果。 我不清楚发生了什么。

为了计算新的位置,我必须把best_particle_vector作为一个参数,以便计算新位置,只改变群( 它自己)

谢谢。class Particle():

def __init__(self):

self.position, self.velocity = initialize_pos_vel(n_dimension)

self.best_position = self.position

self.best_fitness = wish_function(self.best_position)

class Swarm(): #each swarm is a vector of particles

def __init__(self):

self.particles = initialize_swarm(n_particles)

def next_pos_vel(self, best_particle_vector): # calcula as novas posições e velocidade do enxame

for i in range(n_particles):

for j in range(n_dimension):

temp_pos = 0

temp_vel = 0

temp_vel += w*self.particles[i].velocity[j] #inertia

temp_vel += c1*random.random()*(self.particles[i].best_position[j] - self.particles[i].position[j]) #personal

temp_vel += c2*random.random()*(best_particle_vector[j] - self.particles[i].position[j]) #social

temp_pos = self.particles[i].position[j] + temp_vel

if (temp_pos> MAX_position) or (temp_pos

self.particles[i].position, self.particles[i].velocity = initialize_pos_vel(n_dimension) #novas posicoes e velocidades

break

else:

self.particles[i].position[j] = temp_pos #nova posicao

self.particles[i].velocity[j] = temp_vel #nova velocidade

return self

def main():

swarm = Swarm()

best_global_particle_vector = swarm.particles[0].best_position #just initialization

while k

print" < <<1>> >", best_global_particle_vector," < <<1>> >"

swarm = swarm.next_pos_vel(best_gloval_particle_vector) #new positions <<<<<<------ problemaaaaaaaaaaaaaaa

print" < <<2>> >", best_global_particle_vector," < <<2>> >"

'''.. . '''

实际结果:< <<1>> > [-56.362471020090446, 48.27571332531588] < <<1>> >

< <<2>> > [-52.75857277952958, 41.70638206522979] < <<2>> >

它不应该改变列表 best_particle_vector 。 应当:< <<1>> > [-56.362471020090446, 48.27571332531588] < <<1>> >

< <<2>> > [-56.362471020090446, 48.27571332531588] < <<2>> >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值