Skip to content

Commit

Permalink
Add model and parser for request
Browse files Browse the repository at this point in the history
  • Loading branch information
munterfi committed Jul 17, 2024
1 parent d8a627f commit 81114e2
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 3 deletions.
3 changes: 3 additions & 0 deletions notebooks/analyze-depot-vehicles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
"outputs": [],
"source": [
"from rssched.io.reader import import_response\n",
"from rssched.io.reader import import_request\n",
"\n",
"import pandas as pd\n",
"\n",
"SOLVER_REQUEST_FILE = \"../rssched/data/small_test_request.json\"\n",
"SOLVER_RESPONSE_FILE = \"../rssched/data/small_test_response.json\""
]
},
Expand All @@ -30,6 +32,7 @@
"metadata": {},
"outputs": [],
"source": [
"request = import_request(SOLVER_REQUEST_FILE)\n",
"response = import_response(SOLVER_RESPONSE_FILE)"
]
},
Expand Down
4 changes: 2 additions & 2 deletions rssched/data/small_test_request.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
{
"id": "trip_0_seg_1",
"routeSegment": "route_0_seg_1",
"departure": "2023-7-24T12:40:00",
"departure": "2023-07-24T12:40:00",
"passengers": 100,
"seated": 90
}
Expand Down Expand Up @@ -143,7 +143,7 @@
{
"id": "maintenance_slot_0",
"location": "ZH",
"start": "2023-07-24T6:00:00",
"start": "2023-07-24T06:00:00",
"end": "2023-07-24T12:00:00",
"trackCount": 2
},
Expand Down
7 changes: 7 additions & 0 deletions rssched/io/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import datetime
from pathlib import Path

from rssched.model.request import Request
from rssched.model.response import Info, ObjectiveValue, Response, Schedule


Expand All @@ -28,6 +29,12 @@ def convert_keys_to_snake_case(data):
return data


def import_request(file_path: Path) -> Request:
with open(file_path, "r", encoding="utf-8") as file:
data = convert_keys_to_snake_case(json.load(file))
return Request(**data)


def import_response(file_path: Path) -> Response:
with open(file_path, "r", encoding="utf-8") as file:
data = convert_keys_to_snake_case(json.load(file))
Expand Down
108 changes: 108 additions & 0 deletions rssched/model/request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from datetime import datetime
from typing import Dict, List, Optional

from pydantic import BaseModel


class VehicleType(BaseModel):
id: str
capacity: int
seats: int
maximal_formation_count: Optional[int] = None


class Location(BaseModel):
id: str
day_limit: Optional[int] = None


class AllowedType(BaseModel):
vehicle_type: str
upper_bound: Optional[int] = None


class Depot(BaseModel):
id: str
location: str
capacity: int
allowed_types: List[AllowedType]


class RouteSegment(BaseModel):
id: str
order: int
origin: str
destination: str
distance: int
duration: int
maximal_formation_count: Optional[int] = None


class Route(BaseModel):
id: str
vehicle_type: str
segments: List[RouteSegment]


class DepartureSegment(BaseModel):
id: str
route_segment: str
departure: datetime
passengers: int
seated: int


class Departure(BaseModel):
id: str
route: str
segments: List[DepartureSegment]


class MaintenanceSlot(BaseModel):
id: str
location: str
start: datetime
end: datetime
track_count: int


class DeadHeadTrips(BaseModel):
indices: List[str]
durations: List[List[int]]
distances: List[List[int]]


class Shunting(BaseModel):
minimal_duration: int
dead_head_trip_duration: int


class MaintenanceParameters(BaseModel):
maximal_distance: int


class Costs(BaseModel):
staff: int
service_trip: int
maintenance: int
dead_head_trip: int
idle: int


class Parameters(BaseModel):
forbid_dead_head_trips: bool
day_limit_threshold: int
shunting: Shunting
maintenance: MaintenanceParameters
costs: Costs


class Request(BaseModel):
vehicle_types: List[VehicleType]
locations: List[Location]
depots: List[Depot]
routes: List[Route]
departures: List[Departure]
maintenance_slots: List[MaintenanceSlot]
dead_head_trips: DeadHeadTrips
parameters: Parameters
9 changes: 8 additions & 1 deletion tests/io/test_reader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
from rssched.data.access import PkgDataAccess
from rssched.io.reader import import_response
from rssched.io.reader import import_request, import_response


def test_import_request():
request = import_request(PkgDataAccess.locate_request())
print(request)
assert len(request.vehicle_types) == 2
assert len(request.routes) == 2


def test_import_response():
Expand Down

0 comments on commit 81114e2

Please sign in to comment.