Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

from typing import List, Dict, Union import json from scheduler import RandomScheduler, GreedyScheduler, Scheduler from domain import Parcel, Truck, Fleet from distance_map import DistanceMap class

image text in transcribedimage text in transcribed

from typing import List, Dict, Union import json from scheduler import RandomScheduler, GreedyScheduler, Scheduler from domain import Parcel, Truck, Fleet from distance_map import DistanceMap

class SchedulingExperiment: """An experiment in scheduling parcels for delivery.

To complete an experiment involves four stages:

1. Read in all data from necessary files, and create corresponding objects. 2. Run a scheduling algorithm to assign parcels to trucks. 3. Compute statistics showing how good the assignment of parcels to trucks is. 4. Report the statistics from the experiment.

=== Public Attributes === verbose: If is True, print step-by-step details regarding the scheduling algorithm as it runs. scheduler: The scheduler to use in this experiment. parcels: The parcels to schedule in this experiment. fleet: The trucks that parcels are scheduled to in this experiment. dmap: The distances between cities in this experiment.

=== Private Attributes === _stats: A dictionary of statistics. <_stats>'s value is undefined until ._compute_stats is called, at which point it contains keys and values as specified in Step 6 of Assignment 1. _unscheduled: A list of parcels. <_unscheduled>'s value is undefined until .run is called, at which point it contains the list of parcels that could not be scheduled in the experiment.

=== Representation Invariants === - contains at least one truck - contains all of the distances required to compute the length of any possible route for the trucks in delivering the packages in . """ verbose: bool scheduler: Scheduler parcels: List[Parcel] fleet: Fleet dmap: DistanceMap _stats: Dict[str, Union[int, float]] _unscheduled: List[Parcel]

def __init__(self, config: Dict[str, Union[str, bool]]) -> None: """Initialize a new experiment with the configuration specified in .

Precondition: contains keys and values as specified in Assignment 1. """ self.verbose = config['verbose'] # TODO: Use to determine what sort of scheduler we need. # TODO: Then make one of that sort and save it in self.scheduler.

self.parcels = read_parcels(config['parcel_file']) self.fleet = read_trucks(config['truck_file'], config['depot_location']) self.dmap = read_distance_map(config['map_file'])

self._stats = {} self._unscheduled = []

def run(self, report: bool = False) -> Dict[str, Union[int, float]]: """Run the experiment and return statistics on the outcome.

The return value is a dictionary with keys and values are as specified in Step 6 of Assignment 1.

If is True, print a report on the statistics from this experiment. Either way, return the statistics in a dictionary.

If is True, print step-by-step details regarding the scheduling algorithm as it runs. """ # TODO: Ask the scheduler to schedule the parcels onto trucks. # TODO: Save the unscheduled parcels in self._unscheduled.

self._compute_stats() if report: self._print_report() return self._stats

def _compute_stats(self) -> None: """Compute the statistics for this experiment, and store in .stats. Keys and values are as specified in Step 6 of Assignment 1.

Precondition: _run has already been called. """ # TODO: Replace the 0 values below with the correct statistics. self._stats = { 'fleet': 0, 'unused_trucks': 0, 'avg_distance': 0, 'avg_fullness': 0, 'unused_space': 0, 'unscheduled': 0 }

def _print_report(self) -> None: """Report on the statistics for this experiment.

This method is *only* for debugging purposes for your benefit, so the content and format of the report is your choice; we will not call your run method with set to True.

Precondition: _compute_stats has already been called. """ # TODO: Implement this method! pass

# ----- Helper functions -----

def read_parcels(parcel_file: str) -> List[Parcel]: """Read parcel data from and return.

Precondition: is the path to a file containing parcel data in the form specified in Assignment 1. """ # TODO: Initialize any variable(s) as needed. # read and add the parcels to the list. with open(parcel_file, 'r') as file: for line in file: tokens = line.strip().split(',') pid = int(tokens[0].strip()) source = tokens[1].strip() destination = tokens[2].strip() volume = int(tokens[3].strip()) # TODO: Do something with pid, source, destination and volume. # TODO: Return something.

def read_distance_map(distance_map_file: str) -> DistanceMap: """Read distance data from and return a DistanceMap that records it.

Precondition: is the path to a file containing distance data in the form specified in Assignment 1. """ # TODO: Initialize any variable(s) as needed. with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split(',') c1 = tokens[0].strip() c2 = tokens[1].strip() distance1 = int(tokens[2].strip()) distance2 = int(tokens[3].strip()) if len(tokens) == 4 \ else distance1 # TODO: Do something with c1, c2, distance1, and distance2 # TODO: Return something.

def read_trucks(truck_file: str, depot_location: str) -> Fleet: """Read truck data from and return a Fleet containing these trucks, with each truck starting at the .

Precondition: is a path to a file containing truck data in the form specified in Assignment 1. """ # TODO: Initialize any variable(s) as needed. with open(truck_file, 'r') as file: for line in file: tokens = line.strip().split(',') tid = int(tokens[0]) capacity = int(tokens[1]) # TODO: Do something with tid, capacity, and depot_location. # TODO: Return something.

def simple_check(config_file: str) -> None: """Configure and run a single experiment on the scheduling problem defined in .

Precondition: is a json file with keys and values as in the dictionary format defined in Assignment 1. """ # Read an experiment configuration from a file and build a dictionary # from it. with open(config_file, 'r') as file: configuration = json.load(file) # Create and run an experiment with that configuration. experiment = SchedulingExperiment(configuration) experiment.run(report=True)

if __name__ == '__main__': import python_ta python_ta.check_all(config={ 'allowed-io': ['read_parcels', 'read_distance_map', 'read_trucks', '_print_report', 'simple_check'], 'allowed-import-modules': ['doctest', 'python_ta', 'typing', 'json', 'scheduler', 'domain', 'distance_map'], 'disable': ['E1136'], 'max-attributes': 15, })

# ------------------------------------------------------------------------ # The following code can be used as a quick and simple check to see if your # experiment can run without errors. # ------------------------------------------------------------------------ simple_check('data/demo.json')

Task 5: Experiment Requires: knowing how to implement a class understanding inheritance You are ready to complete the code that runs the whole experiment! Take a look at the file experiment.py and complete the code. When calculating the statistics, you can assume there is always at least one parcel and one truck, and that at least one truck will be used. You may also assume that the map file contains every distance that you will need in order to compute the statistics. This module contains class SchedulingExperiment. It can create an experiment with input data and an algorithm configuration specified in a dictionary, then run the experiment, generate statistics as the result of the experiment, and (optionally) report the statistics, This module is responsible for all the reading of data from the data files. from typing import List, Dict, Union import json from scheduler import RandomScheduler, GreedyScheduler, Scheduler from domain import Parcel, Truck, Fleet from distance_map import Distance Map class SchedulingExperiment: "An experiment in scheduling parcels for delivery. To complete an experiment involves four stages: 1. Read in all data from necessary files, and create corresponding objects. 2. Run a scheduling algorithm to assign parcels to trucks. 3. Compute statistics showing how good the assignment of parcels to trucks is 4. Report the statistics from the experiment. === Public Attributes === verbose: If is True, print step-by-step details regarding the scheduling algorithm as it runs. scheduler: The scheduler to use in this experiment. parcels: The parcels to schedule in this experiment. fleet: The trucks that parcels are scheduled to in this experiment. dmap: The distances between cities in this experiment. === Private Attributes un stats: A dictionary of statistics. <_stats>'s value is undefined until ,_compute_stats is called, at which point it contains keys and values as specified in Step 6 of Assignment 1. _unscheduled: A list of parcels. <_unscheduled>'s value is undefined until .run is called, at which point it contains the list of parcels that could not be scheduled in the experiment. === Representation Invariants === - contains at least one truck - contains all of the distances required to compute the length of any possible route for the trucks in cfleet> delivering the packages in None: "Initialize a new experiment with the configuration specified in Precondition: contains keys and values as specified in Assignment 1 self.verbose = configI'verbose') # TODO: Use to determine what sort of scheduler we need. # TODO: Then make one of that sort and save it in self.scheduler. self.parcels = read_parcels(config['parcel_file']) self.fleet = read_trucks(config('truck_file'). config['depot_location']) self.dmap = read_distance_map/config['map_file')) self._stats =) self._unscheduled = 0 def run(self, report:bool = False) -> Dict(str, Union[int, float]]; Run the experiment and return statistics on the outcome. The return value is a dictionary with keys and values are as specified in Step 6 of Assignment 1. If is True, print step-by-step details regarding the scheduling algorithm as it runs. # TODO: Ask the scheduler to schedule the parcels onto trucks # TODO: Save the unscheduled parcels in self_unscheduled, self_compute_stats() if report: self_print_reporti) return self._stats def_compute_stats(self) -> None: Compute the statistics for this experiment, and store in .stats, Keys and values are as specified in Step 6 of Assignment 1. Precondition: _run has already been called # TODO: Replace the O values below with the correct statistics. self._stats =( "fleet': 0, 'unused_trucks': 0, avg_distance: 0 avg_fullness': 'unused space': 0, 'unscheduled": 0 } def _print_report(self) -> None: "Report on the statistics for this experiment. This method is only for debugging purposes for your benefit, so the content and format of the report is your choice; we will not call your run method with List[Parcel): "Read parcel data from and return, Precondition: is the path to a file containing parcel data in the form specified in Assignment 1. # TODO: Initialize any variable(s) as needed. #read and add the parcels to the list. with open(parcel_file, 'r') as file: for line in file: tokens = line.strip().split('') pid = int(tokens[0].strip) source = tokens[1].strip() destination = tokens[2].strip) volume = int(tokens[3].strip()) # TODO: Do something with pid, source, destination and volume. # TODO: Return something. def read_distance_map(distance_map_file: str) -> Distance Map: "Read distance data from and return a Distance Map that records it. Precondition: is the path to a file containing distance data in the form specified in Assignment 1. # TODO: Initialize any variable(s) as needed. with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split('') c1 = tokens[0].strip) c2 = tokens[1].strip) distance1 = int(tokens[2].strip) distance2 = int(tokens[3].strip) if len(tokens) == 41 else distance 1 # TODO: Do something with c1, c2, distance, and distance2 #TODO: Return something. read_trucks(trud file: depot_location: str) - "Read truck data from struck_file> and return a Fleet containing these trucks, with each truck starting at the Precondition: is a path to a file containing truck data in the form specified in Assignment 1. # TODO: Initialize any variable(s) as needed. with open(truck_file, '') as file: for line in file: tokens = line.strip().split('') tid = int(tokens[0]) capacity = int(tokens[1]) # TODO: Do something with tid, capacity, and depot_location. # TODO: Return something. def simple_check(config_file: str) -> None: ***"Configure and run a single experiment on the scheduling problem defined in Precondition: is a json file with keys and values as in the dictionary format defined in Assignment 1. # Read an experiment configuration from a file and build a dictionary # from it. with open(config_file, 'r') as file: configuration = json.load(file) # Create and run an experiment with that configuration experiment = SchedulingExperiment(configuration) experiment.run(report=True) if_name__ == '_main__ import python_ta python_ta.check_all(config={ 'allowed-io": ['read_parcels', 'read_distance_map, 'read_trucks |_print_report', 'simple_check'), allowed-import-modules': ['doctest, python_ta', 'typing! json', 'scheduler', 'domain! distance_map'l. 'disable": ['E1136'), 'max-attributes": 15, >) # # The following code can be used as a quick and simple check to see if your # experiment can run without errors. #-------- simple_check('data/demo.json') Task 5: Experiment Requires: knowing how to implement a class understanding inheritance You are ready to complete the code that runs the whole experiment! Take a look at the file experiment.py and complete the code. When calculating the statistics, you can assume there is always at least one parcel and one truck, and that at least one truck will be used. You may also assume that the map file contains every distance that you will need in order to compute the statistics. This module contains class SchedulingExperiment. It can create an experiment with input data and an algorithm configuration specified in a dictionary, then run the experiment, generate statistics as the result of the experiment, and (optionally) report the statistics, This module is responsible for all the reading of data from the data files. from typing import List, Dict, Union import json from scheduler import RandomScheduler, GreedyScheduler, Scheduler from domain import Parcel, Truck, Fleet from distance_map import Distance Map class SchedulingExperiment: "An experiment in scheduling parcels for delivery. To complete an experiment involves four stages: 1. Read in all data from necessary files, and create corresponding objects. 2. Run a scheduling algorithm to assign parcels to trucks. 3. Compute statistics showing how good the assignment of parcels to trucks is 4. Report the statistics from the experiment. === Public Attributes === verbose: If is True, print step-by-step details regarding the scheduling algorithm as it runs. scheduler: The scheduler to use in this experiment. parcels: The parcels to schedule in this experiment. fleet: The trucks that parcels are scheduled to in this experiment. dmap: The distances between cities in this experiment. === Private Attributes un stats: A dictionary of statistics. <_stats>'s value is undefined until ,_compute_stats is called, at which point it contains keys and values as specified in Step 6 of Assignment 1. _unscheduled: A list of parcels. <_unscheduled>'s value is undefined until .run is called, at which point it contains the list of parcels that could not be scheduled in the experiment. === Representation Invariants === - contains at least one truck - contains all of the distances required to compute the length of any possible route for the trucks in cfleet> delivering the packages in None: "Initialize a new experiment with the configuration specified in Precondition: contains keys and values as specified in Assignment 1 self.verbose = configI'verbose') # TODO: Use to determine what sort of scheduler we need. # TODO: Then make one of that sort and save it in self.scheduler. self.parcels = read_parcels(config['parcel_file']) self.fleet = read_trucks(config('truck_file'). config['depot_location']) self.dmap = read_distance_map/config['map_file')) self._stats =) self._unscheduled = 0 def run(self, report:bool = False) -> Dict(str, Union[int, float]]; Run the experiment and return statistics on the outcome. The return value is a dictionary with keys and values are as specified in Step 6 of Assignment 1. If is True, print step-by-step details regarding the scheduling algorithm as it runs. # TODO: Ask the scheduler to schedule the parcels onto trucks # TODO: Save the unscheduled parcels in self_unscheduled, self_compute_stats() if report: self_print_reporti) return self._stats def_compute_stats(self) -> None: Compute the statistics for this experiment, and store in .stats, Keys and values are as specified in Step 6 of Assignment 1. Precondition: _run has already been called # TODO: Replace the O values below with the correct statistics. self._stats =( "fleet': 0, 'unused_trucks': 0, avg_distance: 0 avg_fullness': 'unused space': 0, 'unscheduled": 0 } def _print_report(self) -> None: "Report on the statistics for this experiment. This method is only for debugging purposes for your benefit, so the content and format of the report is your choice; we will not call your run method with List[Parcel): "Read parcel data from and return, Precondition: is the path to a file containing parcel data in the form specified in Assignment 1. # TODO: Initialize any variable(s) as needed. #read and add the parcels to the list. with open(parcel_file, 'r') as file: for line in file: tokens = line.strip().split('') pid = int(tokens[0].strip) source = tokens[1].strip() destination = tokens[2].strip) volume = int(tokens[3].strip()) # TODO: Do something with pid, source, destination and volume. # TODO: Return something. def read_distance_map(distance_map_file: str) -> Distance Map: "Read distance data from and return a Distance Map that records it. Precondition: is the path to a file containing distance data in the form specified in Assignment 1. # TODO: Initialize any variable(s) as needed. with open(distance_map_file, 'r') as file: for line in file: tokens = line.strip().split('') c1 = tokens[0].strip) c2 = tokens[1].strip) distance1 = int(tokens[2].strip) distance2 = int(tokens[3].strip) if len(tokens) == 41 else distance 1 # TODO: Do something with c1, c2, distance, and distance2 #TODO: Return something. read_trucks(trud file: depot_location: str) - "Read truck data from struck_file> and return a Fleet containing these trucks, with each truck starting at the Precondition: is a path to a file containing truck data in the form specified in Assignment 1. # TODO: Initialize any variable(s) as needed. with open(truck_file, '') as file: for line in file: tokens = line.strip().split('') tid = int(tokens[0]) capacity = int(tokens[1]) # TODO: Do something with tid, capacity, and depot_location. # TODO: Return something. def simple_check(config_file: str) -> None: ***"Configure and run a single experiment on the scheduling problem defined in Precondition: is a json file with keys and values as in the dictionary format defined in Assignment 1. # Read an experiment configuration from a file and build a dictionary # from it. with open(config_file, 'r') as file: configuration = json.load(file) # Create and run an experiment with that configuration experiment = SchedulingExperiment(configuration) experiment.run(report=True) if_name__ == '_main__ import python_ta python_ta.check_all(config={ 'allowed-io": ['read_parcels', 'read_distance_map, 'read_trucks |_print_report', 'simple_check'), allowed-import-modules': ['doctest, python_ta', 'typing! json', 'scheduler', 'domain! distance_map'l. 'disable": ['E1136'), 'max-attributes": 15, >) # # The following code can be used as a quick and simple check to see if your # experiment can run without errors. #-------- simple_check('data/demo.json')

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases Illuminated

Authors: Catherine Ricardo

2nd Edition

1449606008, 978-1449606008

More Books

Students also viewed these Databases questions

Question

Why is the System Build Process an iterative process?

Answered: 1 week ago