write python codes

Description

It seems like much, but it’s not. All of the reading in the pdf is to walk you through the codes and how to implement them.

Don't use plagiarized sources. Get Your Custom Assignment on
write python codes
From as Little as $13/Page

Unformatted Attachment Preview

CS1112 Fall 2023 Project 5
due November 9 at 11pm
You must work either on your own or with one partner. If you work with a partner you must first
register as a group in CMS and then submit your work as a group. Adhere to the Code of Academic
Integrity. A group submission requires that both partners are joint authors of all parts of the
project—splitting a project with each partner working on their own part is a violation of academic
integrity.
For a group, “you” below refers to “your group.” You may discuss background issues and general
strategies with others and seek help from the course staff, but the work that you submit must be
your own. In particular, you may discuss general ideas with others or look up general programming
techniques, but you may not work out the detailed solutions with others, and you may not search
for or use tools to generate solutions to specific parts of the assignment. It is not OK for you to
see or hear another student’s code and it is certainly not OK to copy code from another person or
from published/Internet sources, including generative artificial intelligence tools. If you feel that
you cannot complete the assignment on you own, seek help from the course staff. Please read the
“Academic Integrity” section in the Syllabus for further details and for the rationale of the CS1112
Academic Integrity policy.
Objectives
Completing this project will build your understanding of object-oriented programming, nested lists,
and file input. You will also get practice on developing and testing code incrementally—one class (or
even one method) at a time.
Ground Rule
Since this is an introductory computing course, and in order to give you practice on specific programming concepts, some functions/features are forbidden in assignments. For the required portion of
this project, use only the concepts and tools discussed in class and in this document.
1
Robot Task Allocation
In warehouses, hospitals, and homes, we may have robots that assist us in various ways. Robots as
they are today, however, often have physical limitations such that they are capable of performing only
specific tasks. Scheduling robots under constraints—physical limitations, limited availability, resource
conflicts—is crucial. Some robots can pick up only specific items due to weight lifting limitations or arm
arrangements. In this project, you will write code to delegate tasks amongst a team of heterogeneous
robots.
You will be playing the role of a manager of a robot-assisted warehouse. You have a team of heterogeneous robots, a set of items you need to pick up, and a time period during which the robots have to
pick up as many items as possible. These robots have different skills and abilities, so you must come
up with a plan for the robots for the time period in question.
The plan for the team of robots is called an “allocation.” A successful allocation assigns robots to pick
up specific items, presenting the robots with the paths to take to the items and the windows of time
where they will be performing pickup tasks.
1
We provide the design of the classes that you will implement in this project. Read carefully about
the reasoning behind the design below. You will see how objects of several classes interact in this
robot task allocation project. You will see that one of the classes that we will use is Interval, a
class that we have used in lecture. As we have discussed before, a class that is designed well can
be used in different projects and situations, including being extended for specific purposes.
Another focus of this project is for you to practice testing individual methods and classes, one at
a time. We suggest tests throughout the project description, but you need to generate more tests
beyond our suggestions in order to make sure that the methods you implement are correct. Do
not assume that just passing the one test case that we have suggested implies that your method
is correct. As you have seen in our weekly exercises, each method/function should be tested with
multiple, distinct test cases that are representative of different input scenarios.
1.0
Object-Oriented Design
The object-oriented design component of deciding which classes and methods should be made has
already been done—your job no is to implement methods according their specifications. A part of this
process will involve writing tests as you build new functionality.
Our design involves three classes: Interval, Item, and Robot. We then write a function run robots()
to read the initial positions and attributes of robots and items as data from a file, instantiate objects
of the different classes, and perform task allocation. Class Interval is given, and partial code is given
in the other classes.
Below is a summary of the classes, showing what attributes and methods are available in each class.
Attributes and methods that are “hidden”1 are shown using an open circle, ◦, and those with public
access are shown using a filled circle, •. The skeleton code given in the zip file p5a release files.zip
gives further details on each item. Download the zip and extract the files from the zip—do not work
from the zip without extracting the actual .py files and data files first.
Interval
Attributes:
• left
• right
Methods:
• init ()
• get width()
• shift()
• is in()
• add()
• overlap()
Item
Attributes:
• id
• name
• weight
• arm requirement
• loc
• picked window
• duration
Methods:
• init ()
• valid pickup()
• update pickup status()
• draw()
Robot
Attributes:
◦ id
◦ max weight
◦ total time
◦ occupied periods
◦ locations
◦ items picked
Methods:
• init ()
• get id()
• get items picked()
• get locations()
• where am i()
• steps to arrival()
• pick()
• draw()
1
By convention, attribute and method names that begin with the underscore ‘( )’ are treated as hidden from other
classes. They are to be accessed within their class only, not from other classes. However, in Python no attributes or
methods are truly “private.”
2
So which class should you work on first? Answer: the most independent class, the one that doesn’t
depend on other classes. We will start with class Interval. As you complete the classes, do not change
the names of the attributes or methods. You should not need to add any extra methods or attributes
in the classes.
1.1
Class Interval
Use the interval.py file extracted from the Project 5 zip. (We used multiple versions of the class
definition during lecture; please be sure to use the version released for Project 5.)
Read the class definition and notice the use of default values for some parameters in the initializer. Assuming that all the skeleton files are in your current working directory, experiment with class Interval
by typing the following statements in the Console:
in1 = Interval (3 ,9)
print ( in1 )
print ( in1 . left )
# Instantiate an Interval with endpoints 3 and 9
# Should be 3. The attributes are ” public ,” so it is possible
# to access the attribute left directly .
# Instantiate an Interval with default end points
in2 = Interval ()
print ( in2 )
o = in1 . overlap ( Interval (5 ,15))
# o references an Interval with endpoints 5 and 9
print ( o )
print ( f ” { o . get_width ()=} ” ) # Should be 4 , the width of the Interval
#
referenced by o
Self-documenting expressions in f-strings for debugging
Take a close look at the last print statement and the output that it produced. We know already that
an expression inside curly brackets in an f-string gets evaluated. Now you see that the the expression
followed by an equal sign (=), all inside curly brackets, produces a string that includes both the text of
the expression and the evaluated value of that expression. This is super handy for debugging as you
can see in the printed output both the result and the code that produced that result.
We started a file testscript.py to help you test your classes as you develop them. Read and run
testscript now; you can see that it contains the same kind of code as suggested above for “exercising”
the Interval class. Since class Interval is given, you are not actually testing the methods but instead
you are practicing how to access the attributes and methods of class Interval. For the other classes
you will later add code to testscript.py in order to test the methods that you implement. You will
submit testscript.py as a record of your development process.
If there is anything that you don’t understand in this class, ask and figure it out before moving on! You
want to make sure that you understand everything here before working on another class that depends
on class Interval.
1.2
Class Item
An Item is some product in your warehouse that needs to be picked up by a robot. Read the partially
completed file item.py, paying close attention to the class docstring and the docstring of the given
initializer in order to learn about the attributes of Item.2
2
There is an attribute named id (not id). This is because id is the name of a Python built-in function, so we do not
want to name an attribute the same as a built-in function.
3
Implement the following methods:
• valid pickup()
Test your method immediately after implementation. Look at the given code under
Chaining up references below. As you work on individual methods, be sure to add
code to testscript.py to call that method as a test. Test each method multiple times
with different input values representative of various valid scenarios.3 It is tempting to
rush through coding without stopping to test thoroughly, but that will burn more time
in the end because you will then have to deal with many confounding errors in your
buggy atop buggy code when you finally get around to trying to run your program.
• update pickup status()
• draw() Use the draw rect() function we have provided from the shapes module. To add text
to graphics, use plt.text() function. For example
s= str(u); # u is number
plt.text(x, y, s, horizontalalignment=”center”)
puts the string s at coordinates (x,y) with center-alignment on the current figure. You will
use this method later to animate the simulation.
Chaining up references
Consider the following fragment (assuming all necessary import is done):
# Create an Item with the id 1 , the name ‘ basket ‘, a weight of 2 lbs ,
#
no arm requirement for pickup , a necessary duration for pickup of 3
#
time steps , and positioned at (3 ,4).
i1 = Item (1 , ‘ basket ‘ , 2 , 0 , 3 , [3 ,4])
print ( f ” { i1 . id_ =} ” )
print ( f ” { i1 . loc =} ” )
print ( f ” { i1 . valid_pickup (4 ,2)=} ” )
print ( f ” { i1 . valid_pickup (1 ,0)=} ” )
print ( f ” { i1 . u p d a t e _ p i c k u p _ s t a t u s (2)=} ” )
print ( f ” { i1 . picked_window . left =} ” )
print ( f ” { i1 . picked_window . right =} ” )
#
#
#
#
#
#
#
#
Should be 1
Should be [3 ,4]
Should be True
Should be False
Schedule i1 for pickup starting
at time 2
Should be 2
Should be 5
# The amount of time it takes for a robot to pick up the item should be
#
equal to the item ‘s duration attribute
print ( f ” { i1 . picked_window . get_width ()=} ” ) # Should be 3
Notice how references are “chained together” using the dot notation in the last three statements above.
In the last statement . . .
i1 references an Item.
An Item object has the attribute picked window, which references an Interval. Therefore,
i1.picked window references an Interval.
An Interval has an instance method get width(), therefore
i1.picked window.get width() returns a number (the width).
3
Where the inputs to the methods are things we’d expect (e.g. the correct type).
4
Don’t forget to test your draw method. Here are a couple examples:
# At time 3 , the item is not yet fully picked up , so it should be drawn if
# method draw is called . The statements below should give a red rectangle in
# figure window 1 , centered over (3 ,4) , with “1” ( the id ) inside the rectangle .
plt . figure (1); i1 . draw (3)
# At time 5 , the item is picked up , so it should not be drawn if method draw is
# called . The statements below should show figure window 2 but no rectangle drawn .
plt . figure (2); i1 . draw (5)
Test your methods thoroughly—use more tests than shown in the demonstrations above. Make sure
you understand everything in this class and fix any bugs before moving on.
1.3
Class Robot
Figure 1: A standard robot: No arms (This is the Anki Vector robot)
The Robot has three main states. A waiting state (where it stays motionless at a location), a traveling
state (where it’s navigating to the location of an item), and a pickup state (where it’s in the process
of picking up an item).
This standard Robot is arm-less. It “picks up” items by coming into contact with Items and then
pushing them around as it moves. Note that the max weight attribute is the limit for picking up one
Item and is not a max capacity for holding (storing) Items. As a simplification, we assume that the
Robot has unlimited storage capacity—it can hold any number of Items onboard as long as each one
weighs no more than max weight at pickup. Once an Item is picked up, it is held by the Robot until
the simulation time ends; it cannot be picked up again.
Read the partially completed class Robot in the file robot.py. A detailed description of its attributes
is given in the class docstring; read it carefully. Your job is to implement the constructor and all the
methods.
1.3.1
Implement the initializer
Read the specification (docstring) for the class and initializer carefully before writing any code.
Add code to testscript.py to test your initializer by instantiating a Robot. We will stop reminding
you to test from this point on, but going forward, please test every method that you implement!
1.3.2
Implement the other methods
Now that you have read about the individual attributes, let’s think about the relationship between the
attributes total time and locations before continuing with the rest of the methods. For simplicity
5
we will let total time be the same as the number of time steps for which we will run our simulation.
Our simulation runs from timestep 1 (robot at its starting location) to timestep total time. For
example, if we run the simulation for ten units of time, then total time would be 10, which means
that the locations attribute (a list) may grow to at most length 10. After initializing the locations
attribute with the robot’s starting location, you only append more locations to the locations attribute
when the robot is given a task. This means that the last element of the locations attribute is
the robot’s most recent location, and the time that the robot got to that location is the length of
locations.
get id()
This and the next two “get” methods are super short. Don’t be concerned.
get items picked() Returns a copy of the robot’s items picked. Careful! The items picked
attribute is a list of objects. You need to use the copy.deepcopy function to make a true copy.
get locations() Returns a copy of the robot’s locations. The locations attribute is a nested
list, which means that it is a list of objects. Use the copy.deepcopy function to make a true copy.
where am i() This method returns the most recent location of the robot along with the time that it
corresponded to.
steps to arrival() This returns a list of x-y coordinates (also a list) for each time step of a path
that the robot can take to reach the location loc. Recall that the Robot can move only NESW, one
unit distance in one time step.
pick() This method simulates the robot picking up an Item; it is the first method we write that
has the objects of two classes interact! The method must first determine whether the pickup could
happen due to the requirements of the Item (hint: you wrote a method earlier for checking the validity
of a pickup and you should use it) and due to the simulation time. If the pickup should occur and
we ought to pick, then this method must update its own record of picking up the item (attributes
occupied periods, locations, items picked) as well as the Item’s attributes (hint: you wrote
a method earlier for updating an Item’s pickup status and you should use it).
For updating locations note that if an Item at (x, y) takes 2 time steps to pick up, then there should
be three 3 occurrences of (x, y) in locations—the first occurrence for the Robot to first arrive and
the next two for the process of picking it up.
draw() Draw a blue circle at the robot’s location. Use the draw disk() function we have provided.
We will use this method later for the animation.
1.4
Function run robots()
Let’s use the classes that we have so far to attempt an allocation! The main job of the function
run robots() is to read data from a file, create robot and item objects, and perform an allocation.
Some of the code for reading a data file is given, but you need to add code to complete the job.
Start by taking a look at the data file room1.txt, which contains lines of comma-delimited data. The
first line gives the simulation parameters (number of time steps, horizontal length of the room, vertical
length of the room). Each of the remaining lines contains the data for a Robot or an Item. A line
containing data for a Robot has the following format:
Robot, ID, max weight, [starting x, starting y]
A line containing data for an item has the following format:
Item, ID, name, weight, number of arms needed, [location x, location y], duration
6

Purchase answer to see full
attachment