cuOpt Self-Hosted Thin Client API Example¶
1import os
2from cuopt_sh_client import CuOptServiceSelfHostClient
3
4data = {"cost_matrix_data": {"data": {"0": [[0,1],[1,0]]}},
5 "task_data": {"task_locations": [0,1]},
6 "fleet_data": {"vehicle_locations": [[0,0],[0,0]]}}
7
8# If cuOpt is not running on localhost:5000, edit ip and port parameters
9cuopt_service_client = CuOptServiceSelfHostClient(
10 ip="localhost",
11 port=5000
12)
13
14optimized_routes = cuopt_service_client.get_optimized_routes(data)
15print(optimized_routes)
The data
argument to get_optimized_routes
may be a dictionary of the format shown in Get Routes Open-API spec.
It may also be the path of a file containing such a dictionary as JSON or written using the Python msgpack module (pickle is deprecated).
A JSON file may optionally be compressed with zlib.
To enable HTTPS,
In case of server using public certificates, just enable https
1import os 2from cuopt_sh_client import CuOptServiceSelfHostClient 3 4data = {"cost_matrix_data": {"data": {"0": [[0,1],[1,0]]}}, 5 "task_data": {"task_locations": [0,1]}, 6 "fleet_data": {"vehicle_locations": [[0,0],[0,0]]}} 7 8# If cuOpt is not running on localhost:5000, edit ip and port parameters 9cuopt_service_client = CuOptServiceSelfHostClient( 10 ip="localhost", 11 port=5000, 12 use_https=True 13)
In case of self signed certificate, provide complete path to certificate
1import os 2from cuopt_sh_client import CuOptServiceSelfHostClient 3 4data = {"cost_matrix_data": {"data": {"0": [[0,1],[1,0]]}}, 5 "task_data": {"task_locations": [0,1]}, 6 "fleet_data": {"vehicle_locations": [[0,0],[0,0]]}} 7 8# If cuOpt is not running on localhost:5000, edit ip and port parameters 9cuopt_service_client = CuOptServiceSelfHostClient( 10 ip="localhost", 11 port=5000, 12 use_https=True, 13 self_signed_cert=/complete/path/to/certificate 14)
You can generate self signed certificate easily as follows,
openssl genrsa -out ca.key 2048 openssl req -new -x509 -days 365 -key ca.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=Acme Root CA" -out ca.crt openssl req -newkey rsa:2048 -nodes -keyout server.key -subj "/C=CN/ST=GD/L=SZ/O=Acme, Inc./CN=*.example.com" -out server.csr openssl x509 -req -extfile <(printf "subjectAltName=DNS:example.com,DNS:www.example.com") -days 365 -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt
server.crt
andserver.key
are meant for server,ca.crt
is meant for client.
cuOpt Self-Hosted Thin Client CLI Example¶
Create a data.json file containing this sample data:
{"cost_matrix_data": {"data": {"0": [[0, 1], [1, 0]]}},
"task_data": {"task_locations": [0, 1]},
"fleet_data": {"vehicle_locations": [[0, 0], [0, 0]]}}
Invoke the CLI
# client's default ip address for cuOpt is localhost:5000 if ip/port are not specified
cuopt_sh data.json -i ip -p port
To enable HTTPS,
In case of server is using public certificates, just enable https
cuopt_sh data.json -s -i ip -p port
In case of self signed certificate, provide complete path to certificate
cuopt_sh data.json -s -c /complete/path/to/certificate -i ip -p port