This commit is contained in:
deastern 2025-03-17 16:40:01 +08:00
commit 3f494ad234
384 changed files with 1014420 additions and 0 deletions

3
MANIFEST.in Normal file
View File

@ -0,0 +1,3 @@
recursive-include src/lead/problems */dataset/data/*.json
recursive-include src/lead/problems */problem_config.json
recursive-include src/lead/problems */__init__.py

Binary file not shown.

After

Width:  |  Height:  |  Size: 188 KiB

View File

@ -0,0 +1,69 @@
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
import matplotlib.font_manager as fm
# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号
# 生成随机种群
np.random.seed(42)
n_points = 50
points = np.random.rand(n_points, 2) * 10
# 识别帕累托前沿
def is_pareto_efficient(costs):
is_efficient = np.ones(costs.shape[0], dtype=bool)
for i, c in enumerate(costs):
if is_efficient[i]:
is_efficient[is_efficient] = np.any(costs[is_efficient] < c, axis=1) # Remove dominated points
is_efficient[i] = True
return is_efficient
# 获取帕累托前沿点
pareto_front_mask = is_pareto_efficient(points)
pareto_front = points[pareto_front_mask]
# 创建图形
plt.figure(figsize=(10, 8))
# 绘制非帕累托前沿点
plt.scatter(points[~pareto_front_mask, 0], points[~pareto_front_mask, 1],
c='lightgray', s=100, label='种群个体')
# 绘制帕累托前沿点
plt.scatter(pareto_front[:, 0], pareto_front[:, 1],
c='red', s=100, label='帕累托前沿个体')
# 连接帕累托前沿点
sorted_front = pareto_front[np.argsort(pareto_front[:, 0])]
plt.plot(sorted_front[:, 0], sorted_front[:, 1], 'r--', alpha=0.5)
# 添加密度分布示意
for point in pareto_front:
circle = plt.Circle(point, 0.5, color='blue', fill=False, alpha=0.2)
plt.gca().add_patch(circle)
# 添加箭头和标注
plt.arrow(8, 8, -0.5, -0.5, head_width=0.2, head_length=0.2, fc='k', ec='k')
plt.text(8.2, 8.2, '优化方向', fontsize=20)
# 设置标题和标签
# plt.title('帕累托前沿维护策略示意图', fontsize=14, pad=20)
plt.xlabel('算法性能指标', fontsize=20)
plt.ylabel('运行时间', fontsize=20)
# 添加图例
plt.legend(loc='upper right', fontsize=20)
# 添加网格
plt.grid(True, linestyle='--', alpha=0.3)
# 设置坐标轴范围
plt.xlim(-0.5, 11)
plt.ylim(-0.5, 11)
# 保存图片
plt.savefig('pareto_front_strategy.png', dpi=300, bbox_inches='tight')
plt.close()

BIN
frame_plot/rador.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 722 KiB

213
frame_plot/show_frame.ipynb Normal file
View File

@ -0,0 +1,213 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"from matplotlib.patches import Polygon\n",
"import matplotlib.font_manager as fm\n",
"\n",
"# 设置中文字体\n",
"plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签\n",
"plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号\n",
"\n",
"# 生成随机种群\n",
"np.random.seed(42)\n",
"n_points = 50\n",
"points = np.random.rand(n_points, 2) * 10\n",
"\n",
"# 识别帕累托前沿\n",
"def is_pareto_efficient(costs):\n",
" is_efficient = np.ones(costs.shape[0], dtype=bool)\n",
" for i, c in enumerate(costs):\n",
" if is_efficient[i]:\n",
" is_efficient[is_efficient] = np.any(costs[is_efficient] < c, axis=1) # Remove dominated points\n",
" is_efficient[i] = True\n",
" return is_efficient\n",
"\n",
"# 获取帕累托前沿点\n",
"pareto_front_mask = is_pareto_efficient(points)\n",
"pareto_front = points[pareto_front_mask]\n",
"\n",
"# 创建图形\n",
"plt.figure(figsize=(10, 8))\n",
"\n",
"# 绘制非帕累托前沿点\n",
"plt.scatter(points[~pareto_front_mask, 0], points[~pareto_front_mask, 1], \n",
" c='lightgray', s=100, label='种群个体')\n",
"\n",
"# 绘制帕累托前沿点\n",
"plt.scatter(pareto_front[:, 0], pareto_front[:, 1], \n",
" c='red', s=100, label='帕累托前沿个体')\n",
"\n",
"# 连接帕累托前沿点\n",
"sorted_front = pareto_front[np.argsort(pareto_front[:, 0])]\n",
"plt.plot(sorted_front[:, 0], sorted_front[:, 1], 'r--', alpha=0.5)\n",
"\n",
"# 添加密度分布示意\n",
"for point in pareto_front:\n",
" circle = plt.Circle(point, 0.5, color='blue', fill=False, alpha=0.2)\n",
" plt.gca().add_patch(circle)\n",
"\n",
"# 添加箭头和标注\n",
"plt.arrow(8, 8, -0.5, -0.5, head_width=0.2, head_length=0.2, fc='k', ec='k')\n",
"plt.text(8.2, 8.2, '优化方向', fontsize=20)\n",
"\n",
"# 设置标题和标签\n",
"# plt.title('帕累托前沿维护策略示意图', fontsize=14, pad=20)\n",
"plt.xlabel('算法性能指标', fontsize=20)\n",
"plt.ylabel('运行时间', fontsize=20)\n",
"\n",
"# 添加图例\n",
"plt.legend(loc='upper right', fontsize=20)\n",
"\n",
"# 添加网格\n",
"plt.grid(True, linestyle='--', alpha=0.3)\n",
"\n",
"# 设置坐标轴范围\n",
"plt.xlim(-0.5, 11)\n",
"plt.ylim(-0.5, 11)\n",
"\n",
"# 保存图片\n",
"plt.savefig('pareto_front_strategy.png', dpi=300, bbox_inches='tight')\n",
"plt.close() "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text1 = '''贪心\n",
"回溯\n",
"高度优先\n",
"图着色\n",
"颜色分配\n",
"\n",
"饱和度优先\n",
"回溯\n",
"相邻顶点\n",
"图着色\n",
"颜色约束\n",
"\n",
"贪心\n",
"回溯\n",
"高度优先\n",
"使用次数\n",
"颜色选择\n",
"\n",
"贪心\n",
"相邻未着色\n",
"颜色选择\n",
"冲突减少\n",
"图着色\n",
"\n",
"饱和度优先\n",
"动态更新\n",
"可用颜色\n",
"相邻顶点\n",
"图着色\n",
"\n",
"饱和度\n",
"度数选择\n",
"相邻顶点\n",
"颜色使用\n",
"图着色\n",
"\n",
"动态更新\n",
"可用颜色\n",
"相邻冲突\n",
"图着色\n",
"颜色选择\n",
"\n",
"饱和度优先\n",
"动态更新\n",
"相邻顶点\n",
"颜色选择\n",
"图着色\n",
"'''\n",
"text2 = '''\n",
"贪心\n",
"度数排序\n",
"颜色分配\n",
"图着色\n",
"最低可用颜色\n",
"\n",
"优化\n",
"优先队列\n",
"颜色分配\n",
"贪心\n",
"图着色\n",
"\n",
"度数排序\n",
"邻居检查\n",
"最小颜色\n",
"图着色\n",
"贪心\n",
"\n",
"修改贪心\n",
"邻居历史\n",
"颜色最小化\n",
"图着色\n",
"颜色选择\n",
"\n",
"Welsh-Powell\n",
"度数排序\n",
"颜色分配\n",
"贪心\n",
"图着色\n",
"\n",
"贪心\n",
"迭代着色\n",
"相邻约束\n",
"图着色\n",
"颜色选择\n",
"\n",
"Welsh-Powell\n",
"贪心\n",
"降序着色\n",
"图着色\n",
"颜色分配\n",
"\n",
"贪心\n",
"度数优先\n",
"最小颜色\n",
"相邻约束\n",
"图着色'''"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "lead",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

737
gcdata/DSJC0125.1.txt Normal file
View File

@ -0,0 +1,737 @@
125 736 5
4 0
5 1
7 3
8 3
8 5
10 1
12 4
13 6
13 8
13 12
14 7
15 9
15 11
16 1
17 11
18 4
18 7
18 10
20 6
20 7
21 16
22 12
23 20
24 2
24 9
26 1
26 5
27 8
27 16
27 18
28 0
28 23
29 4
29 14
29 27
30 9
30 26
31 1
31 10
31 17
32 9
33 11
33 31
34 2
34 4
34 11
34 14
34 15
34 23
35 19
35 26
35 34
36 12
36 21
37 2
37 3
37 5
37 16
38 25
39 1
39 36
40 2
40 6
40 15
40 21
40 34
41 5
41 6
41 7
41 11
41 18
41 24
42 28
42 32
42 34
43 0
43 33
44 15
44 23
45 1
45 9
45 11
45 12
45 24
45 25
45 35
46 11
46 19
46 29
46 35
46 45
47 18
47 23
48 8
48 36
48 42
49 21
49 22
49 23
49 31
49 40
49 46
50 6
50 16
50 26
50 34
50 40
50 41
50 47
51 8
51 11
51 13
51 16
51 27
51 47
51 48
52 0
52 27
52 29
52 43
53 21
53 28
53 47
54 14
54 24
54 42
54 48
54 49
54 53
55 2
55 5
55 34
55 37
55 43
56 7
56 13
56 25
56 29
56 35
56 42
56 49
57 7
57 16
57 48
57 51
57 54
58 15
58 36
58 41
58 50
58 56
59 55
59 56
60 8
60 13
60 16
60 30
60 35
60 46
60 51
60 57
61 2
61 7
61 9
61 15
61 23
61 31
61 36
61 44
61 52
61 53
61 54
61 55
62 2
62 10
62 23
62 28
62 29
62 32
62 34
62 37
62 38
62 50
62 51
63 28
63 41
63 44
63 54
63 56
63 57
64 4
64 7
64 20
64 25
64 35
64 36
64 47
64 57
65 8
65 13
65 14
65 17
65 18
65 27
65 36
65 61
65 63
66 5
66 9
66 10
66 21
66 30
66 34
66 36
66 44
66 46
67 21
67 34
67 42
67 52
68 3
68 5
68 14
68 24
68 28
68 58
68 62
68 64
68 66
69 26
69 27
69 30
69 31
69 33
70 10
70 22
70 39
70 55
71 3
71 11
71 25
71 38
71 39
72 6
72 8
72 11
72 23
72 47
72 49
72 50
72 71
73 20
73 24
73 27
73 30
73 38
73 58
73 65
74 5
74 17
74 61
74 64
74 65
75 13
75 23
75 29
75 31
75 34
75 39
76 4
76 6
76 8
76 10
76 36
76 54
76 57
76 59
76 60
76 63
76 68
77 15
77 42
77 48
77 68
77 71
78 0
78 1
78 13
78 38
78 68
78 73
79 0
79 13
79 40
79 51
79 67
79 75
80 9
80 19
80 27
81 4
81 5
81 21
81 25
81 36
81 39
81 50
81 57
81 73
81 77
81 79
81 80
82 12
82 44
82 49
82 53
82 68
82 75
83 4
83 8
83 12
83 16
83 17
83 20
83 23
83 41
83 48
83 52
83 55
83 56
83 62
83 76
83 77
84 19
84 21
84 30
84 31
84 39
84 64
84 78
85 13
85 21
85 31
85 32
85 47
85 67
85 80
85 81
86 9
86 13
86 15
86 43
86 70
86 71
86 75
87 2
87 10
87 23
87 25
87 36
87 37
87 41
88 40
88 44
88 50
88 61
88 73
88 78
88 83
89 17
89 21
89 43
89 52
89 61
89 69
90 8
90 21
90 23
90 30
90 35
90 45
90 49
90 52
90 63
90 67
90 71
90 77
90 79
90 80
90 87
91 4
91 14
91 20
91 26
91 39
91 51
91 61
91 72
91 73
91 85
92 14
92 25
92 32
92 44
92 47
92 58
92 60
92 69
92 82
92 90
93 16
93 37
93 38
93 69
93 70
93 75
93 80
93 89
94 59
94 60
94 76
94 83
94 91
94 93
95 8
95 10
95 12
95 34
95 60
95 68
95 74
95 76
95 90
96 19
96 43
96 53
96 54
96 57
96 67
96 68
96 72
96 76
96 79
96 83
96 89
96 92
96 95
97 6
97 15
97 16
97 18
97 22
97 26
97 31
97 48
97 70
97 74
97 82
97 88
98 3
98 12
98 16
98 26
98 46
98 48
98 55
98 68
98 75
98 88
99 8
99 11
99 13
99 47
99 57
99 84
100 8
100 12
100 31
100 57
100 65
100 76
100 83
100 96
101 16
101 19
101 35
101 41
101 51
101 65
101 83
101 85
102 7
102 10
102 11
102 13
102 26
102 31
102 33
102 35
102 37
102 46
102 58
102 64
102 68
103 15
103 48
103 73
103 75
103 82
103 84
103 101
104 1
104 3
104 7
104 15
104 31
104 38
104 46
104 55
104 72
104 79
104 82
104 87
104 103
105 10
105 15
105 24
105 26
105 41
105 45
105 50
105 63
105 69
105 76
105 79
105 81
105 87
105 92
105 99
106 41
106 44
106 48
106 50
106 71
106 73
106 76
106 87
107 40
107 41
107 50
107 57
107 63
107 65
107 82
108 5
108 68
108 74
109 7
109 9
109 22
109 40
109 48
109 49
109 59
109 70
109 75
109 76
109 77
109 83
109 88
109 96
109 107
110 8
110 24
110 39
110 45
110 47
110 51
110 52
110 60
110 61
110 78
110 97
111 2
111 16
111 23
111 31
111 53
111 57
111 65
111 77
112 0
112 3
112 16
112 18
112 22
112 52
112 70
112 71
112 95
112 107
112 111
113 15
113 17
113 18
113 48
113 50
113 56
113 79
113 82
113 93
113 97
114 2
114 22
114 30
114 46
114 52
114 63
114 77
114 88
114 91
114 92
114 99
114 102
114 108
114 109
114 113
115 2
115 12
115 14
115 25
115 26
115 37
115 66
115 67
115 83
115 106
116 25
116 34
116 49
116 50
116 79
116 81
116 95
116 102
117 4
117 9
117 28
117 51
117 53
117 55
117 69
117 92
117 108
117 109
117 114
118 12
118 30
118 39
118 54
118 57
118 63
118 67
118 70
118 107
118 113
118 115
118 116
118 117
119 0
119 5
119 21
119 23
119 26
119 43
119 46
119 90
119 98
120 4
120 10
120 32
120 51
120 63
120 92
120 112
120 117
121 10
121 16
121 40
121 41
121 43
121 44
121 50
121 52
121 58
121 65
121 83
121 88
121 95
121 100
121 103
121 106
121 107
122 0
122 5
122 17
122 22
122 62
122 80
122 82
122 87
122 91
122 92
122 115
123 19
123 26
123 32
123 39
123 46
123 54
123 62
123 71
123 101
123 104
123 109
123 116
123 121
124 33
124 48
124 59
124 61
124 79
124 84
124 98
124 104
124 109

3892
gcdata/DSJC0125.5.txt Normal file

File diff suppressed because it is too large Load Diff

6962
gcdata/DSJC0125.9.txt Normal file

File diff suppressed because it is too large Load Diff

3219
gcdata/DSJC0250.1.txt Normal file

File diff suppressed because it is too large Load Diff

15669
gcdata/DSJC0250.5.txt Normal file

File diff suppressed because it is too large Load Diff

27898
gcdata/DSJC0250.9.txt Normal file

File diff suppressed because it is too large Load Diff

12459
gcdata/DSJC0500.1.txt Normal file

File diff suppressed because it is too large Load Diff

62625
gcdata/DSJC0500.5.txt Normal file

File diff suppressed because it is too large Load Diff

112438
gcdata/DSJC0500.9.txt Normal file

File diff suppressed because it is too large Load Diff

49630
gcdata/DSJC1000.1.txt Normal file

File diff suppressed because it is too large Load Diff

249827
gcdata/DSJC1000.5.txt Normal file

File diff suppressed because it is too large Load Diff

449450
gcdata/DSJC1000.9.txt Normal file

File diff suppressed because it is too large Load Diff

0
gcdata/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

Binary file not shown.

1640
gcdata/gc.py Normal file

File diff suppressed because it is too large Load Diff

666
gcdata/gc1.py Normal file
View File

@ -0,0 +1,666 @@
def graph_coloring_v8(adj_matrix):
"""高级混合图着色算法
结合多种算法优点使用数学优化和多阶段策略尽可能减少所需颜色数
1. 图分解与重组
2. 多阶段混合策略
3. 高级预处理与结构识别
4. 迭代颜色减少
5. 自适应参数调整
"""
import numpy as np
import random
import heapq
import time
from collections import defaultdict, deque
from itertools import combinations
start_time = time.time()
max_time = 60 # 最大运行时间(秒)
n = adj_matrix.shape[0]
best_coloring = np.full(n, -1)
best_color_count = n
# 构建邻接表以加速计算
adj_lists = [np.where(adj_matrix[i] == 1)[0] for i in range(n)]
# 计算顶点度数
degrees = np.sum(adj_matrix, axis=1)
max_degree = np.max(degrees)
# 1. 高级预处理与结构识别
def find_max_clique():
"""使用Bron-Kerbosch算法寻找最大团"""
def bron_kerbosch(r, p, x, max_clique):
if len(p) == 0 and len(x) == 0:
if len(r) > len(max_clique[0]):
max_clique[0] = r.copy()
return
# 选择枢轴点以优化分支
if p:
pivot = max(p, key=lambda v: len(set(adj_lists[v]) & p))
p_minus_n_pivot = p - set(adj_lists[pivot])
else:
p_minus_n_pivot = p
for v in list(p_minus_n_pivot):
neighbors_v = set(adj_lists[v])
bron_kerbosch(r | {v}, p & neighbors_v, x & neighbors_v, max_clique)
p.remove(v)
x.add(v)
# 提前终止条件
if time.time() - start_time > max_time / 10:
return
max_clique = [set()]
vertices = set(range(n))
# 使用度数排序启发式加速
sorted_vertices = sorted(range(n), key=lambda v: degrees[v], reverse=True)
for v in sorted_vertices[:min(100, n)]: # 限制初始顶点数量
if time.time() - start_time > max_time / 10:
break
r = {v}
p = set(adj_lists[v]) & vertices
x = vertices - p - r
bron_kerbosch(r, p, x, max_clique)
return list(max_clique[0])
def identify_independent_sets():
"""识别大的独立集"""
independent_sets = []
remaining = set(range(n))
while remaining and time.time() - start_time < max_time / 10:
# 选择度数最小的顶点开始
start_vertex = min(remaining, key=lambda v: degrees[v])
ind_set = {start_vertex}
candidates = remaining - {start_vertex} - set(adj_lists[start_vertex])
# 贪心扩展独立集
while candidates:
# 选择度数最小的顶点加入
v = min(candidates, key=lambda v: degrees[v])
ind_set.add(v)
candidates -= {v} | set(adj_lists[v])
independent_sets.append(ind_set)
remaining -= ind_set
return independent_sets
def find_bipartite_subgraphs():
"""识别二分图子结构"""
bipartite_subgraphs = []
visited = np.zeros(n, dtype=bool)
def bfs_bipartite(start):
queue = deque([(start, 0)]) # (vertex, color)
coloring = {start: 0}
component = {start}
while queue and time.time() - start_time < max_time / 10:
v, color = queue.popleft()
next_color = 1 - color
for u in adj_lists[v]:
if u not in coloring:
coloring[u] = next_color
component.add(u)
queue.append((u, next_color))
elif coloring[u] == color: # 冲突,不是二分图
return None
# 分离两个部分
part0 = {v for v, c in coloring.items() if c == 0}
part1 = {v for v, c in coloring.items() if c == 1}
return (part0, part1)
# 寻找所有连通的二分子图
for i in range(n):
if not visited[i] and time.time() - start_time < max_time / 10:
result = bfs_bipartite(i)
if result:
part0, part1 = result
bipartite_subgraphs.append((part0, part1))
for v in part0 | part1:
visited[v] = True
return bipartite_subgraphs
# 2. 图分解技术
def decompose_graph():
"""将图分解为更易处理的组件"""
# 寻找割点和桥
articulation_points = find_articulation_points()
# 移除割点后的连通分量
components = []
visited = np.zeros(n, dtype=bool)
def dfs(v, component):
visited[v] = True
component.add(v)
for u in adj_lists[v]:
if not visited[u] and u not in articulation_points:
dfs(u, component)
# 找出所有连通分量
for i in range(n):
if not visited[i] and i not in articulation_points:
component = set()
dfs(i, component)
if component:
components.append(component)
# 如果没有找到有效分解,返回整个图
if not components:
return [set(range(n))]
return components
def find_articulation_points():
"""使用Tarjan算法寻找割点"""
disc = [-1] * n
low = [-1] * n
visited = [False] * n
ap = set()
timer = [0]
def dfs(u, parent):
children = 0
visited[u] = True
disc[u] = low[u] = timer[0]
timer[0] += 1
for v in adj_lists[u]:
if not visited[v]:
children += 1
dfs(v, u)
low[u] = min(low[u], low[v])
# 检查u是否为割点
if parent != -1 and low[v] >= disc[u]:
ap.add(u)
elif v != parent:
low[u] = min(low[u], disc[v])
# 如果u是根节点且有多个子节点
if parent == -1 and children > 1:
ap.add(u)
for i in range(n):
if not visited[i]:
dfs(i, -1)
return ap
# 3. 核心着色算法
def dsatur_coloring(vertices=None, max_colors=None):
"""增强版DSATUR算法"""
if vertices is None:
vertices = set(range(n))
if max_colors is None:
max_colors = n
n_sub = len(vertices)
vertices_list = list(vertices)
vertex_map = {v: i for i, v in enumerate(vertices_list)}
reverse_map = {i: v for v, i in vertex_map.items()}
# 创建子图的邻接表
sub_adj_lists = [[] for _ in range(n_sub)]
for i, v in enumerate(vertices_list):
for u in adj_lists[v]:
if u in vertex_map:
sub_adj_lists[i].append(vertex_map[u])
colors = np.full(n_sub, -1)
saturation = np.zeros(n_sub, dtype=int)
adj_colors = [set() for _ in range(n_sub)]
# 计算子图中的度数
sub_degrees = np.zeros(n_sub, dtype=int)
for i in range(n_sub):
sub_degrees[i] = len(sub_adj_lists[i])
# 初始化优先队列 (-饱和度, -度数, 顶点ID)
vertex_heap = [(0, -sub_degrees[i], i) for i in range(n_sub)]
heapq.heapify(vertex_heap)
colored_count = 0
colored_vertices = np.zeros(n_sub, dtype=bool)
while colored_count < n_sub and vertex_heap:
# 获取优先级最高的未着色顶点
_, _, vertex = heapq.heappop(vertex_heap)
# 如果顶点已着色,跳过
if colored_vertices[vertex]:
continue
# 计算可用颜色
used = [False] * max_colors
for u in sub_adj_lists[vertex]:
if colors[u] != -1:
used[colors[u]] = True
# 找到最小可用颜色
color = -1
for c in range(max_colors):
if not used[c]:
color = c
break
if color == -1:
# 没有可用颜色
return None
colors[vertex] = color
colored_vertices[vertex] = True
colored_count += 1
# 更新邻居的饱和度
for u in sub_adj_lists[vertex]:
if not colored_vertices[u]:
if color not in adj_colors[u]:
adj_colors[u].add(color)
saturation[u] += 1
# 重新入堆以更新优先级
heapq.heappush(vertex_heap, (-saturation[u], -sub_degrees[u], u))
# 将子图着色映射回原图
result = np.full(n, -1)
for i in range(n_sub):
result[reverse_map[i]] = colors[i]
return result
def kempe_chain_interchange(coloring, v, c1, c2):
"""Kempe链交换"""
if coloring[v] != c1 or c1 == c2:
return False
# 保存原始着色
original = coloring.copy()
# 构建Kempe链
chain = {v}
queue = deque([v])
while queue:
current = queue.popleft()
for u in adj_lists[current]:
if u not in chain and coloring[u] in (c1, c2):
chain.add(u)
queue.append(u)
# 交换颜色
for u in chain:
if coloring[u] == c1:
coloring[u] = c2
elif coloring[u] == c2:
coloring[u] = c1
# 验证交换后的合法性
for u in range(n):
for v in adj_lists[u]:
if coloring[u] == coloring[v] and coloring[u] != -1:
# 恢复原始着色
coloring[:] = original
return False
return True
def iterated_greedy(initial_coloring=None, iterations=100):
"""迭代贪心算法"""
if initial_coloring is None:
# 使用DSATUR生成初始解
coloring = dsatur_coloring()
else:
coloring = initial_coloring.copy()
best = coloring.copy()
best_colors_used = len(set(coloring))
for _ in range(iterations):
if time.time() - start_time > max_time:
break
# 随机选择顶点顺序
vertices = list(range(n))
random.shuffle(vertices)
# 临时移除顶点着色
temp_coloring = coloring.copy()
for v in vertices:
temp_coloring[v] = -1
# 重新着色
for v in vertices:
used = [False] * n
for u in adj_lists[v]:
if temp_coloring[u] != -1:
used[temp_coloring[u]] = True
# 找到最小可用颜色
for c in range(n):
if not used[c]:
temp_coloring[v] = c
break
# 更新最佳解
colors_used = len(set(temp_coloring))
if colors_used < best_colors_used:
best = temp_coloring.copy()
best_colors_used = colors_used
coloring = temp_coloring.copy()
elif random.random() < 0.3: # 有时接受较差解以跳出局部最优
coloring = temp_coloring.copy()
return best
def tabu_search(initial_coloring, max_iterations=1000):
"""禁忌搜索优化"""
coloring = initial_coloring.copy()
best_coloring = coloring.copy()
best_colors_used = len(set(coloring))
# 初始化禁忌表
tabu_list = {}
tabu_tenure = 10
iteration = 0
# 计算冲突
def count_conflicts():
conflicts = 0
for v in range(n):
for u in adj_lists[v]:
if u > v and coloring[u] == coloring[v]:
conflicts += 1
return conflicts
# 找到最佳移动
def find_best_move():
best_delta = float('inf')
best_moves = []
for v in range(n):
current_color = coloring[v]
# 计算当前冲突
current_conflicts = sum(1 for u in adj_lists[v] if coloring[u] == current_color)
# 尝试所有可能的颜色
for c in range(best_colors_used + 1):
if c != current_color:
# 计算新冲突
new_conflicts = sum(1 for u in adj_lists[v] if coloring[u] == c)
delta = new_conflicts - current_conflicts
# 检查禁忌状态
move_key = (v, c)
is_tabu = move_key in tabu_list and iteration < tabu_list[move_key]
# 特赦准则:如果移动导致新的最佳解
if is_tabu and not (new_conflicts == 0 and c < best_colors_used):
continue
if delta <= best_delta:
if delta < best_delta:
best_moves = []
best_delta = delta
best_moves.append((v, c))
if not best_moves:
return None, None
# 随机选择一个最佳移动
v, c = random.choice(best_moves)
return v, c
# 主循环
while iteration < max_iterations and time.time() - start_time < max_time:
v, c = find_best_move()
if v is None:
break
# 执行移动
old_color = coloring[v]
coloring[v] = c
# 更新禁忌表
tabu_list[(v, old_color)] = iteration + tabu_tenure
# 更新最佳解
colors_used = len(set(coloring))
if colors_used < best_colors_used:
best_coloring = coloring.copy()
best_colors_used = colors_used
iteration += 1
return best_coloring
# 4. 多阶段混合策略
def hybrid_coloring():
"""多阶段混合着色策略"""
# 阶段1: 预处理和结构识别
max_clique = find_max_clique()
clique_size = len(max_clique)
# 阶段2: 初始解生成
# 使用DSATUR生成初始解
initial_coloring = dsatur_coloring()
if initial_coloring is None:
initial_coloring = np.zeros(n, dtype=int)
for i in range(n):
used = set()
for j in adj_lists[i]:
if j < i:
used.add(initial_coloring[j])
for c in range(n):
if c not in used:
initial_coloring[i] = c
break
# 阶段3: 迭代优化
# 应用迭代贪心
improved_coloring = iterated_greedy(initial_coloring, iterations=50)
colors_used = len(set(improved_coloring))
# 修复使用nonlocal关键字声明best_color_count是外部变量
nonlocal best_color_count
if colors_used < best_color_count:
best_coloring[:] = improved_coloring
best_color_count = colors_used
# 阶段4: 禁忌搜索优化
if time.time() - start_time < max_time * 0.7:
tabu_coloring = tabu_search(improved_coloring, max_iterations=500)
tabu_colors_used = len(set(tabu_coloring))
if tabu_colors_used < best_color_count:
best_coloring[:] = tabu_coloring
best_color_count = tabu_colors_used
# 阶段5: 颜色合并
if time.time() - start_time < max_time * 0.9:
merged_coloring = color_merging(best_coloring.copy())
merged_colors_used = len(set(merged_coloring))
if merged_colors_used < best_color_count:
best_coloring[:] = merged_coloring
best_color_count = merged_colors_used
def color_merging(coloring):
"""尝试合并颜色类"""
colors_used = sorted(set(coloring))
# 尝试合并每对颜色
for c1, c2 in combinations(colors_used, 2):
# 检查是否可以合并
can_merge = True
vertices_with_c1 = np.where(coloring == c1)[0]
for v in vertices_with_c1:
for u in adj_lists[v]:
if coloring[u] == c2:
can_merge = False
break
if not can_merge:
break
if can_merge:
# 合并颜色
coloring[vertices_with_c1] = c2
# 递归尝试更多合并
return color_merging(coloring)
# 重新映射颜色以确保连续
color_map = {}
new_coloring = np.zeros_like(coloring)
next_color = 0
for i, c in enumerate(coloring):
if c not in color_map:
color_map[c] = next_color
next_color += 1
new_coloring[i] = color_map[c]
return new_coloring
# 5. 图分解与重组
def solve_by_decomposition():
"""通过图分解求解"""
# 分解图
components = decompose_graph()
if len(components) > 1:
# 分别处理每个组件
component_colorings = []
max_colors = 0
for component in components:
# 为组件着色
comp_coloring = dsatur_coloring(component)
# 重新映射颜色
color_map = {}
for v in component:
if comp_coloring[v] not in color_map:
color_map[comp_coloring[v]] = len(color_map)
comp_coloring[v] = color_map[comp_coloring[v]]
component_colorings.append((component, comp_coloring))
max_colors = max(max_colors, max(comp_coloring[list(component)]) + 1)
# 重组解
combined_coloring = np.full(n, -1)
for component, comp_coloring in component_colorings:
for v in component:
combined_coloring[v] = comp_coloring[v]
return combined_coloring
else:
return None
# 6. 迭代颜色减少
def iterative_color_reduction(coloring):
"""迭代尝试减少颜色数"""
colors_used = len(set(coloring))
# 如果颜色数已经等于最大团大小,无法进一步减少
max_clique = find_max_clique()
if colors_used <= len(max_clique):
return coloring
# 尝试减少一种颜色
target_colors = colors_used - 1
# 重新映射颜色以确保连续
color_map = {}
for i, c in enumerate(coloring):
if c not in color_map:
color_map[c] = len(color_map)
coloring[i] = color_map[c]
# 尝试移除最高颜色
highest_color = target_colors
vertices_with_highest = np.where(coloring == highest_color)[0]
# 临时移除这些顶点的颜色
temp_coloring = coloring.copy()
temp_coloring[vertices_with_highest] = -1
# 尝试用更少的颜色重新着色
for v in vertices_with_highest:
used = [False] * target_colors
for u in adj_lists[v]:
if temp_coloring[u] != -1:
used[temp_coloring[u]] = True
# 寻找可用颜色
available_color = -1
for c in range(target_colors):
if not used[c]:
available_color = c
break
if available_color != -1:
temp_coloring[v] = available_color
else:
# 无法减少颜色
return coloring
# 递归尝试进一步减少
if time.time() - start_time < max_time * 0.95:
return iterative_color_reduction(temp_coloring)
else:
return temp_coloring
# 主算法流程
# 1. 尝试通过图分解求解
decomp_coloring = solve_by_decomposition()
if decomp_coloring is not None:
decomp_colors_used = len(set(decomp_coloring))
if decomp_colors_used < best_color_count:
best_coloring = decomp_coloring
best_color_count = decomp_colors_used
# 2. 应用多阶段混合策略
hybrid_coloring()
# 3. 迭代颜色减少
if time.time() - start_time < max_time * 0.95:
reduced_coloring = iterative_color_reduction(best_coloring.copy())
reduced_colors_used = len(set(reduced_coloring))
if reduced_colors_used < best_color_count:
best_coloring = reduced_coloring
best_color_count = reduced_colors_used
# 确保颜色编号连续且从0开始
color_map = {}
final_coloring = np.zeros_like(best_coloring)
next_color = 0
for i, c in enumerate(best_coloring):
if c not in color_map:
color_map[c] = next_color
next_color += 1
final_coloring[i] = color_map[c]
return final_coloring

BIN
gcdata/mcppareto.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 176 KiB

831
gcdata/mctest.ipynb Normal file

File diff suppressed because one or more lines are too long

BIN
gcdata/plot/3color.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 42 KiB

BIN
gcdata/plot/5color.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

View File

@ -0,0 +1,50 @@
0.5214,0.4480,0.4857,0.4840,0.7300,0.7156,0.7411,0.4781,0.4388,0.5228,0.4796,0.7249,0.4785,0.4665,0.7670,0.7117,0.4627,0.5158,0.4686,0.4247,0.4632,0.4657,0.4846,0.4982,0.5022,0.5155,0.4432,0.4617,0.5057,0.4207,0.7311,0.4613,0.4729,0.4835,0.4813,0.4691,0.5151,0.4610,0.4823,0.5397,0.4932,0.4659,0.5579,0.4227,0.7311,0.4450,0.5007,0.4955,0.5110,0.5065
0.4480,0.5497,0.5196,0.7516,0.6931,0.7349,0.5000,0.5000,0.7453,0.7357,0.7311,0.4520,0.4667,0.7454,0.4690,0.5394,0.5438,0.4682,0.4675,0.4922,0.4842,0.4861,0.4679,0.4589,0.4730,0.5051,0.4934,0.4877,0.4456,0.4584,0.4785,0.4193,0.7046,0.4738,0.4693,0.5060,0.5202,0.5019,0.4763,0.7311,0.4517,0.4361,0.4771,0.7311,0.4786,0.4656,0.4874,0.7311,0.5210,0.7372
0.4857,0.5196,0.4952,0.5000,0.7507,0.5006,0.6953,0.4407,0.5184,0.7253,0.7025,0.5019,0.7195,0.4759,0.7311,0.4825,0.4895,0.5207,0.4963,0.4282,0.5259,0.7311,0.5408,0.7043,0.4703,0.5121,0.4656,0.5145,0.7287,0.5466,0.4877,0.4969,0.5077,0.6838,0.4242,0.4914,0.4642,0.4679,0.5507,0.5096,0.7421,0.4244,0.4882,0.5070,0.7020,0.7311,0.5342,0.4455,0.4678,0.7311
0.4840,0.7516,0.5000,0.4877,0.3825,0.7311,0.7084,0.7100,0.7311,0.7268,0.5005,0.4848,0.4960,0.3847,0.7311,0.4471,0.5028,0.4506,0.4606,0.5105,0.4879,0.7311,0.7311,0.4335,0.4849,0.5529,0.5207,0.4171,0.4644,0.4784,0.4512,0.4961,0.5019,0.4988,0.4423,0.4701,0.4820,0.5110,0.3988,0.4718,0.4273,0.4619,0.4367,0.4954,0.4962,0.5000,0.3683,0.5024,0.5268,0.4656
0.7300,0.6931,0.7507,0.3825,0.5248,0.7311,0.7048,0.4400,0.4523,0.6940,0.5105,0.4460,0.4816,0.5013,0.4580,0.4643,0.7311,0.5175,0.5044,0.4616,0.4000,0.4509,0.3972,0.5071,0.4199,0.4625,0.5177,0.4728,0.5274,0.5324,0.4529,0.4701,0.4909,0.4897,0.7311,0.7311,0.4919,0.4704,0.4886,0.4957,0.4922,0.4789,0.4392,0.4587,0.5225,0.5048,0.4921,0.4692,0.4503,0.4938
0.7156,0.7349,0.5006,0.7311,0.7311,0.4806,0.5000,0.7433,0.5000,0.6916,0.5298,0.7311,0.4770,0.4851,0.7311,0.4501,0.5395,0.7311,0.5074,0.6660,0.4515,0.4618,0.5081,0.4308,0.4661,0.6898,0.5198,0.4238,0.5456,0.4766,0.4544,0.4682,0.4887,0.4626,0.7311,0.7311,0.4898,0.3863,0.4665,0.5043,0.4470,0.4931,0.4861,0.4246,0.7647,0.4649,0.4441,0.4958,0.4934,0.4952
0.7411,0.5000,0.6953,0.7084,0.7048,0.5000,0.4652,0.4780,0.7142,0.4679,0.4556,0.4832,0.4139,0.4796,0.4307,0.4979,0.5302,0.4691,0.4830,0.7311,0.5168,0.7059,0.4872,0.4833,0.4752,0.7018,0.5052,0.4953,0.4786,0.4570,0.4531,0.4664,0.5237,0.7383,0.4493,0.7311,0.7311,0.5176,0.7311,0.5433,0.7237,0.5088,0.4527,0.4320,0.5600,0.4628,0.4823,0.5370,0.4878,0.4949
0.4781,0.5000,0.4407,0.7100,0.4400,0.7433,0.4780,0.5282,0.7374,0.7142,0.5006,0.4534,0.4971,0.5218,0.4971,0.4910,0.5058,0.5005,0.4919,0.5101,0.4193,0.4762,0.4562,0.4284,0.7218,0.4597,0.4553,0.4875,0.6956,0.4685,0.4474,0.4817,0.7026,0.4695,0.5271,0.4965,0.4849,0.5090,0.5350,0.7471,0.4569,0.4676,0.4889,0.5000,0.4366,0.4738,0.4640,0.7311,0.7156,0.4675
0.4388,0.7453,0.5184,0.7311,0.4523,0.5000,0.7142,0.7374,0.5179,0.6774,0.4654,0.7311,0.4662,0.7311,0.4800,0.4726,0.5018,0.5283,0.4555,0.4676,0.4460,0.4498,0.5145,0.4476,0.4169,0.5000,0.4870,0.4667,0.4653,0.4105,0.4965,0.4918,0.4807,0.4839,0.4686,0.4764,0.7120,0.4830,0.4762,0.4416,0.7311,0.4419,0.4592,0.4932,0.7311,0.7185,0.4363,0.5193,0.7311,0.4859
0.5228,0.7357,0.7253,0.7268,0.6940,0.6916,0.4679,0.7142,0.6774,0.4654,0.4700,0.4561,0.4687,0.4704,0.4504,0.5056,0.4765,0.4924,0.4605,0.5413,0.4894,0.4515,0.4559,0.4466,0.7199,0.4994,0.4807,0.4879,0.5113,0.5199,0.4294,0.4116,0.4984,0.7723,0.4757,0.5000,0.4646,0.4759,0.4607,0.4770,0.4527,0.4288,0.4824,0.5101,0.7311,0.4543,0.4467,0.4428,0.4705,0.5184
0.4796,0.7311,0.7025,0.5005,0.5105,0.5298,0.4556,0.5006,0.4654,0.4700,0.5452,0.4817,0.6749,0.7385,0.4960,0.7282,0.7217,0.4905,0.7009,0.7326,0.4502,0.4701,0.4653,0.5155,0.4637,0.5026,0.7311,0.5019,0.7311,0.4797,0.6798,0.6909,0.4940,0.4536,0.5074,0.4595,0.4467,0.5154,0.6755,0.4608,0.4602,0.7311,0.7311,0.5155,0.4312,0.4912,0.4737,0.7495,0.4955,0.7067
0.7249,0.4520,0.5019,0.4848,0.4460,0.7311,0.4832,0.4534,0.7311,0.4561,0.4817,0.5415,0.4759,0.5261,0.4843,0.4826,0.5070,0.7032,0.4621,0.4396,0.7311,0.4858,0.7351,0.7086,0.4376,0.4662,0.4841,0.4510,0.4447,0.5230,0.4366,0.4874,0.4742,0.4796,0.5415,0.5788,0.4726,0.4264,0.4468,0.4820,0.5029,0.4848,0.5215,0.7311,0.4334,0.4889,0.5213,0.4387,0.4700,0.4669
0.4785,0.4667,0.7195,0.4960,0.4816,0.4770,0.4139,0.4971,0.4662,0.4687,0.6749,0.4759,0.4015,0.7153,0.6980,0.5000,0.4598,0.4678,0.5000,0.4504,0.5068,0.4482,0.4546,0.6942,0.4660,0.5056,0.4737,0.4300,0.4739,0.4718,0.5042,0.4253,0.4295,0.4917,0.4594,0.4748,0.7311,0.4746,0.4546,0.5136,0.4739,0.4564,0.4961,0.4977,0.5140,0.4861,0.4966,0.5140,0.7311,0.5523
0.4665,0.7454,0.4759,0.3847,0.5013,0.4851,0.4796,0.5218,0.7311,0.4704,0.7385,0.5261,0.7153,0.6089,0.5178,0.7324,0.4595,0.5134,0.7404,0.4412,0.5047,0.4513,0.7311,0.5044,0.4954,0.5008,0.5043,0.4655,0.4751,0.7311,0.4573,0.4853,0.7311,0.4839,0.4974,0.4400,0.4907,0.4758,0.4741,0.4475,0.4609,0.5110,0.4935,0.5105,0.7430,0.4650,0.4560,0.4734,0.4744,0.7311
0.7670,0.4690,0.7311,0.7311,0.4580,0.7311,0.4307,0.4971,0.4800,0.4504,0.4960,0.4843,0.6980,0.5178,0.5006,0.7183,0.5072,0.4465,0.7199,0.7125,0.4331,0.4460,0.4403,0.4819,0.4576,0.4608,0.4648,0.4899,0.4766,0.4696,0.5245,0.5233,0.4779,0.5000,0.4609,0.4810,0.4932,0.7311,0.4940,0.5186,0.5538,0.4626,0.7311,0.5137,0.4842,0.4973,0.4260,0.7068,0.4849,0.4742
0.7117,0.5394,0.4825,0.4471,0.4643,0.4501,0.4979,0.4910,0.4726,0.5056,0.7282,0.4826,0.5000,0.7324,0.7183,0.4942,0.7311,0.5386,0.6957,0.4791,0.4752,0.4486,0.4192,0.6915,0.4993,0.4691,0.5065,0.5057,0.4855,0.7311,0.7311,0.4774,0.4451,0.7182,0.7311,0.4897,0.4636,0.4651,0.6736,0.4757,0.4624,0.4388,0.4943,0.4999,0.4165,0.5202,0.4974,0.7311,0.7311,0.4543
0.4627,0.5438,0.4895,0.5028,0.7311,0.5395,0.5302,0.5058,0.5018,0.4765,0.7217,0.5070,0.4598,0.4595,0.5072,0.7311,0.4296,0.7320,0.7094,0.4823,0.4901,0.4738,0.7311,0.4729,0.5074,0.5089,0.4543,0.7311,0.4786,0.7311,0.5085,0.4769,0.7311,0.5010,0.4535,0.5131,0.4943,0.5095,0.4845,0.7192,0.7311,0.4786,0.7311,0.4972,0.4482,0.7311,0.4731,0.4646,0.5629,0.7063
0.5158,0.4682,0.5207,0.4506,0.5175,0.7311,0.4691,0.5005,0.5283,0.4924,0.4905,0.7032,0.4678,0.5134,0.4465,0.5386,0.7320,0.4223,0.5044,0.7257,0.5118,0.4630,0.4991,0.4891,0.4740,0.4643,0.7241,0.4709,0.4715,0.4930,0.4638,0.4838,0.5113,0.5013,0.4537,0.4795,0.4868,0.7311,0.7311,0.4503,0.4883,0.4799,0.4758,0.4882,0.4509,0.7311,0.7311,0.4516,0.4987,0.4764
0.4686,0.4675,0.4963,0.4606,0.5044,0.5074,0.4830,0.4919,0.4555,0.4605,0.7009,0.4621,0.5000,0.7404,0.7199,0.6957,0.7094,0.5044,0.4493,0.7219,0.5015,0.4866,0.7164,0.4799,0.5036,0.7391,0.7115,0.4691,0.4808,0.4414,0.5164,0.5131,0.4759,0.4700,0.4939,0.7215,0.7350,0.5463,0.5153,0.4887,0.4708,0.7059,0.7311,0.4829,0.4972,0.7311,0.5159,0.4738,0.4571,0.4765
0.4247,0.4922,0.4282,0.5105,0.4616,0.6660,0.7311,0.5101,0.4676,0.5413,0.7326,0.4396,0.4504,0.4412,0.7125,0.4791,0.4823,0.7257,0.7219,0.4617,0.5352,0.4704,0.4741,0.4680,0.4849,0.4750,0.4861,0.5487,0.4749,0.4435,0.4516,0.5216,0.7226,0.5000,0.4310,0.5475,0.5110,0.4708,0.4843,0.4702,0.5061,0.4604,0.5062,0.4414,0.4422,0.7311,0.4663,0.4508,0.4535,0.4495
0.4632,0.4842,0.5259,0.4879,0.4000,0.4515,0.5168,0.4193,0.4460,0.4894,0.4502,0.7311,0.5068,0.5047,0.4331,0.4752,0.4901,0.5118,0.5015,0.5352,0.4761,0.7249,0.7199,0.4589,0.4760,0.7077,0.6664,0.4804,0.7256,0.6976,0.4579,0.6835,0.4490,0.4912,0.4812,0.7311,0.4905,0.5044,0.4672,0.4666,0.4916,0.4695,0.4934,0.4461,0.4488,0.3859,0.5265,0.4823,0.7220,0.4584
0.4657,0.4861,0.7311,0.7311,0.4509,0.4618,0.7059,0.4762,0.4498,0.4515,0.4701,0.4858,0.4482,0.4513,0.4460,0.4486,0.4738,0.4630,0.4866,0.4704,0.7249,0.5052,0.4391,0.6960,0.7311,0.7365,0.6902,0.4587,0.4671,0.5527,0.4810,0.5228,0.4621,0.4707,0.5105,0.7311,0.7311,0.5224,0.4478,0.7311,0.5016,0.6757,0.5040,0.5093,0.4106,0.4573,0.4794,0.4451,0.4777,0.4798
0.4846,0.4679,0.5408,0.7311,0.3972,0.5081,0.4872,0.4562,0.5145,0.4559,0.4653,0.7351,0.4546,0.7311,0.4403,0.4192,0.7311,0.4991,0.7164,0.4741,0.7199,0.4391,0.4815,0.7287,0.7545,0.7641,0.5190,0.7285,0.4982,0.7311,0.4788,0.7311,0.4866,0.4952,0.4884,0.7311,0.4483,0.5030,0.4745,0.4502,0.4659,0.4814,0.4919,0.7311,0.7311,0.4664,0.4627,0.7311,0.4516,0.4850
0.4982,0.4589,0.7043,0.4335,0.5071,0.4308,0.4833,0.4284,0.4476,0.4466,0.5155,0.7086,0.6942,0.5044,0.4819,0.6915,0.4729,0.4891,0.4799,0.4680,0.4589,0.6960,0.7287,0.5034,0.7399,0.4528,0.7192,0.7370,0.4627,0.7333,0.4341,0.5437,0.4683,0.7311,0.6824,0.4495,0.7311,0.4693,0.5130,0.4880,0.5128,0.5110,0.4768,0.4707,0.4701,0.4902,0.7350,0.7165,0.4544,0.4189
0.5022,0.4730,0.4703,0.4849,0.4199,0.4661,0.4752,0.7218,0.4169,0.7199,0.4637,0.4376,0.4660,0.4954,0.4576,0.4993,0.5074,0.4740,0.5036,0.4849,0.4760,0.7311,0.7545,0.7399,0.4314,0.4906,0.7455,0.4387,0.4240,0.7311,0.7311,0.4515,0.4474,0.4953,0.4935,0.4272,0.5149,0.4675,0.5532,0.4664,0.4757,0.7311,0.4318,0.7167,0.4822,0.4793,0.7162,0.5323,0.4897,0.4972
0.5155,0.5051,0.5121,0.5529,0.4625,0.6898,0.7018,0.4597,0.5000,0.4994,0.5026,0.4662,0.5056,0.5008,0.4608,0.4691,0.5089,0.4643,0.7391,0.4750,0.7077,0.7365,0.7641,0.4528,0.4906,0.3917,0.4441,0.7311,0.4782,0.7047,0.7296,0.5093,0.4978,0.4609,0.4928,0.5261,0.5041,0.4290,0.5115,0.4456,0.4663,0.4857,0.5167,0.7311,0.7251,0.4515,0.4889,0.4875,0.5086,0.5000
0.4432,0.4934,0.4656,0.5207,0.5177,0.5198,0.5052,0.4553,0.4870,0.4807,0.7311,0.4841,0.4737,0.5043,0.4648,0.5065,0.4543,0.7241,0.7115,0.4861,0.6664,0.6902,0.5190,0.7192,0.7455,0.4441,0.4854,0.5155,0.7106,0.7339,0.4953,0.4548,0.4832,0.4809,0.5367,0.4588,0.4513,0.4549,0.4786,0.4452,0.7311,0.4721,0.4984,0.7311,0.4779,0.4666,0.5104,0.4665,0.4616,0.4890
0.4617,0.4877,0.5145,0.4171,0.4728,0.4238,0.4953,0.4875,0.4667,0.4879,0.5019,0.4510,0.4300,0.4655,0.4899,0.5057,0.7311,0.4709,0.4691,0.5487,0.4804,0.4587,0.7285,0.7370,0.4387,0.7311,0.5155,0.4750,0.4756,0.5028,0.5008,0.4299,0.5605,0.5217,0.5081,0.4856,0.7439,0.4827,0.5231,0.4941,0.5244,0.4712,0.4421,0.4728,0.4315,0.7311,0.5095,0.7311,0.4623,0.4846
0.5057,0.4456,0.7287,0.4644,0.5274,0.5456,0.4786,0.6956,0.4653,0.5113,0.7311,0.4447,0.4739,0.4751,0.4766,0.4855,0.4786,0.4715,0.4808,0.4749,0.7256,0.4671,0.4982,0.4627,0.4240,0.4782,0.7106,0.4756,0.5797,0.4851,0.4959,0.5013,0.4849,0.4351,0.7311,0.4940,0.4523,0.4800,0.4703,0.4334,0.4717,0.4730,0.5022,0.4482,0.4738,0.5263,0.4560,0.5088,0.4924,0.7311
0.4207,0.4584,0.5466,0.4784,0.5324,0.4766,0.4570,0.4685,0.4105,0.5199,0.4797,0.5230,0.4718,0.7311,0.4696,0.7311,0.7311,0.4930,0.4414,0.4435,0.6976,0.5527,0.7311,0.7333,0.7311,0.7047,0.7339,0.5028,0.4851,0.5201,0.4245,0.4562,0.4887,0.4325,0.4411,0.5414,0.4755,0.4486,0.5179,0.4839,0.6758,0.4832,0.7311,0.5032,0.4664,0.4593,0.4225,0.4433,0.7311,0.4883
0.7311,0.4785,0.4877,0.4512,0.4529,0.4544,0.4531,0.4474,0.4965,0.4294,0.6798,0.4366,0.5042,0.4573,0.5245,0.7311,0.5085,0.4638,0.5164,0.4516,0.4579,0.4810,0.4788,0.4341,0.7311,0.7296,0.4953,0.5008,0.4959,0.4245,0.5129,0.4504,0.4468,0.4446,0.4972,0.4774,0.4652,0.6977,0.5158,0.7330,0.4349,0.4332,0.4932,0.5051,0.5198,0.4419,0.4570,0.4602,0.5315,0.4539
0.4613,0.4193,0.4969,0.4961,0.4701,0.4682,0.4664,0.4817,0.4918,0.4116,0.6909,0.4874,0.4253,0.4853,0.5233,0.4774,0.4769,0.4838,0.5131,0.5216,0.6835,0.5228,0.7311,0.5437,0.4515,0.5093,0.4548,0.4299,0.5013,0.4562,0.4504,0.5070,0.7285,0.4426,0.4098,0.4717,0.7281,0.7131,0.7035,0.7406,0.4506,0.5306,0.4646,0.6676,0.4287,0.7311,0.4910,0.4884,0.6815,0.4277
0.4729,0.7046,0.5077,0.5019,0.4909,0.4887,0.5237,0.7026,0.4807,0.4984,0.4940,0.4742,0.4295,0.7311,0.4779,0.4451,0.7311,0.5113,0.4759,0.7226,0.4490,0.4621,0.4866,0.4683,0.4474,0.4978,0.4832,0.5605,0.4849,0.4887,0.4468,0.7285,0.4887,0.7101,0.5030,0.5333,0.4627,0.4772,0.5000,0.7304,0.4569,0.4668,0.4801,0.4810,0.4752,0.4260,0.7255,0.4976,0.7021,0.4859
0.4835,0.4738,0.6838,0.4988,0.4897,0.4626,0.7383,0.4695,0.4839,0.7723,0.4536,0.4796,0.4917,0.4839,0.5000,0.7182,0.5010,0.5013,0.4700,0.5000,0.4912,0.4707,0.4952,0.7311,0.4953,0.4609,0.4809,0.5217,0.4351,0.4325,0.4446,0.4426,0.7101,0.4765,0.7197,0.7188,0.7311,0.4944,0.7179,0.5000,0.4740,0.5355,0.5311,0.4688,0.5009,0.4960,0.4869,0.4677,0.4682,0.4561
0.4813,0.4693,0.4242,0.4423,0.7311,0.7311,0.4493,0.5271,0.4686,0.4757,0.5074,0.5415,0.4594,0.4974,0.4609,0.7311,0.4535,0.4537,0.4939,0.4310,0.4812,0.5105,0.4884,0.6824,0.4935,0.4928,0.5367,0.5081,0.7311,0.4411,0.4972,0.4098,0.5030,0.7197,0.5185,0.4978,0.7311,0.6793,0.7091,0.4322,0.7311,0.7159,0.4780,0.5376,0.4724,0.3895,0.4441,0.5047,0.5015,0.4828
0.4691,0.5060,0.4914,0.4701,0.7311,0.7311,0.7311,0.4965,0.4764,0.5000,0.4595,0.5788,0.4748,0.4400,0.4810,0.4897,0.5131,0.4795,0.7215,0.5475,0.7311,0.7311,0.7311,0.4495,0.4272,0.5261,0.4588,0.4856,0.4940,0.5414,0.4774,0.4717,0.5333,0.7188,0.4978,0.3528,0.7229,0.4841,0.5522,0.4742,0.4450,0.4229,0.4756,0.4564,0.7478,0.5000,0.7311,0.6741,0.4412,0.4159
0.5151,0.5202,0.4642,0.4820,0.4919,0.4898,0.7311,0.4849,0.7120,0.4646,0.4467,0.4726,0.7311,0.4907,0.4932,0.4636,0.4943,0.4868,0.7350,0.5110,0.4905,0.7311,0.4483,0.7311,0.5149,0.5041,0.4513,0.7439,0.4523,0.4755,0.4652,0.7281,0.4627,0.7311,0.7311,0.7229,0.5183,0.4782,0.7040,0.7266,0.4269,0.4921,0.4476,0.5093,0.7311,0.4769,0.5316,0.4870,0.4710,0.7206
0.4610,0.5019,0.4679,0.5110,0.4704,0.3863,0.5176,0.5090,0.4830,0.4759,0.5154,0.4264,0.4746,0.4758,0.7311,0.4651,0.5095,0.7311,0.5463,0.4708,0.5044,0.5224,0.5030,0.4693,0.4675,0.4290,0.4549,0.4827,0.4800,0.4486,0.6977,0.7131,0.4772,0.4944,0.6793,0.4841,0.4782,0.5025,0.4089,0.7109,0.4645,0.5280,0.5076,0.5037,0.7311,0.4962,0.5018,0.7028,0.4473,0.7311
0.4823,0.4763,0.5507,0.3988,0.4886,0.4665,0.7311,0.5350,0.4762,0.4607,0.6755,0.4468,0.4546,0.4741,0.4940,0.6736,0.4845,0.7311,0.5153,0.4843,0.4672,0.4478,0.4745,0.5130,0.5532,0.5115,0.4786,0.5231,0.4703,0.5179,0.5158,0.7035,0.5000,0.7179,0.7091,0.5522,0.7040,0.4089,0.5185,0.4859,0.4563,0.4343,0.7311,0.7311,0.4651,0.5455,0.5570,0.4695,0.4718,0.5161
0.5397,0.7311,0.5096,0.4718,0.4957,0.5043,0.5433,0.7471,0.4416,0.4770,0.4608,0.4820,0.5136,0.4475,0.5186,0.4757,0.7192,0.4503,0.4887,0.4702,0.4666,0.7311,0.4502,0.4880,0.4664,0.4456,0.4452,0.4941,0.4334,0.4839,0.7330,0.7406,0.7304,0.5000,0.4322,0.4742,0.7266,0.7109,0.4859,0.4668,0.4730,0.4518,0.4806,0.4286,0.5338,0.5240,0.7311,0.4358,0.4824,0.5136
0.4932,0.4517,0.7421,0.4273,0.4922,0.4470,0.7237,0.4569,0.7311,0.4527,0.4602,0.5029,0.4739,0.4609,0.5538,0.4624,0.7311,0.4883,0.4708,0.5061,0.4916,0.5016,0.4659,0.5128,0.4757,0.4663,0.7311,0.5244,0.4717,0.6758,0.4349,0.4506,0.4569,0.4740,0.7311,0.4450,0.4269,0.4645,0.4563,0.4730,0.4881,0.7311,0.4758,0.4967,0.6881,0.4697,0.7477,0.7275,0.4676,0.7250
0.4659,0.4361,0.4244,0.4619,0.4789,0.4931,0.5088,0.4676,0.4419,0.4288,0.7311,0.4848,0.4564,0.5110,0.4626,0.4388,0.4786,0.4799,0.7059,0.4604,0.4695,0.6757,0.4814,0.5110,0.7311,0.4857,0.4721,0.4712,0.4730,0.4832,0.4332,0.5306,0.4668,0.5355,0.7159,0.4229,0.4921,0.5280,0.4343,0.4518,0.7311,0.4407,0.5419,0.5348,0.4460,0.7047,0.7108,0.7482,0.4485,0.5133
0.5579,0.4771,0.4882,0.4367,0.4392,0.4861,0.4527,0.4889,0.4592,0.4824,0.7311,0.5215,0.4961,0.4935,0.7311,0.4943,0.7311,0.4758,0.7311,0.5062,0.4934,0.5040,0.4919,0.4768,0.4318,0.5167,0.4984,0.4421,0.5022,0.7311,0.4932,0.4646,0.4801,0.5311,0.4780,0.4756,0.4476,0.5076,0.7311,0.4806,0.4758,0.5419,0.4857,0.7120,0.7090,0.5090,0.7239,0.4830,0.7247,0.7330
0.4227,0.7311,0.5070,0.4954,0.4587,0.4246,0.4320,0.5000,0.4932,0.5101,0.5155,0.7311,0.4977,0.5105,0.5137,0.4999,0.4972,0.4882,0.4829,0.4414,0.4461,0.5093,0.7311,0.4707,0.7167,0.7311,0.7311,0.4728,0.4482,0.5032,0.5051,0.6676,0.4810,0.4688,0.5376,0.4564,0.5093,0.5037,0.7311,0.4286,0.4967,0.5348,0.7120,0.4884,0.6993,0.4913,0.7370,0.4686,0.6890,0.6969
0.7311,0.4786,0.7020,0.4962,0.5225,0.7647,0.5600,0.4366,0.7311,0.7311,0.4312,0.4334,0.5140,0.7430,0.4842,0.4165,0.4482,0.4509,0.4972,0.4422,0.4488,0.4106,0.7311,0.4701,0.4822,0.7251,0.4779,0.4315,0.4738,0.4664,0.5198,0.4287,0.4752,0.5009,0.4724,0.7478,0.7311,0.7311,0.4651,0.5338,0.6881,0.4460,0.7090,0.6993,0.4681,0.4823,0.7162,0.4953,0.7246,0.6989
0.4450,0.4656,0.7311,0.5000,0.5048,0.4649,0.4628,0.4738,0.7185,0.4543,0.4912,0.4889,0.4861,0.4650,0.4973,0.5202,0.7311,0.7311,0.7311,0.7311,0.3859,0.4573,0.4664,0.4902,0.4793,0.4515,0.4666,0.7311,0.5263,0.4593,0.4419,0.7311,0.4260,0.4960,0.3895,0.5000,0.4769,0.4962,0.5455,0.5240,0.4697,0.7047,0.5090,0.4913,0.4823,0.4953,0.7476,0.7203,0.6975,0.5000
0.5007,0.4874,0.5342,0.3683,0.4921,0.4441,0.4823,0.4640,0.4363,0.4467,0.4737,0.5213,0.4966,0.4560,0.4260,0.4974,0.4731,0.7311,0.5159,0.4663,0.5265,0.4794,0.4627,0.7350,0.7162,0.4889,0.5104,0.5095,0.4560,0.4225,0.4570,0.4910,0.7255,0.4869,0.4441,0.7311,0.5316,0.5018,0.5570,0.7311,0.7477,0.7108,0.7239,0.7370,0.7162,0.7476,0.4710,0.4799,0.5000,0.4737
0.4955,0.7311,0.4455,0.5024,0.4692,0.4958,0.5370,0.7311,0.5193,0.4428,0.7495,0.4387,0.5140,0.4734,0.7068,0.7311,0.4646,0.4516,0.4738,0.4508,0.4823,0.4451,0.7311,0.7165,0.5323,0.4875,0.4665,0.7311,0.5088,0.4433,0.4602,0.4884,0.4976,0.4677,0.5047,0.6741,0.4870,0.7028,0.4695,0.4358,0.7275,0.7482,0.4830,0.4686,0.4953,0.7203,0.4799,0.4652,0.4480,0.6870
0.5110,0.5210,0.4678,0.5268,0.4503,0.4934,0.4878,0.7156,0.7311,0.4705,0.4955,0.4700,0.7311,0.4744,0.4849,0.7311,0.5629,0.4987,0.4571,0.4535,0.7220,0.4777,0.4516,0.4544,0.4897,0.5086,0.4616,0.4623,0.4924,0.7311,0.5315,0.6815,0.7021,0.4682,0.5015,0.4412,0.4710,0.4473,0.4718,0.4824,0.4676,0.4485,0.7247,0.6890,0.7246,0.6975,0.5000,0.4480,0.4379,0.4661
0.5065,0.7372,0.7311,0.4656,0.4938,0.4952,0.4949,0.4675,0.4859,0.5184,0.7067,0.4669,0.5523,0.7311,0.4742,0.4543,0.7063,0.4764,0.4765,0.4495,0.4584,0.4798,0.4850,0.4189,0.4972,0.5000,0.4890,0.4846,0.7311,0.4883,0.4539,0.4277,0.4859,0.4561,0.4828,0.4159,0.7206,0.7311,0.5161,0.5136,0.7250,0.5133,0.7330,0.6969,0.6989,0.5000,0.4737,0.6870,0.4661,0.5172

View File

@ -0,0 +1,50 @@
0,0,0,0,1,1,1,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,1,1,1,1,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
0,0,0,1,1,0,1,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0
0,1,1,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0
1,1,1,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,0,0,0,0,1,1,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
1,1,1,1,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0
0,1,0,1,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0
0,1,0,0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0
0,1,1,1,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,1,0,0,0,0,0,0,0,0,1,0,1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0
1,0,0,0,0,1,0,0,0,0,0,0,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0
1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,0,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,0,0,0,0,0,0,0,0
0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0
0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,0,1,0,0,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,1,1,0,1,0,1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0
0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0
0,0,0,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,1,0,1,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,1,0,0,0,1,0,0,0,0,1,0
0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,0,1,1,0,0,0,0,0,0,1,0,1,0
0,0,1,0,0,0,1,0,0,1,0,0,0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,1,1,0,1,0,0
0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0
0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0
0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,1,0,1,1
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,1,0,1,0,1,1
0,0,1,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,1,1,0,0,1,0,1,1
0,0,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,1,0
0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,1,1,0,0,0,1,0,0,0,1
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0
0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,1,1,1,0,1,0,0

24
setup.py Normal file
View File

@ -0,0 +1,24 @@
from setuptools import setup, find_packages
setup(
name="lead",
version="0.1",
packages=find_packages(where="src"),
package_dir={"": "src"},
package_data={
"lead": ["problems/*/dataset/data/*.json",
"problems/*/problem_config.json"]
},
include_package_data=True,
install_requires=[
"requests>=2.28",
"numpy==1.26.4",
"json5>=0.9",
"scipy==1.13.1",
],
entry_points={
"console_scripts": [
"lead-run=lead.scripts.run_evolution:main"
]
}
)

View File

@ -0,0 +1,8 @@
Metadata-Version: 2.2
Name: lead
Version: 0.1
Requires-Dist: requests>=2.28
Requires-Dist: numpy==1.26.4
Requires-Dist: json5>=0.9
Requires-Dist: scipy==1.13.1
Dynamic: requires-dist

View File

@ -0,0 +1,17 @@
MANIFEST.in
setup.py
src/lead/__init__.py
src/lead.egg-info/PKG-INFO
src/lead.egg-info/SOURCES.txt
src/lead.egg-info/dependency_links.txt
src/lead.egg-info/entry_points.txt
src/lead.egg-info/requires.txt
src/lead.egg-info/top_level.txt
src/lead/problems/nd/problem_config.json
src/lead/problems/nd/dataset/__init__.py
src/lead/problems/sort/__init__.py
src/lead/problems/sort/problem_config.json
src/lead/problems/sort/dataset/__init__.py
src/lead/problems/tsp/problem_config.json
src/lead/scripts/__init__.py
src/lead/scripts/run_evolution.py

View File

@ -0,0 +1 @@

View File

@ -0,0 +1,2 @@
[console_scripts]
lead-run = lead.scripts.run_evolution:main

View File

@ -0,0 +1,4 @@
requests>=2.28
numpy==1.26.4
json5>=0.9
scipy==1.13.1

View File

@ -0,0 +1 @@
lead

6
src/lead/__init__.py Normal file
View File

@ -0,0 +1,6 @@
from .core.evolution import EvolutionEngine
from .core.individual import AlgorithmIndividual
from .core.llm_integration import LLMClient
__version__ = "0.1"
__all__ = ["EvolutionEngine", "AlgorithmIndividual", "LLMClient"]

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,25 @@
import os
# 项目根目录
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
# 包根目录
PACKAGE_DIR = os.path.dirname(os.path.dirname(__file__))
# 问题目录
PROBLEMS_DIR = os.path.join(PACKAGE_DIR, "problems")
# 默认LLM配置
DEFAULT_LLM_CONFIG = {
"model": "gpt-3.5-turbo",
"temperature": 0.7,
"max_tokens": 1000
}
# 默认进化参数
DEFAULT_EVOLUTION_PARAMS = {
"population_size": 8,
"generations": 10,
"mutation_rate": 0.3,
"crossover_rate": 0.5
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

151
src/lead/core/evolution.py Normal file
View File

@ -0,0 +1,151 @@
import os
import json
import numpy as np
import random
from typing import List, Tuple
from ..utils.storage import GenerationStorage
from ..utils.evaluator import AlgorithmEvaluator
from .llm_integration import LLMClient
from .individual import AlgorithmIndividual
from ..config.settings import DEFAULT_EVOLUTION_PARAMS
from .evolution_algorithms.tournament import TournamentEvolution
from .evolution_algorithms.differential import DifferentialEvolution
class EvolutionEngine:
def __init__(self, problem_path: str):
self.problem_path = problem_path
self.storage = GenerationStorage(problem_path)
self.evaluator = AlgorithmEvaluator(problem_path)
self.llm_client = LLMClient.from_config(problem_path)
# 加载进化参数
config = self._load_problem_config()
self.evolution_params = {
**DEFAULT_EVOLUTION_PARAMS, # 先放默认配置
**config.get("evolution_params", {}) # 后放问题配置,这样会覆盖默认值
}
print(f"进化参数:{self.evolution_params}")
# 根据配置选择进化算法
algorithm_type = self.evolution_params.get("algorithm", "TU")
if algorithm_type == "DE":
self.evolution_algorithm = DifferentialEvolution(self.evolution_params, self.llm_client)
elif algorithm_type == "TU":
self.evolution_algorithm = TournamentEvolution(self.evolution_params, self.llm_client)
else:
supported_algorithms = ["DE", "TU"]
raise ValueError(f"不支持的进化算法:{algorithm_type},当前支持的算法有:{supported_algorithms}")
def initialize_population(self, size: int) -> List[AlgorithmIndividual]:
"""使用LLM生成初始种群"""
problem_config = self._load_problem_config()
individuals = []
while len(individuals) < size:
code = self.llm_client.generate_initial_code(
problem_config["description"],
problem_config["function_name"],
problem_config["input_format"],
problem_config["output_format"]
)
if code: # 只添加成功生成的代码
individuals.append(AlgorithmIndividual(code, generation=0))
return individuals
def run_evolution(self, generations: int = None, population_size: int = None):
"""主进化循环"""
generations = generations or self.evolution_params["generations"]
population_size = population_size or self.evolution_params["population_size"]
print(f"开始进化,代数:{generations},种群大小:{population_size}")
population = self.initialize_population(population_size)
# 评估初始种群
print("\n评估初始种群...")
for ind in population:
ind.fitness = self.evaluator.evaluate(ind.code)
print(f"初始个体适应度:{ind.fitness:.4f}")
# 记录每代的最佳个体
best_ever = min(population, key=lambda x: x.fitness or float('inf'))
best_fitness = best_ever.fitness
for gen in range(generations):
print(f"\n开始第 {gen+1}/{generations} 代进化")
# 使用选定的进化算法进行进化
new_population = []
while len(new_population) < population_size:
# 选择父代
num_parents = max(2, int(np.ceil(len(population) * 0.6)))
parents = self.evolution_algorithm.select(population, num_parents)
# 交叉
offspring = self.evolution_algorithm.crossover(parents)
# 变异
for child in offspring:
mutated_child = self.evolution_algorithm.mutate(child)
if mutated_child.code: # 只添加有效的代码
# 立即评估新个体
mutated_child.fitness = self.evaluator.evaluate(mutated_child.code)
print(f"新个体适应度:{mutated_child.fitness:.4f}")
new_population.append(mutated_child)
# 生存选择
population = self.evolution_algorithm.survive(population, new_population, population_size)
# 找到当前代最好的个体
current_best = min(population, key=lambda x: x.fitness or float('inf'))
print(f"当前代最佳适应度:{current_best.fitness:.4f}")
# 更新历史最优
if current_best.fitness < best_fitness:
best_ever = current_best
best_fitness = current_best.fitness
print(f"发现新的最优解!适应度:{best_fitness:.4f}")
# 保存当前代信息
generation_data = {
"generation": gen,
"population": [{
"code": ind.code,
"fitness": ind.fitness,
"generation": ind.generation
} for ind in population],
"best_fitness": current_best.fitness,
"best_code": current_best.code,
"best_ever_fitness": best_fitness,
"best_ever_code": best_ever.code
}
self.storage.save_generation(gen, generation_data)
print("\n进化完成!")
print(f"历史最优适应度:{best_fitness:.4f}")
return population, best_ever
def _tournament_select(self, population: List[AlgorithmIndividual], num_winners: int) -> List[AlgorithmIndividual]:
"""使用锦标赛选择法选择指定数量的个体"""
winners = []
for _ in range(num_winners):
# 随机选择tournament_size个个体
candidates = random.sample(population, min(self.tournament_size, len(population)))
# 选择其中适应度最好(最小)的个体
winner = min(candidates, key=lambda x: x.fitness or float('inf'))
winners.append(winner)
return winners
def _select_parents(self, population: List[AlgorithmIndividual]) -> Tuple[AlgorithmIndividual, AlgorithmIndividual]:
"""使用锦标赛选择两个父代个体"""
return tuple(self._tournament_select(population, 2))
def _load_problem_config(self) -> dict:
"""加载问题配置"""
config_path = os.path.join(self.problem_path, "problem_config.json")
print(f"加载问题配置:{config_path}")
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
# print(f"配置内容:{json.dumps(config, indent=2, ensure_ascii=False)}")
return config

View File

@ -0,0 +1,4 @@
from .tournament import TournamentEvolution
from .base import BaseEvolutionAlgorithm
__all__ = ['TournamentEvolution', 'BaseEvolutionAlgorithm']

View File

@ -0,0 +1,55 @@
from abc import ABC, abstractmethod
from typing import List, Tuple, Any
from ..individual import AlgorithmIndividual
class BaseEvolutionAlgorithm(ABC):
def __init__(self, config: dict):
"""初始化进化算法
Args:
config: 算法配置参数
"""
self.config = config
@abstractmethod
def select(self, population: List[AlgorithmIndividual], num_parents: int) -> List[AlgorithmIndividual]:
"""选择父代个体
Args:
population: 当前种群
num_parents: 需要选择的父代数量
Returns:
选中的父代个体列表
"""
pass
@abstractmethod
def crossover(self, parents: List[AlgorithmIndividual]) -> List[AlgorithmIndividual]:
"""交叉操作
Args:
parents: 父代个体列表
Returns:
子代个体列表
"""
pass
@abstractmethod
def mutate(self, individual: AlgorithmIndividual) -> AlgorithmIndividual:
"""变异操作
Args:
individual: 待变异的个体
Returns:
变异后的个体
"""
pass
@abstractmethod
def survive(self, population: List[AlgorithmIndividual], offspring: List[AlgorithmIndividual],
pop_size: int) -> List[AlgorithmIndividual]:
"""生存选择
Args:
population: 当前种群
offspring: 子代种群
pop_size: 目标种群大小
Returns:
选择后的新种群
"""
pass

View File

@ -0,0 +1,109 @@
from typing import List, Tuple
import random
import numpy as np
from .base import BaseEvolutionAlgorithm
from ..individual import AlgorithmIndividual
from ..llm_integration import LLMClient
class DifferentialEvolution(BaseEvolutionAlgorithm):
def __init__(self, config: dict, llm_client: LLMClient):
super().__init__(config)
# DE特有参数
self.F = config.get("F", 0.8) # 缩放因子
self.CR = config.get("CR", 0.7) # 交叉概率
self.llm_client = llm_client
def select(self, population: List[AlgorithmIndividual], num_parents: int) -> List[AlgorithmIndividual]:
"""DE选择随机选择3个不同的个体作为父代"""
parents_groups = []
for _ in range(num_parents):
# 随机选择3个不同的个体
candidates = random.sample(population, 3)
parents_groups.append(candidates)
return [group[0] for group in parents_groups] # 返回每组的目标向量
def crossover(self, parents: List[AlgorithmIndividual]) -> List[AlgorithmIndividual]:
"""DE风格的交叉操作"""
if len(parents) < 1:
return parents
target = parents[0] # 目标向量
if random.random() < self.CR:
# 构建DE交叉提示词
prompt = f"""请将以下代码进行差分进化风格的交叉操作:
目标代码:
{target.code}
要求
1. 保持函数签名不变
2. 使用差分进化的思想对代码结构进行创新性重组
3. 尝试保留目标代码的优秀特性同时引入新的改进
4. 生成的代码必须是完整可运行的函数
5. 避免过度复杂化保持代码的可读性和效率
建议
- 可以重组代码的核心算法部分
- 可以调整关键参数或阈值
- 可以优化数据结构的使用
- 可以改进算法的某些步骤"""
child_code = self.llm_client.crossover(target.code, target.code) # 使用相同代码作为基础
if child_code:
# 验证生成的代码
function_name = target.code.split("def ")[1].split("(")[0].strip()
verified_code = self.llm_client.verify_code_format(child_code, function_name)
if verified_code:
return [AlgorithmIndividual(verified_code, target.generation + 1)]
return [target]
def mutate(self, individual: AlgorithmIndividual) -> AlgorithmIndividual:
"""DE风格的变异操作"""
if random.random() < self.F:
# 构建DE变异提示词
prompt = f"""请对以下代码进行差分进化风格的变异操作:
原始代码:
{individual.code}
要求
1. 保持函数签名不变
2. 引入差分变异的思想对代码进行局部或全局的改进
3. 变异强度应该适中不要完全改变算法的核心思想
4. 生成的代码必须是完整可运行的函数
5. 保持代码的可读性和效率
建议的变异操作
- 调整算法中的关键参数
- 修改某些判断条件或循环结构
- 优化数据结构的使用方式
- 添加或移除某些优化步骤
- 改变算法某些部分的实现方法"""
mutated_code = self.llm_client.mutate(individual.code)
if mutated_code:
# 验证生成的代码
function_name = individual.code.split("def ")[1].split("(")[0].strip()
verified_code = self.llm_client.verify_code_format(mutated_code, function_name)
if verified_code:
return AlgorithmIndividual(verified_code, individual.generation)
return individual
def survive(self, population: List[AlgorithmIndividual], offspring: List[AlgorithmIndividual],
pop_size: int) -> List[AlgorithmIndividual]:
"""DE的生存选择一对一比较"""
survivors = []
for parent, child in zip(population[:len(offspring)], offspring):
# 选择适应度更好的个体
if (child.fitness or float('inf')) < (parent.fitness or float('inf')):
survivors.append(child)
else:
survivors.append(parent)
# 如果需要更多个体,从原始种群中补充
if len(survivors) < pop_size:
survivors.extend(population[len(survivors):pop_size])
return survivors[:pop_size]

View File

@ -0,0 +1,47 @@
from typing import List
import random
import numpy as np
from .base import BaseEvolutionAlgorithm
from ..individual import AlgorithmIndividual
from ..llm_integration import LLMClient
class TournamentEvolution(BaseEvolutionAlgorithm):
def __init__(self, config: dict, llm_client: LLMClient):
super().__init__(config)
self.tournament_size = config.get("tournament_size", 3)
self.llm_client = llm_client
def select(self, population: List[AlgorithmIndividual], num_parents: int) -> List[AlgorithmIndividual]:
"""锦标赛选择"""
# 计算选择个数,为种群规模的50%向上取整,但不少于2个
select_size = max(2, int(np.ceil(len(population) * 0.5)))
winners = []
for _ in range(select_size):
candidates = random.sample(population, min(self.tournament_size, len(population)))
winner = min(candidates, key=lambda x: x.fitness or float('inf'))
winners.append(winner)
return winners
def crossover(self, parents: List[AlgorithmIndividual]) -> List[AlgorithmIndividual]:
"""LLM代码交叉"""
if len(parents) < 2:
return parents
if random.random() < self.config["crossover_rate"]:
child_code = self.llm_client.crossover(parents[0].code, parents[1].code)
if child_code:
return [AlgorithmIndividual(child_code, parents[0].generation + 1)]
return [parents[0]]
def mutate(self, individual: AlgorithmIndividual) -> AlgorithmIndividual:
"""LLM代码变异"""
if random.random() < self.config["mutation_rate"]:
mutated_code = self.llm_client.mutate(individual.code)
if mutated_code:
return AlgorithmIndividual(mutated_code, individual.generation)
return individual
def survive(self, population: List[AlgorithmIndividual], offspring: List[AlgorithmIndividual],
pop_size: int) -> List[AlgorithmIndividual]:
"""直接用子代替换父代"""
return offspring[:pop_size]

View File

@ -0,0 +1,15 @@
from dataclasses import dataclass, asdict
from typing import Optional, Dict, Any
@dataclass
class AlgorithmIndividual:
code: str
generation: int
fitness: Optional[float] = None
def to_dict(self) -> Dict[str, Any]:
return asdict(self)
@classmethod
def from_dict(cls, data: Dict[str, Any]) -> 'AlgorithmIndividual':
return cls(**data)

View File

@ -0,0 +1,167 @@
import os
import json
import requests
from typing import Optional
from ..config.settings import DEFAULT_LLM_CONFIG
class LLMClient:
def __init__(self, api_key: str, base_url: str, model: str = "gpt-3.5-turbo", temperature: float = 0.7):
self.api_key = api_key
self.base_url = base_url
self.model = model
self.default_temperature = temperature
# 为不同算子设置不同的温度
self.operator_temps = {
"initialize": 1.0, # 初始化时使用高温度,增加多样性
"crossover": 0.5, # 交叉时使用低温度,保持稳定性
"mutation": 0.9, # 变异时使用较高温度,鼓励创新
"verify": 0.1 # 验证时使用低温度,确保代码格式正确
}
# 输出当前LLM配置
print(f"\nLLM配置")
print(f"- 模型:{self.model}")
print(f"- 默认温度:{self.default_temperature}")
print(f"- 初始化温度:{self.operator_temps['initialize']}")
print(f"- 交叉温度:{self.operator_temps['crossover']}")
print(f"- 变异温度:{self.operator_temps['mutation']}")
print(f"- API地址{self.base_url}\n")
@classmethod
def from_config(cls, problem_path: str):
"""从问题配置加载LLM参数"""
config_path = os.path.join(problem_path, "problem_config.json")
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
# 修改合并顺序,让问题配置优先级更高
llm_config = {
**DEFAULT_LLM_CONFIG, # 先放默认配置
**config.get("llm_config", {}) # 后放问题配置,这样会覆盖默认值
}
# 直接使用合并后的配置
return cls(
api_key=llm_config["api_key"],
base_url=llm_config["base_url"],
model=llm_config["model"],
temperature=llm_config["temperature"]
)
def verify_code_format(self, code: str, function_name: str) -> Optional[str]:
"""验证代码格式是否符合要求"""
prompt = f"""请检查以下代码是否满足要求,如果不满足则进行修正:
{code}
要求
1. 只包含函数实现代码不包含任何说明注释或示例
2. 主函数名必须为 {function_name}
3. 不包含main函数或测试代码
4. 只返回修正后的代码不要包含任何其他内容
如果代码已经符合要求直接返回原代码如果需要修正返回修正后的代码"""
result = self._call_llm(prompt, operator="verify")
return result if result else code
def generate_initial_code(self, problem_desc: str, function_name: str, input_fmt: str, output_fmt: str) -> str:
"""生成初始算法代码"""
prompt = f"""请用Python编写一个解决以下问题的函数
问题描述{problem_desc}
函数要求
1. 函数名{function_name}
2. 输入{input_fmt}
3. 返回值{output_fmt}
注意
- 不需要添加类型注解
- 只返回函数代码不要包含任何解释或测试用例
- 我们允许返回的代码中包含了多个函数但是一定要确直接运行{function_name}函数并将数据传入即可完成全部流程并获得预期的返回
- 算法应该尽可能地创新避免完全依赖已有的成熟算法可以接受在成熟算法的基础上进行一定的修改
- 算法应该尽可能地简洁避免不必要的复杂性
"""
code = self._call_llm(prompt, operator="initialize")
if code:
return self.verify_code_format(code, function_name)
return None
def crossover(self, code1: str, code2: str) -> str:
"""代码交叉操作"""
prompt = f"""请将以下两个Python函数进行交叉操作生成一个新的高效实现
函数1:
{code1}
函数2:
{code2}
要求
1. 保持函数签名不变
2. 结合两个实现的优点
3. 生成完整可运行的函数代码"""
code = self._call_llm(prompt, operator="crossover")
if code:
# 从code1中提取函数名
function_name = code1.split("def ")[1].split("(")[0].strip()
return self.verify_code_format(code, function_name)
return None
def mutate(self, code: str) -> str:
"""代码变异操作"""
prompt = f"""请对以下Python函数进行创新性改进
{code}
要求
1. 保持函数签名不变
2. 至少引入一个创新性修改
3. 生成完整可运行的函数代码"""
result = self._call_llm(prompt, operator="mutation")
if result:
function_name = code.split("def ")[1].split("(")[0].strip()
return self.verify_code_format(result, function_name)
return None
def _clean_code(self, code: str) -> str:
"""清理LLM返回的代码移除markdown标记"""
return code.replace('```python', '').replace('```', '').strip()
def _call_llm(self, prompt: str, operator: str = None) -> Optional[str]:
"""调用LLM API"""
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {self.api_key}"
}
# 根据操作类型选择温度
temperature = (self.operator_temps.get(operator, self.default_temperature)
if operator else self.default_temperature)
data = {
"model": self.model,
"messages": [{"role": "user", "content": prompt}],
"temperature": temperature
}
try:
print(f"正在调用LLM (model={self.model}, temperature={temperature})...")
response = requests.post(self.base_url, headers=headers, json=data)
response.raise_for_status()
code = response.json()["choices"][0]["message"]["content"].strip()
return self._clean_code(code)
except Exception as e:
print(f"LLM调用失败{str(e)}")
return None
def _get_code_constraints(self) -> str:
"""获取代码约束提示词"""
return """
注意
1. 请仅返回函数的实现代码不要包含main函数或使用示例
2. 不要包含任何测试代码或调试输出
3. 不要包含任何注释说明或markdown标记
4. 确保代码可以直接运行除了必要运行包之外不要额外的导入语句
5. 确保所有导入的包或者函数一定存在我不希望由于错误的导入造成import error
函数要求
1. 主函数名{function_name}
2. 输入{input_fmt}
3. 返回{output_fmt}"""

View File

@ -0,0 +1,21 @@
{
"description": "尝试开发一个高效的算法用于解决二维装箱问题。你被给定了一组矩形物品和一个固定大小的矩形容器,目标是通过算法的规划将所有物品放入容器中,使得容器的利用率最大化。",
"function_name": "solve_bp",
"input_format": "一个包含多个矩形物品的列表,每个物品由其宽度和高度表示,以及一个表示容器宽度和高度的元组。",
"output_format": "返回一个列表,表示每个物品在容器中的位置和旋转状态。",
"evaluation_timeout": 60,
"llm_config": {
"api_key": "sk-cOqgMkQ605Om9Z28q3UtAfOAtOn7c53tld94Cu01oOEoVTmg",
"base_url": "https://api.chatanywhere.tech/v1/chat/completions",
"model": "gpt-4o-mini-ca",
"temperature": 0.7,
"max_tokens": 4096
},
"evolution_params": {
"algorithm": "GA",
"population_size": 10,
"generations": 5,
"mutation_rate": 0.2,
"crossover_rate": 0.7
}
}

View File

View File

@ -0,0 +1,32 @@
import os
import numpy as np
from typing import Dict
class ProblemDataLoader:
def __init__(self, problem_path: str):
self.base_dir = os.getcwd()
print("当前工作目录:", self.base_dir)
self.data_dir = os.path.join(self.base_dir, "gcdata","DSJC1000.1.txt")
def get_data(self):
"""读取图着色问题数据,构建邻接矩阵
Returns:
np.ndarray: 图的邻接矩阵
"""
# 读取第一行获取顶点数
with open(self.data_dir, 'r') as f:
first_line = f.readline().strip().split()
n_vertices = int(first_line[0])
# 初始化邻接矩阵
adj_matrix = np.zeros((n_vertices, n_vertices), dtype=np.int32)
# 读取每条边并更新邻接矩阵
for line in f:
v1, v2 = map(int, line.strip().split())
# 由于输入的顶点编号从0开始,可以直接使用
adj_matrix[v1][v2] = 1
adj_matrix[v2][v1] = 1 # 无向图需要对称
return adj_matrix

View File

@ -0,0 +1,43 @@
import numpy as np
import time
from typing import Callable, Dict
def evaluate(coloring_func: Callable[[np.ndarray], np.ndarray], adj_matrix: np.ndarray) -> float:
"""评估图着色算法的性能
Args:
coloring_func: 图着色函数
adj_matrix: 图的邻接矩阵
Returns:
float: 适应度分数 = 运行时间 * 使用的颜色数
如果解不合法(存在相邻节点同色)则返回无穷大
"""
try:
# 计时并执行图着色
start_time = time.time()
colors = coloring_func(adj_matrix)
end_time = time.time()
execution_time = end_time - start_time
# 检查解的合法性
n = len(adj_matrix)
for i in range(n):
for j in range(n):
if adj_matrix[i][j] == 1 and colors[i] == colors[j]:
print(f"非法解:节点{i}{j}相邻但颜色相同")
return float('inf')
# 计算使用的颜色数
num_colors = len(set(colors))
# 计算适应度分数
fitness = num_colors + execution_time
print(f"执行时间:{execution_time:.4f}秒, 使用颜色数:{num_colors}")
# print(f"适应度分数:{fitness:.4f}")
return num_colors
except Exception as e:
print(f"评估过程出错: {str(e)}")
return float('inf')

View File

@ -0,0 +1,29 @@
{
"generation": 0,
"population": [
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n random.shuffle(vertices)\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.24461746215820312,
"generation": 0
},
{
"code": "import numpy as np\nfrom collections import defaultdict\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n \n def get_saturation_degree():\n saturation = [0] * n\n for v in range(n):\n for neigh in range(n):\n if adj_matrix[v][neigh] == 1 and colors[neigh] != -1:\n saturation[v] += 1\n return saturation\n\n vertices = list(range(n))\n vertices.sort(key=lambda x: (-degree[x], x))\n\n for vertex in vertices:\n forbidden_colors = defaultdict(int)\n\n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors[colors[neigh]] += 1\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n saturation = get_saturation_degree()\n vertices.sort(key=lambda x: (-saturation[x], -degree[x], x))\n\n return colors",
"fitness": Infinity,
"generation": 1
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.18604254722595215,
"generation": 0
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n available_colors = list(set(range(n)) - forbidden_colors)\n \n if available_colors:\n color = random.choice(available_colors)\n else:\n color = max(colors) + 1 if colors else 0\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.5153002738952637,
"generation": 1
}
],
"best_fitness": 0.18604254722595215,
"best_code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 0.1596202850341797,
"best_ever_code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n colors[vertex] = color\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 1,
"population": [
{
"code": "import numpy as np\nfrom queue import PriorityQueue\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n degree_of_saturation = [0] * n\n vertices = list(range(n))\n\n priority_queue = PriorityQueue()\n\n for vertex in vertices:\n priority_queue.put((-degree_of_saturation[vertex], -degree[vertex], vertex))\n\n while not priority_queue.empty():\n _, _, vertex = priority_queue.get()\n\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n\n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] == -1:\n degree_of_saturation[neigh] += 1\n priority_queue.put((-degree_of_saturation[neigh], -degree[neigh], neigh))\n\n return colors",
"fitness": 20.61351442337036,
"generation": 0
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.21193408966064453,
"generation": 0
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.21193408966064453,
"generation": 0
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n random.shuffle(vertices)\n\n color_count = {}\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n available_colors = {}\n for color in range(n):\n if color not in forbidden_colors:\n available_colors[color] = color_count.get(color, 0)\n\n if available_colors:\n color = min(available_colors, key=available_colors.get)\n else:\n color = max(color_count.keys(), default=-1) + 1\n\n colors[vertex] = color\n\n if color in color_count:\n color_count[color] += 1\n else:\n color_count[color] = 1\n\n return colors",
"fitness": 0.5747973918914795,
"generation": 1
}
],
"best_fitness": 0.21193408966064453,
"best_code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 0.1596202850341797,
"best_ever_code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n colors[vertex] = color\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 2,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.21237635612487793,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.2122499942779541,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.2649049758911133,
"generation": 1
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n\n color = 0\n while color in forbidden_colors:\n color += 1\n\n colors[vertex] = color\n\n return colors",
"fitness": 0.24015069007873535,
"generation": 1
}
],
"best_fitness": 0.2122499942779541,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 0.1596202850341797,
"best_ever_code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = {colors[neigh] for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1}\n \n color = 0\n while color in forbidden_colors:\n color += 1\n colors[vertex] = color\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 3,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.15888738632202148,
"generation": 1
},
{
"code": "import numpy as np\nfrom collections import defaultdict\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = defaultdict(bool)\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors[colors[neigh]] = True\n\n color = 0\n while forbidden_colors[color]:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.16106081008911133,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix, max_colors=None):\n n = len(adj_matrix)\n colors = [-1] * n\n vertices = list(range(n))\n \n def uncolored_neighbors_count(vertex):\n return sum(1 for neigh in range(n) if adj_matrix[vertex][neigh] == 1 and colors[neigh] == -1)\n\n vertices.sort(key=lambda x: uncolored_neighbors_count(x))\n\n for vertex in vertices:\n forbidden_colors = set()\n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n\n if max_colors is not None and color >= max_colors:\n raise ValueError(\"超过最大颜色数量限制\")\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.3682851791381836,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = np.full(n, -1)\n degree = np.sum(adj_matrix, axis=1)\n vertices = np.arange(n)\n\n vertices = vertices[np.argsort(-degree)]\n\n for vertex in vertices:\n forbidden_colors = set()\n\n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n\n color = 0\n while color in forbidden_colors:\n color += 1\n\n colors[vertex] = color\n\n return colors",
"fitness": 0.37351298332214355,
"generation": 1
}
],
"best_fitness": 0.15888738632202148,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 0.15888738632202148,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 4,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n available_colors = [set(range(n)) for _ in range(n)]\n \n for vertex in vertices:\n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n if colors[neigh] in available_colors[vertex]:\n available_colors[vertex].remove(colors[neigh])\n \n if available_colors[vertex]:\n colors[vertex] = min(available_colors[vertex])\n else:\n colors[vertex] = max(colors) + 1\n\n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1:\n if colors[vertex] in available_colors[neigh]:\n available_colors[neigh].remove(colors[vertex])\n \n return colors",
"fitness": 0.48113536834716797,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = np.full(n, -1, dtype=int)\n degree = np.sum(adj_matrix, axis=1)\n vertices = np.argsort(-degree)\n\n for vertex in vertices:\n forbidden_colors = set()\n neighbors = np.where(adj_matrix[vertex] == 1)[0]\n for neigh in neighbors:\n if colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors.tolist()",
"fitness": 0.21640729904174805,
"generation": 2
},
{
"code": "import numpy as np\nfrom collections import defaultdict\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = defaultdict(bool)\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors[colors[neigh]] = True\n\n color = 0\n while forbidden_colors[color]:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.21259117126464844,
"generation": 1
},
{
"code": "import numpy as np\nfrom collections import defaultdict\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"fitness": 0.26233983039855957,
"generation": 2
}
],
"best_fitness": 0.21259117126464844,
"best_code": "import numpy as np\nfrom collections import defaultdict\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = defaultdict(bool)\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors[colors[neigh]] = True\n\n color = 0\n while forbidden_colors[color]:\n color += 1\n \n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 0.15888738632202148,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree = [sum(row) for row in adj_matrix]\n vertices = list(range(n))\n\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n forbidden_colors = set()\n \n for neigh in range(n):\n if adj_matrix[vertex][neigh] == 1 and colors[neigh] != -1:\n forbidden_colors.add(colors[neigh])\n \n color = 0\n while color in forbidden_colors:\n color += 1\n \n colors[vertex] = color\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 0,
"population": [
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n available = [True] * n\n\n degree = [sum(row) for row in adj_matrix]\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n for vertex in vertices:\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = False\n \n color = 0\n while color < n and not available[color]:\n color += 1\n \n colors[vertex] = color\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n available = [True] * n\n \n return colors",
"fitness": 21.157724380493164,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"fitness": 9.858245849609375,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n available_colors = [set(range(n)) for _ in range(n)]\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n neighbor_colors = set()\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n neighbor_colors.add(colors[neighbor])\n\n for color in available_colors[vertex]:\n if color not in neighbor_colors:\n colors[vertex] = color\n break\n else:\n colors = _reset_colors(colors, n)\n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[neighbor].discard(colors[vertex])\n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1:\n available_colors[neighbor].discard(colors[vertex])\n\n return colors\n\ndef _reset_colors(colors, n):\n return [color % n for color in colors]",
"fitness": 31.539689540863037,
"generation": 0
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = {i for i in range(n)}\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n if colors[neighbor] in available_colors:\n available_colors.remove(colors[neighbor])\n\n if available_colors:\n color = random.choice(list(available_colors))\n colors[vertex] = color\n else:\n colors = _reset_colors(colors, n)\n\n return colors\n\ndef _reset_colors(colors, n):\n return [color % n for color in colors]",
"fitness": 53.552066802978516,
"generation": 0
}
],
"best_fitness": 9.858245849609375,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"best_ever_fitness": 9.858245849609375,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 1,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"fitness": 8.56167221069336,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"fitness": 8.56167221069336,
"generation": 1
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n available = [True] * n\n\n degree = [sum(row) for row in adj_matrix]\n \n vertices = list(range(n))\n random.shuffle(vertices)\n\n for vertex in vertices:\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = False\n\n color = 0\n while color < n and not available[color]:\n color += 1\n \n colors[vertex] = color\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n available = [True] * n\n \n return colors",
"fitness": 19.723820686340332,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n\n if not all(len(row) == n for row in adj_matrix):\n raise ValueError(\"邻接矩阵必须是n x n的矩阵\")\n \n colors = [-1] * n\n degree_list = [(sum(row), i) for i, row in enumerate(adj_matrix)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n\n if available_colors:\n colors[vertex] = min(available_colors)\n else:\n colors[vertex] = max(colors) + 1\n\n return colors",
"fitness": 12.688473224639893,
"generation": 2
}
],
"best_fitness": 8.56167221069336,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"best_ever_fitness": 8.56167221069336,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 2,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n adj_list = {i: [] for i in range(n)}\n \n for i in range(n):\n for j in range(n):\n if adj_matrix[i][j] == 1:\n adj_list[i].append(j)\n \n degree_list = [(len(adj_list[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = set(range(n))\n \n for neighbor in adj_list[vertex]:\n if colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n colors[vertex] = min(available_colors)\n else:\n colors[vertex] = max(colors) + 1\n\n return colors",
"fitness": 12.483539581298828,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"fitness": 9.864943027496338,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"fitness": 9.864943027496338,
"generation": 1
},
{
"code": "def graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n \n saturation_degree = [0] * n\n ordered_vertices = sorted(range(n), key=lambda x: (-degree_list[x][0], x))\n\n for vertex in ordered_vertices:\n used_colors = set()\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[vertex] = color\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] == -1:\n saturation_degree[neighbor] += 1\n \n ordered_vertices = sorted(ordered_vertices, key=lambda x: (-saturation_degree[x], -degree_list[x][0], x))\n \n return colors",
"fitness": 28.308301210403442,
"generation": 2
}
],
"best_fitness": 9.864943027496338,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors",
"best_ever_fitness": 8.56167221069336,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 3,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[vertex] = color\n break\n \n return colors",
"fitness": 11.252598285675049,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n used_colors = {colors[neighbor] for neighbor in range(n) if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1}\n\n color = next((c for c in range(n) if c not in used_colors), len(used_colors))\n\n colors[vertex] = color\n\n return colors",
"fitness": 10.060500621795654,
"generation": 1
},
{
"code": "import numpy as np\nimport random\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n adj_matrix_np = np.array(adj_matrix)\n\n for vertex in ordered_vertices:\n available_colors = set(range(n))\n neighbors = np.where(adj_matrix_np[vertex] == 1)[0]\n used_colors = set(colors[neighbor] for neighbor in neighbors if colors[neighbor] != -1)\n available_colors -= used_colors\n \n if available_colors:\n colors[vertex] = random.choice(list(available_colors))\n\n return colors",
"fitness": 35.05064392089844,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n used_colors = [set() for _ in range(n)]\n\n for vertex in ordered_vertices:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n used_color = colors[neighbor]\n available_colors.discard(used_color)\n used_colors[neighbor].add(used_color)\n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1:\n available_colors.difference_update(used_colors[neighbor])\n\n if available_colors:\n colors[vertex] = min(available_colors)\n\n return colors",
"fitness": 22.642488718032837,
"generation": 2
}
],
"best_fitness": 10.060500621795654,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n used_colors = {colors[neighbor] for neighbor in range(n) if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1}\n\n color = next((c for c in range(n) if c not in used_colors), len(used_colors))\n\n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 8.56167221069336,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 4,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[vertex] = color\n break\n \n return colors",
"fitness": 12.693830966949463,
"generation": 1
},
{
"code": "def graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n adj_list = {i: [] for i in range(n)}\n for i in range(n):\n for j in range(n):\n if adj_matrix[i][j] == 1:\n adj_list[i].append(j)\n degree_list = [(len(adj_list[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n for vertex in ordered_vertices:\n used_colors = {colors[neighbor] for neighbor in adj_list[vertex] if colors[neighbor] != -1}\n color = next((c for c in range(n-1, -1, -1) if c not in used_colors), len(used_colors))\n colors[vertex] = color\n return colors",
"fitness": 11.303496837615967,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n used_colors = {colors[neighbor] for neighbor in range(n) if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1}\n\n color = next((c for c in range(n) if c not in used_colors), len(used_colors))\n\n colors[vertex] = color\n\n return colors",
"fitness": 9.874988794326782,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n used_colors = set()\n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n\n color = 0\n while color in used_colors:\n color += 1\n colors[vertex] = color\n\n return colors",
"fitness": 11.717382431030273,
"generation": 2
}
],
"best_fitness": 9.874988794326782,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n used_colors = {colors[neighbor] for neighbor in range(n) if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1}\n\n color = next((c for c in range(n) if c not in used_colors), len(used_colors))\n\n colors[vertex] = color\n\n return colors",
"best_ever_fitness": 8.56167221069336,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n\n degree_list = [(sum(adj_matrix[i]), i) for i in range(n)]\n degree_list.sort(reverse=True, key=lambda x: x[0])\n ordered_vertices = [x[1] for x in degree_list]\n\n for vertex in ordered_vertices:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n\n colors[vertex] = color\n \n if color == 0:\n continue\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 0,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n def greedy_coloring(v):\n for neighbor in range(n):\n if adj_matrix[v][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n\n color = 0\n while color < n and available[color]:\n color += 1\n\n colors[v] = color\n\n for neighbor in range(n):\n if adj_matrix[v][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = False\n\n for vertex in range(n):\n if colors[vertex] == -1:\n greedy_coloring(vertex)\n\n return colors",
"fitness": 18.711456298828125,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.477957487106323,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available_colors = [False] * n\n\n def get_min_color(vertex):\n available_colors[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = True\n for color in range(n):\n if not available_colors[color]:\n return color\n return -1\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = list(range(n))\n vertices.sort(key=lambda x: -degree[x])\n\n for vertex in vertices:\n colors[vertex] = get_min_color(vertex)\n\n return colors",
"fitness": 11.983930110931396,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available_colors = [False] * n\n\n def get_min_color(vertex):\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = True\n\n for color in range(n):\n if not available_colors[color]:\n return color\n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n\n degrees = [sum(adj_matrix[i]) for i in range(n)]\n sorted_vertices = sorted(range(n), key=lambda x: degrees[x], reverse=True)\n\n for vertex in sorted_vertices:\n colors[vertex] = get_min_color(vertex)\n available_colors = [False] * n\n\n return colors",
"fitness": 12.745399236679077,
"generation": 0
}
],
"best_fitness": 8.477957487106323,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"best_ever_fitness": 8.477957487106323,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 1,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.848311424255371,
"generation": 1
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n max_heap = [(-degree[i], i) for i in range(n)]\n heapq.heapify(max_heap)\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n while max_heap:\n _, vertex = heapq.heappop(max_heap)\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.922538757324219,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.430407524108887,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.430407524108887,
"generation": 1
}
],
"best_fitness": 8.430407524108887,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"best_ever_fitness": 8.430407524108887,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 2,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 10.764374017715454,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.150354146957397,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.584442615509033,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.670724868774414,
"generation": 1
}
],
"best_fitness": 8.584442615509033,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"best_ever_fitness": 8.430407524108887,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 3,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n\n color_usage = np.zeros(n)\n for v in range(n):\n if colors[v] != -1:\n color_usage[colors[v]] += 1\n\n min_usage_color = None\n for color in range(n):\n if not available[color]:\n if min_usage_color is None or color_usage[color] < color_usage[min_usage_color]:\n min_usage_color = color\n\n return min_usage_color if min_usage_color is not None else n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 221.27851843833923,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.89192247390747,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.436434984207153,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 8.436434984207153,
"generation": 1
}
],
"best_fitness": 8.436434984207153,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"best_ever_fitness": 8.430407524108887,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors"
}

View File

@ -0,0 +1,29 @@
{
"generation": 4,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.102804183959961,
"generation": 1
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n degree_heap = [(-degree[i], i) for i in range(n)]\n heapq.heapify(degree_heap)\n\n def assign_color(vertex):\n available = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n while degree_heap:\n _, vertex = heapq.heappop(degree_heap)\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 7.107714891433716,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: -degree[x])\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.102804183959961,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n available = [False] * n\n\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: (degree[x], colors[x] == -1))\n\n def assign_color(vertex):\n available[:] = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n for vertex in vertices:\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"fitness": 9.786573886871338,
"generation": 1
}
],
"best_fitness": 7.107714891433716,
"best_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n degree_heap = [(-degree[i], i) for i in range(n)]\n heapq.heapify(degree_heap)\n\n def assign_color(vertex):\n available = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n while degree_heap:\n _, vertex = heapq.heappop(degree_heap)\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors",
"best_ever_fitness": 7.107714891433716,
"best_ever_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n degree_heap = [(-degree[i], i) for i in range(n)]\n heapq.heapify(degree_heap)\n\n def assign_color(vertex):\n available = [False] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available[colors[neighbor]] = True\n \n for color in range(n):\n if not available[color]:\n return color\n return n\n\n while degree_heap:\n _, vertex = heapq.heappop(degree_heap)\n if colors[vertex] == -1:\n colors[vertex] = assign_color(vertex)\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 0,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 6.434539794921875,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for vertex in range(n):\n available_colors = [True] * n\n for adjacent in range(n):\n if adj_matrix[vertex][adjacent] == 1 and colors[adjacent] != -1:\n available_colors[colors[adjacent]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[vertex] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 7.9022369384765625,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 6.434539794921875,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n \n for node in nodes_sorted_by_degree:\n available_colors = [True] * (np.max(degrees) + 1)\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(len(available_colors)):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 35.073790311813354,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def assign_colors(vertex):\n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n colors[vertex] = min(available_colors) if available_colors else -1\n \n for vertex in range(n):\n if colors[vertex] == -1:\n assign_colors(vertex)\n \n return colors",
"fitness": 11.16436767578125,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def greedy_coloring():\n degree_list = [(node, sum(adj_matrix[node])) for node in range(n)]\n degree_list.sort(key=lambda x: x[1], reverse=True)\n \n for node, _ in degree_list:\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.43509554862976,
"generation": 0
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = [0] * n\n \n for i in range(n):\n degree[i] = np.sum(adj_matrix[i])\n \n max_heap = []\n for i in range(n):\n heapq.heappush(max_heap, (-degree[i], i))\n\n def assign_colors(vertex):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[vertex] = color\n break\n\n while max_heap:\n _, vertex = heapq.heappop(max_heap)\n if colors[vertex] == -1:\n assign_colors(vertex)\n\n return colors",
"fitness": 11.356404542922974,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = len(adj_matrix)\n colors = [-1] * n\n max_color = 0\n\n def can_color(vertex, c):\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] == c:\n return False\n return True\n \n def degree_ordering():\n degrees = [sum(adj_matrix[i]) for i in range(n)]\n return sorted(range(n), key=lambda x: degrees[x], reverse=True)\n\n def greedy_coloring():\n nonlocal max_color\n ordered_vertices = degree_ordering()\n for vertex in ordered_vertices:\n available_colors = [True] * (n + 1)\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n + 1):\n if available_colors[color]:\n colors[vertex] = color\n max_color = max(max_color, color)\n break\n\n greedy_coloring()\n \n return colors[:n]",
"fitness": 11.651750087738037,
"generation": 1
}
],
"best_fitness": 6.434539794921875,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"best_ever_fitness": 6.434539794921875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 1,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.115325927734375,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def greedy_coloring():\n degree_list = [(node, sum(adj_matrix[node])) for node in range(n)]\n degree_list.sort(key=lambda x: x[1], reverse=True)\n \n for node, _ in degree_list:\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 10.020317554473877,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n degree = np.sum(adj_matrix, axis=1)\n vertices = sorted(range(n), key=lambda x: degree[x], reverse=True)\n\n for vertex in vertices:\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[vertex][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n for color in range(n):\n if available_colors[color]:\n colors[vertex] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 9.021098613739014,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.115325927734375,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.115325927734375,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.115325927734375,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n unique_colors = len(set(colors))\n \n return colors, unique_colors",
"fitness": Infinity,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 9.532928466796875,
"generation": 2
}
],
"best_fitness": 8.115325927734375,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"best_ever_fitness": 6.434539794921875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 10,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree)[::-1] \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 6.072640419006348,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n uncolored_nodes = set(range(n))\n\n while uncolored_nodes:\n max_degree_node = max(uncolored_nodes, key=lambda node: np.sum(adj_matrix[node]))\n used_colors = set()\n neighbors = np.where(adj_matrix[max_degree_node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[max_degree_node] = color\n uncolored_nodes.remove(max_degree_node)\n\n return colors",
"fitness": 59.296813011169434,
"generation": 5
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.632326602935791,
"generation": 6
},
{
"code": "我们可以对现有的 `graph_coloring` 函数进行创新性改进,通过使用更高效的颜色分配算法来减少颜色的使用,同时保持函数签名不变。下面是改进后的代码,它实现了一个基于贪心算法的图着色,并通过使用对当前可用颜色的优先队列(最小堆)来加速颜色分配过程。\n\n\nimport numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n # 通过将节点按度数排序,保持最大度节点优先\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n _, node = heapq.heappop(nodes_sorted)\n\n # 找到所有邻接节点的颜色\n used_colors = set(colors[neighbor] for neighbor in np.where(adj_matrix[node] == 1)[0] if colors[neighbor] != -1)\n\n # 使用一个优先队列来管理可用颜色,优化选择过程\n available_colors = []\n for color in range(n): # 假设图的最大度数小于n\n if color not in used_colors:\n heapq.heappush(available_colors, color)\n\n # 分配第一个可用的颜色\n if available_colors:\n colors[node] = heapq.heappop(available_colors)\n\n return colors\n\n\n### 改进点说明:\n\n1. **优先队列管理可用颜色**:通过使用小根堆(`heapq`)来管理可用颜色,程序可以更快速地找到第一个可用的颜色,而不需要遍历所有可能的颜色。这样能够有效减少时间复杂度,特别是在高密度图中。\n\n2. **保留函数签名**:函数签名没有变化,符合题目要求。\n\n3. **加速执行**:对于每个节点分配颜色时,使用优先队列来快速得到可用颜色的最小值,能够加快算法运行速度。",
"fitness": Infinity,
"generation": 4
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree) \n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n for neighbor in neighbors:\n if colors[neighbor] == -1:\n neighbor_used_colors = set()\n neighbor_neighbors = np.where(adj_matrix[neighbor] == 1)[0]\n for n_neighbor in neighbor_neighbors:\n if colors[n_neighbor] != -1:\n neighbor_used_colors.add(colors[n_neighbor])\n \n if color not in neighbor_used_colors:\n colors[neighbor] = color\n\n return colors",
"fitness": 189.93464374542236,
"generation": 4
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n def is_valid_color(node, color):\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] == color:\n return False\n return True\n\n def backtrack(node):\n if node == n:\n return True\n \n for color in range(n):\n if is_valid_color(node, color):\n colors[node] = color\n if backtrack(node + 1):\n return True\n colors[node] = -1\n \n return False\n\n backtrack(0)\n\n return colors",
"fitness": 29.9012451171875,
"generation": 5
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def remaining_colors(node):\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n return set(range(n)) - used_colors\n\n nodes_sorted = [(len(remaining_colors(i)), i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n _, node = heapq.heappop(nodes_sorted)\n\n if colors[node] != -1:\n continue\n\n color = 0\n while color in remaining_colors(node):\n color += 1\n \n colors[node] = color\n\n for neighbor in np.where(adj_matrix[node] == 1)[0]:\n if colors[neighbor] == -1:\n heapq.heappush(nodes_sorted, (len(remaining_colors(neighbor)), neighbor))\n\n return colors",
"fitness": Infinity,
"generation": 3
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.846970319747925,
"generation": 4
}
],
"best_fitness": 2.846970319747925,
"best_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.846970319747925,
"best_ever_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 11,
"population": [
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.822190761566162,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n adjacency_list = {i: set(np.where(adj_matrix[i] == 1)[0]) for i in range(n)}\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n for neighbor in adjacency_list[node]:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n \n for neighbor in list(adjacency_list[node]):\n if colors[neighbor] == -1:\n adjacency_list[neighbor].discard(node)\n\n return colors",
"fitness": Infinity,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = np.zeros(n, dtype=bool)\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = True\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.8550069332122803,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n while any(color == -1 for color in colors):\n nodes_sorted = []\n for i in range(n):\n if colors[i] == -1: # 当前节点未着色\n num_colored_neighbors = np.sum(colors[adj_matrix[i] == 1] != -1)\n nodes_sorted.append((num_colored_neighbors, i))\n \n if not nodes_sorted:\n break\n \n # 选择已着色邻居最多的未着色节点\n _, node = max(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": Infinity,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 0.0,
"generation": 5
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n def color_priority(node):\n used_colors = [0] * n\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = 1\n return sum(used_colors), degree[node]\n \n nodes_sorted = [(color_priority(i), i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n _, node = heapq.heappop(nodes_sorted)\n \n used_colors = [0] * n\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = 1\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 6.519218444824219,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n available_colors = set(range(n)) - used_colors\n\n if available_colors:\n colors[node] = min(available_colors)\n else:\n colors[node] = max(colors) + 1\n\n return colors",
"fitness": 5.703986406326294,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.317000865936279,
"generation": 6
}
],
"best_fitness": 2.822190761566162,
"best_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.822190761566162,
"best_ever_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 12,
"population": [
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n color = 0\n while color in used_colors:\n color += 1\n colors[node] = color\n\n return colors",
"fitness": 2.9045660495758057,
"generation": 5
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.833575963973999,
"generation": 7
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n neighbor_color_count = {color: 0 for color in range(n)}\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n neighbor_color_count[colors[neighbor]] += 1\n\n available_colors = set(range(n)) - used_colors\n \n if available_colors:\n colors[node] = min(available_colors)\n else:\n minimal_color = min((color for color in range(n) if neighbor_color_count[color] == 0), \n default=max(colors) + 1)\n colors[node] = minimal_color\n\n return colors",
"fitness": 8.578415155410767,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.223910093307495,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.9226484298706055,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n def color_priority(node):\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n return len(used_colors), degree[node]\n \n nodes_sorted = [(color_priority(i), i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n _, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 6.516119956970215,
"generation": 7
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n _, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n\n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.4609901905059814,
"generation": 5
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.514456272125244,
"generation": 5
}
],
"best_fitness": 2.833575963973999,
"best_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.822190761566162,
"best_ever_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 13,
"population": [
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n color_usage = {}\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n\n color = 0\n while color in used_colors or color in color_usage:\n color += 1\n\n colors[node] = color\n color_usage[color] = color_usage.get(color, 0) + 1\n\n return colors",
"fitness": 15.713274478912354,
"generation": 7
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i, sum(colors[neighbor] != -1 for neighbor in np.where(adj_matrix[i] == 1)[0])) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node, colored_neighbors_count = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = next((c for c in range(len(colors)) if c not in used_colors), len(colors))\n\n colors[node] = color\n\n return colors",
"fitness": 8.46590256690979,
"generation": 7
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.72809624671936,
"generation": 7
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n if n == 0: \n return np.array([], dtype=int)\n \n colors = np.full(n, -1, dtype=int)\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n\n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 8.549617290496826,
"generation": 7
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = [False] * n\n \n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = True\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 6.518443822860718,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n color = 0\n while color in used_colors:\n color += 1\n colors[node] = color\n\n return colors",
"fitness": 4.4496049880981445,
"generation": 5
},
{
"code": "import numpy as np\nimport heapq\nfrom collections import deque\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_queue = deque(range(n))\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n \n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n if colors[node] != -1:\n continue\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n \n for neighbor in neighbors:\n if colors[neighbor] == -1:\n nodes_queue.append(neighbor)\n \n while nodes_queue:\n next_node = nodes_queue.popleft()\n next_degree = np.sum(adj_matrix[next_node])\n heapq.heappush(nodes_sorted, (-next_degree, next_node))\n \n return colors",
"fitness": 73.65824127197266,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.301597356796265,
"generation": 4
}
],
"best_fitness": 4.301597356796265,
"best_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.822190761566162,
"best_ever_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 14,
"population": [
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = [False] * n\n \n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = True\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.2828478813171387,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = [False] * n\n \n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = True\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.2828478813171387,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.223240375518799,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n _, node = heapq.heappop(nodes_sorted)\n\n available_colors = [True] * n\n neighbors = np.nonzero(adj_matrix[node])[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n color = 0\n while color < n and not available_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.930685043334961,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n if colors[neighbor] in used_colors:\n while colors[neighbor] in used_colors:\n colors[neighbor] += 1\n heapq.heappush(nodes_sorted, (-degree[neighbor], neighbor))\n\n return colors",
"fitness": Infinity,
"generation": 4
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n color_used = [False] * n\n \n for neighbor in np.where(adj_matrix[node] == 1)[0]:\n if colors[neighbor] != -1:\n color_used[colors[neighbor]] = True\n \n color = 0\n while color < n and color_used[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 47.81918954849243,
"generation": 8
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n color = 0\n while color in used_colors:\n color += 1\n colors[node] = color\n\n return colors",
"fitness": 2.8121449947357178,
"generation": 6
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n def get_node_priority(node):\n neighbors = np.where(adj_matrix[node] == 1)[0]\n neighbor_colors = {colors[neighbor] for neighbor in neighbors if colors[neighbor] != -1}\n return len(neighbor_colors)\n \n nodes_sorted = [(-degree[i], -get_node_priority(i), i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, _, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 7.160622596740723,
"generation": 7
}
],
"best_fitness": 2.8121449947357178,
"best_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n color = 0\n while color in used_colors:\n color += 1\n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.8121449947357178,
"best_ever_code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n color = 0\n while color in used_colors:\n color += 1\n colors[node] = color\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 2,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def greedy_coloring():\n degrees = np.sum(adj_matrix, axis=1)\n order = np.argsort(-degrees)\n \n for node in order:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n greedy_coloring()\n \n used_colors = set(colors)\n color_mapping = {old_color: new_color for new_color, old_color in enumerate(sorted(used_colors))}\n colors = [color_mapping[color] if color != -1 else -1 for color in colors]\n \n return colors",
"fitness": 8.470590591430664,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.075698852539062,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def greedy_coloring():\n degree_list = [(node, np.sum(adj_matrix[node])) for node in range(n)]\n degree_list.sort(key=lambda x: x[1], reverse=True)\n \n for node, _ in degree_list:\n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n colors[node] = min(available_colors)\n\n greedy_coloring()\n return colors",
"fitness": 9.862933874130249,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.075698852539062,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.075698852539062,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n degrees = np.array([np.sum(adj_matrix[i]) for i in range(n)])\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 11.359083414077759,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.02410888671875,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.075698852539062,
"generation": 0
}
],
"best_fitness": 8.02410888671875,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"best_ever_fitness": 6.434539794921875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 3,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 6.4322967529296875,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n degree_order = sorted(range(n), key=lambda x: -sum(adj_matrix[x]))\n\n for node in degree_order:\n used_colors = set(colors[neighbor] for neighbor in range(n) if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1)\n for color in range(n):\n if color not in used_colors:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 11.280726432800293,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def degree(node):\n return sum(adj_matrix[node])\n\n def greedy_coloring():\n nodes_sorted = sorted(range(n), key=degree, reverse=True)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 8.721734762191772,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 9.518722534179688,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n def greedy_coloring():\n degrees = np.sum(adj_matrix, axis=1)\n order = np.argsort(-degrees)\n \n for node in order:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n greedy_coloring()\n \n used_colors = set(colors)\n color_mapping = {old_color: new_color for new_color, old_color in enumerate(sorted(used_colors))}\n colors = [color_mapping[color] if color != -1 else -1 for color in colors]\n \n return colors",
"fitness": 11.26867151260376,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n degrees = np.sum(adj_matrix, axis=1)\n nodes = list(range(n))\n nodes.sort(key=lambda x: degrees[x], reverse=True)\n \n for node in nodes:\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n\n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n \n used_colors = len(set(colors)) - (1 if -1 in colors else 0)\n \n return colors, used_colors",
"fitness": Infinity,
"generation": 0
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def sorted_nodes_by_degree():\n degrees = [(sum(adj_matrix[node]), node) for node in range(n)]\n degrees.sort(reverse=True)\n return [node for degree, node in degrees]\n\n def greedy_coloring():\n sorted_nodes = sorted_nodes_by_degree()\n for node in sorted_nodes:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = available_colors.pop()\n\n greedy_coloring()\n return colors",
"fitness": 11.715296745300293,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 9.448440551757812,
"generation": 1
}
],
"best_fitness": 6.4322967529296875,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"best_ever_fitness": 6.4322967529296875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 4,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 7.043421983718872,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def degree(node):\n return sum(adj_matrix[node])\n\n def greedy_coloring():\n nodes_sorted = sorted(range(n), key=degree, reverse=True)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 10.14957308769226,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n sorted_nodes = np.argsort(-degrees)\n\n def greedy_coloring():\n for node in sorted_nodes:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = available_colors.pop()\n\n greedy_coloring()\n return colors",
"fitness": 8.479966640472412,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def can_color(node, color):\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] == color:\n return False\n return True\n\n def degree(node):\n return sum(adj_matrix[node])\n\n def greedy_coloring():\n nodes_sorted = sorted(range(n), key=degree, reverse=True)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors",
"fitness": 16.915062189102173,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n color_count = 0\n \n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n\n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n color_count = max(color_count, color + 1)\n break\n\n return colors, color_count",
"fitness": Infinity,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n available_colors = [True] * n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 8.472599744796753,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def degree_sorting():\n degree = np.sum(adj_matrix, axis=1)\n return np.argsort(-degree)\n\n def greedy_coloring():\n node_order = degree_sorting()\n for node in node_order:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n greedy_coloring()\n return colors",
"fitness": 8.439783573150635,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n for node in range(n):\n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n colors[node] = min(available_colors)\n\n return colors",
"fitness": 11.051467895507812,
"generation": 1
}
],
"best_fitness": 7.043421983718872,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"best_ever_fitness": 6.4322967529296875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 5,
"population": [
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n \n nodes_heap = [(-degrees[i], i) for i in range(n)]\n heapq.heapify(nodes_heap)\n\n while nodes_heap:\n degree, node = heapq.heappop(nodes_heap)\n if colors[node] != -1:\n continue\n \n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n colors[node] = available_colors.pop()\n else:\n raise ValueError(\"No available colors left to paint the graph.\")\n \n return colors",
"fitness": 11.57004451751709,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 8.459875106811523,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n occupied_colors = set()\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n occupied_colors.add(colors[neighbor])\n \n for color in range(n):\n if color not in occupied_colors:\n colors[node] = color\n break\n \n return colors",
"fitness": 11.278717279434204,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def degree_sorting():\n degree = np.sum(adj_matrix, axis=1)\n return np.argsort(-degree)\n\n def greedy_coloring():\n node_order = degree_sorting()\n for node in node_order:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n greedy_coloring()\n return colors",
"fitness": 8.46054482460022,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n color_count = np.zeros(n)\n\n for node in nodes_sorted:\n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n if colors[neighbor] in available_colors:\n available_colors.discard(colors[neighbor])\n color_count[neighbor] += 1\n\n if available_colors:\n colors[node] = available_colors.pop()\n\n nodes_sorted = np.argsort(color_count)\n\n for node in nodes_sorted:\n if colors[node] == -1:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n colors[node] = available_colors.pop()\n\n return colors",
"fitness": 39.35127782821655,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n used_colors = set()\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 8.438444137573242,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = np.argsort(degree)\n \n for node in nodes_sorted:\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 10.820674896240234,
"generation": 1
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n color_usage = {}\n \n for node in nodes_sorted:\n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n min_color = min(available_colors, key=lambda color: color_usage.get(color, 0))\n colors[node] = min_color\n color_usage[min_color] = color_usage.get(min_color, 0) + 1\n\n return colors",
"fitness": 62.67428398132324,
"generation": 2
}
],
"best_fitness": 8.438444137573242,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n used_colors = set()\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 6.4322967529296875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 6,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n return colors",
"fitness": 11.244561672210693,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = np.full(n, -1)\n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n used_colors = np.zeros(n, dtype=bool)\n neighbors = np.nonzero(adj_matrix[node])[0]\n\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = True\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 33.920536279678345,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 9.099769592285156,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n return colors",
"fitness": 8.43710470199585,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n used_colors = set()\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n color_dict = {}\n for node, color in enumerate(colors):\n if color not in color_dict:\n color_dict[color] = []\n color_dict[color].append(node)\n\n return colors, color_dict",
"fitness": Infinity,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n saturation = np.zeros(n)\n\n def get_next_node():\n max_saturation = -1\n node = -1\n for i in range(n):\n if colors[i] == -1:\n if saturation[i] > max_saturation or (saturation[i] == max_saturation and degrees[i] > degrees[node]):\n max_saturation = saturation[i]\n node = i\n return node\n\n for _ in range(n):\n node = get_next_node()\n if node == -1:\n break\n \n used_colors = set()\n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n saturation[neighbor] += 1\n\n color = 0\n while color in used_colors:\n color += 1\n\n colors[node] = color\n\n return colors",
"fitness": 18.38308358192444,
"generation": 3
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n \n nodes_heap = [(-degrees[i], i) for i in range(n)]\n heapq.heapify(nodes_heap)\n\n while nodes_heap:\n _, node = heapq.heappop(nodes_heap)\n if colors[node] != -1:\n continue\n \n available_colors = {i: True for i in range(n)}\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 9.908474683761597,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n color_usage = np.zeros(n, dtype=int)\n\n for node in nodes_sorted:\n available_colors = set(range(n))\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n \n if available_colors:\n min_usage = min(color_usage[color] for color in available_colors)\n best_colors = [color for color in available_colors if color_usage[color] == min_usage]\n\n chosen_color = min(best_colors)\n colors[node] = chosen_color\n color_usage[chosen_color] += 1\n\n return colors",
"fitness": 78.35030555725098,
"generation": 3
}
],
"best_fitness": 8.43710470199585,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = set(range(n))\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n if available_colors:\n colors[node] = min(available_colors)\n\n return colors",
"best_ever_fitness": 6.4322967529296875,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n\n def greedy_coloring():\n for node in range(n):\n available_colors = [True] * n\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n greedy_coloring()\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 7,
"population": [
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n \n nodes_heap = [(-degrees[i], i) for i in range(n)]\n heapq.heapify(nodes_heap)\n\n while nodes_heap:\n _, node = heapq.heappop(nodes_heap)\n if colors[node] != -1:\n continue\n \n available_colors = {i: True for i in range(n)}\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 11.268001794815063,
"generation": 2
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n\n uncolored_nodes = set(range(n))\n \n while uncolored_nodes:\n min_node = min(uncolored_nodes, key=lambda x: degree[x])\n used_colors = set()\n neighbors = np.where(adj_matrix[min_node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[min_node] = color\n uncolored_nodes.remove(min_node)\n \n degree[min_node] = -1\n\n return colors",
"fitness": 5.055182933807373,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degrees = np.sum(adj_matrix, axis=1)\n saturation = np.zeros(n)\n\n def get_next_node():\n max_saturation = -1\n node = -1\n for i in range(n):\n if colors[i] == -1:\n if saturation[i] > max_saturation or (saturation[i] == max_saturation and degrees[i] > degrees[node]):\n max_saturation = saturation[i]\n node = i\n return node\n\n for _ in range(n):\n node = get_next_node()\n if node == -1:\n break\n \n used_colors = set()\n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n saturation[neighbor] += 1\n\n color = 0\n while color in used_colors:\n color += 1\n\n colors[node] = color\n\n return colors",
"fitness": 22.493141651153564,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = -np.ones(n, dtype=int)\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n used_colors = set()\n\n def backtrack(node_index):\n if node_index == n:\n return True\n\n node = nodes_sorted[node_index]\n available_colors = set(range(n)) - {colors[neighbor] for neighbor in np.where(adj_matrix[node] == 1)[0] if colors[neighbor] != -1}\n\n for color in available_colors:\n colors[node] = color\n used_colors.add(color)\n\n if backtrack(node_index + 1):\n return True\n\n colors[node] = -1\n used_colors.remove(color)\n\n return False\n\n backtrack(0)\n\n unique_colors = {color: idx for idx, color in enumerate(sorted(used_colors))}\n for i in range(n):\n if colors[i] != -1:\n colors[i] = unique_colors[colors[i]]\n\n return colors.tolist()",
"fitness": 8.54158067703247,
"generation": 4
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = [True] * n\n\n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n return colors",
"fitness": 8.515461683273315,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.460548400878906,
"generation": 3
},
{
"code": "import numpy as np\nimport matplotlib.pyplot as plt\n\ndef graph_coloring(adj_matrix, use_min_degree_first=True):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n \n if use_min_degree_first:\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree)\n else:\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n available_colors = np.ones(n, dtype=bool)\n \n for neighbor in range(n):\n if adj_matrix[node][neighbor] == 1 and colors[neighbor] != -1:\n available_colors[colors[neighbor]] = False\n \n for color in range(n):\n if available_colors[color]:\n colors[node] = color\n break\n\n plt.figure(figsize=(8, 6))\n plt.title('Graph Coloring')\n for i in range(n):\n for j in range(n):\n if adj_matrix[i][j] == 1:\n plt.plot([i, j], [0, 0], 'k-')\n for i in range(n):\n plt.scatter(i, 0, s=500, color=plt.cm.jet(colors[i] / max(colors) * 10), edgecolor='black')\n\n plt.yticks([])\n plt.xticks(range(n))\n plt.show()\n\n return colors",
"fitness": Infinity,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = np.full(n, -1)\n degrees = np.sum(adj_matrix, axis=1)\n nodes_sorted_by_degree = np.argsort(-degrees)\n\n for node in nodes_sorted_by_degree:\n available_colors = set(range(n))\n neighbors = np.nonzero(adj_matrix[node])[0]\n\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n available_colors.discard(colors[neighbor])\n\n if available_colors:\n colors[node] = min(available_colors)\n\n return colors",
"fitness": 9.565579175949097,
"generation": 4
}
],
"best_fitness": 5.055182933807373,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n\n uncolored_nodes = set(range(n))\n \n while uncolored_nodes:\n min_node = min(uncolored_nodes, key=lambda x: degree[x])\n used_colors = set()\n neighbors = np.where(adj_matrix[min_node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[min_node] = color\n uncolored_nodes.remove(min_node)\n \n degree[min_node] = -1\n\n return colors",
"best_ever_fitness": 5.055182933807373,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n\n uncolored_nodes = set(range(n))\n \n while uncolored_nodes:\n min_node = min(uncolored_nodes, key=lambda x: degree[x])\n used_colors = set()\n neighbors = np.where(adj_matrix[min_node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[min_node] = color\n uncolored_nodes.remove(min_node)\n \n degree[min_node] = -1\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 8,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.591156005859375,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.8550069332122803,
"generation": 5
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = np.argsort(degree)[::-1]\n\n for node in nodes_sorted:\n if colors[node] != -1:\n continue\n \n used_colors = [False] * n\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors[colors[neighbor]] = True\n \n color = 0\n while color < n and used_colors[color]:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.6143412590026855,
"generation": 4
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.275478363037109,
"generation": 5
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.591156005859375,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n uncolored_nodes = set(range(n))\n \n nodes_sorted = sorted(range(n), key=lambda x: degree[x], reverse=True)\n \n for min_node in nodes_sorted:\n if min_node not in uncolored_nodes:\n continue\n \n used_colors = set()\n neighbors = np.where(adj_matrix[min_node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[min_node] = color\n uncolored_nodes.remove(min_node)\n \n for neighbor in neighbors:\n if neighbor in uncolored_nodes:\n degree[neighbor] -= 1\n\n return colors",
"fitness": 10.156270265579224,
"generation": 4
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.591156005859375,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.591156005859375,
"generation": 3
}
],
"best_fitness": 2.8550069332122803,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.8550069332122803,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors"
}

View File

@ -0,0 +1,49 @@
{
"generation": 9,
"population": [
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.6149139404296875,
"generation": 5
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.466556549072266,
"generation": 3
},
{
"code": "import numpy as np\nimport heapq\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n \n nodes_sorted = [(-degree[i], i) for i in range(n)]\n heapq.heapify(nodes_sorted)\n\n while nodes_sorted:\n current_degree, node = heapq.heappop(nodes_sorted)\n \n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 4.2828452587127686,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.466556549072266,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.389441728591919,
"generation": 5
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree)[::-1] \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 3.132963180541992,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(degree) \n \n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 5.466556549072266,
"generation": 3
},
{
"code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"fitness": 2.912602663040161,
"generation": 5
}
],
"best_fitness": 2.912602663040161,
"best_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors",
"best_ever_fitness": 2.8550069332122803,
"best_ever_code": "import numpy as np\n\ndef graph_coloring(adj_matrix):\n n = adj_matrix.shape[0]\n colors = [-1] * n\n degree = np.sum(adj_matrix, axis=1)\n nodes_sorted = np.argsort(-degree)\n\n for node in nodes_sorted:\n used_colors = set()\n neighbors = np.where(adj_matrix[node] == 1)[0]\n \n for neighbor in neighbors:\n if colors[neighbor] != -1:\n used_colors.add(colors[neighbor])\n \n color = 0\n while color in used_colors:\n color += 1\n \n colors[node] = color\n\n return colors"
}

Some files were not shown because too many files have changed in this diff Show More