Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the keys in metrics #68

Merged
merged 14 commits into from
Sep 23, 2020
Merged
23 changes: 11 additions & 12 deletions maro/simulator/scenarios/cim/business_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
metrics_desc = """
CIM metrics used provide statistics information until now (may be in the middle of current tick), it contains following keys:

perf (float): performance (accumulative fulfillment / accumulative orders) until now
total_shortage (int): accumulative shortage until now
total_cost (int): total empty transfer (both load and discharge) cost, the cost factors can be configured in configuration file at section "transfer_cost_factors"
order_requirements (int): accumulative orders until now
container_shortage (int): accumulative shortage until now
operation_number (int): total empty transfer (both load and discharge) cost, the cost factors can be configured in configuration file at section "transfer_cost_factors"
"""


Expand Down Expand Up @@ -59,7 +59,7 @@ def __init__(self, event_buffer: EventBuffer, topology: str, start_tick: int, ma
self._dsch_cost_factor: float = transfer_cost_factors["dsch"]

# used to collect total cost to avoid to much snapshot querying
self._total_transfer_cost: float = 0
self._total_operate_num: float = 0

self._init_frame()

Expand Down Expand Up @@ -231,7 +231,7 @@ def reset(self):
# insert departure event again
self._load_departure_events()

self._total_transfer_cost = 0
self._total_operate_num = 0

def action_scope(self, port_idx: int, vessel_idx: int) -> ActionScope:
"""
Expand Down Expand Up @@ -268,9 +268,9 @@ def get_metrics(self) -> DocableDict:
total_booking = sum([p.acc_booking for p in self._ports])

return DocableDict(metrics_desc,
perf = (total_booking - total_shortage)/total_booking if total_booking != 0 else 1,
total_shortage = total_shortage,
total_cost = self._total_transfer_cost
order_requirements = total_booking,
container_shortage = total_shortage,
operation_number = self._total_operate_num
)

def get_node_mapping(self) -> dict:
Expand Down Expand Up @@ -634,11 +634,10 @@ def _on_action_received(self, evt: Event):
evt.event_type = CimEventType.DISCHARGE_EMPTY if move_num > 0 else CimEventType.LOAD_EMPTY

# update cost
cost_factor = self._dsch_cost_factor if evt.event_type == CimEventType.DISCHARGE_EMPTY else self._load_cost_factor
cost = cost_factor * abs(move_num)
num = abs(move_num)

# update transfer cost for port and metrics
self._total_transfer_cost += cost
port.transfer_cost += cost
self._total_operate_num += num
port.transfer_cost += num

self._vessel_plans[vessel_idx, port_idx] += self._data_cntr.vessel_period[vessel_idx]
16 changes: 9 additions & 7 deletions maro/simulator/scenarios/citi_bike/business_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
metrics_desc = """
Citi bike metrics used to provide statistics information at current point (may be in the middle of a tick), it contains following keys:

perf (float): performance (fulfillment/total_trips) until now
trip_requirements (int): accumulative trips until now

total_trips (int): accumulative trips until this now
bike_shortage (int): accumulative shortage until now

total_shortage (int): accumulative shortage until this now
operation_number (int): accumulative operation cost until now

"""

Expand All @@ -66,6 +66,7 @@ def __init__(self, event_buffer: EventBuffer, topology: str, start_tick: int, ma

self._total_trips: int = 0
self._total_shortages: int = 0
self._total_operate_num: int = 0

self._init()

Expand Down Expand Up @@ -143,6 +144,7 @@ def get_node_mapping(self)->dict:
def reset(self):
"""Reset after episode"""
self._total_trips = 0
self._total_operate_num = 0
self._total_shortages = 0

self._frame.reset()
Expand All @@ -169,9 +171,9 @@ def get_metrics(self) -> dict:
total_shortage = self._total_shortages

return DocableDict(metrics_desc,
perf = (total_trips - total_shortage) / total_trips if total_trips != 0 else 1,
total_trips = total_trips,
total_shortage = total_shortage)
trip_requirements = total_trips,
bike_shortage = total_shortage,
operation_number = self._total_operate_num)

def __del__(self):
"""Collect resource by order"""
Expand Down Expand Up @@ -276,7 +278,6 @@ def _init_adj_matrix(self):
self._trips_adj = MatrixAttributeAccessor(self._matrices_node, "trips_adj", station_num, station_num)

def _init_frame(self, station_num: int):
# TODO: read the station number later
self._frame = build_frame(station_num, self.calc_max_snapshots())
self._snapshots = self._frame.snapshots

Expand Down Expand Up @@ -419,6 +420,7 @@ def _on_bike_deliver(self, evt: Event):

if max_accept_number > 0:
station.transfer_cost += max_accept_number
self._total_operate_num += max_accept_number

station.bikes = station_bikes + max_accept_number

Expand Down
5 changes: 5 additions & 0 deletions maro/simulator/scenarios/citi_bike/decision_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ def move_to_neighbor(self, src_station: Station, cur_station: Station, bike_numb
NOTE: since we have a full neighbors list now, we do not need the N-step way to move, just 1 for now,
we use distance (order index) as factor of extra cost now
"""
total_cost = 0
cost = 0

neighbors = self._get_neighbors(cur_station.index)
Expand All @@ -239,13 +240,17 @@ def move_to_neighbor(self, src_station: Station, cur_station: Station, bike_numb
cost = self._calculate_extra_cost(
accept_number, distance, order_index)

total_cost += cost

self._set_extra_cost(src_station, cur_station, neighbor, cost)

bike_number = bike_number - accept_number

if bike_number == 0:
break

return total_cost

def reset(self):
"""Reset internal states"""
for filter_instance in self._filters:
Expand Down