1
0
Fork 0
mirror of https://github.com/samsapti/bachelor-project.git synced 2024-11-29 08:17:53 +00:00

Make matchmaking work with cases as prefs

This commit is contained in:
Sam A. 2023-05-08 03:38:19 +02:00
parent 8960b9a69c
commit 35540af207
Signed by: samsapti
GPG key ID: CBBBE7371E81C4EA

View file

@ -6,9 +6,12 @@ from Compiler.util import *
from Compiler.oram import OptimalORAM from Compiler.oram import OptimalORAM
from Compiler.library import for_range, do_while, time, if_, print_ln, crash, print_str from Compiler.library import for_range, do_while, time, if_, print_ln, crash, print_str, break_loop
from Compiler.gs import OMatrix, OStack from Compiler.gs import OMatrix, OStack
# Optimizations
program.use_edabit(True)
class Matchmaker: class Matchmaker:
""" """
@ -27,19 +30,22 @@ class Matchmaker:
self.unpaired.append(patient, for_real) self.unpaired.append(patient, for_real)
def request_therapist(self, patient, therapist, for_real): def request_therapist(self, patient, therapist, for_real):
(requested_therapist,), free = self.paired_patients.read(therapist) experience = self.t_exps[therapist][0]
(old_patient,), free = self.paired_patients.read(therapist) # patient paired to therapist
paired = 1 - free paired = 1 - free
rank_patient = self.t_exps[therapist][patient] rank_patient = self.p_cases[patient][0]
(rank_requested_therapist,), worst_therapist = self.t_exps[therapist].read(paired*requested_therapist) rank_old_patient = self.p_cases[old_patient][0] * paired
leaving = self.int_type(rank_patient) < self.int_type(rank_requested_therapist) matches_exp = self.int_type(rank_patient) == self.int_type(experience)
if self.M < self.N: same_as_old = self.int_type(rank_patient) != self.int_type(rank_old_patient)
leaving = 1 - (1 - leaving) * (1 - worst_therapist) leaving = paired * matches_exp * same_as_old
print_str('therapist: %s, patient: %s, requested therapist: %s, worst therapist: %s, ',
*(x.reveal() for x in (therapist, patient, requested_therapist, worst_therapist))) print_str('therapist: %s, patient: %s, old patient: %s, ', # worst therapist: %s, ',
print_ln('rank patient: %s, rank requested therapist: %s, paired: %s, leaving: %s', *(x.reveal() for x in (therapist, patient, old_patient))) # , worst_therapist)))
print_ln('rank patient: %s, rank old patient: %s, paired: %s, leaving: %s',
*(x.reveal() for x in *(x.reveal() for x in
(rank_patient, rank_requested_therapist, paired, leaving))) (rank_patient, rank_old_patient, paired, leaving)))
self.unpair(requested_therapist, therapist, paired * leaving * for_real)
self.unpair(old_patient, therapist, paired * leaving * for_real)
self.pair(patient, therapist, (1 - (paired * (1 - leaving))) * for_real) self.pair(patient, therapist, (1 - (paired * (1 - leaving))) * for_real)
self.unpaired.append(patient, paired * (1 - leaving) * for_real) self.unpaired.append(patient, paired * (1 - leaving) * for_real)
@ -50,6 +56,7 @@ class Matchmaker:
else: else:
loop = for_range(n_loops) loop = for_range(n_loops)
init_rounds = n_loops / self.M init_rounds = n_loops / self.M
self.paired_therapists = \ self.paired_therapists = \
self.oram_type(self.N, entry_size=log2(self.N), self.oram_type(self.N, entry_size=log2(self.N),
init_rounds=0, value_type=self.basic_type) init_rounds=0, value_type=self.basic_type)
@ -72,40 +79,54 @@ class Matchmaker:
rounds.iadd(1) rounds.iadd(1)
time() time()
patient = self.unpaired.pop() patient = self.unpaired.pop()
pref = self.int_type(request_therapist[patient]) pref = self.int_type(p_cases[patient][0]) # patient suffers from pref
if self.M < self.N and n_loops is None: therapist = types.MemValue(self.int_type(0))
@if_((pref == self.M).reveal())
# Get index of next free therapist who has experience with pref
@for_range(self.N)
def _(i):
(_,), free = self.paired_patients.read(i)
@if_(((pref == t_exps[i][0]) * free).reveal())
def _():
therapist.write(self.int_type(i))
break_loop()
@if_((i == self.N).reveal())
def _(): def _():
print_ln('run out of acceptable therapists') print_ln('run out of acceptable therapists')
crash() crash()
request_therapist[patient] = pref + 1
self.request_therapist(patient, self.p_cases[patient][pref], True) request_therapist[patient] = therapist.read()
self.request_therapist(patient, therapist.read(), True)
print_ln('patient: %s, pref: %s, left: %s', print_ln('patient: %s, pref: %s, left: %s',
*(x.reveal() for x in (patient, pref, self.unpaired.size))) *(x.reveal() for x in (patient, pref, self.unpaired.size)))
return types.regint((self.unpaired.size > 0).reveal()) return types.regint((self.unpaired.size > 0).reveal())
print_ln('%s rounds', rounds) print_ln('%s rounds', rounds)
print_ln('\nPRINTING PAIRS\n')
@for_range(init_rounds) @for_range(init_rounds)
def _(i): def _(i):
types.cint(i).print_reg('ther') print_ln('patient %s : therapist %s', i, self.paired_therapists[i].reveal())
self.paired_patients[i].reveal().print_reg('pati') #print_ln('therapist %s : patient %s', i, self.paired_patients[i].reveal())
#types.cint(i).print_reg('ther')
#self.paired_patients[i].reveal().print_reg('pati')
def __init__(self, N, p_cases, t_exps, M=1, reverse=False, def __init__(self, p_cases, t_exps, N, M=None, reverse=False,
oram_type=OptimalORAM, int_type=types.sint): oram_type=OptimalORAM, int_type=types.sint):
self.N = N self.N = N
self.M = M self.M = N if M is None else M
self.p_cases = p_cases self.p_cases = p_cases
self.t_exps = t_exps self.t_exps = t_exps
self.reverse = reverse self.reverse = reverse
self.oram_type = oram_type self.oram_type = oram_type
self.int_type = int_type self.int_type = int_type
self.basic_type = int_type.basic_type self.basic_type = int_type.basic_type
print('match', N, M)
# Constants # Constants
PLAYERS = 3 PLAYERS = 3
MATCHING_SIZE = 10 MATCHING_SIZE = 50
p_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint) p_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint)
t_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint) t_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint)
@ -114,28 +135,18 @@ t_shares = Matrix(rows=PLAYERS, columns=MATCHING_SIZE, value_type=types.sint)
# The matrix is ordered as m[row:player][col:share] # The matrix is ordered as m[row:player][col:share]
@for_range(PLAYERS) @for_range(PLAYERS)
def _(i): def _(i):
p_index = MemValue(cint(0)) @for_range(2 * MATCHING_SIZE)
t_index = MemValue(cint(0)) def _(j):
index = j % MATCHING_SIZE
typ = sint.get_input_from(i)
@do_while @if_((typ == -100).reveal())
def _(): def _():
try: p_shares[i][index] = sint.get_input_from(i)
typ = cint.get_input_from(i)
@if_e(typ == -100) @if_((typ == -200).reveal())
def _(): def _():
p_shares[i][p_index.read()] = sint.get_input_from(i) t_shares[i][index] = sint.get_input_from(i)
p_index.iadd(1)
@else_
def _():
@if_(typ == -200)
def _():
t_shares[i][t_index.read()] = sint.get_input_from(i)
t_index.iadd(1)
return 1
except Exception:
return 0
# Add entire column together to reveal secret-shared input # Add entire column together to reveal secret-shared input
p_cases = OMatrix(N=MATCHING_SIZE, M=1, oram_type=OptimalORAM, int_type=types.sint) p_cases = OMatrix(N=MATCHING_SIZE, M=1, oram_type=OptimalORAM, int_type=types.sint)
@ -144,7 +155,10 @@ t_exps = OMatrix(N=MATCHING_SIZE, M=1, oram_type=OptimalORAM, int_type=types.sin
@for_range(MATCHING_SIZE) @for_range(MATCHING_SIZE)
def _(i): def _(i):
p_cases[i][0] = sum(p_shares.get_column(i)) p_cases[i][0] = sum(p_shares.get_column(i))
print_ln('p_case %s: %s', i, p_cases[i][0].reveal())
t_exps[i][0] = sum(t_shares.get_column(i)) t_exps[i][0] = sum(t_shares.get_column(i))
print_ln('t_exp %s: %s', i, t_exps[i][0].reveal())
mm = Matchmaker(MATCHING_SIZE, p_cases, t_exps) # Run algorithm
mm = Matchmaker(p_cases, t_exps, N=MATCHING_SIZE, M=1)
mm.match() mm.match()