粒子群算法解决背包问题的代码
时间: 2023-10-16 12:12:20 浏览: 62
以下是使用粒子群算法解决背包问题的 Python 代码示例:
```python
import random
class Particle:
def __init__(self, items, capacity):
self.items = items
self.capacity = capacity
self.position = [random.randint(0, 1) for _ in range(len(items))]
self.velocity = [random.uniform(0, 1) for _ in range(len(items))]
self.best_position = self.position.copy()
self.best_value = self.evaluate(self.position)
def evaluate(self, position):
total_weight = sum([self.items[i][0] * position[i] for i in range(len(position))])
total_value = sum([self.items[i][1] * position[i] for i in range(len(position))])
if total_weight > self.capacity:
return 0
else:
return total_value
def update_velocity(self, global_best_position, omega, phi_p, phi_g):
for i in range(len(self.velocity)):
rp = random.uniform(0, 1)
rg = random.uniform(0, 1)
self.velocity[i] = omega * self.velocity[i] + phi_p * rp * (self.best_position[i] - self.position[i]) + phi_g * rg * (global_best_position[i] - self.position[i])
def update_position(self):
for i in range(len(self.position)):
self.position[i] = 1 if random.uniform(0, 1) < self.sigmoid(self.velocity[i]) else 0
def sigmoid(self, x):
return 1 / (1 + pow(2.718, -x))
class PSO:
def __init__(self, items, capacity, num_particles, num_iterations, omega, phi_p, phi_g):
self.items = items
self.capacity = capacity
self.num_particles = num_particles
self.num_iterations = num_iterations
self.omega = omega
self.phi_p = phi_p
self.phi_g = phi_g
self.particles = [Particle(items, capacity) for _ in range(num_particles)]
self.global_best_position = self.particles[0].best_position.copy()
self.global_best_value = self.particles[0].best_value
def optimize(self):
for i in range(self.num_iterations):
for particle in self.particles:
if particle.evaluate(particle.position) > particle.evaluate(particle.best_position):
particle.best_position = particle.position.copy()
particle.best_value = particle.evaluate(particle.position)
if particle.evaluate(particle.position) > self.global_best_value:
self.global_best_position = particle.position.copy()
self.global_best_value = particle.evaluate(particle.position)
for particle in self.particles:
particle.update_velocity(self.global_best_position, self.omega, self.phi_p, self.phi_g)
particle.update_position()
return self.global_best_position, self.global_best_value
# Example usage
items = [(6, 30), (3, 14), (4, 16), (2, 9)]
capacity = 10
num_particles = 10
num_iterations = 50
omega = 0.5
phi_p = 0.5
phi_g = 0.5
pso = PSO(items, capacity, num_particles, num_iterations, omega, phi_p, phi_g)
best_position, best_value = pso.optimize()
print("Best position:", best_position)
print("Best value:", best_value)
```
在上面的代码中,我们首先定义了一个 Particle 类来表示粒子,每个粒子都有一个二进制向量表示哪些物品被放入背包中,以及它们的速度和当前最优位置和价值。然后,我们定义了一个 PSO 类来实现整个算法,它包含了一个粒子群(即一组粒子),以及一些参数(如惯性权重 omega,个体和全局的加速因子 phi_p 和 phi_g)。在 optimize 方法中,我们迭代一定次数,对于每个粒子,我们计算它的价值并更新它的最优位置和价值。然后,我们根据全局最优位置和一些参数更新每个粒子的速度和位置。最后,我们返回全局最优位置和价值作为结果。
需要注意的是,此代码仅适用于 0/1 背包问题,如果要解决其他类型的背包问题,则需要相应地调整代码。
阅读全文