1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Route4Me; |
4
|
|
|
|
5
|
|
|
use Route4Me\Address; |
6
|
|
|
use Route4Me\Common; |
7
|
|
|
use Route4Me\RouteParameters; |
8
|
|
|
use Route4Me\OptimizationProblemParams; |
9
|
|
|
use Route4Me\Route; |
10
|
|
|
use Route4Me\Route4Me; |
11
|
|
|
use GuzzleHttp\Client; |
12
|
|
|
|
13
|
|
|
class OptimizationProblem extends Common |
14
|
|
|
{ |
15
|
|
|
static public $apiUrl = '/api.v4/optimization_problem.php'; |
16
|
|
|
static public $apiUrl_addr = '/api.v4/address.php'; |
17
|
|
|
static public $apiHybridUrl = '/api.v4/hybrid_date_optimization.php'; |
18
|
|
|
static public $apiHybridDepotUrl = '/api/change_hybrid_optimization_depot.php'; |
19
|
|
|
|
20
|
|
|
public $optimization_problem_id; |
21
|
|
|
public $user_errors = array(); |
22
|
|
|
public $state; |
23
|
|
|
public $parameters; |
24
|
|
|
public $sent_to_background; |
25
|
|
|
public $addresses = array(); |
26
|
|
|
public $routes = array(); |
27
|
|
|
public $links = array(); |
28
|
|
|
|
29
|
|
|
function __construct() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
$this->parameters = new RouteParameters; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public static function fromArray(array $params) |
35
|
|
|
{ |
36
|
|
|
$problem = new OptimizationProblem; |
37
|
|
|
$problem->optimization_problem_id = Common::getValue($params, 'optimization_problem_id'); |
38
|
|
|
$problem->user_errors = Common::getValue($params, 'user_errors', array()); |
39
|
|
|
$problem->state = Common::getValue($params, 'state', array()); |
40
|
|
|
$problem->sent_to_background = Common::getValue($params, 'sent_to_background', array()); |
41
|
|
|
$problem->links = Common::getValue($params, 'links', array()); |
42
|
|
|
|
43
|
|
|
if (isset($params['parameters'])) { |
44
|
|
|
$problem->parameters = RouteParameters::fromArray($params['parameters']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
if (isset($params['addresses'])) { |
|
|
|
|
48
|
|
|
$addresses = array(); |
49
|
|
|
foreach ($params['addresses'] as $address) { |
50
|
|
|
$addresses[] = Address::fromArray($address); |
51
|
|
|
} |
52
|
|
|
$problem->addresses = $addresses; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
View Code Duplication |
if (isset($params['routes'])) { |
|
|
|
|
56
|
|
|
$routes = array(); |
57
|
|
|
foreach ($params['routes'] as $route) { |
58
|
|
|
$routes[] = Route::fromArray($address); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
$problem->routes = $routes; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $problem; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public static function optimize(OptimizationProblemParams $params) |
67
|
|
|
{ |
68
|
|
|
$optimize = Route4Me::makeRequst(array( |
69
|
|
|
'url' => self::$apiUrl, |
70
|
|
|
'method' => 'POST', |
71
|
|
|
'query' => array( |
72
|
|
|
'directions' => isset($params->directions) ? $params->directions: null, |
73
|
|
|
'format' => isset($params->format) ? $params->format: null, |
74
|
|
|
'route_path_output' => isset($params->route_path_output) ? $params->route_path_output: null, |
75
|
|
|
'optimized_callback_url' => isset($params->optimized_callback_url) ? $params->optimized_callback_url: null |
76
|
|
|
), |
77
|
|
|
'body' => array( |
78
|
|
|
'addresses' => $params->getAddressesArray(), |
79
|
|
|
'parameters' => $params->getParametersArray() |
80
|
|
|
) |
81
|
|
|
)); |
82
|
|
|
|
83
|
|
|
return OptimizationProblem::fromArray($optimize); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public static function get($params) |
87
|
|
|
{ |
88
|
|
|
$optimize = Route4Me::makeRequst(array( |
89
|
|
|
'url' => self::$apiUrl, |
90
|
|
|
'method' => 'GET', |
91
|
|
|
'query' => array( |
92
|
|
|
'state' => isset($params['state']) ? $params['state'] : null, |
93
|
|
|
'limit' => isset($params['limit']) ? $params['limit'] : null, |
94
|
|
|
'offset' => isset($params['offset']) ? $params['offset'] : null, |
95
|
|
|
'optimization_problem_id' => isset($params['optimization_problem_id']) |
96
|
|
|
? $params['optimization_problem_id'] : null, |
97
|
|
|
'wait_for_final_state' => isset($params['wait_for_final_state']) |
98
|
|
|
? $params['wait_for_final_state'] : null, |
99
|
|
|
) |
100
|
|
|
)); |
101
|
|
|
|
102
|
|
|
if (isset($optimize['optimizations'])) { |
103
|
|
|
$problems = array(); |
104
|
|
|
foreach($optimize['optimizations'] as $problem) { |
105
|
|
|
$problems[] = OptimizationProblem::fromArray($problem); |
106
|
|
|
} |
107
|
|
|
return $problems; |
108
|
|
|
} else { |
109
|
|
|
return OptimizationProblem::fromArray($optimize); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public static function reoptimize($params) |
114
|
|
|
{ |
115
|
|
|
$param = new OptimizationProblemParams; |
116
|
|
|
$param->optimization_problem_id = isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null; |
117
|
|
|
$param->reoptimize = 1; |
118
|
|
|
|
119
|
|
|
return self::update((array)$param); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public static function update($params) |
123
|
|
|
{ |
124
|
|
|
$optimize = Route4Me::makeRequst(array( |
125
|
|
|
'url' => self::$apiUrl, |
126
|
|
|
'method' => 'PUT', |
127
|
|
|
'query' => array( |
128
|
|
|
'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null, |
129
|
|
|
'addresses' => isset($params['addresses']) ? $params['addresses'] : null, |
130
|
|
|
'reoptimize' => isset($params['reoptimize']) ? $params['reoptimize'] : null, |
131
|
|
|
) |
132
|
|
|
)); |
133
|
|
|
|
134
|
|
|
return $optimize; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getOptimizationId() |
138
|
|
|
{ |
139
|
|
|
return $this->optimization_problem_id; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getRoutes() |
143
|
|
|
{ |
144
|
|
|
return $this->routes; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function getRandomOptimizationId($offset,$limit) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$query['limit'] = isset($params['limit']) ? $params['limit'] : 30; |
|
|
|
|
150
|
|
|
$query['offset'] = isset($params['offset']) ? $params['offset'] : 0; |
151
|
|
|
|
152
|
|
|
$json = Route4Me::makeRequst(array( |
153
|
|
|
'url' => self::$apiUrl, |
154
|
|
|
'method' => 'GET', |
155
|
|
|
'query' => $query |
156
|
|
|
)); |
157
|
|
|
|
158
|
|
|
$optimizations = array(); |
159
|
|
|
foreach($json as $optimization) { |
160
|
|
|
if (gettype($optimization)!="array") continue; |
161
|
|
|
foreach ($optimization as $otp1) { |
162
|
|
|
$optimizations[] = $otp1; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$num=rand(0,sizeof($optimizations)-1); |
167
|
|
|
//echo "num=$num.<br>".sizeof($optimizations)."<br>"; |
|
|
|
|
168
|
|
|
$rOptimization=$optimizations[$num]; |
169
|
|
|
return $rOptimization; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function getAddresses($opt_id) |
173
|
|
|
{ |
174
|
|
|
if ($opt_id==null) return null; |
175
|
|
|
|
176
|
|
|
$params = array( "optimization_problem_id" => $opt_id ); |
177
|
|
|
|
178
|
|
|
$optimization = (array)$this->get($params); |
179
|
|
|
|
180
|
|
|
$addresses = $optimization["addresses"]; |
181
|
|
|
|
182
|
|
|
return $addresses; |
183
|
|
|
|
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function getRandomAddressFromOptimization($opt_id) |
187
|
|
|
{ |
188
|
|
|
$addresses = (array)$this->getAddresses($opt_id); |
189
|
|
|
|
190
|
|
|
if ($addresses == null) { |
191
|
|
|
echo "There are no addresses in this optimization!.. Try again."; |
192
|
|
|
return null; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
$num=rand(0,sizeof($addresses)-1); |
196
|
|
|
$rAddress=$addresses[$num]; |
197
|
|
|
|
198
|
|
|
return $rAddress; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
View Code Duplication |
public function removeAddress($params) |
|
|
|
|
202
|
|
|
{ |
203
|
|
|
$response = Route4Me::makeRequst(array( |
204
|
|
|
'url' => self::$apiUrl_addr, |
205
|
|
|
'method' => 'DELETE', |
206
|
|
|
'query' => array( |
207
|
|
|
'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null, |
208
|
|
|
'route_destination_id' => isset($params['route_destination_id']) ? $params['route_destination_id'] : null, |
209
|
|
|
) |
210
|
|
|
)); |
211
|
|
|
|
212
|
|
|
return $response; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
View Code Duplication |
public function removeOptimization($params) |
|
|
|
|
216
|
|
|
{ |
217
|
|
|
$response = Route4Me::makeRequst(array( |
218
|
|
|
'url' => self::$apiUrl, |
219
|
|
|
'method' => 'DELETE', |
220
|
|
|
'query' => array( |
221
|
|
|
'redirect' => isset($params['redirect']) ? $params['redirect'] : null, |
222
|
|
|
), |
223
|
|
|
'body' => array( |
224
|
|
|
'optimization_problem_ids' => isset($params['optimization_problem_ids']) ? $params['optimization_problem_ids'] : null, |
225
|
|
|
) |
226
|
|
|
)); |
227
|
|
|
|
228
|
|
|
return $response; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
View Code Duplication |
public function getHybridOptimization($params) |
|
|
|
|
232
|
|
|
{ |
233
|
|
|
$optimize = Route4Me::makeRequst(array( |
234
|
|
|
'url' => self::$apiHybridUrl, |
235
|
|
|
'method' => 'GET', |
236
|
|
|
'query' => array( |
237
|
|
|
'target_date_string' => isset($params['target_date_string']) ? $params['target_date_string'] : null, |
238
|
|
|
'timezone_offset_minutes' => isset($params['timezone_offset_minutes']) ? $params['timezone_offset_minutes'] : null |
239
|
|
|
) |
240
|
|
|
)); |
241
|
|
|
|
242
|
|
|
return $optimize; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
View Code Duplication |
Public function addDepotsToHybrid($params) |
|
|
|
|
246
|
|
|
{ |
247
|
|
|
$depots = Route4Me::makeRequst(array( |
248
|
|
|
'url' => self::$apiHybridDepotUrl, |
249
|
|
|
'method' => 'POST', |
250
|
|
|
'query' => array( |
251
|
|
|
'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null, |
252
|
|
|
), |
253
|
|
|
'body' => array( |
254
|
|
|
'optimization_problem_id' => isset($params['optimization_problem_id']) ? $params['optimization_problem_id'] : null, |
255
|
|
|
'delete_old_depots' => isset($params['delete_old_depots']) ? $params['delete_old_depots'] : null, |
256
|
|
|
'new_depots' => isset($params['new_depots']) ? $params['new_depots'] : null, |
257
|
|
|
) |
258
|
|
|
)); |
259
|
|
|
|
260
|
|
|
return $depots; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.