Starting from:
$35

$29

Homework Assignment 3 Solution

Assignment Policies



Collaboration Policy. This homework may be done in individually or in pairs. Use of the Internet is allowed, but should not include searching for existing solutions.




Under absolutely no circumstances code can be exchanged between students.




Excerpts of code presented in class can be used.




Assignments from previous o erings of the course must not be reused. Vio-lations will be penalized appropriately.




Late Policy. Late submissions are allowed with a penalty of 2 points per hour past the deadline.




Assignment



The idea behind this assignment is to get familiar with Erlang and to learn how to work with some of the data structure provided by the language; notably lists, maps, and records. You are asked to implement various operations that provide support to a shipping company. The shipping company administers (cargo) ships. Ships transport containers and navigate from one port to another. Each port has a number of docks where the ships may load and unload. Containers can either be in transit on a ship or in a port (in the latter case, no distinction is made regarding the speci c dock). You are supplied with two les:




shipping.hrl. The Erlang header le for the assignment. It contains all the record declarations you should need to complete this assignment. These are described in further detail below.



shipping.erl. The module le that includes all the functions that need to be completed. This is the le you will be editing.



1






2.0.1 Shipping Header File (shipping.hrl)




Ship This record contains the elds: name a string value




id a unique integer value




container cap the maximum number of containers that a ship can hold Container This record contains the elds:




id a unique integer value




weight any positive integer value Port This record contains the elds:




name a string value




id a unique integer value




docks a list of docks for the port. A dock can be represented by an unique integer or a character and is only required to be unique at a given port (i.e. Port "New York", Dock ’A’ and Port "Los Angeles", Dock ’A’ are both valid)




container cap the maximum number of containers that a ship can hold Shipping State This record contains the elds:




ships a list of the ship records currently in the system




containers a list of the container records currently in the system ports a list of the port records currently in the system




ship locations a tuple containing a port id, dock id, and ship id (i.e. (1,’A’,3)) if port 1, dock ’A’ contains ship 3




ship inventory a map that takes a ship id and maps it to the list of contain-ers ids on that ship.




port inventory a map that takes a port id and maps it to the list of con-tainers ids at that port.







Shipping Module



We next describe each of the functions you are asked to implement. All of them have to be de ned in shipping.erl.




To illustrate some of the functions described below we will use a sample shipping company ShipCo. The shipping state of ShipCo may be obtained by calling shipping:shipco().







2






It is provided for you in the stub. Before you try out the examples below, remember to compile and then load the record de nitions into the interpreter, as follows:







1 c(shipping).




shipping.erl:2: Warning: export_all flag enabled - all functions will be exported {ok,shipping}




2 rr(shipping).




[container,port,ship,shipping_state]




get ship(Shipping State, Ship ID)



This method returns a ship record for the given id. For example, shipping:get_ship(shipping:shipco(), will return the ship whose id is 1.







3 shipping:get_ship(shipping:shipco(),1).




#ship{id = 1,name = "Santa Maria",container_cap = 20}




get container(Shipping State, Container ID)



This method returns a container record for the given id. For example:







4 shipping:get_container(shipping:shipco(),4).




#container{id = 4,weight = 62}




get port(Shipping State, Port ID)



This method returns a port records for the given id.







5 shipping:get_port(shipping:shipco(),3).




#port{id = 3,name = "Miami",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200}




get occupied docks(Shipping State, Port ID)



This method returns a list of all the occupied docks for a given port.







6 shipping:get_occupied_docks(shipping:shipco(),3).




[’C’]




get ship location(Shipping State, Ship ID)



This method returns the location, fPort ID, Dock IDg, of a given ship.







7 shipping:get_ship_location(shipping:shipco(),3).




{1,’A’}




get container weight(Shipping State, Container IDs)



This method returns the total weight of all of the container ids in the list Container IDs.







8 shipping:get_container_weight(shipping:shipco(),[3,5]).




243




3






get ship weight(Shipping State, Ship ID)



This method returns the total weight of a ship, measured by the total weight of the ship’s containers.







9 shipping:get_ship_weight(shipping:shipco(),2).




676




load ship(Shipping State, Ship ID, Container IDs)



This method returns a shipping state in which the containers in the list of Container IDs are moved from a port to the ship with the given Ship ID. Make sure that all the containers are at the same port as the ship they are loading onto. In the case that loading the ship would put the ship over capacity, return an atom error and do not add any containers to the ship. Here is an example. Notice how containers 16, 18 and 20 have been removed from dock 1 and loaded onto ship 1.







10 shipping:load_ship(shipping:shipco(), 1, [16,18,20]).




{ok,#shipping_state{




ships = [#ship{id = 1,name = "Santa Maria",container_cap = 20},




#ship{id = 2,name = "Nina",container_cap = 20},




#ship{id = 3,name = "Pinta",container_cap = 20},




#ship{id = 4,name = "SS Minnow", container_cap = 20},




#ship{id = 5,name = "Sir Leaks-A-Lot",container_cap = 20}],




containers = [...], %% not shown




ports = [#port{id = 1,name = "New York",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200},




#port{id = 2,name = "San Francisco",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200},




#port{id = 3,name = "Miami",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200}],




ship_locations = [{1,’B’,1},




{1,’A’,3},




{3,’C’,2},




{2,’D’,4},




{2,’B’,5}],

ship_inventory = #{1 = [14,15,9,2,6,16,18,20], %% loaded here




2 = [1,3,4,13],




3 = [],




4 = [2,8,11,7],




5 = [5,10,12]},




port_inventory = #{1 = [17,19], %% removed from port 1 2 = [21,22,23,24,25],




3 = [26,27,28,29,30]}}}




unload ship all(Shipping State, Ship ID)



This method returns a shipping state in which all of the containers that are on a given







4






ship are o oaded to the port in which the ship is docked. In the case that o oading to a port would put the port over capacity, return an error and do not unload any containers to the port.







11 shipping:unload_ship_all(shipping:shipco(), 2).




{ok,#shipping_state{




ships = [#ship{id = 1,name = "Santa Maria",container_cap = 20},




#ship{id = 2,name = "Nina",container_cap = 20},




#ship{id = 3,name = "Pinta",container_cap = 20},




#ship{id = 4,name = "SS Minnow",container_cap = 20},




#ship{id = 5,name = "Sir Leaks-A-Lot",container_cap = 20}],




containers = [...], %% not shown




ports = [#port{id = 1,name = "New York",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200},




#port{id = 2,name = "San Francisco",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200},




#port{id = 3,name = "Miami",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200}],




ship_locations = [{1,’B’,1},




{1,’A’,3},




{3,’C’,2},




{2,’D’,4},




{2,’B’,5}],




ship_inventory = #{1 = [14,15,9,2,6],

2 = [], %% no more containers




3 = [],




4 = [2,8,11,7],




5 = [5,10,12]},




port_inventory = #{1 = [16,17,18,19,20],




2 = [21,22,23,24,25],




3 = [26,27,28,29,30,1,3,4,13]} %% loaded here




}}




unload ship(Shipping State, Ship ID, Container IDs)



This method returns a shipping state in which the given containers on a ship are o oaded to the port in which the ship is docked. Make sure that all the containers are located on the ship. In the case that o oading to a port would put the port over capacity, return an error and do not o oad any containers to the port.







12 shipping:unload_ship(shipping:shipco(), 1, [2,16,18]).




The given conatiners are not all on the same ship...




error




13 shipping:unload_ship(shipping:shipco(), 1, [14,2]).




{ok,#shipping_state{




ships = [#ship{id = 1,name = "Santa Maria",container_cap = 20},







5






#ship{id = 2,name = "Nina",container_cap = 20},




#ship{id = 3,name = "Pinta",container_cap = 20},




#ship{id = 4,name = "SS Minnow",container_cap = 20},




#ship{id = 5,name = "Sir Leaks-A-Lot",container_cap = 20}],




containers = [...], %% not shown




ports = [#port{id = 1,name = "New York",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200},




#port{id = 2,name = "San Francisco",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200},




#port{id = 3,name = "Miami",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200}],




ship_locations = [{1,’B’,1},




{1,’A’,3},




{3,’C’,2},




{2,’D’,4},




{2,’B’,5}],




ship_inventory = #{1 = [15,9,6], %% removed from here




2 = [1,3,4,13],




3 = [],




4 = [2,8,11,7],




5 = [5,10,12]},

port_inventory = #{1 = [16,17,18,19,20,14,2], %% placed here




2 = [21,22,23,24,25],




3 = [26,27,28,29,30]}}}




set sail(Shipping State, Ship ID, fPort ID, Dockg)



This method changes the given ship’s port and dock location to the new port and dock location. Be sure to check whether or not the new port and dock is occupied. If it is, then return the atom error.







14 shipping:set_sail(shipping:shipco(), 4, {2,’B’}).




error




15 shipping:set_sail(shipping:shipco(), 4, {3,’A’}).




{ok,#shipping_state{




ships = [#ship{id =
1,name =
"Santa Maria",container_cap = 20},
#ship{id =
2,name =
"Nina",container_cap = 20},
#ship{id =
3,name =
"Pinta",container_cap = 20},
#ship{id =
4,name =
"SS Minnow",container_cap = 20},
#ship{id =
5,name =
"Sir Leaks-A-Lot",container_cap = 20}],
containers = [...],
%% not shown
ports = [#port{id =
1,name =
"New York",
docks = [’A’,’B’,’C’,’D’],
container_cap
= 200},
#port{id =
2,name =
"San Francisco",
docks = [’A’,’B’,’C’,’D’],
container_cap
= 200},



6






#port{id = 3,name = "Miami",




docks = [’A’,’B’,’C’,’D’],




container_cap = 200}],




ship_locations = [{1,’B’,1},




{1,’A’,3},




{3,’C’,2},




{3,’A’,4}, %% new port and dock




{2,’B’,5}],




ship_inventory = #{1 = [14,15,9,2,6],




2 = [1,3,4,13],




3 = [],




4 = [2,8,11,7],




5 = [5,10,12]},




port_inventory = #{1 = [16,17,18,19,20],




2 = [21,22,23,24,25],




3 = [26,27,28,29,30]}}}







Your Task



Your task is to complete all the functions in shipping.erl as mentioned above. You DO NOT have to touch shipping.hrl. Some example modules to help you complete some of these functions would be the lists and maps modules.




Submission Instructions



Submit a le hw1 Surname.zip (where Surname should be replaced by your surname) through Canvas containing all the les included in the stub but where all required operations have been implemented.



































































7

More products