lolamontes69

Ch5 Ex5-Programming Collective Intelligence

Jul 7th, 2013
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.62 KB | None | 0 0
  1. """ Chapter 5 Exercise 5: Pairing students.
  2.  
  3.    Imagine if instead of listing dorm preferences, students had to express their preferences for a roommate. How would you represent solutions to pairing students? What would the cost function look like?
  4. """
  5.  
  6. import random as random
  7. import math as math
  8.  
  9. # The dorms, each of which has two available spaces
  10. dorms=['Zeus','Athena','Hercules','Bacchus','Pluto']
  11.  
  12. # People, along with their first and second choices of person
  13. prefs1=[('Toby', ('Jeff', 'Neil')),('Steve', ('Sarah', 'Laura')),('Andrea', ('Suzie', 'Laura')),
  14.         ('Sarah', ('Toby', 'Jeff')),('Dave', ('Neil', 'Fred')),('Jeff', ('Toby', 'Dave')),
  15.         ('Fred', ('Steve', 'Dave')),('Suzie', ('Andrea', 'Laura')),('Laura', ('Suzie', 'Andrea')),
  16.         ('Neil', ('Fred', 'Toby'))]
  17.  
  18.  
  19. # [(0,9),(0,8),(0,7),....(0,0)]]
  20. domain=[(0,(len(dorms)*2)-i-1) for i in range(0,len(dorms)*2)]
  21.  
  22. def printsolution(vec):
  23.     slots=[]
  24.     # Create two slots for each dorm
  25.     for i in range(len(dorms)): slots+=[i,i]
  26.  
  27.     # Loop over each students assignment printing out the results
  28.     print "\nStudent          Assigned Dorm"
  29.     print "------------------------------"
  30.     for i in range(len(vec)):
  31.         x=int(vec[i])
  32.         # Choose the slots from the remaining ones
  33.         dorm=dorms[slots[x]]
  34.         # Show the student and assigned dorm
  35.         print prefs[i][0],' '*(15-len(prefs[i][0])),dorm
  36.         # Remove this slot
  37.         del slots[x]
  38.  
  39. def partner_dormcost(vec):
  40.     cost = 0
  41.     count = 0   # 0 = first person in dorm, 1 = second person in dorm.
  42.     assigned=[] # A list to be populated with assigned dorms.
  43.     slots=[]
  44.     for i in range(len(dorms)): slots+=[i,i]
  45.  
  46.     # Loop over each students assignment and append to assigned.
  47.     for i in range(len(vec)):
  48.         x=int(vec[i])
  49.         assigned.append(slots[x])
  50.         del slots[x]
  51.  
  52.     # Cost the dorm assignment based on the assigned vs the preferred.
  53.     for i in range(10):
  54.         if count == 0:
  55.             if prefs1[assigned[i+1]][0] in prefs1[assigned[i]][1][0]: cost+=0   # If first choice of sleeping partner
  56.             elif prefs1[assigned[i+1]][0] in prefs1[assigned[i]][1][1]: cost+=1 # If second choice of sleeping partner
  57.             else: cost+=3                                                       # If not a chosen sleeping partner
  58.             count += 1
  59.         else:
  60.             if prefs1[assigned[i-1]][0] in prefs1[assigned[i]][1]: cost+=0      # Same as above for the second dorm slot
  61.             elif prefs1[assigned[i-1]][0] in prefs1[assigned[i]][1][1]: cost+=1
  62.             else: cost+=3
  63.             count = 0
  64.     return cost
Advertisement
Add Comment
Please, Sign In to add comment