/
activityselection.py
executable file
·182 lines (139 loc) · 4.61 KB
/
activityselection.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/python
import sys
import getopt
from collections import namedtuple
import math
filename = sys.argv[1]
#print "filename is:", filename, "\n"
try:
file = open(filename, "r");
except IOError:
print 'Can\'t open file for reading'
sys.exit(0)
firstline = file.readline(); # read first line
#split the line read into the variables.
n, k = map(int, firstline.split(" "))
#declare a named tuple to represent our activities
activity = namedtuple('activity', ['i', 's', 'f', 'p'])
activitylist = [] #list of activities
a = activity(0,0,0,0)
activitylist.append(a)
# iterate through n lines and read them to a namedtuple for each activity
for i in range(n):
oneline = file.readline()
i,s,f,p = map(int, oneline.split(" "))
a = activity(i,s,f,p)
activitylist.append(a)
#for a in activitylist:
# print a.i, a.s, a.f,a.p
def isCompatible(a,b):
""" isCompatible:
compare two activities and return true if start time
and finish times are compatible. The activity being included is a
and the activity being considered to be included or not is b
"""
if(b.f <= a.s):
return True
else:
return False
def isCharitable(a):
""" isCharitable:
returns true if the activity is a charity event
"""
if(a.p == 0):
return True
else: return False
# isCharitable definition above
def max(x,y):
""" max:
returns maximum of the two values
"""
if ( x > y): return x
elif (y >= x): return y
else: # one of them is negInf
if (math.isnan(x)):
if(math.isnan(y)):
return x
else: return y
else: return x
# code for asp1 problem
asp1 = []
asp1.append(0)
#print asp1[0]
def asp1MaxProfit(asp1):
""" asp1MaxProfit
returns the maximum profit from the asp1 array already computed
"""
maximum = 0
for ap in asp1:
maximum = max(maximum, ap)
return maximum
# for a in activitylist: #iterate through the activities in increasing order
# if (isCharitable(a)):
# maxAsp = 0
# for j in range(0,a.i):
# if(isCompatible(a,activitylist[j])):
# maxAsp = max(maxAsp, asp1[j])
# asp1.append(maxAsp) #maxAsp + 0 appended for charitable events
# if (not isCharitable(a)): # a is a profitable event
# maxAsp = 0
# for m in range(0,a.i):
# if (isCompatible(a, activitylist[m]) and isCharitable(activitylist[m])):
# maxAsp = max(maxAsp, asp1[m])
# asp1.append(maxAsp + a.p)
# print asp1MaxProfit(asp1)
# print asp1
asp1new = [0 for i in range(0,n+1)]
for i in range(1,n+1):
if (isCharitable(activitylist[i])):
p = 0
for j in range(i-1,-1,-1):
if(isCompatible(activitylist[i],activitylist[j])):
p = max(p,asp1new[j])
asp1new[i] = p + activitylist[i].p
if(not isCharitable(activitylist[i])):
p = 0
for j in range(i-1,-1,-1):
if(isCompatible(activitylist[i],activitylist[j]) and isCharitable(activitylist[j])):
p = max(p,asp1new[j])
asp1new[i] = p + activitylist[i].p
#print asp1new
print asp1MaxProfit(asp1new)
#Activity selection 2 solution
negInf = float('nan')
#print "Asp2 :-------"
#print n, k
asp2 = [ [ 0 for j in range(k+1)] for i in range(n+1)] # base case when i = 0, j = 0
#print asp2
for j in range(1,k+1): # base case when i = 0 and j > 0
asp2[0][j] = negInf
#print asp2
# base case when j = 0
for i in range(1,n+1):
q = asp2[i-1][0]
for m in range(i-1,-1,-1):
if(isCompatible(activitylist[i],activitylist[m])):
q = max(q,asp2[m][0] + activitylist[i].p)
break
asp2[i][0] = q
#print asp2
#print "base cases over"
for i in range(1,n+1):
for j in range(1,k+1):
if (isCharitable(activitylist[i])):
q = asp2[i-1][j] # don't include current activity. prev act gives best sol
for m in range(i-1,-1,-1): # count downward till 1 decrement by one
if (isCompatible(activitylist[i],activitylist[m])): # if a_k is compatible with a_i
q = max(q, asp2[m][j-1])
break
asp2[i][j] = q
if (not isCharitable(activitylist[i])): # if it's a for_profit activity
q = asp2[i-1][j]
for m in range(i-1,-1,-1):
if (isCompatible(activitylist[i],activitylist[m])):
q = max(q,asp2[m][j] + activitylist[i].p)
break
asp2[i][j] = q
#print asp2
print asp2[n][k]
#todo add when 0 charitable event needs to be scheduled