Compare commits

..

10 commits

14 changed files with 783 additions and 25 deletions

2
.gitignore vendored
View file

@ -10,3 +10,5 @@
*build*
test/output/*
/.idea/*
assignment_output/

1
assignment/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*_solution.py

91
assignment/atomicdevs.py Normal file
View file

@ -0,0 +1,91 @@
### EDIT THIS FILE ###
from pypdevs.DEVS import AtomicDEVS
from environment import *
import random
import dataclasses
class Queue(AtomicDEVS):
def __init__(self, ship_sizes):
super().__init__("Queue")
# self.state = QueueState(...)
# def extTransition(self, inputs):
# pass
# def timeAdvance(self):
# pass
# def outputFnc(self):
# pass
# def intTransition(self):
# pass
PRIORITIZE_BIGGER_SHIPS = 0
PRIORITIZE_SMALLER_SHIPS = 1
class RoundRobinLoadBalancer(AtomicDEVS):
def __init__(self,
lock_capacities=[3,2], # two locks of capacities 3 and 2.
priority=PRIORITIZE_BIGGER_SHIPS,
):
super().__init__("RoundRobinLoadBalancer")
# self.state = LoadBalancerState(...)
# def extTransition(self, inputs):
# pass
# def timeAdvance(self):
# pass
# def outputFnc(self):
# pass
# def intTransition(self):
# pass
class FillErUpLoadBalancer(AtomicDEVS):
def __init__(self,
lock_capacities=[3,2], # two locks of capacities 3 and 2.
priority=PRIORITIZE_BIGGER_SHIPS,
):
super().__init__("FillErUpLoadBalancer")
# self.state = LoadBalancerState(...)
# def extTransition(self, inputs):
# pass
# def timeAdvance(self):
# pass
# def outputFnc(self):
# pass
# def intTransition(self):
# pass
class Lock(AtomicDEVS):
def __init__(self,
capacity=2, # lock capacity (2 means: 2 ships of size 1 will fit, or 1 ship of size 2)
max_wait_duration=60.0,
passthrough_duration=60.0*15.0, # how long does it take for the lock to let a ship pass through it
):
super().__init__("Lock")
# self.state = LockState(...)
# def extTransition(self, inputs):
# pass
# def timeAdvance(self):
# pass
# def outputFnc(self):
# pass
# def intTransition(self):
# pass
### EDIT THIS FILE ###

View file

@ -0,0 +1,234 @@
<dtml-var simple_html_header>
<h2 id="practical-stuff">Practical stuff</h2>
<ul>
<li> <b>Due Date:</b> Sunday 5 January 2025, before 23:59 (Blackboard's clock).</li>
<li> <b>Team Size:</b> 2 (pair design/programming)!<br/>
<i>Note that as of the 2017-2018 Academic Year, each International student should team up with "local"
(i.e., whose Bachelor degree was obtained at the University of Antwerp).</i></li>
<li> <b>Submitting your solution:</b>
<ul>
<li>
<em>Only one</em> member of each team submits a full solution (<a href="#zip">ZIP file</a>).
</li>
<li>
The other team member <em>must</em> submit a single (plain text or HTML) file containing <em>only</em> the names of both team members. This will allow us to put in grades for both team members in BlackBoard.
</li>
</ul>
</li>
<li> <b>Submission Medium:</b>
<a href="http://blackboard.uantwerpen.be">BlackBoard</a>.
</li>
<li> <b>Contact / TA:</b>
<a href="mailto:Joeri.Exelmans@uantwerpen.be">Joeri Exelmans</a>.
</li>
</ul>
<h2>Introduction</h2>
<p>You will use (classic) DEVS to model a queueing and load balancing system for a set of waterway locks. A conceptual view of the system is shown here:</p>
<img src="concept.svg"/>
<p>Ships move in the direction of the arrows. A generator generates ships at pseudo-random time intervals, which are added to a queue. Whenever the queue has a ship available, and one of the locks has enough remaining capacity for that ship, the load balancer pulls a ship from the queue and sends it to that lock. A lock may fit more than one ship, so as long as it is not filled up to full capacity, it may wait for more ships to arrive before the lock doors close and the ships can pass through to the other side of the lock. At the end of the system, we have a Sink, where all ships are collected, so we can extract statistics to analyse performance.</p>
<p>Ships can have different sizes. For simplicity, the size of a ship is a small integer (e.g., 1 or 2). Locks can have different capacities: for instance, a lock of capacity 3 will fit either:
<ul>
<li>3 ships of size 1</li>
<li>1 ship of size 2 + 1 ship of size 1</li>
</ul>
</p>
<h2>Specification</h2>
<p>We now give an overview of the different DEVS components, and their behavior, and their parameters. Although many of the parameters are fixed, your solution must work with different parameters as well. In other words, don't hardcode the parameter values in your DEVS blocks!</p>
<h3>Atomic DEVS blocks</h3>
<ul>
<li>Generator
<ul>
<li><b>seed (int)</b>: Seed for the random number generator.</li>
<li><b>gen_num (int)</b>: The number of ships to generate during a simulation run. This parameter is fixed at 500.</li>
<li><b>gen_rate (float)</b>: The average ship generation rate (in ships/second). This parameter is fixed at 1/60/4 = once every 4 minutes.</li>
<li><b>gen_types (list of int)</b>: The different ship sizes to generate, and their likelihood. This parameter is fixed at [1,1,2], meaning, we have ship sizes 1 and 2, and size 1 is twice as likely to be generated as 2 (sizes are uniformly sampled from this sequence)</li>
</ul>
</li>
<li>Queue
<ul>
<li><b>ship_sizes (set of int)</b>: The different ship sizes. We will use {1,2}. The Queue will internally create one FIFO queue per ship size. This makes it possible to give bigger ships a higher priority over smaller ships, or vice versa.</li>
</ul>
</li>
<li>Load Balancer
<ul>
<li><b>lock_capacities (list of int)</b>: The load balancer needs to know about the number of locks and their capacities. We will use [3,2], meaning two locks, of capacities 3 and 2.</li>
<li><b>priority (enum)</b>: Whether to give higher priority to bigger or smaller ships.</li>
</ul>
<p>Further, two different load balancer strategies will be implemented. Each strategy will be implemented as a separate Atomic DEVS block:</p>
<ul>
<li><b>RoundRobinLoadBalancer</b>: will iterate over the locks, and attempt to move a ship into each lock in a round-robin fashion. The priority-parameter is taken into account: if bigger ships have higher priority, then it will first try to move the biggest ship into the lock, then the next-biggest-ship, etc.
<p>TIP: This load balancer will need to remember (in its DEVS state) the lock into which a ship was moved most recently, so for the next ship, it will start with the lock after it.</p>
<p>Initially, the RoundRobinLoadBalancer must start with the first lock in the <b>lock_capacities</b>-list.</p>
</li>
<li><b>FillErUpLoadBalancer</b>: will prioritize, above all, a &quot;move&quot; that maximally fills up a lock.
<p>Example: if ships of sizes 1 and 2 are available, and the locks have remaining capacities 3 and 2, then the possible &quot;moves&quot; are:
<ul>
<li>move ship size 1 -> lock with cap 3 => remaining lock cap = 2</li>
<li>move ship size 1 -> lock with cap 2 => remaining lock cap = 1</li>
<li>move ship size 2 -> lock with cap 3 => remaining lock cap = 1</li>
<li>move ship size 2 -> lock with cap 2 => remaining lock cap = 0</li>
</ul>
this strategy will prioritize the last move, because it results in the smallest remaining capacity (in this case, completely filling up the lock).</p>
<p>NOTE: This load balancer also has too take into account the priority-parameter. For instance, if bigger ships are prioritized, and the following moves can be made:
<ul>
<li>move ship size 1 -> lock with cap 2 => remaining lock cap = 1</li>
<li>move ship size 1 -> lock with cap 1 => remaining lock cap = 0</li>
<li>move ship size 2 -> lock with cap 2 => remaining lock cap = 0</li>
<li>move ship size 2 -> lock with cap 1 => remaining lock cap = 1</li>
</ul>
this time, both 1->1 and 2->2 completely fill up the lock, but the move 2->2 will be chosen, because it involves a bigger ship.
</p>
<p>Finally, if the same ship can be moved to different locks with equal priority (filling both locks up equally), then the earliest lock in the <b>lock_capacities</b>-list is chosen.</p>
</li>
</ul>
</li>
<li>Lock
<ul>
<li><b>capacity (int)</b>: Capacity of the lock. E.g., 3 or 2.</li>
<li><b>passthrough_duration (float)</b>: Time duration (in seconds) of the &quot;passthrough&quot;-procedure. This procedure conceptually involves closing the lock doors, changing the water level, and opening the lock doors on the other side, and the ships leaving the lock. In our simulation, it is only an amount of time during which the lock has zero remaining capacity, after which the ships are sent to the sink.
<p>For simplicity, there is no time delay between sending the ships to the sink, and the lock becoming available again (at original capacity).</p>
<li><b>max_wait_duration (float)</b>: When a lock is completely filled up (zero remaining capacity), the &quot;passthrough&quot;-procedure starts immediately. The procedure may also start if the lock is non-empty, and a certain amount of time has passed since the <em>first</em> ship has entered the lock. This parameter is that amount of time.
</li>
</ul>
</li>
</ul>
<p>The specification of the semantics of the Atomic DEVS blocks is entirely deterministic. If you implement everything correctly, the system as-a-whole will behave 100% identical to the teacher's solution.</p>
<h3>Coupled DEVS</h3>
<p>The system as a whole is modeled as a Coupled DEVS block. Its parameters are mostly passed as-is to the underlying Atomic DEVS blocks. They are:
<ul>
<li><b>gen_num (int)</b> -> Generator.</li>
<li><b>gen_rate (float)</b> -> Generator.</li>
<li><b>gen_types (list of int)</b> -> Generator and Queue. The Queue only needs this parameter to know the different ship sizes.</li>
<li><b>lock_capacities (list of int)</b> -> LoadBalancer and each Lock its respective capacity.</li>
<li><b>priority (enum)</b> -> LoadBalancer.</li>
<li><b>max_wait_duration (float)</b> -> each Lock (same value for all locks).</li>
<li><b>passthrough_duration (float)</b> -> each Lock (same value for all locks).</li>
</ul>
</p>
<h2>What is expected</h2>
<p>First of all, you are <em>given</em> an implementation of the following AtomicDEVS blocks, which you must not edit:</p>
<ul>
<li>Generator
<ul><li><b>out_ship (Ship)</b>: output port on which an event is sent when a ship is generated.</li></ul>
</li>
<li>Sink
<ul><li><b>in_ships (list of Ship)</b>: input port on which an event is sent when ships leave a lock. For each ship, the time duration spent in the system is computed and stored.</li></ul>
</li>
</ul>
<p>You will:</p>
<ul>
<li>Implement the AtomicDEVS blocks for Queue, RoundRobinLoadBalancer, FillErUpLoadBalancer and Lock.</li>
<li>Think of the interfaces of these blocks (input/output ports and their events) and the protocol spoken by them.</li>
<li>Finally, in the CoupledDEVS block representing the entire system, you will have to make the right connections (which of course depends on the input/output ports that you have defined in your AtomicDEVS).</li>
</ul>
<p>An indication of the complexity: my own solution of the AtomicDEVS blocks is about 300 lines of code (including comments).</p>
<h2 id="goal">Goal: Performance Analysis</h2>
<p>Once you have implemented the system, we will do performance analysis, comparing combinations of the following parameter values:</p>
<ul>
<li><b>max_wait_duration (float)</b>: we will try 0, 2, 4, 6, 8 minutes.</li>
<li><b>priority (enum)</b>: we will try giving priority to bigger and smaller ships.</li>
<li><b>strategy (enum)</b>: we will try &quot;round-robin&quot; and &quot;fill-er-up&quot;.</li>
</ul>
<p>More specifically, we would like to know under which (combinations of) parameter values the (avg/min/max) duration that ships spend in the system is minimized. Also, we'd like to know if one choice (e.g., prioritize bigger) always better than another choice (e.g., prioritize smaller), or does it depend on the choices made for the other parameters?</p>
<h2 id="gettingstarted">Getting Started</h2>
<ol>
<li>Clone the <tt>mosis24</tt> branch of <a href="https://msdl.uantwerpen.be/git/jexelmans/joeriPDEVS/src/mosis24">this git repository</a>.</li>
<li>Under the <tt>assignment</tt> directory, you'll find the following files:
<ul>
<li><tt>runner.py</tt> This script runs the simulation for all combinations of parameter values as described in the <a href="#goal">Performance Analysis</a> section. It will generate <tt>.csv</tt> files with the time durations that each ship has spent in the system. Every row is a ship (500 ships, so 500 rows total), and every column represents a different value for the <b>max_wait_duration</b> parameter. It will also generate a <tt>plot.gnuplot</tt> file, which you can run with gnuplot as follows:
<pre>gnuplot plot.gnuplot</pre>
which will result in a number of SVG files containing plots of the CSV files.
<p>You are only allowed to make temporary changes (for debugging) to this file.</p>
</li>
<li><tt>system.py</tt> Contains the full system, modeled as CoupledDEVS. You need to edit this file.</li>
<li><tt>atomicdevs.py</tt> Contains skeletons for the AtomicDEVS blocks that you must implement. You need to edit this file.</li>
<li><tt>environment.py</tt> Contains implementations of the Generator, Sink and Ship types. You must not edit this file.</li>
</ul>
</li>
<li>Write a report, where you:
<ul>
<li>explain and motivate the interfaces/protocol of the AtomicDEVS blocks</li>
<li>show the plotted results</li>
<li>interpret the plotted results</li>
</ul>
</li>
<li id="zip">Submit via BlackBoard, a ZIP file, containing:
<ul>
<li>Your code (only the <tt>assignment</tt> directory)</li>
<li>Your report (PDF)</li>
<li>The generated CSV- and SVG-files</li>
</ul>
</li>
<li>Deadline: Sunday 5 December 2025, 23:59</li>
</ol>
<h2 id="attention">Attention!</h2>
<p>You <b><u><i>must</i></u></b> stick to the rules of DEVS:</p>
<ul>
<li>The functions <tt>timeAdvance</tt> and <tt>outputFnc</tt> are purely <em>getters</em>! They must not mutate the DEVS state, or any variable, anywhere.</li>
<li>The functions <tt>extTransition</tt> and <tt>intTransition</tt> may mutate <em>ONLY</em> the DEVS state.</li>
</ul>
<p>Any violation of these rules results in an incorrect solution. Points will be subtracted.</p>
<h2>Coding Conventions</h2>
<p>Please follow these coding conventions:</p>
<ul>
<li>Write your AtomicDEVS methods always in the following order:
<ul>
<li>extTransition</li>
<li>timeAdvance</li>
<li>outputFnc</li>
<li>intTransition</li>
</ul>
This reflects the order in which the methods are called by the simulator:
<ul>
<li>extTransition always has highest priority (can interrupt anything)</li>
<li>timeAdvance is called before outputFnc</li>
<li>outputFnc is called right before intTransition</li>
</ul>
</li>
<li>Input/output port attributes start with 'in_' and 'out_'</li>
</ul>
<h2>Tips</h2>
<ul>
<li>Test/debug your integrated solution with only one ship (set <b>num_ships</b> parameter to 1).</li>
<li>To observe the changing State in the PyPDVES debug output, write <tt>__repr__</tt>-methods for your State-classes.</li>
</ul>
<h2>Troubleshooting</h2>
<p>Common mistakes include:
<ul>
<li>did you forget to return <tt>self.state</tt> from intTransition or extTransition ?</li>
<li>did you accidentally write to <tt>self.x</tt> instead of <tt>self.state.x</tt> ?</li>
<li>did you modify the state in timeAdvance or outputFnc ? (<a href="#attention">NOT ALLOWED!!</a>)</li>
</ul>
</p>
<h2>Extra Material</h2>
<ul>
<li>This assignment was inspired by the <a href="https://msdl.uantwerpen.be/git/jexelmans/joeriPDEVS/src/mosis24/examples/queueing">queueuing example</a>.</li>
</ul>
<dtml-var standard_html_footer>

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 38 KiB

View file

@ -0,0 +1,61 @@
<mxfile host="Electron" modified="2024-12-13T11:21:58.197Z" agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) draw.io/24.4.0 Chrome/126.0.6478.234 Electron/31.7.2 Safari/537.36" etag="HG30_Pus9FxNusYfN5k-" version="24.3.1" type="device">
<diagram name="Page-1" id="PQKqzUdAGHVOVxXedEcz">
<mxGraphModel dx="888" dy="728" grid="1" gridSize="10" guides="0" tooltips="1" connect="1" arrows="1" fold="1" page="1" pageScale="1" pageWidth="850" pageHeight="1100" math="0" shadow="0">
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-3" style="rounded=0;orthogonalLoop=1;jettySize=auto;html=1;" edge="1" parent="1" source="0JCrL0P5o5JfQ6rzsXf7-1" target="0JCrL0P5o5JfQ6rzsXf7-2">
<mxGeometry relative="1" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-1" value="Generator" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="280" y="140" width="110" height="50" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-2" value="Queue" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="300" y="250" width="70" height="50" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-4" value="Load Balancer" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="280" y="360" width="110" height="50" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-5" value="Lock" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="190" y="460" width="110" height="50" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-6" value="Lock" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="340" y="460" width="110" height="50" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-7" value="" style="endArrow=classic;html=1;rounded=0;" edge="1" parent="1" source="0JCrL0P5o5JfQ6rzsXf7-2" target="0JCrL0P5o5JfQ6rzsXf7-4">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="160" y="340" as="sourcePoint" />
<mxPoint x="210" y="290" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-8" value="" style="endArrow=classic;html=1;rounded=0;" edge="1" parent="1" source="0JCrL0P5o5JfQ6rzsXf7-4" target="0JCrL0P5o5JfQ6rzsXf7-5">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="329" y="300" as="sourcePoint" />
<mxPoint x="160" y="390" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-9" value="" style="endArrow=classic;html=1;rounded=0;" edge="1" parent="1" source="0JCrL0P5o5JfQ6rzsXf7-4" target="0JCrL0P5o5JfQ6rzsXf7-6">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="329" y="300" as="sourcePoint" />
<mxPoint x="160" y="390" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-10" value="Sink" style="rounded=0;whiteSpace=wrap;html=1;" vertex="1" parent="1">
<mxGeometry x="280" y="560" width="70" height="50" as="geometry" />
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-12" value="" style="endArrow=classic;html=1;rounded=0;" edge="1" parent="1" source="0JCrL0P5o5JfQ6rzsXf7-6" target="0JCrL0P5o5JfQ6rzsXf7-10">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="520" y="600" as="sourcePoint" />
<mxPoint x="570" y="550" as="targetPoint" />
</mxGeometry>
</mxCell>
<mxCell id="0JCrL0P5o5JfQ6rzsXf7-13" value="" style="endArrow=classic;html=1;rounded=0;" edge="1" parent="1" source="0JCrL0P5o5JfQ6rzsXf7-5" target="0JCrL0P5o5JfQ6rzsXf7-10">
<mxGeometry width="50" height="50" relative="1" as="geometry">
<mxPoint x="520" y="600" as="sourcePoint" />
<mxPoint x="570" y="550" as="targetPoint" />
</mxGeometry>
</mxCell>
</root>
</mxGraphModel>
</diagram>
</mxfile>

98
assignment/environment.py Normal file
View file

@ -0,0 +1,98 @@
### DO NOT EDIT THIS FILE ###
from pypdevs.DEVS import AtomicDEVS
import random
import dataclasses
# The reason for annotating the *State-classes as 'dataclass', is because this automatically generates a nice __repr__-function, so that if the simulator is set to verbose, you can actually see what the state is.
class Ship:
def __init__(self, size, creation_time):
self.size = size
self.creation_time = creation_time
# useful in verbose mode:
def __repr__(self):
return f"Ship(size={self.size},created={self.creation_time})"
@dataclasses.dataclass
class GeneratorState:
current_time: float
time_until_next_ship: float
to_generate: int
random: random.Random
def __init__(self, seed=0, gen_num=1000):
self.current_time = 0.0 # for statistics only
self.time_until_next_ship = 0.0
self.to_generate = gen_num
self.random = random.Random(seed)
class Generator(AtomicDEVS):
def __init__(self,
seed=0, # random seed
lambd=1.0/60.0, # how often to generate a ship - in this example, once per minute
gen_types=[1,1,2], # ship sizes to generate, will be sampled uniformly - in this example, size 1 is twice as likely as size 2.
gen_num=1000, # number of ships total to generate
):
super().__init__("Generator")
# State (for everything that is mutable)
self.state = GeneratorState(seed=seed, gen_num=gen_num)
# I/O
self.out_ship = self.addOutPort("out_event")
# Parameters (read-only)
self.lambd = lambd
self.gen_types = gen_types
def timeAdvance(self):
return self.state.time_until_next_ship
def outputFnc(self):
size = self.state.random.choice(self.gen_types) # uniformly sample from gen_types
# watch out: outputFnc is called *before* intTransition!
creation = self.state.current_time + self.state.time_until_next_ship
return { self.out_ship: Ship(size, creation) }
def intTransition(self):
self.state.current_time += self.state.time_until_next_ship
self.state.to_generate -= 1
if self.state.to_generate > 0:
self.state.time_until_next_ship = self.state.random.expovariate(self.lambd)
else:
# stop generating
self.state.time_until_next_ship = float('inf')
return self.state
@dataclasses.dataclass
class SinkState:
current_time: float
ships: list
def __init__(self):
self.current_time = 0.0
self.ships = []
class Sink(AtomicDEVS):
def __init__(self):
super().__init__("Sink")
self.state = SinkState()
# On this input port, the Sink expects to receive a *list* of Ships. This is because a Lock can contain more than one Ship, and the Lock can send them all at once to the Sink (with a single event).
self.in_ships = self.addInPort("in_ships")
def extTransition(self, inputs):
self.state.current_time += self.elapsed
if self.in_ships in inputs:
ships = inputs[self.in_ships]
for ship in ships:
ship.finished_time = self.state.current_time
# amount of time spent in the system:
ship.queueing_duration = ship.finished_time - ship.creation_time
self.state.ships.extend(ships)
return self.state
### DO NOT EDIT THIS FILE ###

View file

@ -0,0 +1,86 @@
def make_plot_ships_script(priority:str, strategy:str, max_waits:list[float], gen_num:int):
return (f"""
### priority={priority}, strategy={strategy} ###
set terminal svg size 1200 900
# plot 1. x-axis: ships, y-axis: queuing duration of ship
set out 'plot_ships_{strategy}_{priority}.svg'
set title "Queueing duration (strategy={strategy}, priority={priority})"
set xlabel "Ship #"
set ylabel "Seconds"
#unset xlabel
#unset xtics
set key title "Max Wait"
set key bottom center out
set key horizontal
"""
# + '\n'.join([
# f"set style line {i+1} lw 4"
# for i in range(len(max_waits))
# ])
+ f"""
# set yrange [0:90000]
set xrange [0:{gen_num}]
set style fill solid
plot 'output_{strategy}_{priority}.csv' \\\n """ + ", \\\n '' ".join([
f"using 1:{i+2} title '{max_wait}' w boxes ls {i+1}"
for i, max_wait in enumerate(max_waits)
]))
def make_plot_box_script(priority:str, strategy:str, max_waits:list[float], gen_num:int):
return (f"""
# plot 2. x-axis: max-wait parameter, y-axis: queueing durations of ships
set out 'plot_box_{strategy}_{priority}.svg'
set title "Queueing duration (strategy={strategy}, priority={priority})"
set style fill solid 0.25 border -1
set style boxplot outliers pointtype 7
set style data boxplot
set key off
set xlabel "Max Wait"
unset xrange
unset yrange
set xtics (""" + ', '.join([ f"'{max_wait}' {i}"
for i, max_wait in enumerate(max_waits)]) + f""")
plot 'output_{strategy}_{priority}.csv' \\\n """ + ", \\\n '' ".join([
f"using ({i}):{i+2} title '{max_wait}'"
for i, max_wait in enumerate(max_waits)
]))
def make_plot_frequency_script(priority:str, strategy:str, max_waits:list[float], gen_num:int):
return (f"""
# plot 3. x-axis: queueing duration interval, y-axis: number of ships
bin_width = 5*60;
set out 'plot_freq_{strategy}_{priority}.svg'
set title "Frequency of queueing durations (strategy={strategy}, priority={priority})"
set boxwidth (bin_width) absolute
set style fill solid 1.0 noborder
set key title "Max Wait"
set key bottom center out
# set key horizontal
set xtics auto
set xrange [0:]
set xlabel "Queueing duration (interval)"
set ylabel "Number of ships"
bin_number(x) = floor(x/bin_width)
rounded(x) = bin_width * ( bin_number(x) + 0.5 )
plot 'output_{strategy}_{priority}.csv' \\\n """ + ", \\\n '' ".join([
f"using (rounded(${i+2})):(1) title '{max_wait}' smooth frequency with boxes"
for i, max_wait in list(enumerate(max_waits))
]))

106
assignment/runner.py Normal file
View file

@ -0,0 +1,106 @@
import os
from pypdevs.simulator import Simulator
from plot_template import make_plot_ships_script, make_plot_box_script, make_plot_frequency_script
# from system_solution import * # Teacher's solution
from system import *
## Parameters ##
gen_num = 500 # how many ships to generate
# How often to generate a ship (on average)
gen_rate = 1/60/4 # once every 4 minutes
# Ship size will be sampled uniformly from the following list.
gen_types = [1,1,2] # ship size '1' twice as likely to be generated as ship size '2'
# Load balancer...
priorities = {
# you can outcomment one of these lines to reduce the number of experiments (useful for debugging):
PRIORITIZE_BIGGER_SHIPS: "bigger",
PRIORITIZE_SMALLER_SHIPS: "smaller",
}
strategies = {
# you can outcomment one of these lines to reduce the number of experiments (useful for debugging):
STRATEGY_ROUND_ROBIN: "roundrobin",
STRATEGY_FILL_ER_UP: "fillerup",
}
# The number of locks and their capacities
lock_capacities=[3,2] # two locks, of capacity 3 and 2
# The different parameters to try for max_wait_duration
max_wait_durations = [ 0.0+i*120.0 for i in range(5) ] # all these values will be attempted
# max_wait_durations = [ 15.0 ] # <-- uncomment if you only want to run an experiment with this value (useful for debugging)
# How long does it take for a ship to pass through a lock
passthrough_duration = 60.0*15 # 15 minutes
outdir = "assignment_output"
plots_ships = []
plots_box = []
plots_freq = []
os.makedirs(outdir, exist_ok=True)
# try all combinations of priorities and strategies (4 total)
for priority in priorities:
for strategy in strategies:
values = []
# and in each experiment, try a bunch of different values for the 'max_wait_duration' parameter:
for max_wait_duration in max_wait_durations:
print("Run simulation:", priorities[priority], strategies[strategy], "max_wait =",max_wait_duration)
sys = LockQueueingSystem(
# See system.py for explanation of these values:
seed=0,
gen_num=gen_num,
gen_rate=gen_rate,
gen_types=gen_types,
load_balancer_strategy=strategy,
lock_capacities=lock_capacities,
priority=priority,
max_wait_duration=max_wait_duration,
passthrough_duration=passthrough_duration,
)
sim = Simulator(sys)
sim.setClassicDEVS()
# sim.setVerbose() # <-- uncomment to see what's going on
sim.simulate()
# all the ships that made it through
ships = sys.sink.state.ships
values.append([ship.queueing_duration for ship in ships])
# Write out all the ship queueuing durations for every 'max_wait_duration' parameter
# for every ship, we write a line:
# <ship_num>, time_max_wait0, time_max_wait1, time_max_wait2, ... time_max_wait10
filename = f'{outdir}/output_{strategies[strategy]}_{priorities[priority]}.csv'
with open(filename, 'w') as f:
try:
for i in range(gen_num):
f.write("%s" % i)
for j in range(len(values)):
f.write(", %5f" % (values[j][i]))
f.write("\n")
except IndexError as e:
raise Exception("There was an IndexError, meaning that fewer ships have made it to the sink than expected.\nYour model is not (yet) correct.") from e
# Generate gnuplot code:
for f, col in [(make_plot_ships_script, plots_ships), (make_plot_box_script, plots_box), (make_plot_frequency_script, plots_freq)]:
col.append(f(
priority=priorities[priority],
strategy=strategies[strategy],
max_waits=max_wait_durations,
gen_num=gen_num,
))
# Finally, write out a single gnuplot script that plots everything
with open(f'{outdir}/plot.gnuplot', 'w') as f:
# first plot the ships
f.write('\n\n'.join(plots_ships))
# then do the box plots
f.write('\n\n'.join(plots_box))
f.write('\n\n'.join(plots_freq))

65
assignment/system.py Normal file
View file

@ -0,0 +1,65 @@
### EDIT THIS FILE ###
from pypdevs.DEVS import CoupledDEVS
from atomicdevs import *
STRATEGY_ROUND_ROBIN = 0
STRATEGY_FILL_ER_UP = 1
class LockQueueingSystem(CoupledDEVS):
def __init__(self,
# See runner.py for an explanation of these parameters!!
seed,
gen_num,
gen_rate,
gen_types,
load_balancer_strategy,
lock_capacities,
priority,
max_wait_duration,
passthrough_duration,
):
super().__init__("LockQueueingSystem")
# Instantiate sub-models with the right parameters, and add them to the CoupledDEVS:
generator = self.addSubModel(Generator(
seed=seed, # random seed
lambd=gen_rate,
gen_types=gen_types,
gen_num=gen_num,
))
queue = self.addSubModel(Queue(
ship_sizes=set(gen_types), # the queue only needs to know the different ship sizes (and create a FIFO queue for each)
))
if load_balancer_strategy == STRATEGY_ROUND_ROBIN:
LoadBalancer = RoundRobinLoadBalancer
elif load_balancer_strategy == STRATEGY_FILL_ER_UP:
LoadBalancer = FillErUpLoadBalancer
load_balancer = self.addSubModel(LoadBalancer(
lock_capacities=lock_capacities,
priority=priority,
))
locks = [ self.addSubModel(Lock(
capacity=lock_capacity,
max_wait_duration=max_wait_duration,
passthrough_duration=passthrough_duration))
for lock_capacity in lock_capacities ]
sink = self.addSubModel(Sink())
# Don't forget to connect the input/output ports of the different sub-models:
# for instance:
# self.connectPorts(generator.out_ship, queue.in_ship)
# ...
# Our runner.py script needs access to the 'sink'-state after completing the simulation:
self.sink = sink
### EDIT THIS FILE ###

View file

@ -1,5 +1,4 @@
from pypdevs.simulator import Simulator
import random
# Import the model we experiment with
from system import QueueSystem
@ -22,7 +21,6 @@ values = []
# Loop over different configurations
for i in range(1, max_processors):
# Make sure each of them simulates exactly the same workload
random.seed(1)
# Set up the system
procs = [speed] * i
m = QueueSystem(mu=1.0/time, size=size, num=num, procs=procs)
@ -30,6 +28,7 @@ for i in range(1, max_processors):
# PythonPDEVS specific setup and configuration
sim = Simulator(m)
sim.setClassicDEVS()
sim.setVerbose() # <- uncomment to see what's going on
sim.simulate()
# Gather information for output

View file

@ -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}

View file

@ -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})"

View file

@ -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