Coords-NSGA2 - 基于坐标的多目标优化算法库

本文最后更新于 2025年8月24日 晚上

Coords-NSGA2: 基于坐标的多目标优化算法库

GitHub Tag

概述

Coords-NSGA2 是一个专门为坐标点布局优化而设计的Python库,基于经典的NSGA-II(非支配排序遗传算法II)算法改进而来。

主要特性

  • 坐标优化专用:专门为优化坐标点布局而设计
  • 专业约束条件:内置支持点间距、边界限制和自定义约束
  • 定制遗传算子:专门作用于坐标点的交叉和变异算子
  • 多目标优化:基于成熟的NSGA-II算法
  • 并行计算加速:支持计算密集型问题的并行处理加速
  • 灵活区域定义:支持多边形和矩形区域
  • 轻量级可扩展:易于自定义算子和约束条件
  • 进度跟踪:内置进度条和优化历史记录
  • 保存/加载功能:保存和恢复优化状态

应用场景

  • 风力发电机布局优化
  • 传感器网络部署
  • 设施选址问题
  • 其他需要优化坐标点布局的场景

安装方法

从PyPI安装(推荐)

1
pip install coords-nsga2

从源码安装

1
2
3
git clone https://github.com/ZXF1001/coords-nsga2.git
cd coords-nsga2
pip install -e .

系统要求

  • Python 3.8+
  • NumPy >= 1.23
  • tqdm >= 4
  • Shapely >= 2
  • SciPy (可选,用于距离计算)

快速开始

以下是一个演示如何运行基于坐标的NSGA-II优化的最小示例(支持任意多个目标):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
from scipy.spatial import distance
from coords_nsga2 import CoordsNSGA2, Problem
from coords_nsga2.spatial import region_from_points

# 定义优化区域
region = region_from_points([
[0, 0],
[1, 0],
[2, 1],
[1, 1],
])

# 定义目标函数
def objective_1(coords):
"""最大化x和y坐标的和"""
return np.sum(coords[:, 0]) + np.sum(coords[:, 1])

def objective_2(coords):
"""最大化点的分布"""
return np.std(coords[:, 0]) + np.std(coords[:, 1])

# 定义约束条件
spacing = 0.05
def constraint_1(coords):
"""点之间的最小间距"""
dist_list = distance.pdist(coords)
penalty_list = spacing - dist_list[dist_list < spacing]
return np.sum(penalty_list)

# 设置问题(传入目标函数列表)
problem = Problem(
objectives=[objective_1, objective_2],
n_points=10,
region=region,
constraints=[constraint_1]
)

# 初始化优化器
optimizer = CoordsNSGA2(
problem=problem,
pop_size=20,
prob_crs=0.5,
prob_mut=0.1
)

# 运行优化
result = optimizer.run(1000)

# 可视化各目标函数的最优布局
optimizer.plot.optimal_coords(obj_indices=0)

# 访问结果
print(f"结果形状: {result.shape}")
print(f"优化历史长度: {len(optimizer.P_history)}")

交流与反馈

贡献

欢迎贡献!请随时提交拉取请求。对于重大更改,请先打开一个问题来讨论您想要更改的内容。

  1. Fork 该仓库
  2. 创建您的功能分支 (git checkout -b feature/AmazingFeature)
  3. 提交您的更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 打开拉取请求

引用

如果您在研究中使用了这个库,请引用:

1
2
3
4
5
6
@software{Coords-NSGA2,
title={Coords-NSGA2: A Python library for coordinate-based multi-objective optimization},
author={Zhang, Xiaofeng},
year={2025},
url={https://github.com/ZXF1001/coords-nsga2}
}