-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtspwc.ts
221 lines (211 loc) · 9.86 KB
/
tspwc.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
import { addHours, dateIsBetween, dateIsEarlierThan, dateIsLaterThan, getDateTime } from './lib/helpers'
import { INodeResult, INodeResultMsgParams, ITspResult, ITspStop, NodeResultMsg, TNodeResultKey, TspResult } from './lib/interfaces'
export const getNodeResultMessage = (key: TNodeResultKey, params: INodeResultMsgParams): string => {
if (key === 'arriveBetweenFailure' || key === 'arriveBetweenSuccess') {
return NodeResultMsg[key].replace('{arrivalTime}', params.arrivalTime ? params.arrivalTime.toString() : '')
.replace('{deadline}', params.deadline !== undefined ? params.deadline.toString() : '')
.replace('{deadline2}', params.deadline2 !== undefined ? params.deadline2.toString() : '')
} else if (params.arrivalTime) {
return NodeResultMsg[key].replace('{arrivalTime}', params.arrivalTime ? params.arrivalTime.toString() : '')
.replace('{deadline}', params.deadline ? params.deadline.toString() : '')
} else {
return NodeResultMsg[key].replace('{load}', params.load ? params.load.toString() : '')
.replace('{capacity}', params.capacity ? params.capacity.toString() : '')
}
}
export const nodeConditions = (stops: any, src: number, dest: number, time?: Date, load?: number,
verbose: boolean = false): INodeResult => {
const res: INodeResult = {
from: stops[src].id,
hours: stops[dest].times[src] || 0,
to: stops[dest].id,
valid: true,
}
if (time) {
if (stops[dest].arriveBy) {
const hours: number = stops[dest].times[src] || 0
const timeOfArrival: Date = addHours(time, hours)
const arriveBy: Date = stops[dest].arriveBy || new Date()
if (!dateIsEarlierThan(timeOfArrival, arriveBy)) {
if (verbose) {
res.arriveBy = getNodeResultMessage('arriveByFailure', {
arrivalTime: getDateTime(timeOfArrival),
deadline: getDateTime(arriveBy),
})
}
res.valid = false
} else if (verbose) {
if (verbose) {
res.arriveBy = getNodeResultMessage('arriveBySuccess', {
arrivalTime: getDateTime(timeOfArrival),
deadline: getDateTime(arriveBy),
})
}
}
} else if (stops[dest].arriveAfter) {
const hours: number = stops[dest].times[src] || 0
const timeOfArrival: Date = addHours(time, hours)
const arriveAfter: Date = stops[dest].arriveAfter || new Date()
if (!dateIsLaterThan(timeOfArrival, arriveAfter)) {
if (verbose) {
res.arriveBy = getNodeResultMessage('arriveAfterFailure', {
arrivalTime: getDateTime(timeOfArrival),
deadline: getDateTime(arriveAfter),
})
}
res.valid = false
} else if (verbose) {
res.arriveBy = getNodeResultMessage('arriveAfterSuccess', {
arrivalTime: getDateTime(timeOfArrival),
deadline: getDateTime(arriveAfter),
})
}
} else if (stops[dest].arriveBetween) {
const hours: number = stops[dest].times[src] || 0
const timeOfArrival: Date = addHours(time, hours)
const arriveBetween: Date[] = stops[dest].arriveBetween || []
if (!dateIsBetween(timeOfArrival, arriveBetween[0], arriveBetween[1])) {
if (verbose) {
res.arriveBy = getNodeResultMessage('arriveBetweenFailure', {
arrivalTime: getDateTime(timeOfArrival),
deadline: getDateTime(arriveBetween[0]),
deadline2: getDateTime(arriveBetween[1]),
})
}
res.valid = false
} else if (verbose) {
res.arriveBy = getNodeResultMessage('arriveBetweenSuccess', {
arrivalTime: getDateTime(timeOfArrival),
deadline: getDateTime(arriveBetween[0]),
deadline2: getDateTime(arriveBetween[1]),
})
}
}
}
if (load && stops[dest].load) {
if (load + stops[dest].load > 100) {
res.valid = false
if (verbose) {
res.load = getNodeResultMessage('loadCapacityFailure', {
capacity: Math.abs(100 - (load + stops[dest].load)),
load,
})
}
} else if (load + stops[dest].load <= 0) {
res.valid = false
if (verbose) {
res.load = getNodeResultMessage('unloadCapacityFailure', {
load,
})
}
} else if (verbose) {
if (stops[dest].load > 0) {
res.load = getNodeResultMessage('loadCapacitySuccess', {
capacity: stops[dest].load,
load,
})
res.capacity = load - stops[dest].load
} else {
res.load = getNodeResultMessage('unloadCapacitySuccess', {
capacity: Math.abs(stops[dest].load),
load,
})
res.capacity = load + stops[dest].load
}
}
}
return res
}
let bCost: number = Infinity
export const solve = (stops: ITspStop[], startTime?: Date, load?: number, ignore?: boolean,
path: number[] = [0], cost?: number): TspResult => {
if (stops.length === 1 || path.length > stops.length || (load !== undefined && (load > 100 || load < 0 ))) {
return []
}
let res: number[][] = []
let conditions: INodeResult | any = { valid: true }
for (let i: number = 1; i < stops.length; i++) {
if (path.indexOf(i) > -1) { continue }
if (ignore !== true) { conditions = nodeConditions(stops, path[path.length - 1], i, startTime, load) }
if (conditions.valid === true) {
const nPath: number[] = path.slice(0)
nPath.push(i)
if (i === stops.length - 1 && path.length !== 1) {
if ((cost || 0) + stops[i].times[path[path.length - 1]] > bCost) { continue }
if (nPath.length === stops.length) { bCost = (cost || 0) + stops[i].times[path[path.length - 1]] }
res.push(nPath)
} else if (path[path.length - 1] !== i) {
if ((cost || 0) + stops[i].times[path[path.length - 1]] > bCost) { continue }
if (nPath.length === stops.length) { bCost = (cost || 0) + stops[i].times[path[path.length - 1]] }
const paths = solve(stops,
startTime !== undefined ?
addHours(startTime, stops[i].times[path[path.length - 1]]) : undefined,
load !== undefined ?
load + (stops[i].load || 0) : undefined,
ignore,
nPath,
(cost || 0) + stops[i].times[path[path.length - 1]])
res.push(...((paths as any).filter((p: number[]) => p.length === stops.length)))
}
}
}
if (path.length === 1) { return prune(stops, res, startTime, load) }
return res
}
export const prune = (stops: ITspStop[], paths: number[][], startTime?: Date, load?: number): ITspResult[] => {
const result: ITspResult[] = []
let bc: number = Infinity
paths.forEach ((p: number[]) => {
let cost: number = 0
for (let i = 1; i < p.length; i++) {
cost += stops[p[i]].times[p[i - 1]]
if (cost > bc || cost > bCost) { break }
}
if (cost < bc && cost <= bCost) {
bc = cost
let rt: Date | undefined = startTime
let rl: number | undefined = load
const details: INodeResult[] = []
for (let i = 1; i < p.length; i++) {
details.push(nodeConditions(stops, p[i - 1], p[i], rt, rl, true))
if (rt) { rt = addHours(rt, stops[p[i]].times[p[i - 1]]) }
if (rl) { rl += (stops[p[i]].load || 0) }
}
result.push({
cost,
details,
path: p,
startTime,
})
}
})
return result.sort((a: any, b: any) => a.cost < b.cost ? -1 : 1)
}
export const sortStopsByCriteria = (stops: ITspStop[]): ITspStop[] => {
const indexMap: any = {}
for (let i = 0; i < stops.length; i++) { indexMap[stops[i].id] = i}
const origin: ITspStop | undefined = stops.shift()
const destination: ITspStop | undefined = stops.pop()
const ss = stops.sort((a: ITspStop, b: ITspStop) => {
const ac: Date | null = a.arriveBy ? a.arriveBy : a.arriveBetween
&& a.arriveBetween[1] ? a.arriveBetween[1] : null
const bc: Date | null = b.arriveBy ? b.arriveBy : b.arriveBetween
&& b.arriveBetween[1] ? b.arriveBetween[1] : null
return ac ? bc ? ac.valueOf() < bc.valueOf() ? -1 : 1 : -1 : bc ? 1 : 0
})
const times: number[][] = []
ss.forEach((stop: any, index: number) => times[index] = stop.times.slice(0))
ss.forEach((stop: any, i: number) => {
if (indexMap[stop.id] - 1 !== i) {
ss.forEach((s: any, j: number) => {
times[j][indexMap[stop.id]] = s.times[i + 1]
times[j][i + 1] = s.times[indexMap[stop.id]]
})
}
})
ss.forEach((stop: any, index: number) => stop.times = times[index])
const result: ITspStop[] = [(origin as ITspStop)]
result.push(...ss)
result.push((destination as ITspStop))
return result
}