-
Notifications
You must be signed in to change notification settings - Fork 1
/
_parameters.py
218 lines (138 loc) · 4.7 KB
/
_parameters.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
import contextlib
import enum
from typing import Type, Union
from pydantic import confloat, conint
from scipy.integrate import RK23, RK45, DOP853, Radau, BDF, LSODA
from ._base import mpe_module, ImmutableDataObject
@mpe_module
class TravelTimeOrder(enum.IntEnum):
"""The enumeration of travel time computation orders
Orders:
- **first** -- the first ordered travel time computation
- **second** -- the second ordered travel time computation
"""
first = 1
second = 2
SupportedOdeSolvers = Union[RK23, RK45, DOP853, Radau, BDF, LSODA]
@mpe_module
class OdeSolverMethod(str, enum.Enum):
"""The enumeration of supported ODE solver methods
"""
RK23 = 'RK23', RK23
RK45 = 'RK45', RK45
DOP853 = 'DOP853', DOP853
Radau = 'Radau', Radau
BDF = 'BDF', BDF
LSODA = 'LSODA', LSODA
def __new__(cls, value: str, solver: Type[SupportedOdeSolvers]):
obj = str.__new__(cls, value) # noqa
obj._value_ = value
obj.solver = solver
return obj
@mpe_module
class Parameters(ImmutableDataObject):
"""MPE algorithm parameters model
.. py:attribute:: travel_time_spacing
The travel time computation spacing
| default: 1.0
.. py:attribute:: travel_time_order
The travel time computation order
| default: ``TravelTimeOrder.first``
.. py:attribute:: travel_time_cache
Use or not travel time computation cache for extracting paths with way points
| default: True
.. py:attribute:: ode_solver_method
ODE solver method
| default: 'RK45'
.. py:attribute:: integrate_time_bound
Integration time bound
| default: 10000
.. py:attribute:: integrate_min_step
Integration minimum step
| default: 0.0
.. py:attribute:: integrate_max_step
Integration maximum step
| default: 4.0
.. py:attribute:: dist_tol
Distance tolerance for control path evolution
| default: 1e-03
.. py:attribute:: max_small_dist_steps
The max number of small distance steps while path evolution
| default: 100
"""
travel_time_spacing: confloat(gt=0.0) = 1.0
travel_time_order: TravelTimeOrder = TravelTimeOrder.second
travel_time_cache: bool = False
ode_solver_method: OdeSolverMethod = OdeSolverMethod.RK45
integrate_time_bound: confloat(gt=0.0) = 10000.0
integrate_min_step: confloat(ge=0.0) = 0.0
integrate_max_step: confloat(gt=0.0) = 4.0
dist_tol: confloat(ge=0.0) = 1e-3
max_small_dist_steps: conint(strict=True, gt=1) = 100
def __str__(self): # pragma: no cover
return self.__repr_str__('\n')
_default_parameters = Parameters()
@mpe_module
@contextlib.contextmanager
def parameters(**kwargs):
"""Context manager for using specified parameters
Parameters
----------
kwargs : mapping
The parameters
Examples
--------
.. code-block:: python
>>> from skmpe import parameters
>>> with parameters(integrate_time_bound=200000) as params:
>>> print(params.__repr__())
Parameters(
travel_time_spacing=1.0,
travel_time_order=<TravelTimeOrder.first: 1>,
travel_time_cache=False,
ode_solver_method=<OdeSolverMethod.RK45: 'RK45'>,
integrate_time_bound=200000.0,
integrate_min_step=0.0,
integrate_max_step=4.0,
dist_tol=0.001,
max_small_dist_steps=100
)
.. code-block:: python
from skmpe import parameters, mpe
...
with parameters(integrate_time_bound=200000):
path_result = mpe(start_point, end_point)
"""
global _default_parameters
prev_default_parameters = _default_parameters
_default_parameters = Parameters(**kwargs)
try:
yield _default_parameters
finally:
_default_parameters = prev_default_parameters
@mpe_module
def default_parameters() -> Parameters:
"""Returns the default parameters
Returns
-------
parameters : Parameters
Default parameters
Examples
--------
.. code-block:: python
>>> from skmpe import default_parameters
>>> print(default_parameters().__repr__())
Parameters(
travel_time_spacing=1.0,
travel_time_order=<TravelTimeOrder.first: 1>,
travel_time_cache=False,
ode_solver_method=<OdeSolverMethod.RK45: 'RK45'>,
integrate_time_bound=10000.0,
integrate_min_step=0.0,
integrate_max_step=4.0,
dist_tol=0.001,
max_small_dist_steps=100
)
"""
return _default_parameters