Question
# Problem 3 def greedy_cow_transport(cows,oneTripWeightLimit=10, numberOfTrips=3): Uses a greedy heuristic to determine an allocation of cows that attempts to maxminize the intelligence (sum of
# Problem 3
def greedy_cow_transport(cows,oneTripWeightLimit=10, numberOfTrips=3): """ Uses a greedy heuristic to determine an allocation of cows that attempts to maxminize the intelligence (sum of IQs) of the cows that can be transported back to Aurock. The returned allocation of cows may or may not be optimal. The greedy heuristic should follow the following method:
1. As long as the current trip can fit another cow based on your critera, add the cow that will fit to the trip 2. Once the trip is full, begin a new trip to transport the remaining cows 3. Stop the trasportation after the number of limitted trips
Parameters: cows - a list of cow objects oneTripWeightLimit - weight limit of the spaceship (an int) for one trip numberOfTrips -- limit of number of trips for the whole transportation Returns: A tuple composed of two int values and a list. The first value presents the total sum of the IQs of the cows transported The second value presents the total sum of weights (tons) of the cows transported The third is a list of lists, with each inner list containing cows (cow objects) transported on a particular trip and the overall list containing all the trips """ trips = [] totalValue = 0 totalCost = 0 while numberOfTrips >0: numberOfTrips = numberOfTrips - 1 #call greedy to return one trip result and value result, oneTripValue, oneTripCost = greedy(cows, oneTripWeightLimit, Cow.getDensity #based on returned data from the greedy call, #update the trips #update the cows data #update totalValue by adding the sum of IQs of the cows transported by the single trip #update the totalCost by adding the sum of weights of the cows transported by the trip #and update the cows so that cows is used to keep track of the cows that haven't been transported #update another information you think necessary
return totalValue, totalCost, trips
cows = load_cows("a1_cow_data.txt") totalValue, totalCost, trips = greedy_cow_transport(cows) for i in range(len(trips)): print("Trip " + str(i) + ":") for j in range(len(trips[i])): print(trips[i][j]) print(" ") print("Total IQs transported = " + str(totalValue)) print("Total weights (tons) transported = " + str(totalCost))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started