Skip to content

Commit

Permalink
reformat server schedule inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
geraintpalmer committed Apr 16, 2024
1 parent 63273ce commit 4285f33
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 63 deletions.
23 changes: 12 additions & 11 deletions ciw/schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ class Schedule:
----------
schedule_type : str
Type of the schedule.
schedule_dates : List[float]
shift_end_dates : List[float]
List of shift end dates.
schedule_servers : List[int]
numbers_of_servers : List[int]
List of corresponding server numbers.
preemption : Union[bool, str]
Pre-emption option.
Expand All @@ -37,15 +37,16 @@ class Schedule:
get_next_shift()
Updates the next shifts from the generator.
"""
def __init__(self, schedule: List[Tuple[int, float]], preemption: Union[bool, str] = False, offset: float = 0.0) -> NoReturn:
def __init__(self, numbers_of_servers: List[int], shift_end_dates: List[float], preemption: Union[bool, str] = False, offset: float = 0.0) -> NoReturn:
"""
Initializes the instance of the Schedule object.
Parameters
----------
schedule : List[Tuple[int, float]]
A list of tuples representing shifts, where each tuple contains
the number of servers and the shift date.
numbers_of_servers : List[int]
A list containing the number of servers working at each shift
shift_end_dates : List[float]
A list containing the end dates of each shift.
preemption : Union[bool, str], optional
Pre-emption option, should be either 'resume', 'restart',
'resample', or False.
Expand All @@ -58,10 +59,10 @@ def __init__(self, schedule: List[Tuple[int, float]], preemption: Union[bool, st
if offset < 0.0:
raise ValueError("Offset should be a positive float.")
self.schedule_type = 'schedule'
self.schedule_dates = [shift[1] for shift in schedule]
self.schedule_servers = [shift[0] for shift in schedule]
self.shift_end_dates = shift_end_dates
self.numbers_of_servers = numbers_of_servers
self.preemption = preemption
self.cyclelength = self.schedule_dates[-1]
self.cyclelength = self.shift_end_dates[-1]
self.offset = offset

def initialise(self) -> NoReturn:
Expand All @@ -70,8 +71,8 @@ def initialise(self) -> NoReturn:
"""
self.c = 0
self.next_shift_change_date = self.offset
self.next_c = self.schedule_servers[0]
self.schedule_generator = self.get_schedule_generator(self.schedule_dates, self.schedule_servers, self.offset)
self.next_c = self.numbers_of_servers[0]
self.schedule_generator = self.get_schedule_generator(self.shift_end_dates, self.numbers_of_servers, self.offset)

def get_schedule_generator(self, boundaries:List[float], values:List[int], offset:float) -> Generator[Tuple[float, int], None, None]:
"""
Expand Down
12 changes: 6 additions & 6 deletions ciw/tests/test_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def test_create_network_from_dictionary(self):
ciw.dists.Exponential(7.0),
ciw.dists.Deterministic(0.7),
],
"number_of_servers": [ciw.Schedule(schedule=[[1, 20], [4, 50]]), 3],
"number_of_servers": [ciw.Schedule(numbers_of_servers=[1, 4], shift_end_dates=[20, 50]), 3],
"routing": [[0.5, 0.2], [0.0, 0.0]],
"queue_capacities": [10, float("inf")],
}
Expand All @@ -214,8 +214,8 @@ def test_create_network_from_dictionary(self):
self.assertEqual(N.service_centres[0].queueing_capacity, 10)
self.assertTrue(type(N.service_centres[0].number_of_servers), ciw.schedules.Schedule)
self.assertEqual(N.service_centres[0].class_change_matrix, None)
self.assertEqual(N.service_centres[0].number_of_servers.schedule_dates, [20, 50])
self.assertEqual(N.service_centres[0].number_of_servers.schedule_servers, [1, 4])
self.assertEqual(N.service_centres[0].number_of_servers.shift_end_dates, [20, 50])
self.assertEqual(N.service_centres[0].number_of_servers.numbers_of_servers, [1, 4])
self.assertEqual(N.service_centres[1].queueing_capacity, float("inf"))
self.assertEqual(N.service_centres[1].number_of_servers, 3)
self.assertEqual(N.service_centres[1].class_change_matrix, None)
Expand Down Expand Up @@ -681,7 +681,7 @@ def test_network_from_kwargs(self):
ciw.dists.Exponential(7.0),
ciw.dists.Deterministic(0.7),
],
number_of_servers=[ciw.Schedule(schedule=[[1, 20], [4, 50]]), 3],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 4], shift_end_dates=[20, 50]), 3],
routing=[[0.5, 0.2], [0.0, 0.0]],
queue_capacities=[10, float("inf")],
)
Expand All @@ -691,8 +691,8 @@ def test_network_from_kwargs(self):
self.assertEqual(N.service_centres[0].queueing_capacity, 10)
self.assertEqual(type(N.service_centres[0].number_of_servers), ciw.schedules.Schedule)
self.assertEqual(N.service_centres[0].class_change_matrix, None)
self.assertEqual(N.service_centres[0].number_of_servers.schedule_dates, [20, 50])
self.assertEqual(N.service_centres[0].number_of_servers.schedule_servers, [1, 4])
self.assertEqual(N.service_centres[0].number_of_servers.shift_end_dates, [20, 50])
self.assertEqual(N.service_centres[0].number_of_servers.numbers_of_servers, [1, 4])
self.assertFalse(N.service_centres[0].number_of_servers.preemption)
self.assertEqual(N.service_centres[1].queueing_capacity, float("inf"))
self.assertEqual(N.service_centres[1].number_of_servers, 3)
Expand Down
22 changes: 11 additions & 11 deletions ciw/tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def test_init_method(self):
"Class 0": [ciw.dists.Exponential(0.05), ciw.dists.Exponential(0.04)],
"Class 1": [ciw.dists.Exponential(0.04), ciw.dists.Exponential(0.06)],
},
number_of_servers=[ciw.Schedule(schedule=[[1, 30], [2, 60], [1, 90], [3, 100]]), 3],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2, 1, 3], shift_end_dates=[30, 60, 90, 100]), 3],
queue_capacities=[float("Inf"), 10],
service_distributions={
"Class 0": [ciw.dists.Deterministic(5.0), ciw.dists.Exponential(0.2)],
Expand All @@ -160,8 +160,8 @@ def test_init_method(self):
N.update_next_event_date()
self.assertEqual(N.schedule.cyclelength, 100)
self.assertEqual(N.c, 1)
self.assertEqual(N.schedule.schedule_dates, [30, 60, 90, 100])
self.assertEqual(N.schedule.schedule_servers, [1, 2, 1, 3])
self.assertEqual(N.schedule.shift_end_dates, [30, 60, 90, 100])
self.assertEqual(N.schedule.numbers_of_servers, [1, 2, 1, 3])
self.assertEqual(N.next_event_date, 30)
self.assertEqual(N.interrupted_individuals, [])
self.assertFalse(N.reneging)
Expand Down Expand Up @@ -757,7 +757,7 @@ def test_reset_individual_attributes(self):
self.assertFalse(ind.exit_date)

def test_date_from_schedule_generator(self):
sg = ciw.Schedule(schedule=[[1, 30], [0, 60], [2, 90], [3, 100]])
sg = ciw.Schedule(numbers_of_servers=[1, 0, 2, 3], shift_end_dates=[30, 60, 90, 100])
sg.initialise()
sg.get_next_shift()
self.assertEqual(sg.c, 1)
Expand Down Expand Up @@ -891,7 +891,7 @@ def test_server_utilisation_with_schedules(self):
N = ciw.create_network(
arrival_distributions=[ciw.dists.Sequential([2.0, 4.0, 4.0, 0.0, 7.0, 1000.0])],
service_distributions=[ciw.dists.Sequential([4.0, 2.0, 6.0, 6.0, 3.0])],
number_of_servers=[ciw.Schedule(schedule=[[1, 9], [2, 23]])],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2], shift_end_dates=[9, 23])],
)
Q = ciw.Simulation(N)
Q.simulate_until_max_time(23)
Expand Down Expand Up @@ -1228,7 +1228,7 @@ def test_reneging_with_schedules(self):
N = ciw.create_network(
arrival_distributions=[ciw.dists.Deterministic(7)],
service_distributions=[ciw.dists.Deterministic(11)],
number_of_servers=[ciw.Schedule(schedule=[[1, 16], [0, 10000]])],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 0], shift_end_dates=[16, 10000])],
reneging_time_distributions=[ciw.dists.Deterministic(3)],
)
Q = ciw.Simulation(N)
Expand Down Expand Up @@ -1862,7 +1862,7 @@ def test_data_records_for_interrupted_individuals(self):
N = ciw.create_network(
arrival_distributions=[ciw.dists.Deterministic(7)],
service_distributions=[ciw.dists.Deterministic(4)],
number_of_servers=[ciw.Schedule(schedule=[[1, 24], [0, 29], [1, 37]], preemption="resume")],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 0, 1], shift_end_dates=[24, 29, 37], preemption="resume")],
)
ciw.seed(0)
Q = ciw.Simulation(N)
Expand Down Expand Up @@ -1912,7 +1912,7 @@ def test_preemptive_priorities_resume_options_due_to_schedule(self):
"Class 0": [ciw.dists.Deterministic(10)],
"Class 1": [ciw.dists.Sequential([6, 3])],
},
number_of_servers=[ciw.Schedule(schedule=[[1, 5], [2, 100]])],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2], shift_end_dates=[5, 100])],
priority_classes=({"Class 0": 0, "Class 1": 1}, ["restart"]),
)
Q = ciw.Simulation(N)
Expand Down Expand Up @@ -1941,7 +1941,7 @@ def test_preemptive_priorities_resume_options_due_to_schedule(self):
"Class 0": [ciw.dists.Deterministic(10)],
"Class 1": [ciw.dists.Sequential([6, 3])],
},
number_of_servers=[ciw.Schedule(schedule=[[1, 5], [2, 100]])],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2], shift_end_dates=[5, 100])],
priority_classes=({"Class 0": 0, "Class 1": 1}, ["resume"]),
)
Q = ciw.Simulation(N)
Expand Down Expand Up @@ -1970,7 +1970,7 @@ def test_preemptive_priorities_resume_options_due_to_schedule(self):
"Class 0": [ciw.dists.Deterministic(10)],
"Class 1": [ciw.dists.Sequential([6, 3])],
},
number_of_servers=[ciw.Schedule(schedule=[[1, 5], [2, 100]])],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2], shift_end_dates=[5, 100])],
priority_classes=({"Class 0": 0, "Class 1": 1}, ["resample"]),
)
Q = ciw.Simulation(N)
Expand Down Expand Up @@ -2020,7 +2020,7 @@ def test_shift_change_before_arrival(self):
N = ciw.create_network(
arrival_distributions=[ciw.dists.Sequential(sequence=[2.0, float('inf')])],
service_distributions=[ciw.dists.Deterministic(value=1.0)],
number_of_servers=[ciw.Schedule(schedule=[[1, 1.0], [2, 10.0]], preemption=False)]
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2], shift_end_dates=[1.0, 10.0], preemption=False)]
)
Q = ciw.Simulation(N)
Q.simulate_until_max_time(10.5)
Expand Down
2 changes: 1 addition & 1 deletion ciw/tests/test_processor_sharing.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@
"Class 0": [ciw.dists.Exponential(0.05), ciw.dists.Exponential(0.04)],
"Class 1": [ciw.dists.Exponential(0.04), ciw.dists.Exponential(0.06)],
},
number_of_servers=[ciw.Schedule(schedule=[[1, 30], [2, 60], [1, 90], [3, 100]]), 3],
number_of_servers=[ciw.Schedule(numbers_of_servers=[1, 2, 1, 3], shift_end_dates=[30, 60, 90, 100]), 3],
queue_capacities=[float("Inf"), 10],
service_distributions={
"Class 0": [ciw.dists.Deterministic(5.0), ciw.dists.Exponential(0.2)],
Expand Down
Loading

0 comments on commit 4285f33

Please sign in to comment.