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