将下面代码中的粒子群算法改成蚁群算法,直接给我修改好的完整代码:# -- coding: utf-8 -- import matplotlib…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
将下面代码中的粒子群算法改成蚁群算法,直接给我修改好的完整代码:# -- coding: utf-8 -- import matplotlib matplotlib.rcParams['font.sans-serif'] = ['SimHei'] matplotlib.rcParams['axes.unicode_minus'] = False import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import Circle, Arc, Rectangle from matplotlib.widgets import Button, RadioButtons import random
class DrillLayoutApp: def init(self): self.points = [] self.arrows = [] self.start_point = None self.contour_type = 'Rectangle'
self.fig, self.ax = plt.subplots()
plt.subplots_adjust(left=0.3)
self._init_ui()
self.draw_contour(self.ax)
self.fig.canvas.mpl_connect('button_press_event', self.on_press)
self.fig.canvas.mpl_connect('button_release_event', self.on_release)
def _init_ui(self):
ax_radio = plt.axes([0.05, 0.5, 0.2, 0.3])
self.radio = RadioButtons(ax_radio, ('Rectangle', 'Circle', 'Arc'))
self.radio.on_clicked(self.contour_select)
ax_clear = plt.axes([0.05, 0.4, 0.15, 0.05])
self.btn_clear = Button(ax_clear, '清空布孔')
self.btn_clear.on_clicked(self.clear_all)
ax_auto = plt.axes([0.05, 0.33, 0.15, 0.05])
self.btn_auto = Button(ax_auto, '自动布孔')
self.btn_auto.on_clicked(self.auto_drill)
ax_pso = plt.axes([0.05, 0.26, 0.15, 0.05])
self.btn_pso = Button(ax_pso, '孔序规划')
self.btn_pso.on_clicked(self.drill_sequence_plan)
def draw_contour(self, ax):
ax.clear()
ax.set_aspect('equal')
if self.contour_type == 'Rectangle':
rect = Rectangle((0, 0), 6, 5, fill=False, color='black', linewidth=2)
ax.add_patch(rect)
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 6)
elif self.contour_type == 'Circle':
circ = Circle((3, 3), 3, fill=False, color='black', linewidth=2)
ax.add_patch(circ)
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 7)
elif self.contour_type == 'Arc':
arc = Arc((3, 0), 6, 6, theta1=0, theta2=180, color='black', linewidth=2)
ax.add_patch(arc)
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 4)
ax.set_title(f'布孔界面 - {self.contour_type}')
ax.grid(True)
def on_press(self, event):
if event.inaxes != self.ax:
return
if event.button == 1:
self.start_point = (event.xdata, event.ydata)
elif event.button == 3:
if self.points:
click_point = np.array([event.xdata, event.ydata])
point_arr = np.array(self.points)
dists = np.linalg.norm(point_arr - click_point, axis=1)
min_index = np.argmin(dists)
if dists[min_index] < 0.3:
self.points.pop(min_index)
self.arrows.pop(min_index)
self.update_plot()
def on_release(self, event):
if event.inaxes != self.ax or self.start_point is None:
return
if event.button == 1:
end_point = (event.xdata, event.ydata)
self.points.append(self.start_point)
dx = end_point[0] - self.start_point[0]
dy = end_point[1] - self.start_point[1]
self.arrows.append((self.start_point, (dx, dy)))
self.start_point = None
self.update_plot()
def update_plot(self):
self.draw_contour(self.ax)
if self.points:
x, y = zip(*self.points)
self.ax.plot(x, y, 'ro')
for arrow in self.arrows:
x0, y0 = arrow[0]
dx, dy = arrow[1]
self.ax.arrow(x0, y0, dx, dy, head_width=0.1, head_length=0.15, fc='blue', ec='blue')
mid_x = x0 + dx / 2
mid_y = y0 + dy / 2
depth = np.hypot(dx, dy) * 3.0
self.ax.text(mid_x, mid_y, f"{depth:.1f}m", fontsize=10, color='green', ha='center')
self.fig.canvas.draw_idle()
def clear_all(self, event=None):
self.points.clear()
self.arrows.clear()
self.update_plot()
def contour_select(self, label):
self.contour_type = label
self.clear_all()
def calculate_total_distance_open(self, order, points):
if name == 'main': app = DrillLayoutApp() app.run()
以下是使用蚁群算法(Ant Colony Optimization, ACO)替换粒子群算法(PSO)的完整代码:
要求增加指定初始点的功能
我为您修改了代码,增加了指定初始点的功能。主要修改如下:
return sum(np.linalg.norm(np.array(points[order[i]]) - np.array(points[order[i+1]]))
for i in range(len(order)-1))
def total_cost(self, order, points, alpha=1.0):
"""路径长度 + 平滑度综合代价"""
total_length = sum(np.linalg.norm(np.array(points[order[i]]) - np.array(points[order[i+1]]))
for i in range(len(order)-1))
smooth_cost = 0
for i in range(1, len(order)-1):
p0 = np.array(points[order[i-1]])
p1 = np.array(points[order[i]])
p2 = np.array(points[order[i+1]])
v1 = p1 - p0
v2 = p2 - p1
norm1 = np.linalg.norm(v1)
norm2 = np.linalg.norm(v2)
if norm1 > 1e-6 and norm2 > 1e-6:
cos_theta = np.dot(v1, v2) / (norm1 * norm2)
smooth_cost += (1 - cos_theta) # 越小越好,夹角越平缓
total = total_length + alpha * smooth_cost
return total
def swap_operator(self, order):
a, b = random.sample(range(len(order)), 2)
order[a], order[b] = order[b], order[a]
return order
def pso_tsp_smooth_path(self, points, num_particles=50, max_iter=500, c1=1.5, c2=1.5, alpha=1.0):
num_points = len(points)
particles = []
pbest = []
pbest_cost = []
for _ in range(num_particles):
order = list(range(num_points))
random.shuffle(order)
particles.append(order)
cost = self.total_cost(order, points, alpha)
pbest.append(order[:])
pbest_cost.append(cost)
gbest = pbest[np.argmin(pbest_cost)][:]
gbest_cost = min(pbest_cost)
for iter in range(max_iter):
for i in range(num_particles):
new_order = particles[i][:]
if random.random() < 0.9:
new_order = self.swap_operator(new_order)
new_cost = self.total_cost(new_order, points, alpha)
if new_cost < pbest_cost[i]:
pbest[i] = new_order[:]
pbest_cost[i] = new_cost
if new_cost < gbest_cost:
gbest = new_order[:]
gbest_cost = new_cost
if random.random() < c1 / (c1 + c2):
new_order = pbest[i][:]
else:
new_order = gbest[:]
particles[i] = new_order
if iter % 50 == 0:
print(f'迭代 {iter}: 当前总代价 = {gbest_cost:.2f}')
return gbest, gbest_cost
def drill_sequence_plan(self, event=None):
if not self.points:
print("无布孔点,无法规划!")
return
order, dist = self.pso_tsp_smooth_path(self.points, alpha=1.0)
print(f'规划完成,最短路径总长:{dist:.2f} m')
self.arrows.clear()
for i in range(len(order)-1):
p1 = self.points[order[i]]
p2 = self.points[order[i+1]]
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
self.arrows.append((p1, (dx, dy)))
self.update_plot()
def auto_drill(self, event=None):
self.points.clear()
self.arrows.clear()
if self.contour_type == 'Rectangle':
width, height = 6, 5
for y in np.arange(0.5, height, 1):
for x in np.arange(0.5, width, 1):
self.points.append((x, y))
self.arrows.append(((x, y), (0, 0.5)))
elif self.contour_type == 'Circle':
center_x, center_y, radius = 3, 3, 2.5
for angle in np.linspace(0, 360, 16, endpoint=False):
theta = np.deg2rad(angle)
x = center_x + radius * np.cos(theta)
y = center_y + radius * np.sin(theta)
self.points.append((x, y))
self.arrows.append(((x, y), (0.2*np.cos(theta), 0.2*np.sin(theta))))
elif self.contour_type == 'Arc':
center_x, center_y, radius = 3, 0, 3
for angle in np.linspace(0, 180, 12):
theta = np.deg2rad(angle)
x = center_x + radius * np.cos(theta)
y = center_y + radius * np.sin(theta)
self.points.append((x, y))
self.arrows.append(((x, y), (0, 0.5)))
self.update_plot()
def run(self):
plt.show()
# -*- coding: utf-8 -*-
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Arc, Rectangle
from matplotlib.widgets import Button, RadioButtons
import random
class DrillLayoutApp:
def __init__(self):
self.points = []
self.arrows = []
self.start_point = None
self.contour_type = 'Rectangle'
self.fig, self.ax = plt.subplots()
plt.subplots_adjust(left=0.3)
self._init_ui()
self.draw_contour(self.ax)
self.fig.canvas.mpl_connect('button_press_event', self.on_press)
self.fig.canvas.mpl_connect('button_release_event', self.on_release)
def _init_ui(self):
ax_radio = plt.axes([0.05, 0.5, 0.2, 0.3])
self.radio = RadioButtons(ax_radio, ('Rectangle', 'Circle', 'Arc'))
self.radio.on_clicked(self.contour_select)
ax_clear = plt.axes([0.05, 0.4, 0.15, 0.05])
self.btn_clear = Button(ax_clear, '清空布孔')
self.btn_clear.on_clicked(self.clear_all)
ax_auto = plt.axes([0.05, 0.33, 0.15, 0.05])
self.btn_auto = Button(ax_auto, '自动布孔')
self.btn_auto.on_clicked(self.auto_drill)
ax_aco = plt.axes([0.05, 0.26, 0.15, 0.05])
self.btn_aco = Button(ax_aco, '孔序规划')
self.btn_aco.on_clicked(self.drill_sequence_plan)
def draw_contour(self, ax):
ax.clear()
ax.set_aspect('equal')
if self.contour_type == 'Rectangle':
rect = Rectangle((0, 0), 6, 5, fill=False, color='black', linewidth=2)
ax.add_patch(rect)
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 6)
elif self.contour_type == 'Circle':
circ = Circle((3, 3), 3, fill=False, color='black', linewidth=2)
ax.add_patch(circ)
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 7)
elif self.contour_type == 'Arc':
arc = Arc((3, 0), 6, 6, theta1=0, theta2=180, color='black', linewidth=2)
ax.add_patch(arc)
ax.set_xlim(-1, 7)
ax.set_ylim(-1, 4)
ax.set_title(f'布孔界面 - {self.contour_type}')
ax.grid(True)
def on_press(self, event):
if event.inaxes != self.ax:
return
if event.button == 1:
self.start_point = (event.xdata, event.ydata)
elif event.button == 3:
if self.points:
click_point = np.array([event.xdata, event.ydata])
point_arr = np.array(self.points)
dists = np.linalg.norm(point_arr - click_point, axis=1)
min_index = np.argmin(dists)
if dists[min_index] < 0.3:
self.points.pop(min_index)
self.arrows.pop(min_index)
self.update_plot()
def on_release(self, event):
if event.inaxes != self.ax or self.start_point is None:
return
if event.button == 1:
end_point = (event.xdata, event.ydata)
self.points.append(self.start_point)
dx = end_point[0] - self.start_point[0]
dy = end_point[1] - self.start_point[1]
self.arrows.append((self.start_point, (dx, dy)))
self.start_point = None
self.update_plot()
def update_plot(self):
self.draw_contour(self.ax)
if self.points:
x, y = zip(*self.points)
self.ax.plot(x, y, 'ro')
for arrow in self.arrows:
x0, y0 = arrow[0]
dx, dy = arrow[1]
self.ax.arrow(x0, y0, dx, dy, head_width=0.1, head_length=0.15, fc='blue', ec='blue')
mid_x = x0 + dx / 2
mid_y = y0 + dy / 2
depth = np.hypot(dx, dy) * 3.0
self.ax.text(mid_x, mid_y, f"{depth:.1f}m", fontsize=10, color='green', ha='center')
self.fig.canvas.draw_idle()
def clear_all(self, event=None):
self.points.clear()
self.arrows.clear()
self.update_plot()
def contour_select(self, label):
self.contour_type = label
self.clear_all()
def calculate_total_distance_open(self, order, points):
"""非闭合路径长度"""
return sum(np.linalg.norm(np.array(points[order[i]]) - np.array(points[order[i+1]]))
for i in range(len(order)-1))
def total_cost(self, order, points, alpha=1.0):
"""路径长度 + 平滑度综合代价"""
total_length = sum(np.linalg.norm(np.array(points[order[i]]) - np.array(points[order[i+1]]))
for i in range(len(order)-1))
smooth_cost = 0
for i in range(1, len(order)-1):
p0 = np.array(points[order[i-1]])
p1 = np.array(points[order[i]])
p2 = np.array(points[order[i+1]])
v1 = p1 - p0
v2 = p2 - p1
norm1 = np.linalg.norm(v1)
norm2 = np.linalg.norm(v2)
if norm1 > 1e-6 and norm2 > 1e-6:
cos_theta = np.dot(v1, v2) / (norm1 * norm2)
smooth_cost += (1 - cos_theta) # 越小越好,夹角越平缓
total = total_length + alpha * smooth_cost
return total
def aco_tsp_smooth_path(self, points, num_ants=50, max_iter=500, alpha=1.0, beta=2.0, rho=0.5, Q=100):
num_points = len(points)
# 初始化信息素矩阵
pheromone = np.ones((num_points, num_points))
np.fill_diagonal(pheromone, 0)
# 计算距离矩阵
distance_matrix = np.zeros((num_points, num_points))
for i in range(num_points):
for j in range(num_points):
distance_matrix[i][j] = np.linalg.norm(np.array(points[i]) - np.array(points[j]))
best_order = None
best_cost = float('inf')
for iter in range(max_iter):
ant_paths = []
ant_costs = []
for _ in range(num_ants):
# 蚂蚁构建路径
unvisited = list(range(num_points))
current = random.choice(unvisited)
path = [current]
unvisited.remove(current)
while unvisited:
probabilities = []
for next_city in unvisited:
prob = (pheromone[current][next_city] ** alpha) * ((1.0 / (distance_matrix[current][next_city] + 1e-10)) ** beta)
probabilities.append(prob)
probabilities = np.array(probabilities)
probabilities /= probabilities.sum()
next_city = np.random.choice(unvisited, p=probabilities)
path.append(next_city)
unvisited.remove(next_city)
current = next_city
path_cost = self.total_cost(path, points, alpha)
ant_paths.append(path)
ant_costs.append(path_cost)
# 更新全局最优解
if path_cost < best_cost:
best_order = path[:]
best_cost = path_cost
# 信息素蒸发
pheromone *= (1 - rho)
# 信息素更新
for path, path_cost in zip(ant_paths, ant_costs):
for i in range(len(path) - 1):
pheromone[path[i]][path[i+1]] += Q / path_cost
pheromone[path[i+1]][path[i]] += Q / path_cost
if iter % 50 == 0:
print(f'迭代 {iter}: 当前总代价 = {best_cost:.2f}')
return best_order, best_cost
def drill_sequence_plan(self, event=None):
if not self.points:
print("无布孔点,无法规划!")
return
order, dist = self.aco_tsp_smooth_path(self.points, alpha=1.0)
print(f'规划完成,最短路径总长:{dist:.2f} m')
self.arrows.clear()
for i in range(len(order)-1):
p1 = self.points[order[i]]
p2 = self.points[order[i+1]]
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
self.arrows.append((p1, (dx, dy)))
self.update_plot()
def auto_drill(self, event=None):
self.points.clear()
self.arrows.clear()
if self.contour_type == 'Rectangle':
width, height = 6, 5
for y in np.arange(0.5, height, 1):
for x in np.arange(0.5, width, 1):
self.points.append((x, y))
self.arrows.append(((x, y), (0, 0.5)))
elif self.contour_type == 'Circle':
center_x, center_y, radius = 3, 3, 2.5
for angle in np.linspace(0, 360, 16, endpoint=False):
theta = np.deg2rad(angle)
x = center_x + radius * np.cos(theta)
y = center_y + radius * np.sin(theta)
self.points.append((x, y))
self.arrows.append(((x, y), (0.2*np.cos(theta), 0.2*np.sin(theta))))
elif self.contour_type == 'Arc':
center_x, center_y, radius = 3, 0, 3
for angle in np.linspace(0, 180, 12):
theta = np.deg2rad(angle)
x = center_x + radius * np.cos(theta)
y = center_y + radius * np.sin(theta)
self.points.append((x, y))
self.arrows.append(((x, y), (0, 0.5)))
self.update_plot()
def run(self):
plt.show()
if __name__ == '__main__':
app = DrillLayoutApp()
app.run()
主要修改的部分是将pso_tsp_smooth_path方法替换为aco_tsp_smooth_path方法,并相应地更新了按钮标签和方法名称。蚁群算法(ACO)的实现包括:
关键参数说明:
num_ants:蚂蚁数量max_iter:最大迭代次数alpha:信息素重要性参数beta:启发式信息重要性参数rho:信息素蒸发率Q:信息素常数其他代码逻辑保持不变。运行效果与之前的粒子群算法类似,但使用了蚁群算法的路径规划策略。
# -*- coding: utf-8 -*-
import matplotlib
matplotlib.rcParams['font.sans-serif'] = ['SimHei']
matplotlib.rcParams['axes.unicode_minus'] = False
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Arc, Rectangle
from matplotlib.widgets import Button, RadioButtons, TextBox
import random
class DrillLayoutApp:
def __init__(self):
self.points = []
self.arrows = []
self.start_point = None
self.contour_type = 'Rectangle'
self.initial_point_index = None # 新增初始点索引
self.fig, self.ax = plt.subplots()
plt.subplots_adjust(left=0.3)
self._init_ui()
self.draw_contour(self.ax)
self.fig.canvas.mpl_connect('button_press_event', self.on_press)
self.fig.canvas.mpl_connect('button_release_event', self.on_release)
def _init_ui(self):
# 原有的UI初始化代码...
# 增加初始点设置输入框
ax_initial_point = plt.axes([0.05, 0.19, 0.15, 0.05])
self.txt_initial_point = TextBox(ax_initial_point, '初始点索引')
self.txt_initial_point.on_submit(self.set_initial_point)
def set_initial_point(self, text):
try:
index = int(text)
if 0 <= index < len(self.points):
self.initial_point_index = index
print(f"已设置初始点为索引 {index}")
else:
print("索引超出范围")
except ValueError:
print("请输入有效的数字索引")
def aco_tsp_smooth_path(self, points, num_ants=50, max_iter=500, alpha=1.0, beta=2.0, rho=0.5, Q=100, initial_point_index=None):
num_points = len(points)
# 初始化信息素矩阵
pheromone = np.ones((num_points, num_points))
np.fill_diagonal(pheromone, 0)
# 计算距离矩阵
distance_matrix = np.zeros((num_points, num_points))
for i in range(num_points):
for j in range(num_points):
distance_matrix[i][j] = np.linalg.norm(np.array(points[i]) - np.array(points[j]))
best_order = None
best_cost = float('inf')
for iter in range(max_iter):
ant_paths = []
ant_costs = []
for _ in range(num_ants):
# 蚂蚁构建路径
unvisited = list(range(num_points))
# 如果指定了初始点,从初始点开始
if initial_point_index is not None:
current = initial_point_index
path = [current]
unvisited.remove(current)
else:
current = random.choice(unvisited)
path = [current]
unvisited.remove(current)
while unvisited:
probabilities = []
for next_city in unvisited:
prob = (pheromone[current][next_city] ** alpha) * ((1.0 / (distance_matrix[current][next_city] + 1e-10)) ** beta)
probabilities.append(prob)
probabilities = np.array(probabilities)
probabilities /= probabilities.sum()
next_city = np.random.choice(unvisited, p=probabilities)
path.append(next_city)
unvisited.remove(next_city)
current = next_city
path_cost = self.total_cost(path, points, alpha)
ant_paths.append(path)
ant_costs.append(path_cost)
# 更新全局最优解
if path_cost < best_cost:
best_order = path[:]
best_cost = path_cost
# 信息素蒸发
pheromone *= (1 - rho)
# 信息素更新
for path, path_cost in zip(ant_paths, ant_costs):
for i in range(len(path) - 1):
pheromone[path[i]][path[i+1]] += Q / path_cost
pheromone[path[i+1]][path[i]] += Q / path_cost
if iter % 50 == 0:
print(f'迭代 {iter}: 当前总代价 = {best_cost:.2f}')
return best_order, best_cost
def drill_sequence_plan(self, event=None):
if not self.points:
print("无布孔点,无法规划!")
return
# 使用初始点索引(如果已设置)
order, dist = self.aco_tsp_smooth_path(
self.points,
alpha=1.0,
initial_point_index=self.initial_point_index
)
print(f'规划完成,最短路径总长:{dist:.2f} m')
self.arrows.clear()
for i in range(len(order)-1):
p1 = self.points[order[i]]
p2 = self.points[order[i+1]]
dx = p2[0] - p1[0]
dy = p2[1] - p1[1]
self.arrows.append((p1, (dx, dy)))
self.update_plot()
# 其他方法保持不变
主要增加和修改的功能:
在_init_ui()方法中增加了一个文本输入框,用于输入初始点的索引。
增加了set_initial_point()方法,用于处理初始点索引的设置。
修改aco_tsp_smooth_path()方法,增加了initial_point_index参数,使算法能够从指定的初始点开始构建路径。
在drill_sequence_plan()方法中,调用aco_tsp_smooth_path()时传入初始点索引。
使用方法:
注意事项:
这个版本保留了原有的所有功能,同时增加了指定初始点的灵活性。