Skip to content

Commit

Permalink
Merge pull request #40 from nvbeusekom/simul_integration
Browse files Browse the repository at this point in the history
Fixing bugs in NN_2OPT
  • Loading branch information
jdfekete authored Sep 19, 2022
2 parents f68d7ee + cfa34d2 commit bfe8314
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ coverage
*~
#*
*.swp
/nbproject/private/
17 changes: 16 additions & 1 deletion examples/matrix.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ function matrix(json) {

return nodes.map(function(n) { return n.spectral; });
}

function computeNN2OPT() {
var order = leafOrder(adjacency);

order.forEach(function(lo, i) {
nodes[i].leafOrder = lo;
});
return nodes.map(function(n) { return n.leafOrder; });
}

// Precompute the orders.
var orders = {
Expand All @@ -114,7 +123,8 @@ function matrix(json) {
leafOrderDist: computeLeaforderDist,
barycenter: computeBarycenter,
rcm: computeRCM,
spectral: computeSpectral
spectral: computeSpectral,
nn2opt: computeNN2OPT
};

// The default sort order.
Expand Down Expand Up @@ -240,6 +250,11 @@ function matrix(json) {
order("leafOrderDist");
//d3.select("#order").property("selectedIndex", 4);
}
else if (currentOrder == 'nn2opt') {
orders.nn2opt = computeNN2OPT;
order("nn2opt");
//d3.select("#order").property("selectedIndex", 4);
}

// leafOrder.forEach(function(lo, i) {
// nodes[lo].leafOrder = i;
Expand Down
1 change: 1 addition & 0 deletions examples/miserables/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ <h1><i>Les Misérables</i> Co-occurrence</h1>
<option value="barycenter">by Crossing Reduction</option>
<option value="rcm">by Bandwidth Reduction (RCM)</option>
<option value="spectral">Spectral</option>
<option value="nn2opt">NN-2OPT</option>
</select>

<p>Distance: <select id="distance">
Expand Down
2 changes: 1 addition & 1 deletion examples/timeseries/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<div style="margin-top:4px;margin-bottom: 4px">
<button type="button" onclick="random_permute(tables)">Random Order</button>
<button type="button" onclick="initial_order_permute(tables)">Initial Order</button>
<button type="button" onclick="max_mi_gx(tables)">NN-2OPT on Graph Gx</button>
<button type="button" onclick="nn_2opt_gx(tables)">NN-2OPT on Graph Gx</button>

</div>
<div style="margin-top:4px;margin-bottom: 4px">
Expand Down
4 changes: 2 additions & 2 deletions examples/timeseries/order_functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function random_permute(t) {
computeQualities(t);
}

function max_mi_gx(t) {
function nn_2opt_gx(t) {
let timestep = getTimestep();
let start = new Date().getTime();

Expand All @@ -60,7 +60,7 @@ function max_mi_gx(t) {
let dist_rows = dist(matrices[timestep]);
// let transpose = reorder.transpose(matrices[timestep]),
// dist_cols = reorder.dist()(transpose);
let order = reorder.optimal_leaf_order(),
let order = reorder.nn_2opt(),
row_perm = order.distanceMatrix(dist_rows)(matrices[timestep]);
// let col_perm = order.distanceMatrix(dist_cols)(transpose);
let end = new Date().getTime();
Expand Down
3 changes: 3 additions & 0 deletions nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
files.encoding=UTF-8
site.root.folder=
source.folder=
9 changes: 9 additions & 0 deletions nbproject/project.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://www.netbeans.org/ns/project/1">
<type>org.netbeans.modules.web.clientproject</type>
<configuration>
<data xmlns="http://www.netbeans.org/ns/clientside-project/1">
<name>reorder.js</name>
</data>
</configuration>
</project>
47 changes: 27 additions & 20 deletions src/nn_2opt.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ export function nn_2opt() {
if (distanceMatrix === null) {
distanceMatrix = dist().distance(distance)(matrix);
}
let lowest_dist = -1;
let best_order = [];
// Try each row as the initial permutation
let lowest_dist_all = Number.MAX_VALUE;
let best_order_all = [];

// Try each row as the initial permutation for NN-2OPT
for (let s = 0; s < distanceMatrix.length; s++) {
let initial = s;
let order = [];
order.push(initial);
order.push(s);
// NN
while (order.length < distanceMatrix.length) {
let nearest = -1;
Expand All @@ -52,44 +52,49 @@ export function nn_2opt() {
}
order.push(nearest);
}
let oldm = 0;
let newm = getTotal(order);
let newdist = getTotal(order);
let olddist = newdist - (epsilon + 1);
// 2-OPT
while (newm - oldm > epsilon) {
let lowest_dist_2opt = Number.MAX_VALUE;
let best_order_2opt = [];
while (newdist - olddist > epsilon) {
for (let i = 0; i < order.length; i++) {
for (let j = i + 2; j < order.length - 1; j++) {
// edge 1: (i,i+1) edge2: (j,j+1)
let olddist = distanceMatrix[i][i + 1] + distanceMatrix[j][j + 1];
let newdist = distanceMatrix[i][j] + distanceMatrix[i + 1][j + 1];
if (newdist < olddist) {
let currentd = distanceMatrix[order[i]][order[i + 1]] + distanceMatrix[order[j]][order[j + 1]];
let candidated = distanceMatrix[order[i]][order[j]] + distanceMatrix[order[i + 1]][order[j + 1]];
if (candidated < currentd) {
let check = [];
for (let k = 0; k < order.length; k++) {
check[k] = order[k];
}
// Reverse i+1 to j
for (let k = 0; k < j - (i + 1); k++) {
// TODO: check indices
for (let k = 0; k <= j - (i + 1); k++) {
check[i + 1 + k] = order[j - k];
}
order = check;
}
}
}
oldm = newm;
newm = getTotal(order);
if (newm > lowest_dist) {
lowest_dist = newm;
best_order = order;
olddist = newdist;
newdist = getTotal(order);
if (newdist < lowest_dist_2opt) {
lowest_dist_2opt = newdist;
best_order_2opt = order;
}
}
if (lowest_dist_2opt < lowest_dist_all) {
lowest_dist_all = lowest_dist_2opt;
best_order_all = best_order_2opt;
}
}
return best_order;
return best_order_all;
}

function getTotal(order) {
let sum = 0;
for (let i = 0; i < order.length - 1; i++) {
sum += distanceMatrix[i][i + 1];
sum += distanceMatrix[order[i]][order[i + 1]];
}
return sum;
}
Expand All @@ -103,5 +108,7 @@ export function nn_2opt() {
return nn_2opt;
};

nn_2opt.distanceMatrix = nn_2opt.distance_matrix; // compatability

return nn_2opt;
}

0 comments on commit bfe8314

Please sign in to comment.