初期研修医が8人いる某病院にて、彼(女)らが色々な診療科を回る順番を決めることになった。珍しいことに、そのうちの1人がプログラムで決めると言い張ったためパソコンによる厳正なマッチングが行われた。以下はプログラム片とコメント。
8人に対して選択肢も8種類ある。希望順に番号を書いてもらったが、少なすぎると1番から8番までの選択肢が網羅されない。とりあえず4ほど出しておけば、不具合なく決まるだろうということで実行した。
from itertools import combinations_with_replacement all = [[5,6,1,7], [3,6,2,1], [6,8,3,2], [1,6,2,4], [7,4,2,5], [5,6,2,1], [5,7,2,4], [2,5,6,8]] N_selection = 4 N_person = 8 ans = [] score = 0 weight = [6,4,2,1] for idx in combinations_with_replacement(range(N_selection), N_person): ans_tmp = [all[i][idx[i]] for i in range(N_person)] # check if ans_tmp is validate if len(set(ans_tmp)) == N_person: # represent the score when idx is chosen score_tmp = sum([weight[i] for i in idx]) if score_tmp > score: score = score_tmp ans = [ans_tmp] # several answers may exist elif score_tmp == score: ans.append(ans_tmp) print("ans: ", ans)
結果は以下。
ans: [[5, 3, 6, 1, 7, 2, 4, 8]]
後の3人が前5人の犠牲になる場合が全体の幸福度が高いようだ。とはいえそれぞれのプログラムの違いは回る順番でしかなく無事話し合いは終了した。