-
Notifications
You must be signed in to change notification settings - Fork 69
/
dorm.py
58 lines (42 loc) · 1.26 KB
/
dorm.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
import math
import random
dorms = ['Zeus', 'Athena', 'Hercules', 'Bacchus', 'Pluto']
prefs=[('Toby', ('Bacchus', 'Hercules')),
('Steve', ('Zeus', 'Pluto')),
('Karen', ('Athena', 'Zeus')),
('Sarah', ('Zeus', 'Pluto')),
('Dave', ('Athena', 'Bacchus')),
('Jeff', ('Hercules', 'Pluto')),
('Fred', ('Pluto', 'Athena')),
('Suzie', ('Bacchus', 'Hercules')),
('Laura', ('Bacchus', 'Hercules')),
('James', ('Hercules', 'Athena'))]
# [(0, 9), (0, 8), ... , (0, 1)]
domain = [(0, len(dorms)*2 - i - 1) for i in range(0, len(dorms)*2)]
def slotlist(l):
slots = []
for i in range(l): slots += [i, i]
return slots
def printsolution(vec):
slots = slotlist(len(dorms))
for i in range(len(vec)):
x = vec[i]
print prefs[i][0], dorms[slots[x]]
del slots[x]
def dormcost(vec):
cost = 0
slots = slotlist(len(dorms))
for i in range(len(vec)):
x = vec[i]
dorm = dorms[slots[x]]
pref = prefs[i][1]
if pref[0] == dorm: cost += 0
elif pref[1] == dorm: cost += 1
else: cost += 3
del slots[x]
return cost
if __name__ == '__main__':
printsolution([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
import optimization
s = optimization.geneticoptimize(domain, dormcost)
printsolution(s)