Home A Ciw Implementation of SimPy's Movie Renege Example
Post
Cancel

A Ciw Implementation of SimPy's Movie Renege Example

This post provides an implementation of SimPy’s Movie Renege example. In this implementation I subclassed three times. One class is TicketArrivalNode which replaces the ordinary ciw.ArrivalNode in order for individuals to be given tickets when they arrive in the simulation. The TheatreNode class keeps track of which movies there are and whether they are sold out. Lastly RenegeAtServiceDist is responsible for keeping track of the change in number of tickets and assigning whether a customer reneged.

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
import random
from typing import NoReturn

import ciw
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns

movies = ['Python Unchained', 'Kill Process', 'Pulp Implementation']


class TicketArrivalNode(ciw.ArrivalNode):
    """Node generating customers requiring tickets.

    Attributes:
        None

    Methods:
        send_individual: Assigns individuals a random number of tickets and a movie choice.

    """
    
    def send_individual(self, next_node: ciw.Node, next_individual: ciw.Individual) -> NoReturn:
        """Assigns individuals to have a required number of tickets for a movie.

        Args:
            next_node: The next node in the simulation.
            next_individual: The individual to assign tickets to.

        Returns:
            None

        """
        next_individual.num_tickets = random.randint(1,6)
        next_individual.movie = random.choice(movies)
        next_node.accept(next_individual)


class TheatreNode(ciw.Node):
    """Node representing the theatre in the simulation.

    Attributes:
        num_renegers (dict): Dictionary to track the number of reneging customers.
        available_movies (dict): Dictionary to track available tickets for each movie.
        sold_out (dict): Dictionary to track whether each movie is sold out.
        sold_out_time (dict): Dictionary to track the time each movie sold out.

    Methods:
        __init__: Initializes the TheatreNode object.
        write_individual_record: Writes a data record for an individual when leaving the node.

    """
    def __init__(self, *args, **kwargs) -> NoReturn:
        """Initializes the TheatreNode object."""
        super().__init__(*args, **kwargs)

        self.num_renegers = {}
        self.available_movies = {movie:50 for movie in movies}
        self.sold_out = {movie:False for movie in movies}
        self.sold_out_time = {movie:float('inf') for movie in movies}
        

    def write_individual_record(self, individual: ciw.Individual):
        """Write a data record for an individual when leaving a node.

        Args:
            individual: The individual leaving the node.

        Returns:
            None

        """
        if ciw.node.isinf(self.c) or self.slotted:
            server_id = False
        else:
            server_id = individual.server.id_number

        record = ciw.DataRecord(
            id_number=individual.id_number,
            customer_class=individual.previous_class,
            original_customer_class=individual.original_class,
            node=self.id_number,
            arrival_date=individual.arrival_date,
            waiting_time=individual.service_start_date - individual.arrival_date,
            service_start_date=individual.service_start_date,
            service_time=individual.service_end_date - individual.service_start_date,
            service_end_date=individual.service_end_date,
            time_blocked=individual.exit_date - individual.service_end_date,
            exit_date=individual.exit_date,
            destination=individual.destination,
            queue_size_at_arrival=individual.queue_size_at_arrival,
            queue_size_at_departure=individual.queue_size_at_departure,
            server_id=server_id,
            record_type="renege" if individual.reneged_at_service else "service",
        )
        individual.data_records.append(record)

    

class RenegeAtServiceDist(ciw.dists.Distribution):
    """Distribution to handle reneging at service time.

    Attributes:
        dist_accept: Distribution for accepting customers.
        dist_renege: Distribution for reneging customers.

    Methods:
        __init__: Initializes the RenegeAtServiceDist object.
        sample: Samples the distribution to determine if a customer will renege.

    """

    def __init__(self, dist_accept: ciw.dists.Distribution, dist_renege: ciw.dists.Distribution) -> float:
        """Initializes the RenegeAtServiceDist object."""
        self.dist_accept = dist_accept
        self.dist_renege = dist_renege

    def sample(self, t=None, ind=None):
        """Samples the distribution to determine if a customer will renege.

        Args:
            t: Time parameter for the distribution (default is None).
            ind: Individual to sample for (default is None).

        Returns:
            float: The sampled value from the distribution.

        """
        selected_individual = ind
        selected_movie = selected_individual.movie
        current_node = selected_individual.simulation.nodes[selected_individual.node]
        available_tickets = current_node.available_movies[selected_movie]

        movie_sold_out = current_node.sold_out[selected_movie]
        insufficient_tickets = available_tickets < selected_individual.num_tickets
        
        if movie_sold_out or insufficient_tickets:
            selected_individual.reneged_at_service = True
        else:
            selected_individual.reneged_at_service = False
            current_node.available_movies[selected_movie] -= selected_individual.num_tickets
            if current_node.available_movies[selected_movie] < 2:
                current_node.sold_out[selected_movie] = True
                available_tickets = 0
                current_node.sold_out_time[selected_movie] = selected_individual.simulation.current_time
                
        
        if ind.reneged_at_service:
            return self.dist_renege.sample()
        return self.dist_accept.sample()


network = ciw.create_network(
    arrival_distributions=[ciw.dists.Exponential(2)],
    service_distributions=[
        RenegeAtServiceDist(
            ciw.dists.Deterministic(1.0),
            ciw.dists.Deterministic(0.5)
            )
        ],
    number_of_servers=[1]
    )

results = []

for i in range(10):
    simulation = ciw.Simulation(network, arrival_node_class=TicketArrivalNode, node_class=TheatreNode)
    simulation.simulate_until_max_time(120)
    results.append(simulation.nodes[1].sold_out_time)

results = pd.DataFrame(results, columns=results[0].keys())
sns.boxplot(data=results)
plt.savefig('ciw_simpy_movie_renege.png', dpi=300)
plt.close()
This post is licensed under CC BY 4.0 by the author.

A Rust Implementation of a Simple Car DES

Paul Grogan's DES Example and Its Implementation in Ciw