forked from siv2r/kidney-exchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline.py
executable file
·260 lines (239 loc) · 8.17 KB
/
pipeline.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import csv
import os
import time
import datetime
import argparse
from solution import print_solution
from kidney_exchange import maximize_total_weight
from kidney_exchange import maximize_total_transplants
from kidney_exchange import maximize_pairwise_exchange
from precomputation import CyclePrecomputation
from jsonConversion import DataConvert
import sys
import shutil
# from kidney_exchange import kidney_exchange
import sys
sys.path.insert(1, '/home/shan/kidney_exchange')
names = []
edges = []
weight = {}
altruistic_donors = []
fnames = [
'Total Patients',
'Altruistic Donors',
'Maximum cycle size',
'Maximum chain size',
'Transplants',
'Constraint']
def fillexcel(
patients,
altruistic_donors,
max_cycle_length,
max_chain_length,
transplants,
Constraint):
with open('kidney.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([patients,
altruistic_donors,
max_cycle_length,
max_chain_length,
transplants,
Constraint])
def pipeline(file_name, option, max_cycle_length, max_chain_length, ilp):
x = datetime.datetime.now()
path = os.getcwd() + '/'
dirName = 'result'
if not os.path.exists(dirName):
os.mkdir(dirName)
else:
print("Directory ", dirName, " already exists")
destination = dirName
shutil.copy(file_name, destination)
graph = dirName + '/' + "graph"
f = open(graph, "w")
json_convert = DataConvert(json_file)
precomputation = CyclePrecomputation()
names, edges, weight, altruistic_donors = json_convert.convert_altruistic()
f.write(str(len(names)) +
" " +
str(len(altruistic_donors)) +
" " +
str(len(edges)))
for name in names:
f.write(str(name) + "\n")
for altruistic_donor in altruistic_donors:
f.write(str(altruistic_donor) + "\n")
for edge in edges:
f.write("[" + edge[0] + " " + edge[1] + "] " +
str(weight[tuple(edge)]) + "\n")
f.close()
# option = int(input("Choose option \n1.Maximize the number of effective pairwise exchange \n2.Maximize the total number of transplants\n3.Maximize the total number of backarcs\n4.Maximize the total weight\n5.Minimize the number of 3-way exchange\n6.Maximize the number of pairwise exchange "))
# Basically maximizing the total number of cycles which does not involve
# altruistic donor
start = time.time()
if option == 1:
cyclesAndChains = precomputation.findCyclesAndChains(
names, max_cycle_length, max_chain_length, altruistic_donors, edges)
end = time.time()
print(cyclesAndChains)
cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight)
solution_values = maximize_pairwise_exchange(
cyclesAndChains, names, dirName, edges, ilp)
transplants = print_solution(
1,
"Maximize the number of effective pairwise exchange",
max_cycle_length,
solution_values,
cyclesAndChains,
altruistic_donors,
edges,
cycleandchain_wt,
names,
dirName)
fillexcel(
len(names),
len(altruistic_donors),
max_cycle_length,
max_chain_length,
transplants,
"Maximize the number of effective pairwise exchange")
# Maximize the number of patients that get a kidney. This will involve
# altruistic donors as well
elif option == 2:
cyclesAndChains = precomputation.findCyclesAndChains(
names, max_cycle_length, max_chain_length, altruistic_donors, edges)
end = time.time()
cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight)
solution_values = maximize_total_transplants(
cyclesAndChains, names + altruistic_donors, dirName, ilp)
transplants = print_solution(
1,
"Maximize the total number of transplants",
max_cycle_length,
solution_values,
cyclesAndChains,
altruistic_donors,
edges,
cycleandchain_wt,
names,
dirName)
fillexcel(
len(names),
len(altruistic_donors),
max_cycle_length,
max_chain_length,
transplants,
"Maximize the total number of transplants")
# No of backarcs in 3-way exchange should be maximized(a 3-way exchange
# can contain more than one backarc.)
elif option == 3:
print("Yet to be done")
sys.exit()
# Maximize the total weight calculated by summing over all cycles and
# chains
elif option == 4:
cyclesAndChains = precomputation.findCyclesAndChains(
names, max_cycle_length, max_chain_length, altruistic_donors, edges)
end = time.time()
cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight)
solution_values = maximize_total_weight(
cyclesAndChains,
names +
altruistic_donors,
cycleandchain_wt,
dirName,
ilp)
transplants = print_solution(
1,
"Maximize the total weight",
max_cycle_length,
solution_values,
cyclesAndChains,
altruistic_donors,
edges,
cycleandchain_wt,
names,
dirName)
fillexcel(
len(names),
len(altruistic_donors),
max_cycle_length,
max_chain_length,
transplants,
"Maximize the total weight")
elif option == 5:
print("Yet to be done")
sys.exit()
# Maximize the number of pairwise exchange plus unused altruists:-
elif option == 6:
cyclesAndChains = precomputation.findCyclesAndChains(
names, 2, 1, altruistic_donors, edges)
end = time.time()
cycleandchain_wt = precomputation.findwt(cyclesAndChains, weight)
solution_values = maximize_total_transplants(
cyclesAndChains, names + altruistic_donors, dirName, ilp)
transplants = print_solution(
1,
"Maximize pairwise exchange",
2,
solution_values,
cyclesAndChains,
altruistic_donors,
edges,
cycleandchain_wt,
names,
dirName)
fillexcel(
len(names),
len(altruistic_donors),
max_cycle_length,
max_chain_length,
transplants,
"Maximize the number of pairwise exchange")
print("Runtime of the program is ", end - start)
if __name__ == "__main__":
option_string = "Choose option \n1.Maximize the number of effective pairwise exchange \n2.Maximize the total number of transplants\n3.Maximize the total number of backarcs\n4.Maximize the total weight\n5.Minimize the number of 3-way exchange\n6.Maximize the number of pairwise exchange "
parser = argparse.ArgumentParser()
my_parser = argparse.ArgumentParser()
my_parser.add_argument(
'-f',
'--file',
help='path to compatibility graph json file',
required=True,
default='genjson-0.json')
my_parser.add_argument(
'-s',
'--max_size',
action='store',
type=int,
help='maximum cycle size',
default=3)
my_parser.add_argument(
'-a',
'--altruistic',
action='store',
type=int,
help='number of altruistic donors',
default=0)
my_parser.add_argument(
'-o',
'--option',
action='store',
type=int,
help=option_string,
default=1)
my_parser.add_argument(
'-i',
'--ilp',
action='store',
type=bool,
help='using ILP vs LP',
default=True)
args = my_parser.parse_args()
json_file = args.file
max_size = args.max_size
altruistic_donors = args.altruistic
option = args.option
ilp = args.ilp
pipeline(json_file, option, max_size, altruistic_donors, ilp)