1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
|
3
|
1 |
|
""" |
4
|
|
|
An Optimization Problem refers to a collection of addresses that need to be |
5
|
|
|
visited. |
6
|
|
|
|
7
|
|
|
The optimization problem takes into consideration all of the addresses that |
8
|
|
|
need to be visited and all the constraints associated with each address and |
9
|
|
|
depot. |
10
|
|
|
|
11
|
|
|
It is preferable to create an optimization problem with as many orders in it |
12
|
|
|
as possible, so that the optimization engine is able to consider the entire |
13
|
|
|
problem set. |
14
|
|
|
|
15
|
|
|
This is different from a :class:`~route4me.sdk.resources.routes.Route`, which |
16
|
|
|
is a sequence of addresses that need to be visited by a single vehicle and |
17
|
|
|
driver in a fixed time period. Solving an Optimization Problem results in |
18
|
|
|
a number of routes. (Possibly recurring in the future) |
19
|
|
|
|
20
|
|
|
.. seealso:: https://route4me.io/docs/#optimizations |
21
|
|
|
|
22
|
|
|
""" |
23
|
|
|
|
24
|
1 |
|
import pydash |
25
|
|
|
|
26
|
1 |
|
from .._net import NetworkClient |
27
|
1 |
|
from ..models import Optimization |
28
|
|
|
|
29
|
1 |
|
from ..errors import Route4MeApiError |
30
|
|
|
|
31
|
|
|
|
32
|
1 |
|
class Optimizations(object): |
33
|
|
|
""" |
34
|
|
|
Optimizations resource |
35
|
|
|
""" |
36
|
|
|
|
37
|
1 |
|
def __init__(self, api_key=None, _network_client=None): |
38
|
1 |
|
nc = _network_client |
39
|
1 |
|
if nc is None: |
40
|
1 |
|
nc = NetworkClient(api_key) |
41
|
1 |
|
self.__nc = nc |
42
|
|
|
|
43
|
1 |
|
def create( |
44
|
|
|
self, |
45
|
|
|
optimization_data, |
46
|
|
|
callback_url=None, |
47
|
|
|
): |
48
|
|
|
""" |
49
|
|
|
Create a new optimization through the Route4Me API |
50
|
|
|
|
51
|
|
|
You could pass any valid URL as :paramref:`callback_url` |
52
|
|
|
parameter. |
53
|
|
|
|
54
|
|
|
The callback URL is a URL that gets called when the optimization is |
55
|
|
|
solved, or if there is an error. The callback is called with a |
56
|
|
|
``POST`` request. The POST data sent is: |
57
|
|
|
|
58
|
|
|
- ``timestamp`` (seconds) |
59
|
|
|
- ``optimization_problem_id`` |
60
|
|
|
- ``state`` (id of the optimization state) |
61
|
|
|
|
62
|
|
|
The state is a value from the enumeration |
63
|
|
|
:class:`route4me.sdk.enums.OptimizationStateEnum` |
64
|
|
|
|
65
|
|
|
:param optimization_data: Optimization data |
66
|
|
|
:type optimization_data: ~route4me.sdk.models.Optimization or dict |
67
|
|
|
:param callback_url: *Optimization done* callback URL |
68
|
|
|
:type callback_url: str or None |
69
|
|
|
:returns: New optimization |
70
|
|
|
:rtype: ~route4me.sdk.models.Optimization |
71
|
|
|
""" |
72
|
|
|
|
73
|
1 |
|
query = None |
74
|
1 |
|
if callback_url: |
75
|
1 |
|
query = { |
76
|
|
|
'optimized_callback_url': str(callback_url), |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
data = optimization_data |
80
|
|
|
|
81
|
|
|
# if isinstance(optimization_data, BaseModel): |
82
|
|
|
# data = optimization_data.raw |
83
|
|
|
|
84
|
1 |
|
res = self.__nc.post( |
85
|
|
|
'/api.v4/optimization_problem.php', |
86
|
|
|
subdomain='www', |
87
|
|
|
query=query, |
88
|
|
|
data=data, |
89
|
|
|
) |
90
|
1 |
|
return Optimization(res) |
91
|
|
|
|
92
|
1 |
|
def get(self, ID): |
93
|
|
|
""" |
94
|
|
|
GET a single optimization by ID. |
95
|
|
|
|
96
|
|
|
:param ID: Optimization Problem ID |
97
|
|
|
:type ID: str |
98
|
|
|
:returns: Optimization data |
99
|
|
|
:rtype: ~route4me.sdk.models.Optimization |
100
|
|
|
|
101
|
|
|
:raises ~route4me.sdk.errors.Route4MeEntityNotFoundError: if \ |
102
|
|
|
optimization was not found |
103
|
|
|
""" |
104
|
|
|
|
105
|
1 |
|
res = self.__nc.get( |
106
|
|
|
'/api.v4/optimization_problem.php', |
107
|
|
|
subdomain='www', |
108
|
|
|
query={ |
109
|
|
|
'optimization_problem_id': str(ID), |
110
|
|
|
} |
111
|
|
|
) |
112
|
|
|
|
113
|
1 |
|
return Optimization(res) |
114
|
|
|
|
115
|
1 |
|
def list(self): |
116
|
|
|
pass |
117
|
|
|
|
118
|
1 |
|
def update(self): |
119
|
|
|
pass |
120
|
|
|
|
121
|
1 |
|
def remove(self, ID): |
122
|
|
|
""" |
123
|
|
|
Remove an existing optimization belonging to an user. |
124
|
|
|
|
125
|
|
|
:param ID: Optimization Problem ID |
126
|
|
|
:type ID: str |
127
|
|
|
:returns: Always :data:`True` |
128
|
|
|
:rtype: bool |
129
|
|
|
|
130
|
|
|
:raises ~route4me.sdk.errors.Route4MeApiError: if Route4Me API \ |
131
|
|
|
returned not expected response |
132
|
|
|
:raises ~route4me.sdk.errors.Route4MeEntityNotFoundError: if \ |
133
|
|
|
optimization was not found |
134
|
|
|
""" |
135
|
|
|
|
136
|
1 |
|
res = self.__nc.delete( |
137
|
|
|
'/api.v4/optimization_problem.php', |
138
|
|
|
subdomain='www', |
139
|
|
|
query={ |
140
|
|
|
'optimization_problem_id': str(ID), |
141
|
|
|
} |
142
|
|
|
) |
143
|
|
|
|
144
|
1 |
|
if not pydash.get(res, 'status'): |
145
|
|
|
# TODO: this exception should contain METHOD and URL fields |
146
|
1 |
|
raise Route4MeApiError( |
147
|
|
|
'Not expected response', |
148
|
|
|
code='route4me.sdk.api_error', |
149
|
|
|
details={ |
150
|
|
|
'res': res, |
151
|
|
|
}, |
152
|
|
|
method='DELETE', |
153
|
|
|
# url='' |
154
|
|
|
) |
155
|
|
|
|
156
|
|
|
return True |
157
|
|
|
|