-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation.py
More file actions
161 lines (130 loc) · 5.3 KB
/
simulation.py
File metadata and controls
161 lines (130 loc) · 5.3 KB
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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
"""CSC148 Assignment 1 - Simulation
=== CSC148 Fall 2018 ===
Department of Computer Science,
University of Toronto
=== Module description ===
This contains the main Simulation class that is actually responsible for
creating and running the simulation. You'll also find the function `sample_run`
here at the bottom of the file, which you can use as a starting point to run
your simulation on a small configuration.
Note that we have provided a fairly comprehensive list of attributes for
Simulation already. You may add your own *private* attributes, but should not
remove any of the existing attributes.
"""
# You may import more things from these modules (e.g., additional types from
# typing), but you may not import from any other modules.
from typing import Dict, List, Any
import algorithms
from algorithms import Direction
from entities import Person, Elevator
from visualizer import Visualizer
class Simulation:
"""The main simulation class.
=== Attributes ===
arrival_generator: the algorithm used to generate new arrivals.
elevators: a list of the elevators in the simulation
moving_algorithm: the algorithm used to decide how to move elevators
num_floors: the number of floors
visualizer: the Pygame visualizer used to visualize this simulation
waiting: a dictionary of people waiting for an elevator
(keys are floor numbers, values are the list of waiting people)
"""
arrival_generator: algorithms.ArrivalGenerator
elevators: List[Elevator]
moving_algorithm: algorithms.MovingAlgorithm
num_floors: int
visualizer: Visualizer
waiting: Dict[int, List[Person]]
def __init__(self,
config: Dict[str, Any]) -> None:
"""Initialize a new simulation using the given configuration."""
# Initialize the visualizer.
# Note that this should be called *after* the other attributes
# have been initialized.
self.visualizer = Visualizer([], # should be self.elevators
2, # should be self.num_floors
config['visualize'])
############################################################################
# Handle rounds of simulation.
############################################################################
def run(self, num_rounds: int) -> Dict[str, Any]:
"""Run the simulation for the given number of rounds.
Return a set of statistics for this simulation run, as specified in the
assignment handout.
Precondition: num_rounds >= 1.
Note: each run of the simulation starts from the same initial state
(no people, all elevators are empty and start at floor 1).
"""
for i in range(num_rounds):
self.visualizer.render_header(i)
# Stage 1: generate new arrivals
self._generate_arrivals(i)
# Stage 2: leave elevators
self._handle_leaving()
# Stage 3: board elevators
self._handle_boarding()
# Stage 4: move the elevators using the moving algorithm
self._move_elevators()
# Pause for 1 second
self.visualizer.wait(1)
return self._calculate_stats()
def _generate_arrivals(self, round_num: int) -> None:
"""Generate and visualize new arrivals."""
pass
def _handle_leaving(self) -> None:
"""Handle people leaving elevators."""
pass
def _handle_boarding(self) -> None:
"""Handle boarding of people and visualize."""
pass
def _move_elevators(self) -> None:
"""Move the elevators in this simulation.
Use this simulation's moving algorithm to move the elevators.
"""
pass
############################################################################
# Statistics calculations
############################################################################
def _calculate_stats(self) -> Dict[str, int]:
"""Report the statistics for the current run of this simulation.
"""
return {
'num_iterations': 0,
'total_people': 0,
'people_completed': 0,
'max_time': 0,
'min_time': 0,
'avg_time': 0
}
def sample_run() -> Dict[str, int]:
"""Run a sample simulation, and return the simulation statistics."""
config = {
'num_floors': 6,
'num_elevators': 6,
'elevator_capacity': 3,
'num_people_per_round': 2,
# Random arrival generator with 6 max floors and 2 arrivals per round.
'arrival_generator': algorithms.RandomArrivals(6, 2),
'moving_algorithm': algorithms.RandomAlgorithm(),
'visualize': True
}
sim = Simulation(config)
stats = sim.run(15)
return stats
if __name__ == '__main__':
# Uncomment this line to run our sample simulation (and print the
# statistics generated by the simulation).
# print(sample_run())
import python_ta
python_ta.check_all(config={
'extra-imports': ['entities', 'visualizer', 'algorithms', 'time'],
'max-nested-blocks': 4
})