UNPKG

4.11 kBJavaScriptView Raw
1/*
2 * To change this license header, choose License Headers in Project Properties.
3 * To change this template file, choose Tools | Templates
4 * and open the template in the editor.
5 */
6"use strict";
7
8var http = require('http');
9var _ = require('lodash');
10var fs = require("fs");
11
12
13/**
14 * optimize problem
15 *
16 */
17function sendDataToServer(user,pwd,host,noOfCust,threads) {
18 var user_data = JSON.stringify({
19 "username": user,
20 "password": pwd,
21 });
22 var token = '';
23 var options = {
24 host: host,
25 path: '/',
26 port: '80',
27 method: 'POST',
28 headers: {
29 'Content-Type': 'application/json',
30 'Content-Length': '0'
31 }
32 };
33 //http call to login and retrieve token
34 _httpCall({
35 path: '/cvrp/rest/login', headers: {
36 'Content-Length': Buffer.byteLength(user_data), 'Content-Type': 'application/json'
37 }
38 }, user_data, _loginCallBack);
39 function _httpCall(inputs, inputData, callBack) {
40 options = _.extend(options, inputs);
41 var req = http.request(options, function (response) {
42 var str = '';
43 response.on('data', function (chunk) {
44 console.log("data");
45 str += chunk;
46 });
47 response.on('error', function (data) {
48 console.log("daerrta");
49 callBack(data);
50 });
51 response.on('end', function () {
52 console.log("end");
53 callBack(str);
54 });
55 });
56 if (inputData !== "")
57 req.write(inputData);
58 req.end();
59 }
60 /**
61 * login API callback
62 * @param {type} data
63 *
64 */
65 function _loginCallBack(data) {
66 // console.log("data recieved :"+data);
67 var out = JSON.parse(data);
68 token = out.token;
69
70 console.log("data recieved for user :" + out.token);
71 //var noOfCust = 100;
72 // noOfCust = getArg("n");
73 var profileFileName = "./optimize_test/samples/problem_" + noOfCust + ".json";
74 if (fs.existsSync(profileFileName)) {
75 var problem = JSON.parse(fs.readFileSync(profileFileName));
76 var d = new Date();
77 var n = d.getMilliseconds();
78 problem["_id"] = problem["_id"] + "_" + n;
79 problem["name"] = problem["name"] + "_" + n;
80 var out = JSON.stringify(problem);
81
82 _httpCall({
83 path: '/cvrp/rest/problem', headers: {
84 'Content-Length': Buffer.byteLength(out), 'Content-Type': 'application/json',
85 'x-access-token': token
86 }
87 }, out, _createProblemCallback);
88
89 } else {
90 console.warn(profileFileName + " does not exist");
91 }
92
93 }
94 /**
95 * CreateProblem API callback
96 * @param {type} data
97 *
98 */
99 function _createProblemCallback(data) {
100 console.log("prob call back :");
101 var out = JSON.parse(data);
102 var prob_Id = out["_id"];
103
104 console.log("prob id :" + prob_Id);
105 var path = '/cvrp/rest/geocode_problem/' + prob_Id;
106 _httpCall({
107 path: '/cvrp/rest/geocode_problem/' + prob_Id, method: 'GET', headers: {
108 'x-access-token': token
109 }
110 }, "", _geocodeCallBack);
111 }
112
113 /**
114 * Geocode API callback
115 * @param {type} data
116 *
117 */
118 function _geocodeCallBack(data) {
119 var out = JSON.parse(data);
120 var prob_Id = out["_id"];
121
122 console.log("prob id after geo code :" + prob_Id);
123
124 var out = JSON.stringify({
125 "engine": "siwei",
126 "solutionId": "test_01",
127 "maxIterations": 300,
128 "nThreads": threads,
129 "constraints": "capacity_timeWindow_restrictJobsToSpecificVehicles_priorityJob"
130 });
131
132 _httpCall({
133 path: '/cvrp/rest/optimize_problem/' + prob_Id, method: 'POST', headers: {
134 'Content-Length': Buffer.byteLength(out), 'Content-Type': 'application/json',
135 'x-access-token': token
136 }
137 }, out, function (data) {
138 var out = JSON.parse(data);
139 var duration = out["calc_duration"];
140
141 console.log("completed optimization in :" + duration);
142 });
143 }
144}
145
146function getArg(key) {
147 if (process.argv.indexOf("-" + key) !== -1) {
148 return process.argv[process.argv.indexOf("-" + key) + 1];
149 }
150}
151exports.optimize = function(user,pwd,host,noOfCust,threads){
152 console.log("data"+threads);
153 sendDataToServer(user,pwd,host,noOfCust,threads);
154};