Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,14 @@ ipcMain.on("start:route-generation", (event, routingArgs) => {
nodes: {}, dist: {}, cost: {}
}

let minNumVehicles = Math.max(routingArgs.numVehicles, Math.floor(routingArgs.stops.length / routingArgs.maxCapacity));
type_Capacity = typeof routingArgs.maxCapacity;
if (type_Capacity !== 'object') {
routingArgs.maxCapacity = [Number(routingArgs.maxCapacity)];
}
routingArgs.maxCapacity.sort((a,b)=> b-a);
let minNumVehicles = Math.max(routingArgs.numVehicles, Math.floor(routingArgs.stops.length / routingArgs.maxCapacity[0]));
routingArgs.numVehicles = minNumVehicles;
routeOptimizer.optimize(cachedODMatrix, routingArgs)
routeOptimizer.optimize(cachedODMatrix, routingArgs);
})

// Evento chamado pelo nosso worker quando ele terminar de gerar a rota
Expand Down
1 change: 1 addition & 0 deletions src/main/routing/clarke-wright-schoolbus-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class ClarkeWrightSchoolBusRouting {
// console.log("PASSENGERS", totalPassengers, "MAX CAPACITY", this.maxCapacity)
// console.log("TRAV DISTANCE", totalTravDistance, "MAX DIST", this.maxTravDist)
// console.log("TRAV TIME", totalTravTime, "MAX TIME", this.maxTravTime)
// console.log("\n")

// We can merge!
let canMerge = false;
Expand Down
70 changes: 60 additions & 10 deletions src/main/routing/routing-optimization.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,23 @@ var ClarkeWrightSchoolBusRouting = require("./clarke-wright-schoolbus-routing.js
var TwoOpt = require("./twoopt.js");
var SchoolBusKMeans = require("./kmeans.js");
var { Worker, isMainThread, parentPort, workerData } = require('worker_threads');
const { toNumber } = require("lodash");

class RoutingOptimizationWorker {
constructor(cachedODMatrix, routingParams, spatialiteDB) {
this.cachedODMatrix = cachedODMatrix;
this.routingParams = routingParams;
this.spatialiteDB = spatialiteDB;
this.reverseMap = new Map();

// IDENTIFICANDO O MODO DE SOLUÇÃO
if (typeof this.routingParams["maxCapacity"] == typeof []) {
this.heterogeneous = true;
console.log("\n *** FROTA HETEROGÊNEA ***\n")
} else {
this.heterogeneous = false;
console.log("\n *** FROTA HOMOGÊNEA ***\n")
}

routingParams["stops"].forEach((s) => {
let key = Number(s["lat"]).toFixed(10) + "-" + Number(s["lng"]).toFixed(10);
Expand Down Expand Up @@ -45,6 +55,34 @@ class RoutingOptimizationWorker {
return stops;
}

SortClustersMaxToMin(clusterizedStops){
let ClustersSorted = {};
for (let i = 0; i < clusterizedStops.length; i++) {
let clusterStops = clusterizedStops[i];
let countPassagers = 0;
for (let z = 0; z < clusterStops.length; z++) {
countPassagers = countPassagers + clusterStops[z].passengers;
}
ClustersSorted[countPassagers] = clusterStops;
}
//console.log("ClustersSorted = ",ClustersSorted);

let IndexByCluester = Array();
Object.keys(ClustersSorted).forEach((index)=>{
IndexByCluester.push(Number(index));
});
IndexByCluester.sort((a,b)=> b-a);
//console.log("IndexByCluester = ",IndexByCluester);

let SortClusters = Array();
for (let i = 0; i < IndexByCluester.length; i++) {
SortClusters.push(ClustersSorted[IndexByCluester[i]]);
}

//console.log("SortClusters = ",SortClusters);
return SortClusters;
}

optimize() {
return new Promise((resolve, reject) => {
// Activate spatial db
Expand All @@ -57,33 +95,46 @@ class RoutingOptimizationWorker {
let busRoutes = new Array();
let kmeans = new SchoolBusKMeans(this.routingParams);
let routingGraph;
let IterCapacity = 0;

kmeans.partition(this.routingParams["numVehicles"])
.then(clusters => {
let clusterizedStops = new Array();
clusters.forEach((c) => clusterizedStops.push(this.getStops(c.cluster)))

// CASO PARTICULAR PARA FROTA HETEROGÊNEA
if (this.heterogeneous == true) {
clusterizedStops = this.SortClustersMaxToMin(clusterizedStops);
}

let clarkAlgorithmsPromise = new Array();
clusterizedStops.forEach((cs) => {
let param = Object.assign({}, this.routingParams);
param["stops"] = cs;

// CASO PARTICULAR PARA FROTA HETEROGÊNEA
if (this.heterogeneous == true) {
param["maxCapacity"] = this.routingParams["maxCapacity"][IterCapacity];
IterCapacity++;
}

// Deixar apenas as escolas que atendem os alunos no conjunto
let clusterSchoolsSet = new Set()
cs.forEach(student => clusterSchoolsSet.add(student["school"]))
let clusterSchools = new Array()
let clusterSchoolsSet = new Set();
cs.forEach(student => clusterSchoolsSet.add(student["school"]));
let clusterSchools = new Array();
this.routingParams.schools.forEach(school => {
if (clusterSchoolsSet.has(school["key"])) {
clusterSchools.push(school);
}
})
});
param["schools"] = clusterSchools;

let cwalg = new ClarkeWrightSchoolBusRouting(this.cachedODMatrix, param, this.spatialiteDB);
clarkAlgorithmsPromise.push(cwalg.spatialRoute());
routers.push(cwalg);
})


});

// let schoolBusRouter = new ClarkeWrightSchoolBusRouting(this.routingParams, this.spatialiteDB);
// schoolBusRouter.spatialRoute().then((busRoutes) => {
return Promise.all(clarkAlgorithmsPromise)
Expand Down Expand Up @@ -164,7 +215,6 @@ class RoutingOptimizationWorker {
};
}


if (isMainThread) {
module.exports = class RoutingOptimization {
constructor(app, dbPath) {
Expand Down Expand Up @@ -218,12 +268,12 @@ if (isMainThread) {
.then((res) => {
console.log("WORKER FINISHED")
parentPort.postMessage({ error: false, result: res })
// process.exit(0)
//process.exit(0)
})
.catch((err) => {
console.log("WORKER ERROR")
parentPort.postMessage({ error: true, result: err })
// process.exit(1)
//process.exit(1)
})

})
Expand Down
Binary file added src/renderer/img/icones/lancha-marcador2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/renderer/img/icones/onibus-marcador2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions src/renderer/modules/rota/rota-sugestao-ctrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,6 @@ function transformaEscolaEmArray(mapaEscolas) {
return escolasArray;
}


function listaElementos() {
alunos = transformaAlunosEmArray([...alunoMap])
escolas = transformaEscolaEmArray([...escolaMap])
Expand Down Expand Up @@ -938,16 +937,17 @@ function initSimulation() {
loadingFn("Simulando...")

// Juntar dados em um objeto
// Prof. deixei assim para facilitar sua correção os códigos original estão ao lado.
let routeGenerationInputData = {
"maxTravDist": Number($("#maxDist").val()) * 1000,
"maxTravTime": Number($("#maxTime").val()) * 60,
"maxTravDist": 999999999,//Number($("#maxDist").val()) * 1000,
"maxTravTime": 999999999,//Number($("#maxTime").val()) * 60,
"optTarget": "maxTravDist",
"numVehicles": Number($("#numVehicles").val()),
"maxCapacity": Number($("#maxCapacity").val()),
"numVehicles": 1,//Number($("#numVehicles").val()),
"maxCapacity": [4,1,1],//Number($("#maxCapacity").val()),
"busSpeed": Number($("#velMedia").val()) / 3.6, // converte de km/h para m/s
"garage": garagens,
"stops": alunos,
"schools": escolas,
"schools": escolas
};

ipcRenderer.send('start:route-generation', routeGenerationInputData);
Expand Down