-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrp.py
87 lines (59 loc) · 1.88 KB
/
srp.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
An implementation of the Irving Algorithm for the Stable roommates problem by:
Vincent LIU - MAIN - Polytech Sorbonne 2017/18
Comments are in French.
"""
from etape1 import etape1
from etape2 import etape2
from etape3 import etape3
from interface import msgStage, msgStage2
from easygui import msgbox
def srp(pref,argPref):
"""
Solve the Stable rommates problem.
"""
###################
##### Etape 1 #####
###################
offer = etape1(pref,argPref)
msgStage(pref, "Preference list of the participants by descending order of preference.")
msgStage2(offer, "Phase 1: Best Offer after the stage 1 for each participant.")
# Test 1: pour savoir si tout le monde a recu une offre ou non
test1 = True
for e in offer:
if offer[e] == None:
test1 = False
break
if test1 == True:
###################
##### Etape 2 #####
###################
# Test 2: savoir si
stable_solution = 0 # On affiche les solutions stables
stage3 = 1 # On passe à l'étape 3
no_stable_solution = 2 # Il n'y a pas de solution stable
prefReduit = etape2(pref,argPref,offer)
msgStage(prefReduit,"Everyone got an offer, then go to Phase 2")
test2 = stable_solution
for e in prefReduit:
if len(prefReduit[e]) > 1:
###################
##### Etape 3 #####
###################
test2 = stage3
break
if len(prefReduit[e]) == 0:
test2 = no_stable_solution
break
if test2 == stable_solution:
msgStage(prefReduit, "Phase 2: this matching is stable.")
elif test2 == stage3:
prefReduit = etape3(prefReduit)
if prefReduit != None:
msgStage(prefReduit,"Phase 3: this matching is stable.")
elif test2 == 2:
msgbox("There is no stable solution for this problem.","Stable Roommates Problem","OK :(")
else:
msgbox("There is no solution stable.","Stable Roommates Problem","OK :(")