From 7230fcf584727ac2fb005a5d9716d002f5ba4c5b Mon Sep 17 00:00:00 2001 From: Joeri Exelmans Date: Fri, 13 Dec 2024 16:57:06 +0100 Subject: [PATCH] update HTML + changes made to queueing example during tutorial --- assignment/doc/assignment.html | 3 ++- examples/queueing/generator.py | 27 ++++++++++++++++++--------- examples/queueing/job.py | 3 +++ examples/queueing/queue.py | 28 ++++++++++++++-------------- 4 files changed, 37 insertions(+), 24 deletions(-) diff --git a/assignment/doc/assignment.html b/assignment/doc/assignment.html index 6fd0bf3..e1e9ac0 100644 --- a/assignment/doc/assignment.html +++ b/assignment/doc/assignment.html @@ -155,6 +155,7 @@ The specification of the semantics of the Atomic DEVS blocks is entirely determi
  • The generated CSV- and SVG-files
  • +
  • Deadline: Sunday 5 December 2025, 23:59
  • Attention!

    @@ -198,7 +199,7 @@ The specification of the semantics of the Atomic DEVS blocks is entirely determi

    Extra Material

    diff --git a/examples/queueing/generator.py b/examples/queueing/generator.py index 57423eb..05b1587 100644 --- a/examples/queueing/generator.py +++ b/examples/queueing/generator.py @@ -1,9 +1,17 @@ from pypdevs.DEVS import AtomicDEVS from job import Job import random +import dataclasses # Define the state of the generator as a structured object +@dataclasses.dataclass class GeneratorState: + current_time: float + remaining: float + to_generate: int + next_job: None + random: random.Random + def __init__(self, gen_num, seed=0): # Current simulation time (statistics) self.current_time = 0.0 @@ -34,14 +42,22 @@ class Generator(AtomicDEVS): # Determine size of the event to generate size = max(1, int(self.state.random.gauss(self.size_param, 5))) # Calculate current time (note the addition!) - creation = self.state.current_time + self.state.remaining + creation = self.state.current_time # Update state self.state.next_job = Job(size, creation) self.state.remaining = self.state.random.expovariate(self.gen_param) + def timeAdvance(self): + # Return remaining time; infinity when generated enough + return self.state.remaining + + def outputFnc(self): + # Output the new event on the output port + return {self.out_event: self.state.next_job} + def intTransition(self): # Update simulation time - self.state.current_time += self.timeAdvance() + self.state.current_time += self.state.remaining # Update number of generated events self.state.to_generate -= 1 if self.state.to_generate == 0: @@ -53,10 +69,3 @@ class Generator(AtomicDEVS): self.__nextJob() return self.state - def timeAdvance(self): - # Return remaining time; infinity when generated enough - return self.state.remaining - - def outputFnc(self): - # Output the new event on the output port - return {self.out_event: self.state.next_job} diff --git a/examples/queueing/job.py b/examples/queueing/job.py index 54a7314..d8ade30 100644 --- a/examples/queueing/job.py +++ b/examples/queueing/job.py @@ -3,3 +3,6 @@ class Job: # Jobs have a size and creation_time parameter self.size = size self.creation_time = creation_time + + def __repr__(self): + return f"Job(size={self.size},creation_time={self.creation_time})" \ No newline at end of file diff --git a/examples/queueing/queue.py b/examples/queueing/queue.py index 5b11cab..8fbe89c 100644 --- a/examples/queueing/queue.py +++ b/examples/queueing/queue.py @@ -29,20 +29,6 @@ class Queue(AtomicDEVS): self.in_event = self.addInPort("in_event") self.in_finish = self.addInPort("in_finish") - def intTransition(self): - # Is only called when we are outputting an event - # Pop the first idle processor and clear processing event - self.state.idle_procs.pop(0) - if self.state.queue and self.state.idle_procs: - # There are still queued elements, so continue - self.state.processing = self.state.queue.pop(0) - self.state.remaining_time = self.processing_time - else: - # No events left to process, so become idle - self.state.processing = None - self.state.remaining_time = float("inf") - return self.state - def extTransition(self, inputs): # Update the remaining time of this job self.state.remaining_time -= self.elapsed @@ -73,3 +59,17 @@ class Queue(AtomicDEVS): # Output the event to the processor port = self.out_proc[self.state.idle_procs[0]] return {port: self.state.processing} + + def intTransition(self): + # Is only called when we are outputting an event + # Pop the first idle processor and clear processing event + self.state.idle_procs.pop(0) + if self.state.queue and self.state.idle_procs: + # There are still queued elements, so continue + self.state.processing = self.state.queue.pop(0) + self.state.remaining_time = self.processing_time + else: + # No events left to process, so become idle + self.state.processing = None + self.state.remaining_time = float("inf") + return self.state