54名学生分14间三床房6间两床房:偏好分配优化求助
班级旅行宿舍分配问题求助
我在解决班级旅行的宿舍分配问题时遇到了困难:
- 共有54名学生,需要分配到14间三床房和6间两床房
- 每名学生都有一个或多个希望同住的偏好对象,偏好数据格式为:
preferences = {'student1': [student2, student3], 'student2': [student3, student1], ...}
我已经计算出所有可能的三人互相偏好组合(triple match)和两人互相偏好组合(normal match),并且设想通过为不同质量的房间分配积分来寻找最优方案,积分规则如下:
- 优质三床房:6分
- 优质两床房:4分
- 中等三床房:5分
- 中等两床房:4分
- 偏差三床房:3分
- 差三床房:2分
- 极差三床房:1分
- 中等偏下两床房:1分
但我现在无从下手,不知道如何在不使用超级计算机的前提下实现这个方案。以下是我计算完匹配后写的代码:
students_with_room = [] real_rooms = [] exit_loop = False real_rooms = {'2A': [], '2B': [], '2C': [], '2D': [], '2E': [], '2F': [],'3A': [], '3B': [], '3C': [], '3D': [], '3E': [], '3F': [], '3G': [], '3H': [], '3I': [], '3J': [], '3K': [], '3L': [], '3M': [], '3N': [],} for student in students: if student not in students_with_room: if triple_matches[student] == None: for room in real_rooms: if real_rooms[room] == [] and '2' in room: if len(matches[student]) != 0: for i in range(len(matches[student])): if len(set([matches[student][i], student]).intersection(students_with_room)) == 0: real_rooms[room] = [matches[student][i], student] students_with_room = students_with_room + [matches[student][i]] students_with_room = students_with_room + [student] exit_loop = True break else: pass else: pass if exit_loop == True: break if real_rooms[room] == [] and '3' in room: pass exit_loop = False else: for room in real_rooms: if student not in students_with_room: if real_rooms[room] == [] and '3' in room: real_rooms[room] = [triple_matches[student][0], triple_matches[student][1], student] students_with_room = students_with_room + triple_matches[student] students_with_room = students_with_room + [student] else: pass for triple_match in triple_matches: already_done = False if triple_matches[student] == None: pass else: if student in students_with_room: already_done = True if not already_done: students_with_room.append(triple_matches[student][0]) real_rooms
内容的提问来源于stack exchange,提问作者ProProgrammee
相关产品推荐
相关产品推荐

