-
Notifications
You must be signed in to change notification settings - Fork 47
/
tsp_worker.h
65 lines (44 loc) · 1.96 KB
/
tsp_worker.h
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
#ifndef NODE_OR_TOOLS_TSP_WORKER_3C61C259EF2C_H
#define NODE_OR_TOOLS_TSP_WORKER_3C61C259EF2C_H
#include <nan.h>
#include "adaptors.h"
#include "types.h"
#include <memory>
#include <utility>
#include <vector>
struct TSPWorker final : Nan::AsyncWorker {
using Base = Nan::AsyncWorker;
TSPWorker(std::shared_ptr<const CostMatrix> costs_, Nan::Callback* callback, const RoutingModelParameters& modelParams_,
const RoutingSearchParameters& searchParams_, std::int32_t numNodes, std::int32_t numVehicles,
std::int32_t vehicleDepot)
: Base(callback), costs{std::move(costs_)}, model{numNodes, numVehicles, NodeIndex{vehicleDepot}, modelParams_},
modelParams{modelParams_}, searchParams{searchParams_} {}
void Execute() override {
auto costAdaptor = makeBinaryAdaptor(*costs);
auto costEvaluator = makeCallback(costAdaptor);
model.SetArcCostEvaluatorOfAllVehicles(costEvaluator);
const auto* assignment = model.SolveWithParameters(searchParams);
if (!assignment || (model.status() != RoutingModel::Status::ROUTING_SUCCESS))
SetErrorMessage("Unable to find a solution");
model.AssignmentToRoutes(*assignment, &routes);
if (routes.size() != 1)
SetErrorMessage("Expected route for one vehicle");
}
void HandleOKCallback() override {
Nan::HandleScope scope;
const auto& route = routes.front();
auto jsRoute = Nan::New<v8::Array>(route.size());
for (std::size_t j = 0; j < route.size(); ++j)
(void)Nan::Set(jsRoute, j, Nan::New<v8::Number>(route[j].value()));
const auto argc = 2u;
v8::Local<v8::Value> argv[argc] = {Nan::Null(), jsRoute};
callback->Call(argc, argv);
}
std::shared_ptr<const CostMatrix> costs; // inc ref count to keep alive for async cb
RoutingModel model;
RoutingModelParameters modelParams;
RoutingSearchParameters searchParams;
// Stores solution until we can translate back to v8 objects
std::vector<std::vector<NodeIndex>> routes;
};
#endif