Above and beyond, A critique request
Vincent Davis
vincent at vincentdavis.net
Sun Feb 15 19:29:09 EST 2009
So I am new to python and not much of a programmer. Mostly program using
statistical packages. I been working on a project to simulate the
medical residency match for about 2 weeks. I don't know any python
programmers so I would greatly appreciate any comment or suggestions you may
have to improve my programing skills. My program does work but I have no
comparison or experience to know if I have used "best practices". Suggestion
may be broad or specific all input is appreciated. I have
already benefited from the group and am appreciative of it.
Due to python-list at python.org limited post size I have only included the
final step. If someone is willing to review my complete program ~250 lines
it can be downloaded at http://vincentdavis.org/match
In word this is what it does.
- Creates a population of institutions and applicants, quality and score
are generated for each by drawing from a random dist.
- The is an observation error, that is the institutions do not see the
true applicant score and the applicant do not see the true institution
quality.
- applicants apply to institution that are near there ability
- institutions reject if applicant is not qualified
- applicant rank the institutions 0 best large values worse
- institutions rank applicants
- The Match is best described here
http://www.nrmp.org/res_match/about_res/algorithms.html
I have a lot of print functions mostly to track what was going on during
development.
Thanks
Vincent Davis
720-301-3003
# NumApp is the number of applicants
# NumInst is the number of institutions
# ari is a list of institutions rank by each applicant, [[5, 6, 2], [8, 3,
6, 2], [......... So applicant 1 ranked institution 2 first and institution
6 second, applicant 2 ranked institution 6 third
# ira institutions ranking of applicants same format as ari but from the
institutions perspective
# iram is a "matrix" version of ira, rows are applicants, colums are
institutions values represent the rank, that is the rank the institution
gave the applicant
# I think that is all that is used in this part that is not defined below.
#### Starts here ####
app_match = [None]*NumApp # the list of institution each applicant is
matched to
try_list = [] # makes a copy of ari
try_list.extend(ari) # makes a copy of ari
inst_match=[[] for x in range(NumInst)] # this is a list of applicants each
Institution accepted
#get list of applicants not matched
notmatched = [x for x in range(NumApp) if (app_match[x] == None and
len(try_list[x]) > 0)]
print 'notmatched', notmatched
print 'len(notmatched)', len(notmatched)
while len(notmatched) > 0:
for x in notmatched:
try_inst = try_list[x][0] # try this institution
try_rank = ari[x].index(try_inst) # try_inst is ranked --
app_ranked = iram[x][try_inst] # this is what try_inst ranked the
applicant
print 'Try to match Applicant', x, ' to rank', try_rank, 'at inst',
try_inst
print 'Who is in inst', try_inst, inst_match[try_inst]
print 'Institution', try_inst, 'ranked applicant', x, app_ranked
ranklist = [iram[i][try_inst] for i in inst_match[try_inst]] # this
is the rank of each applicant matched to try_inst
if len(ranklist) > 0:
max_rank = max(ranklist) # the max rank value "lowest rank"
max_app = inst_match[try_inst][ranklist.index(max_rank)] # the
applicant corresponding to max_rank
if len(inst_match[try_inst]) < NumAccept : #Does the institution
have an empty spot.
print 'Institution', try_inst, 'has an empty spot for', x
inst_match[try_inst].append(x) # Add to institutions list
print x, 'in now in', try_inst, 'with', inst_match[try_inst]
app_match[x] = try_inst # assign inst to app
try_list[x].remove(try_inst) # remove the institution so it is
not tried later
elif (len(inst_match[try_inst]) == NumAccept and app_ranked <
max_rank) :
print 'Applicant',x , 'is ranked', app_ranked, 'which is higher
than', max_app, 'who was ranked', max_rank
print 'so applicant', x, 'bumps', max_app
app_match[max_app] = None
inst_match[try_inst].remove(max_app)
app_match[x] = try_inst
inst_match[try_inst].append(x)
try_list[x].remove(try_inst) # remove the institution so it is
not tried later
elif (len(inst_match[try_inst]) == NumAccept and iram[x][try_inst] >
max_rank) :
print 'Applicant',x , 'is ranked', app_ranked, 'which is Lower
than', max_app, 'who was ranked', max_rank
print 'therefor', x, 'does not match at', try_inst
try_list[x].remove(try_inst) # remove the institution so it is
not tried later
elif (len(inst_match[try_inst]) > NumAccept) :
print '#################### to many matched to institution, fix
this, some thing is broke ###################'
else:
print '#################### fix this, some thing is broke
###################'
print 'finished with applicant', x
notmatched = [x for x in range(NumApp) if (app_match[x] == None and
len(try_list[x]) > 0)]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090215/eb026281/attachment.html>
More information about the Python-list
mailing list