-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_examples.py
More file actions
224 lines (183 loc) · 6.93 KB
/
agent_examples.py
File metadata and controls
224 lines (183 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import numpy as np
import torch.nn as nn
import torch.optim as optim
from rlgt.agents import DeepCrossEntropyAgent, PPOAgent, ReinforceAgent
from rlgt.environments import (
GlobalFlipEnvironment,
LinearBuildEnvironment,
LocalSetEnvironment,
create_fixed_graph_generator,
)
from rlgt.graphs import CycleGraph, Graph, GraphFormat
def graph_invariant(graph_batch: Graph) -> np.ndarray:
r"""
This function computes the graph invariant
\[
\mu - \max_{v \in V} \left( m(v)^2 / d(v) + m(v) \right)
\]
for a provided batch of graphs, where $\mu$ is the Laplacian spectral radius, $d(v)$ is the
degree of a vertex $v$, $m(v)$ is the average degree of the neighbors of a vertex $v$, and the
maximum is taken over all the graph vertices $v$. An input graph is assumed to be connected;
otherwise, a score of -10.0 is returned.
:param graph_batch: The provided batch of graphs, given as a `Graph` object.
:return: The computed batch of graph invariant values, given as a `numpy.ndarray` of type
`numpy.float32`.
"""
# Extract the adjacency matrices.
adjacency_matrix_batch = graph_batch.adjacency_matrix_colors.astype(np.float64)
# Compute the vertex degrees.
d_batch = adjacency_matrix_batch.sum(axis=2)
d_batch_fixed = np.maximum(d_batch, 1)
# Compute the average degrees of the vertex neighbors.
m_batch = adjacency_matrix_batch @ d_batch[..., None]
m_batch = m_batch[..., 0] / d_batch_fixed
m_batch_fixed = np.maximum(m_batch, 1)
# Compute the Laplacian matrices.
laplacian_matrix_batch = -adjacency_matrix_batch
index_range = np.arange(adjacency_matrix_batch.shape[1])
laplacian_matrix_batch[:, index_range, index_range] += d_batch
# Compute the Laplacian spectral radii.
spectrum_batch = np.linalg.eigvalsh(laplacian_matrix_batch)
mu_batch = spectrum_batch[:, -1]
# Compute the differences between the left-hand side and the right-hand side.
right_hand_side_batch = np.max(m_batch_fixed**2 / d_batch_fixed + m_batch_fixed, axis=1)
result = mu_batch - right_hand_side_batch
# Determine whether each of the graphs is connected or disconnected.
temp = graph_batch.adjacency_matrix_colors.astype(bool) | np.eye(
graph_batch.graph_order, dtype=bool
)
power = 1
while power < graph_batch.graph_order - 1:
temp = (temp @ temp).astype(bool)
power *= 2
# Punish the disconnected graphs.
result[~np.all(temp[:, 0, :], axis=1)] = -10.0
return result.astype(np.float32)
def a1_example(graph_order: int):
policy_network = nn.Sequential(
nn.Linear(graph_order * (graph_order - 1), 72),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(72, 12),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(12, 2),
)
agent = DeepCrossEntropyAgent(
environment=LinearBuildEnvironment(
graph_invariant=graph_invariant,
graph_order=graph_order,
),
policy_network=policy_network,
optimizer=optim.Adam(policy_network.parameters(), lr=0.003),
)
print("Deep Cross-Entropy agent + Linear Build environment")
print("Starting...")
agent.reset()
while True:
agent.step()
print(f"Learning iterations: {agent.step_count}. Best score: {agent.best_score:.3f}.")
if agent.best_score > 0.0001:
print("Success! The following graph is a solution:")
print(agent.best_graph.adjacency_matrix_colors)
break
if agent.step_count >= 1000:
print("Restarting...")
agent.reset()
def a2_example(graph_order: int):
policy_network = nn.Sequential(
nn.Linear(graph_order * (graph_order - 1) // 2, 72),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(72, 12),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(12, graph_order * (graph_order - 1)),
)
agent = ReinforceAgent(
environment=GlobalFlipEnvironment(
graph_invariant=graph_invariant,
graph_order=graph_order,
episode_length=30,
flip_only=True,
initial_graph_generator=create_fixed_graph_generator(
fixed_graph=CycleGraph(
graph_formats={GraphFormat.FLATTENED_ROW_MAJOR_COLORS},
graph_order=graph_order,
),
graph_format=GraphFormat.FLATTENED_ROW_MAJOR_COLORS,
),
),
policy_network=policy_network,
optimizer=optim.Adam(policy_network.parameters(), lr=0.001),
)
print("REINFORCE agent + Global Flip environment")
print("Starting...")
agent.reset()
while True:
agent.step()
print(f"Learning iterations: {agent.step_count}. Best score: {agent.best_score:.3f}.")
if agent.best_score > 0.0001:
print("Success! The following graph is a solution:")
print(agent.best_graph.adjacency_matrix_colors)
break
if agent.step_count >= 200:
print("Restarting...")
agent.reset()
def a3_example(graph_order: int):
policy_network = nn.Sequential(
nn.Linear(graph_order * (graph_order - 1) // 2 + graph_order, 72),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(72, 12),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(12, graph_order * 2),
)
value_network = nn.Sequential(
nn.Linear(graph_order * (graph_order - 1) // 2 + graph_order, 72),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(72, 12),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(12, 1),
)
agent = PPOAgent(
environment=LocalSetEnvironment(
graph_invariant=graph_invariant,
graph_order=graph_order,
episode_length=30,
initial_graph_generator=create_fixed_graph_generator(
fixed_graph=CycleGraph(
graph_formats={GraphFormat.FLATTENED_ROW_MAJOR_COLORS},
graph_order=graph_order,
),
graph_format=GraphFormat.FLATTENED_ROW_MAJOR_COLORS,
),
),
policy_network=policy_network,
value_network=value_network,
optimizer=optim.Adam(
list(policy_network.parameters()) + list(value_network.parameters()), lr=0.001
),
)
print("PPO agent + Local Set environment")
print("Starting...")
agent.reset()
while True:
agent.step()
print(f"Learning iterations: {agent.step_count}. Best score: {agent.best_score:.3f}.")
if agent.best_score > 0.0001:
print("Success! The following graph is a solution:")
print(agent.best_graph.adjacency_matrix_colors)
break
if agent.step_count >= 200:
print("Restarting...")
agent.reset()
if __name__ == "__main__":
a1_example(graph_order=16)
print()
a2_example(graph_order=16)
print()
a3_example(graph_order=16)