forked from davidcheungo123/outTree_ec_minimization
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_3steps.js
130 lines (97 loc) · 4.81 KB
/
main_3steps.js
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
import * as d3 from 'd3';
import * as fs from 'fs';
import {PythonShell} from 'python-shell';
"STEP 1-----------------------------------------------------------------------"
const fileName = "nodes_links_222"
let unprocessedData = fs.readFileSync(`data/pure/${fileName}.json`)
// let unprocessedData = fs.readFileSync(`${rootDir}/finalInput/${fileName}`)
unprocessedData = JSON.parse(unprocessedData)
let nodes = unprocessedData["nodes"]
let links = unprocessedData["links"]
const ticked = () => {
console.log(`running... alpha: ${Math.round(simulation.alpha()*1000)/1000}`)
}
const ended = (nodes, links, fileName) => {
"STEP 2-----------------------------------------------------------------------"
// const fileName = "Untitled-Graph-1_nodesLinks"
// let rawdData = fs.readFileSync(`data/annealed/${fileName}.json`)
// let parsedData = JSON.parse(rawdData)
// // the data structure of nodes and links are {id : <int> , num : <int> , x : <float> , y : <float> , vx : <float> , vy : <float>, cluster : <string>}
// // and {id : <int> , source : <nodesObject> , target : <nodeObject> , len : <float> , index : <int> }
// let nodes = parsedData["nodes"]
// let links = parsedData["links"]
let annealedData = JSON.stringify({nodes, links});
// fs.writeFileSync(`data/annealed_gen/${fileName}.json`, annealedData)
fs.writeFileSync(`data/annealed_gen/${fileName}_annealed.json`, annealedData)
"an array of stringified nodeIDs"
// const tracking = ["143", "130", "135"]
const tracking = []
const idsMapToIndex = {}
tracking.forEach((nodeID) => {
nodes.forEach((node, index) => {
if (node["id"].toString() === nodeID) {
idsMapToIndex[nodeID] = index
}
})
})
let trackingCoordinate = tracking.map((_) => [])
console.log(idsMapToIndex)
let shell = new PythonShell('optimization.py', { mode: 'json'});
shell.send(JSON.stringify(nodes))
shell.send(JSON.stringify(links))
shell.on("message", function (message) {
if ("message" in message) {
console.log(message["message"])
} else {
let processedNodes = message["nodes"]
let processedLinks = message["links"]
let finishedData = JSON.stringify({nodes : processedNodes, links : processedLinks});
"Output in Step 2*************************************************"
fs.writeFileSync(`results/${fileName}_step2Finished.json`, finishedData)
"STEP 3-----------------------------------------------------------------------"
const finalTicked = () => {
console.log(`running... alpha: ${Math.round(simulation.alpha()*1000)/1000}`)
tracking.forEach((nodeID, index) => {
let node = processedNodes[idsMapToIndex[nodeID]]
trackingCoordinate[index].push(`${node.x},${node.y}`)
})
}
const finalEnded = () => {
"Output in Step 3*************************************************"
fs.writeFileSync(`results/${fileName}_step3Finished.json`, JSON.stringify({nodes : processedNodes, links : processedLinks}))
fs.writeFileSync(`tracking/${fileName}_trackingResults.json`, JSON.stringify(trackingCoordinate))
}
const simulation2 = d3.forceSimulation()
.force("link", d3.forceLink().id(d => d.id).distance(d => 30000* d.len).iterations(1000).strength(0.1))
.force("charge", d3.forceManyBody().strength((d) => -0.5*(1 - simulation.alpha())*(d.num+1)))
.force("collide", d3.forceCollide((d) => d.num).strength(1))
.alphaDecay(0.01)
simulation2
.nodes(processedNodes)
.on("tick",finalTicked)
.on("end", finalEnded);
simulation2.force("link")
.links(processedLinks)
"END STEP 3-----------------------------------------------------------------------"
}
})
shell.end(function (err,code,signal) {
if (err) throw err;
console.log('The exit code was: ' + code);
console.log('The exit signal was: ' + signal);
console.log('finished');
});
"END STEP 2-----------------------------------------------------------------------"
}
const simulation = d3.forceSimulation()
.force("link", d3.forceLink().id(d => d.id).distance(d => 10000* d.len).iterations(1000).strength(2))
.force("charge", d3.forceManyBody().strength(-1))
.force("collide", d3.forceCollide((d) => d.num).strength(2))
simulation
.nodes(nodes)
// activate step 2
.on("tick",ticked)
.on("end", () => ended(nodes, links, fileName));
simulation.force("link")
.links(links)
"END STEP 1-----------------------------------------------------------------------"