-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps1a.py
155 lines (133 loc) · 5.24 KB
/
ps1a.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
###########################
# 6.0002 Problem Set 1a: Space Cows
# Name: Nick Link
# Collaborators: Me, Myself and I
# Time:
from ps1_partition import get_partitions
import time
import os
import pdb
import operator
#================================
# Part A: Transporting Space Cows
#================================
# Problem 1
def load_cows(filename):
"""
Read the contents of the given file. Assumes the file contents contain
data in the form of comma-separated cow name, weight pairs, and return a
dictionary containing cow names as keys and corresponding weights as values.
Parameters:
filename - the name of the data file as a string
Returns:
a dictionary of cow name (string), weight (int) pairs
"""
# TODO: Your code here
with open(filename) as f:
data = f.read().split('\n')
data2 = dict([[line.split(',')[0],int(line.split(',')[1])] for line in data])
data = [line.split(',') for line in data]
data = dict(data)
#print(data2)
for k in data.keys():
data[k] = int(data[k])
#print(data)
return data
# Problem 2
def greedy_cow_transport(cows,limit=10):
"""
Uses a greedy heuristic to determine an allocation of cows that attempts to
minimize the number of spaceship trips needed to transport all the cows. 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, add the largest cow that will fit
to the trip
2. Once the trip is full, begin a new trip to transport the remaining cows
Does not mutate the given dictionary of cows.
Parameters:
cows - a dictionary of name (string), weight (int) pairs
limit - weight limit of the spaceship (an int)
Returns:
A list of lists, with each inner list containing the names of cows
transported on a particular trip and the overall list containing all the
trips
"""
# TODO: Your code here
trips = []
cows = sorted(cows.items(), key=operator.itemgetter(1), reverse = True)
newTrip=['blank']
while (len(cows)>0 and newTrip!=[]):
newTrip=[]
spaceOnTrip = limit
for cow in cows:
if cow[1] <= spaceOnTrip:
cows.remove(cow)
spaceOnTrip -= cow[1]
newTrip.append(cow[0])
trips.append(newTrip)
if newTrip == []:
print('could not fill all of the space ships. Sorry cows')
return trips
# Problem 3
def brute_force_cow_transport(cows,limit=10):
"""
Finds the allocation of cows that minimizes the number of spaceship trips
via brute force. The brute force algorithm should follow the following method:
1. Enumerate all possible ways that the cows can be divided into separate trips
Use the given get_partitions function in ps1_partition.py to help you!
2. Select the allocation that minimizes the number of trips without making any trip
that does not obey the weight limitation
Does not mutate the given dictionary of cows.
Parameters:
cows - a dictionary of name (string), weight (int) pairs
limit - weight limit of the spaceship (an int)
Returns:
A list of lists, with each inner list containing the names of cows
transported on a particular trip and the overall list containing all the
trips
"""
"C:\Users\Nick\Documents\Code\Data Science PS1\ps1_partition.py"
minNum = len(cows)
bestParty=None
cow_partitions = get_partitions(cows.keys())
for party in cow_partitions:
partyGood = True
#pdb.set_trace()
for spaceShip in party:
shipSum=0
for cow in spaceShip:
shipSum += cows[cow]
if shipSum > limit: # ship don't fit!
partyGood = False
if partyGood and (len(party) < minNum):
bestParty = party
minNum = len(party)
return bestParty
# Problem 4
def compare_cow_transport_algorithms():
"""
Using the data from ps1_cow_data.txt and the specified weight limit, run your
greedy_cow_transport and brute_force_cow_transport functions here. Use the
default weight limits of 10 for both greedy_cow_transport and
brute_force_cow_transport.
Print out the number of trips returned by each method, and how long each
method takes to run in seconds.
Returns:
Does not return anything.
"""
# TODO: Your code here
os.chdir('C:\Users\Nick\Documents\Code\Data Science PS1') #where should this go?
cows = load_cows('ps1_cow_data.txt')
start = time.time()
trips_brute = brute_force_cow_transport(cows,limit=10)
end = time.time()
print('brute force took ' + str(end - start) + ' time and had ' + str(len(trips_brute)) + 'ships')
start = time.time()
trips_greedy = greedy_cow_transport(cows, 10)
end = time.time()
print('greedy alg took ' + str(end - start) + ' time and had ' + str(len(trips_greedy)) + 'ships')
print(trips_brute)
print(trips_greedy)
print(cows)
pass
compare_cow_transport_algorithms()