-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid_wanderer.rs
286 lines (250 loc) · 9.82 KB
/
grid_wanderer.rs
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use std::collections::{HashMap, HashSet};
use fdg_sim::petgraph::adj::NodeIndex;
// use fdg_sim::petgraph::stable_graph::NodeIndex;
use fdg_sim::{Field, Force, ForceGraph, Node};
use localsearch::optim::{LocalSearchOptimizer, TabuList, TabuSearchOptimizer};
use localsearch::OptModel;
use localsearch::OptProgress;
use permutation::Permutation;
use rand::seq::IteratorRandom;
use fdg_sim::petgraph::adj::IndexType;
use crate::crossings_with_permutation;
struct WanderingGridGraphModel<'a, T: Field, const D: usize, N: Clone, E: Send> {
graph: &'a ForceGraph<T, D, N, E>,
neighborhood_size: usize,
graph_neighbors: Option<HashMap<NodeIndex<u32>, HashSet<NodeIndex<u32>>>>,
// permutation: Permutation,
// other fields as needed
}
use fdg_sim::petgraph::visit::Bfs;
impl<'a, T: Field, const D: usize, N: Send + Sync + Clone, E: Send + Sync>
WanderingGridGraphModel<'a, T, D, N, E>
{
pub fn new(graph: &'a ForceGraph<T, D, N, E>, neighborhood_size: usize) -> Self {
Self {
graph,
neighborhood_size,
graph_neighbors: None,
}
}
pub fn cache_neighbors(&mut self) {
let mut graph_neighbors: HashMap<NodeIndex, HashSet<NodeIndex>> = HashMap::new();
let neighborhood_size = self.neighborhood_size;
for node in self.graph.node_indices() {
let mut level = 0;
let mut neighbors = HashSet::new();
let mut visited = HashSet::new();
let mut start = HashSet::from_iter(vec![node]);
while level < neighborhood_size {
// let new_neighbors = self
// .graph
// .neighbors_undirected(node);
let new_neighbors = start
.iter()
.flat_map(|&node| self.graph.neighbors_undirected(node))
// .map(|node| node.index())
.collect::<HashSet<_>>();
visited.extend(start.iter().cloned());
neighbors.extend(new_neighbors.iter().map(|node| node.index() as u32));
start = new_neighbors
.difference(&visited)
.cloned()
.collect::<HashSet<_>>();
level += 1;
}
graph_neighbors.insert(node.index() as u32, neighbors);
}
}
}
type SolutionType = Permutation;
type ScoreType = usize;
type TransitionType = ();
impl<'a, T: Field, const D: usize, N: Send + Sync + Clone, E: Send + Sync> OptModel
for WanderingGridGraphModel<'a, T, D, N, E>
where
f64: From<T>,
{
type SolutionType = SolutionType;
type TransitionType = TransitionType;
type ScoreType = ScoreType;
fn evaluate_solution(&self, state: &Self::SolutionType) -> Self::ScoreType {
let score = crossings_with_permutation(&self.graph, state).unwrap();
// println!("score: {}, permutation: {:?}", score, state);
score
}
fn generate_trial_solution<R: rand::Rng>(
&self,
current_solution: &Self::SolutionType,
rng: &mut R,
current_score: Option<Self::ScoreType>,
) -> (Self::SolutionType, Self::TransitionType, Self::ScoreType) {
// this time we're going to pick a random node and swap it with another random node within the specified neighborhood size
let graph = self.graph;
let node = graph.node_indices().choose(rng).unwrap();
let neighborhood_size = self.neighborhood_size;
// we need to recursively collect the neighbors of the node within the neighborhood size
// let neighbors = graph.neighbors(node).collect::<HashSet<_>>();
// pick a random neighbor
// if the node has no neighbors, then we can't swap it with anything so we'll just return the current solution
let mut neighbors;
let neighbors = match &self.graph_neighbors {
Some(graph_neighbors) => graph_neighbors.get(&(node.index() as u32)).unwrap(),
None => {
let mut level = 0;
neighbors = HashSet::new();
let mut visited = HashSet::new();
let mut start = HashSet::from_iter(vec![node]);
while level < neighborhood_size {
// let new_neighbors = self
// .graph
// .neighbors_undirected(node);
let new_neighbors = start
.iter()
.flat_map(|&node| self.graph.neighbors_undirected(node))
.collect::<HashSet<_>>();
visited.extend(start.iter().cloned());
neighbors.extend(new_neighbors.iter().map(|node| node.index() as u32));
start = new_neighbors
.difference(&visited)
.cloned()
.collect::<HashSet<_>>();
level += 1;
}
&neighbors
}
};
let node_index = node.index() as u32;
let neighbor = neighbors.iter().choose(rng).unwrap_or(&node_index);
if (node.index() as u32) == *neighbor {
return (current_solution.clone(), (), current_score.unwrap());
}
let mut idx = Vec::from_iter(0..self.graph.node_count());
let a = node.index();
let b = neighbor.index();
idx.swap(a, b);
let new_solution = &Permutation::oneline(idx).inverse() * current_solution;
let new_score = self.evaluate_solution(&new_solution);
let trial = (new_solution, (), new_score);
// println!("trial: {:?}", trial);
trial
}
fn generate_random_solution<R: rand::Rng>(
&self,
rng: &mut R,
) -> Result<Self::SolutionType, Box<dyn std::error::Error>> {
// let mut irs = Irs::default();
// let mut idx = Vec::from_iter(0..self.graph.node_count());
// irs.shuffle(&mut idx, rng);
// Ok(Permutation::oneline(idx).inverse())
Ok(Permutation::one(self.graph.node_count()))
}
}
#[derive(Debug, Clone)]
struct PermutationTabuList {
tabu_list: Vec<SolutionType>,
max_size: usize,
}
impl PermutationTabuList {
pub fn new(max_size: usize) -> Self {
Self {
tabu_list: Vec::<SolutionType>::new(),
max_size,
}
}
}
impl TabuList for PermutationTabuList {
type Item = (SolutionType, TransitionType);
fn contains(&self, item: &Self::Item) -> bool {
self.tabu_list.iter().any(|x| x == &item.0)
}
fn append(&mut self, item: Self::Item) {
self.tabu_list.push(item.0);
if self.tabu_list.len() > self.max_size {
self.tabu_list.remove(0);
}
}
}
pub struct Swapper {
neighborhood_size: usize,
optimizer: TabuSearchOptimizer<PermutationTabuList>,
tabu_list: Option<PermutationTabuList>,
// other fields as needed
}
impl Swapper {
pub fn new(
neighborhood_size: usize,
patience: usize,
n_trials: usize,
return_iter: usize,
) -> Self {
// let optimizer = TabuSearchOptimizer::new(100, 100, 1);
// let tabu_list = PermutationTabuList::new(20);
let optimizer =
TabuSearchOptimizer::<PermutationTabuList>::new(patience, n_trials, return_iter);
Self {
neighborhood_size,
optimizer,
tabu_list: None,
// initialize other fields as needed
}
}
}
impl<T: Field, const D: usize, N: Send + Sync + Clone, E: Send + Sync> Force<T, D, N, E> for Swapper
where
f64: From<T>,
{
fn apply(&mut self, graph: &mut ForceGraph<T, D, N, E>) {
let mut model = WanderingGridGraphModel {
graph: graph,
neighborhood_size: self.neighborhood_size,
graph_neighbors: None,
};
model.cache_neighbors();
let callback = |op: OptProgress<SolutionType, ScoreType>| {
// println!("progress {:?}", op);
// pb.set_message(format!("best score {:e}", op.score.into_inner()));
// pb.set_position(op.iter as u64);
};
let initial = Permutation::one(graph.node_count());
let current_score = crossings_with_permutation(&graph, &initial).unwrap();
if current_score == 0 {
return;
}
// let tabu_list = PermutationTabuList::new(2000);
// if we already have a tabu list, use it
let tabu_list = match &self.tabu_list {
Some(tabu_list) => tabu_list.clone(),
None => PermutationTabuList::new(200000),
};
let (best_permutation, best_score, tabu_list) =
self.optimizer
.optimize(&model, Some(initial), 1000, Some(&callback), tabu_list);
self.tabu_list = Some(tabu_list);
println!(
"best score: {}, permutation: {:?}",
best_score, best_permutation
);
// apply the best permutation to the graph
let shuffled_node_weights: Vec<Node<T, D, N>>;
{
// Limit the scope of the first mutable borrow here
let node_weights = graph.node_weights().collect::<Vec<_>>();
let shuffled_indices = (0..graph.node_count())
.map(|i| best_permutation.apply_idx(i))
.collect::<Vec<_>>();
shuffled_node_weights = shuffled_indices
.iter()
.map(|&i| node_weights[i].clone())
.collect::<Vec<_>>();
// shuffled_node_weights = best_permutation.apply(node_weights);
// let shuffled_node_weights: Vec<Node<T, D, N>> = best_permutation.iter().map(|&i| node_weights[i]).collect();
}
// graph.node_indices()
graph
.node_weights_mut()
.enumerate()
.for_each(|(i, node_weight)| {
node_weight.1 = shuffled_node_weights[i].clone().1;
});
}
}