Currently, the Skeleton Program creates a delivery route for a company based on the order in which a company’s outlets have been created – the route starts with the oldest outlet, then goes to the second oldest outlet and so on until all the outlets have been included in the route.
The route produced specifies the order in which the outlets are visited when making deliveries – the longer the route, the higher the delivery cost for the company.
This question extends the Skeleton Program by using a greedy algorithm to try to reduce the total distance of the delivery route. A greedy algorithm is one that takes the choice which seems to be the best each time; it is not guaranteed to find the optimal solution.
Figure 6 shows how the greedy algorithm should work.
Figure 6
The greedy algorithm should:
1. start by adding the oldest outlet (the one in position 0 in Outlets) to the route
2. then find the company’s outlet that is nearest (has the smallest distance) to the outlet that was last added to the route, and which has not yet been added to the route, and add this to the end of the route
3. repeat step 2 until all the company’s outlets have been added to the route.
Figure 7 shows how the delivery cost is arrived at for the company Paltry Poultry which has four outlets. The outlet numbers (0–3) refer to the index of an outlet within the Outlets data structure for that company. This example may help you check your code works correctly.
Figure 7
The oldest outlet (outlet 0) is at coordinates (800,390) within the settlement; this will be the first outlet on the route.
From this outlet the closest outlet is outlet 2 which is at (820,370) so outlet 2 will be added as the second outlet on the route.
From outlet 2 the closest outlet not yet in the route is outlet 3 which is at (800,600) so outlet 3 will be added as the third outlet on the route.
From outlet 3 the closest outlet not yet in the route is outlet 1 which is at (400,390) so outlet 1 will be added as the fourth outlet on the route.
The correct order that the outlets should be visited is 0, 2, 3, 1
The delivery cost would be 6.96708
What you need to do
Task 1 Create a new method GetOrderedListOfOutlets in the Company class. The new method should return a list that represents the route created using the algorithm in Figure 6. Each outlet should be represented in the list by the index of its position in Outlets.
Task 2 Modify the CalculateDeliveryCost method in the Company class so that it uses the list returned by the new method GetOrderedListOfOutlets instead of the list returned by the method GetListOfOutlets.
Task 3 Test that the changes you have made work:
run the Skeleton Program
choose to use a normal size settlement
choose to use the default companies
choose the menu option to display the details of the companies.