CS计算机代考程序代写 scheme python data structure algorithm Account
Account
Dashboard
Courses
Groups Calendar Inbox History
Course Evals Help
!
CSC148H1 S All Sec!ons
Pages
Assignment 1
2021 Winter
Home Announcements Lectures
Zoom
Piazza
Office Hours
Bb Collaborate Weekly Preps Weekly Labs Assignments Tests
So&ware Guide Ramp-up Session Course Team
CS Teaching Labs MarkUs
Assignment 1
An Experiment to Compare Algorithms for Parcel Delivery
Due date: Tuesday, March 2, 2021 before 1:00 pm sharp, Toronto !me. You may complete this assignment individually or with one partner.
Learning Goals
By the end of this assignment you be able to:
read complex code you didn’t write and understand its design and implementa!on, including:
reading the class and method docstrings carefully (including a”ributes, representa!on invariants, precondi!ons, etc.) determining rela!onships between classes, by applying your knowledge of composi!on and inheritance
complete a par!al implementa!on of a class, including:
reading the representa!on invariants to enforce important facts about implementa!on decisions reading the precondi!ons to factor in assump!ons that they permit
wri!ng the required methods according to their docstrings
use inheritance to define a subclass of an abstract parent class
design a class, given its name and purpose, including
designing an appropriate interface
weighing some op!ons and choosing an appropriate data structure to implement a class recording important facts about an implementa!on decision using representa!on invariants
make reasonable decisions about which classes should be responsible for what
use an ADT to solve a problem, without having to think about how it is implemented perform unit tes!ng on a program with many interac!ng classes
Please read this handout carefully and ask ques!ons if there are any steps you do not understand.
The assignment involves code that is larger and more complex than Assignment 0. If you find it difficult to understand at first, that is normal – we are stretching you to do more challenging things. Expect that you will need to read carefully, and that you will need to go back over things mul!ple !mes.
We will guide you through a sequence of tasks, in order to gradually build the pieces of your implementa!on. Note that the pieces are laid out in logical order, not in order of difficulty.
Coding Guidelines
These guidelines are designed to help you write well-designed code that maintaints the interfaces we have defined (and thus will be able to pass our test cases).
For class DistanceMap , all a”ributes must be private.
For all other classes except Parcel and Truck , You must NOT:
change the interface (parameters, parameter type annota!ons, or return types) to any of the methods you have been given in the starter code. Note: in the scheduler subclasses, you are permi”ed to change the interface for the inherited ini!alizer, if needed.
change the type annota!ons of any public or private a”ributes you have been given in the starter code.
create any new public a”ributes.
create any new public methods except to override the schedule method that your classes Greedy and Random inherit from their abstract parent. add any more import statements to your code.
You will be designing classes Parcel and Truck , so we make these excep!ons:
You will be designing the a”ributes for these classes Parcel and Truck , and are allowed to make some or all of them public. Use your judgment.
You may add public methods to Parcel and Truck . You may:
remove unused imports from the Typing module. (We have included those that we think you might want to use in DistanceMape , Parcel and Truck .) create new private helper methods for the classes you have been given.
if you do create new private methods, you must provide type annota!ons for every parameter and return value. You must also write a full docstring
for such methods, as described in the Func!on Design Recipe. create new private a”ributes for the classes you have been given.
if you do create new private a”ributes you must give them a type annota!on and include a descrip!on of them in the class’s docstring as described in the Class Design Recipe.
Excep!on: In class PriorityQueue we have defined all the a”ributes needed. Do not define any new a”ributes, even private.
You may assume that all arguments passed to a method or func!on will sa!sfy its precondi!ons. All code that you write should follow the Func!on Design Recipe and the Class Design Recipe.
Introduc!on
Consider what happens when a delivery company like FedEx or Purolator receives a plane load of parcels at Pearson Airport to be delivered by truck to ci!es all over southern Ontario. They have to schedule the parcels for delivery by assigning parcels to trucks and determining the route each truck will take to make its deliveries.
Depending on how well these decisions are made, trucks may be well-packed and have short, efficient routes, or trucks may not be fully filled and may have to travel unnecessary distances.
For this assignment, you will write code to try out different algorithms to perform parcel scheduling and compare their performance. Problem descrip!on
Be sure to read through this sec!on carefully. Your Python code must accurately model all of the details described here.
The parcel delivery domain
Each parcel has a source and a des!na!on, which are the name of the city the parcel came from and the name of the city where it must be delivered to,
respec!vely. Each parcel also has a volume, which is a posi!ve integer, measured in units of cubic cen!metres (cc).
Each truck can store mul!ple parcels, but has a volume capacity, which is also a posi!ve integer and is in units of cc. The sum of the volumes of the parcels
on a truck cannot exceed its volume capacity. Each truck also has a route, which is an ordered list of city names that it is scheduled to travel through. Each parcel has a unique ID, that is, no two parcels can have the same ID. Each truck also has a unique ID, that is, no two trucks can have the same ID.
Depot
There is a special city that all parcels and trucks start from, and all trucks return to at the end of their route. We’ll refer to this city as the depot. (You can imagine all parcels have been shipped from their source city to the depot.) Our algorithms will schedule delivery of parcels from the depot to their des!na!ons.
You may assume that no parcels have the depot as their des!na!on. There is only one depot.
Truck routes
All trucks are ini!ally empty and only have the depot on their route (since it is their ini!al loca!on). A truck’s route is determined as follows: When a parcel is scheduled to be delivered by a truck, that parcel’s des!na!on is added to the end of the truck’s route, unless that city is already the last des!na!on on the truck’s route.
Example: Consider a truck at the start of the simula!on, and suppose that the depot is Toronto. The truck’s route at that point is just Toronto. Now suppose parcels are packed onto that truck in this order:
a parcel going to Windsor. The truck’s route is now Toronto, Windsor.
another parcel going to Windsor. The truck’s route is unchanged.
a parcel going to London. The truck’s route is now Toronto, Windsor, London.
a parcel going to Windsor. The truck’s route becomes Toronto, Windsor, London, Windsor. (Yes, this is a silly route, so the order in which we pack parcels onto trucks is going to ma”er.)
Whatever its route, at the end, a truck must return directly to the depot.
Scheduling parcels
You will implement two different algorithms for choosing which parcels go onto which trucks, and in what order. As we saw above, this will determine the routes of all the trucks.
(1) Random algorithm
The random algorithm will go through the parcels in random order. For each parcel, it will schedule it onto a randomly chosen truck (from among those trucks that have capacity to add that parcel). Because of this randomness, each !me you run your random algorithm on a given problem, it may generate a different solu!on.
(2) Greedy algorithm
The greedy algorithm tries to be more strategic. Like the random algorithm, it processes parcels one at a !me, picking a truck for each, but it tries to pick the “best” truck it can for each parcel. Our greedy algorithm is quite short-sighted: it makes each choice without looking ahead to possible consequences of the choice (that’s why we call it “greedy”).
The greedy algorithm has two configurable features: the order in which parcels are considered, and how a truck is chosen for each parcel. These are described below.
Parcel order
There are four possible orders that the algorithm could use to process the parcels:
In order by parcel volume, either smallest to largest (non-decreasing) or largest to smallest (non-increasing).
In order by parcel des!na!on, either smallest to largest (non-decreasing) or largest to smallest (non-increasing). Since des!na!ons are strings, larger and smaller is determined by comparing strings (city names) alphabe!cally.
Ties are broken using the order in which the parcels are read from our data file (see below).
Truck choice
When the greedy algorithm processes a parcel, it must choose which truck to assign it to. The algorithm first does the following to compute the eligible trucks:
1. It only considers trucks that have enough unused volume to add the parcel.
2. Among these trucks, if there are any that already have the parcel’s des!na!on at the end of their route, only those trucks are considered. Otherwise, all
trucks that have enough unused volume are considered.
Given the eligible trucks, the algorithm can be configured one of two ways to make a choice:
choose the eligible truck with the most available space, or choose the eligible truck with the least available space
Ties are broken using the order in which the trucks are read from our data file. If there are no eligible trucks, then the parcel is not scheduled onto any truck.
Observa!ons about the Greedy Algorithm
Since there are four op!ons for parcel priority and two op!ons for truck choice, our greedy algorithm can be configured eight different ways in total.
No!ce that there is no randomness in the greedy algorithm; it is completely “determinis!c”. This means that no ma”er how many !mes you run your greedy algorithm on a given problem, it will always generate the same solu!on.
Pu%ng it all together
For this assignment, your program will create an experiment by reading in some parcel, truck, and configura!on data from a set of files. Your code will be able to apply the random algorithm or any of the varia!ons of the greedy algorithm to a scheduling problem, and then report on sta!s!cs of interest, such as the average distance travelled by the trucks. This would allow the delivery company to compare the algorithms and decide which is best, given the company’s priori!es.
Starter code
Download this zip file containing the starter files for this assignment. Extract its contents into assignments/a1 in the csc148 folder that you set up by
following the So&ware Guide.
The module that drives the whole program is experiment . It contains class SchedulingExperiment .
A SchedulingExperiment can be created based on a dic!onary that specifies the loca!on of necessary data files as well as an algorithm configura!on. Then the experiment can be run and sta!s!cs can be generated (and op!onally reported).
The experiment module contains a main block that sets up and runs a single experiment. It can be used as a quick check to see if your experiment code can run without errors; it does not check the correctness of the resuts. For tes!ng the correctness of your code, you should add unit tests to a1_test.py and run that.
An experiment can be run in verbose mode. In that case, it should print step-by-step details regarding the scheduling algorithm as it runs. You may find this helpful for debugging. The specific verbose output doesn’t ma”er, as we will never test your code in verbose mode.
You are free to change the name of the configura!on file that is read in the main block of module experiment , because we will not test your code by running module experiment . Our tests will import the experiment module and use class SchedulingExperiment .
Format of the Configura!on Dic!onary
The configura!on dic!onary stores the configura!on of the scheduling experiment as a set of key-value pairs:
depot_loca!on: the name of the city where each parcels is, and from which it must be delivered to its des!na!on. Also the city where all trucks start at and return to.
parcel_file: the name of a file containing parcel data.
truck_file: the name of a file containing truck data.
map_file: the name of a file containing distance data.
algorithm: either ‘random’ or ‘greedy’. If ‘random’, none of the remaining keys are required in the configura!on file (and if they are present, they will be ignored).
parcel_priority: either ‘volume’ or ‘des!na!on’.
parcel_order: either
‘non-decreasing’ (meaning we process parcels in order from smallest to largest), or
‘non-increasing’ (meaning we process parcels in order from largest to smallest). truck_order: either
‘non-decreasing’ (meaning we choose the eligible truck with the least available space, and as go through the parcels we will choose trucks with greater available space), or
‘non-increasing’ (meaning we choose the eligible truck with the most available space, and as we go through the parcels we will choose trucks with less available space).
verbose: either true or false. Note the lack of quote marks; the code we’ve wri”en to read in the configura!on dic!onary can read such values directly into a boolean.
Format of the Data Files
The parcel file has one parcel per line, with the format: