-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
97 lines (81 loc) · 3.21 KB
/
app.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
const { promises: fs } = require("fs");
const inquirer = require('inquirer');
const eol = require("eol");
const TSPSolverExato = require("./src/TSPSolverExato");
const TSPSolverAproximativo = require("./src/TSPSolverAproximativo");
const NS_PER_SEC = 1e9;
const MS_PER_NS = 1e-6;
const TSPdir = './data';
const enumAlgorithms = {
EXATO: "exato",
APROXIMATIVO: "aproximativo",
};
(async () => {
try {
// Lê as instâncias do caixeiro viajante dentro da pasta /data
async function ls(path) {
const dir = await fs.opendir(path)
const files = []
for await (const dirent of dir) {
files.push(dirent.name)
}
return files
}
// Lê uma instância do caixeiro viajante
async function loadTSP(file) {
return await fs.readFile(`${TSPdir}/${file}`, 'utf-8')
}
// Menu de opções
inquirer.prompt([
{
type: 'list',
name: 'algorithm',
message: 'Selecione o tipo de algoritimo que tu deseja utilizar',
choices: [enumAlgorithms.EXATO, enumAlgorithms.APROXIMATIVO],
},
{
type: 'list',
name: 'file',
message: 'Selecione a instância do caixeiro viajante que tu deseja resolver',
choices: [...await ls('./data')],
},
])
.then(async ({algorithm, file}) => {
// Lê as instâncias do caixeiro viajante
const instanciaTSP = await loadTSP(file);
// Transforma o conteúdo do txt em matrix[number][number]
let lines = eol.split(instanciaTSP)
const TSPMatrix = [];
lines.forEach(line => {
let TSPLine = []
line.split(' ')
.filter(char => !!char && char)
.forEach(char => TSPLine.push(parseInt(char)))
TSPMatrix.push(TSPLine)
})
// inicio cronometro
const time = process.hrtime();
if(algorithm == enumAlgorithms.EXATO) {
// Execução do TSPSolver Exato
const tspSolverExato = new TSPSolverExato(TSPMatrix, lines.length)
tspSolverExato.calculateMinPath((min_path) => {
// Término cronometro
const diff = process.hrtime(time)
console.log(`Tempo de execução: ${(diff[0] * NS_PER_SEC + diff[1]) * MS_PER_NS } milisegundos`)
console.log(`Resultado: ${min_path}`)
})
} else {
// Execução do TSPSolver Aproximativo
TSPSolverAproximativo(TSPMatrix, (min_path) => {
// Término cronometro
const diff = process.hrtime(time)
console.log(`Tempo de execução: ${(diff[0] * NS_PER_SEC + diff[1]) * MS_PER_NS } milisegundos`)
console.log(`Resultado: ${min_path}`)
})
}
}
)
} catch (e) {
console.log("e", e)
}
})()