-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdnode.ts
394 lines (322 loc) · 10.7 KB
/
rdnode.ts
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
export namespace RDNode {
let output = true
let debug = false
let snapshot = true
let graphcount = 0
interface Identifier {
id: string
}
function log(text: String) {
if (!output) {
return
}
console.log(text)
}
function ifdebug(f: () => void) {
if (debug) {
f()
}
}
function logDebug(text: String) {
if (!debug) {
return
}
console.log(text)
}
function logList(ls: RDNode[]) {
const strlist = ls.map((n) => n.uniqueId)
log(strlist.join(" "))
}
class RDArrow implements Identifier {
id: "RDArrow" = "RDArrow"
to: RDNode|null = null
from: RDNode|null = null
uniqueId: string
distance: number = 0
constructor(uniqueId: string) {
this.uniqueId = uniqueId
}
}
interface Env {
nodeid: number
edgeid: number
}
class RDNode implements Identifier {
env: Env;
id: "RDNode" = "RDNode"
to: RDArrow[] = []
from: RDArrow[] = []
toGoal: number = 0
fromStart: number|null = null
uniqueId: string
closed: boolean = false
constructor(uniqueId: string, env: Env) {
this.env = env
this.uniqueId = uniqueId
}
toNodeSymbol(graphid:string = ""): string {
return "\"id" + this.uniqueId + "(d=" + this.fromStart + ")" +":"+graphid+ "\"";
}
toString(typ: "dot" = "dot", table: any = {}): string {
const graphIdstr = graphcount.toString()
let ret = ""
table[this.uniqueId] = true
this.to.forEach((n) => {
ret += this.toNodeSymbol(graphIdstr) + " -> " + n.to!.toNodeSymbol(graphIdstr) + `[label="${n.distance}"] ;\n`
if (table[n.to!.uniqueId]) {
return
}
ret += n.to!.toString(typ, table)
})
return ret
}
getMostMinumDistanceParent(): RDNode | null {
if (this.from.length == 0) {
return null
}
if (this.from.length == 1) {
return this.from[0].from
}
let ret = this.from[0]
this.from.forEach((x) => {
if (ret.from == null) {
ret = x
return
}
if (ret.from.fromStart == null) {
ret = x
}
if (x.from == null) {
return
}
if (x.from.from == null) {
return
}
logDebug("min check1:" + ret.from!.toNodeSymbol() + "distance:" + ret.distance);
logDebug("min check2:" + x.from!.toNodeSymbol() + "distance:" + x.distance);
if (ret.from!.fromStart! + ret.distance > x.from!.fromStart! + x.distance) {
logDebug("min update" + x.from.toNodeSymbol());
ret = x
}
})
return ret.from
}
parents(rootUniqueId:string): RDNode[] {
if (this.uniqueId == rootUniqueId) {
return [this];
}
const minParent = this.getMostMinumDistanceParent()
if (minParent == null) {
return [this];
}
let ret = minParent.parents(rootUniqueId)
ret.push(this)
return ret
}
add(node: RDNode, distance:number): RDNode {
setArrow(this, node, distance, this.env)
return node
}
}
function setArrow(from: RDNode, to: RDNode, distance:number, env: { nodeid: number, edgeid: number }): boolean {
env.edgeid++;
let arrow = new RDArrow(env.edgeid.toString())
arrow.distance = distance
arrow.to = to
arrow.from = from
from.to.push(arrow)
to.from.push(arrow)
return true
}
function getNumberList(num: number): number[] {
let ret: number[] = []
for (let i = 0; i < num; i = i + 1) {
ret.push(i)
}
return ret
}
function popRandom<T>(ls: T[]): T | null {
if (ls.length == 0) {
return null
}
const index = Math.floor(Math.random() * ls.length)
const ret = ls.splice(index, 1)
if (ret.length == 0) {
return null
}
return ret[0]
}
// function popRandomPair<T>(ls: T[]): { first: T, second: T | null } | null {
// if (ls.length == 0) {
// return null
// }
// return { first: popRandom(ls)!, second: popRandom(ls) }
// }
// function makeRandomTree(ls: Tree[]): Tree | null {
// logDebug("makeRandomTree:" + ls.length)
// ifdebug(() => {
// logList(ls)
// })
// if (ls.length == 0) {
// return null
// }
// if (ls.length == 1) {
// return ls[0]
// }
// const parent = popRandom(ls)!
// // log("parent"+parent.value)
// const childs = popRandomPair(ls)
// if (childs == null) {
// return parent;
// }
// setTree(parent, childs.first)
// if (childs.second) {
// setTree(parent, childs.second)
// }
// ls.push(parent)
// return makeRandomTree(ls)
// }
function getString(tree: RDNode, lines: RDNode[][] = [], typ: "dot" = "dot") {
let list: string[] = []
graphcount++
const graphIDStr = (graphcount).toString()
const first = `subgraph cluster${graphIDStr} {\n`
list.push(first)
const edge = "edge [style = \"solid\"];\n"
list.push(edge)
const node = `node [color = \"#000000\"]\n;`
list.push(node)
const colorList = ["\"#00FF00\"", "\"#FF0000\"", "\"#0000FF\""]
if (lines.length != 0) {
lines.forEach((line) => {
let color = colorList.shift()
if (!color) {
color = "\"#888888\""
}
line.forEach((t) => {
const el = t.toNodeSymbol(graphIDStr) + ` [color=${color}] ;\n`
list.push(el)
})
})
}
const treeStr = tree.toString(typ)
list.push(treeStr)
const end = "}\n"
list.push(end)
return list.join("")
}
// function getNumberTrees(num: number): Tree[] {
// const nums = getNumberList(num)
// const trees = nums.map((num) => {
// return new Tree(num.toString())
// })
// return trees
// }
// function sampleNetwork(num: number): Tree {
// return makeRandomTree(getNumberTrees(num))!
// }
export function treeTest() {
const env: Env = { nodeid: 0, edgeid: 0 }
const node1 = new RDNode("1", env)
const node2 = new RDNode("2", env)
const node3 = new RDNode("3", env)
const node4 = new RDNode("4", env)
const node5 = new RDNode("5", env)
const node6 = new RDNode("6", env)
const node7 = new RDNode("7", env)
node1.add(node2, 3)
node1.add(node3, 6)
node2.add(node1, 2)
node2.add(node4, 2)
node2.add(node5, 2)
node3.add(node4, 3)
node3.add(node6, 3)
node4.add(node6, 2)
node4.add(node7, 5)
node5.add(node7, 3)
node6.add(node7, 2)
node1.toGoal = 3
node2.toGoal = 2
node3.toGoal = 2
node4.toGoal = 1
node5.toGoal = 1
node6.toGoal = 1
// console.log(getString(node1))
console.log("digraph graph_name {\n");
const result = search_Dijkstra("7", node1)
console.log(getString(node1, [result.history, result.result!.parents(node1.uniqueId)]))
console.log("}\n")
}
// export function searchTest() {
// const num = 1000;
// const tree = makeRandomTree(getNumberTrees(num))!
// const target = Math.floor(Math.random() * num)
// // console.log("SerchTarget:"+target)
// const result = search_DepthFirst(target.toString(), tree)
// if (result.result != null) {
// const resStr = getString(tree, [result.hisotry, result.result.parents()]);
// console.log(resStr);
// } else {
// console.log("not found...")
// }
// }
function search_Dijkstra(goal: string, node: RDNode): { result: RDNode | null, history: RDNode[] } {
let list: RDNode[] = [node]
let history: RDNode[] = []
node.fromStart = 0
while (true) {
logDebug("Search:")
ifdebug(() => { logList(list) })
let target = list.shift()
if (!target) {
logDebug("nothing..")
return { result: null, history: history }
}
history.push(target)
if (snapshot) {
const parents = target.parents(node.uniqueId);
const st = getString(node, [history, parents])
log(st);
}
/// targetからつながっているノードの更新
let nextTarget: RDNode | null = null
let waitNodes: RDNode[] = []
target.to.forEach((n) => {
if (!n.to) {
return
}
if (n.to.closed) {
return
}
if (nextTarget == null) {
nextTarget = n.to
}
const fromStart = target!.fromStart! + n.distance
if (n.to.fromStart == null || n.to.fromStart > fromStart) {
n.to.fromStart = fromStart
}
if (nextTarget.fromStart == null || nextTarget.fromStart > n.to.fromStart) {
if (nextTarget) {
waitNodes.push(nextTarget);
}
nextTarget = n.to
} else {
waitNodes.push(n.to);
}
})
if (nextTarget!=null) {
target.closed = true
if (nextTarget!.uniqueId == goal) {
logDebug("find!")
return { result: nextTarget, history: history }
}
list.push(nextTarget)
}
waitNodes.forEach((n) => {
list.push(n)
})
}
}
}
//Tree.treeTest()
RDNode.treeTest()