Decide to assign a laboratory with Python (fiction)

Let's organize the situation

Laboratory name

#Laboratory name
labs = list("ABCDEFG")
labs
['A', 'B', 'C', 'D', 'E', 'F', 'G']

Minimum number of assignments for each laboratory

#Minimum number of assignments for each laboratory
min_assign = [2 for x in range(len(labs))]
min_assign
[2, 2, 2, 2, 2, 2, 2]

Maximum number of assignments in each laboratory

#Maximum number of assignments in each laboratory
max_assign = [4 for x in range(len(labs))]
max_assign
[4, 4, 4, 4, 4, 4, 4]

Student ID

#Student ID
n_students = 20
students = [x + 1 for x in range(n_students)]
students
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Student grade ranking

#Student grade ranking
import random
grades = [x + 1 for x in range(n_students)]
random.shuffle(grades)
grades
[9, 4, 13, 14, 7, 17, 5, 2, 10, 1, 19, 16, 18, 20, 3, 15, 12, 6, 8, 11]

Laboratory assignment order survey

#Laboratory assignment order survey
questionnaire = []
for x in range(n_students):
    hope = [x + 1 for x in range(len(labs))]
    random.shuffle(hope)
    if x > 0 and random.random() < 0.1: #Make people who have the same desired order
        hope = questionnaire[x - 1]
    questionnaire.append(hope)
questionnaire
[[2, 7, 3, 5, 4, 6, 1],
 [2, 6, 1, 7, 3, 4, 5],
 [4, 7, 6, 5, 2, 1, 3],
 [6, 3, 2, 4, 1, 7, 5],
 [2, 5, 4, 3, 7, 1, 6],
 [3, 7, 2, 6, 5, 1, 4],
 [1, 7, 3, 2, 6, 5, 4],
 [4, 1, 7, 3, 6, 2, 5],
 [7, 6, 4, 3, 1, 2, 5],
 [5, 3, 4, 7, 1, 6, 2],
 [3, 2, 7, 4, 6, 1, 5],
 [6, 7, 5, 4, 1, 2, 3],
 [6, 7, 5, 4, 1, 2, 3],
 [2, 3, 7, 5, 1, 4, 6],
 [7, 3, 5, 1, 6, 4, 2],
 [6, 5, 7, 4, 3, 2, 1],
 [2, 3, 1, 5, 6, 4, 7],
 [4, 2, 1, 6, 7, 5, 3],
 [6, 2, 7, 4, 1, 3, 5],
 [2, 5, 4, 6, 1, 3, 7]]

Randomly decide the assignment

#Randomly decide the assignment
assignment = [[] for x in range(len(labs))]
rand_stu = [x for x in range(n_students)]
random.shuffle(rand_stu)
lab = 0
for stu in rand_stu:
    assignment[lab % len(labs)].append(stu)
    lab += 1
assignment
[[8, 13, 6],
 [1, 5, 17],
 [18, 16, 3],
 [9, 2, 11],
 [12, 10, 14],
 [0, 4, 15],
 [19, 7]]

Who wants what as a result of assignment

def resulted(assignment, questionnaire):
    return [[questionnaire[stu][i] for j, stu in enumerate(lab)] for i, lab in enumerate(assignment)]
resulted(assignment, questionnaire)
[[7, 2, 1], [6, 7, 2], [7, 1, 2], [7, 5, 4], [1, 6, 6], [6, 1, 2], [7, 5]]

The size of dissatisfaction as a whole

def fuman(assignment, questionnaire):
    ary = resulted(assignment, questionnaire)
    return sum([sum(x) - len(x) for x in ary])
fuman(assignment, questionnaire)
65

Prioritize those with good grades (do not consider the minimum number of assignments)

#How to prioritize the desired laboratories from those with good grades (without considering the minimum number of assignments)
assignment = [[] for x in range(len(labs))]
for i in range(len(students)):
    stu = grades.index(i + 1) #Student with grade i
    que = questionnaire[stu] #Grade ranking i-th student's desired ranking
    for j in range(len(labs)):
        lab = que.index(j + 1) #Laboratory of desired order j
        if len(assignment[lab]) < max_assign[lab]:
            assignment[lab].append(stu)
            break
assignment
[[6, 13],
 [7, 10],
 [1, 17, 16, 3],
 [14],
 [9, 18, 8, 19],
 [4, 2, 11, 5],
 [0, 15, 12]]

Who wants what as a result of assignment

resulted(assignment, questionnaire)
[[1, 2], [1, 2], [1, 1, 1, 2], [1], [1, 1, 1, 1], [1, 1, 2, 1], [1, 1, 3]]

The size of dissatisfaction as a whole

fuman(assignment, questionnaire)
6

Priority is given to those with good grades (consider the minimum number of assignments)

First fill in the minimum number of assignments

#How to prioritize the desired laboratories from those with good grades (fill in the minimum number of assignments first)
assignment = [[] for x in range(len(labs))]
assigned_students = []
for i in range(len(students)):
    stu = grades.index(i + 1) #Student with grade i
    que = questionnaire[stu] #Grade ranking i-th student's desired ranking
    for j in range(len(labs)):
        lab = que.index(j + 1) #Laboratory of desired order j
        if len(assignment[lab]) < min_assign[lab]:
            assignment[lab].append(stu)
            assigned_students.append(stu)
            break
assignment
[[6, 19], [7, 16], [1, 17], [14, 3], [9, 18], [4, 8], [0, 2]]
assigned_students
[9, 7, 14, 1, 6, 17, 4, 18, 0, 8, 19, 16, 2, 3]

Assign unassigned students

#How to prioritize the desired laboratories from those with good grades (assign unassigned students)
for i in range(len(students)):
    stu = grades.index(i + 1) #Student with grade i
    que = questionnaire[stu] #Grade ranking i-th student's desired ranking
    if stu in assigned_students:
        continue
    for j in range(len(labs)):
        lab = que.index(j + 1) #Laboratory of desired order j
        if len(assignment[lab]) < max_assign[lab]:
            assignment[lab].append(stu)
            assigned_students.append(stu)
            break
assignment
[[6, 19, 13],
 [7, 16],
 [1, 17],
 [14, 3],
 [9, 18, 11, 12],
 [4, 8, 5, 10],
 [0, 2, 15]]

Who wants what as a result of assignment

resulted(assignment, questionnaire)
[[1, 2, 2], [1, 3], [1, 1], [1, 4], [1, 1, 1, 1], [1, 2, 1, 1], [1, 3, 1]]

The size of dissatisfaction as a whole

fuman(assignment, questionnaire)
10

As a matter of fact

It's difficult because there are various laboratories where popularity is concentrated and laboratories that are not popular at all (´ ・ ω ・ `)

Recommended Posts

Decide to assign a laboratory with Python (fiction)
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
Try to draw a life curve with python
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
Steps to create a Twitter bot with python
Try to make a dihedral group with Python
I want to write to a file with Python
A layman wants to get started with Python
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python
[Python] How to draw a line graph with Matplotlib
Try to make a command standby tool with python
I tried to draw a route map with Python
I want to work with a robot in python.
From buying a computer to running a program with python
I tried to automatically generate a password with Python3
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
[Python] How to create a 2D histogram with Matplotlib
I want to run a quantum computer with Python
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
Connect to BigQuery with Python
A road to intermediate Python
Connect to Wikipedia with Python
Post to slack with Python 3
Make a fortune with Python
Switch python to 2.7 with alternatives
Write to csv with Python
Create a directory with python
How to convert an array to a dictionary with Python [Application]
I made a package to filter time series with python
I wrote a program quickly to study DI with Python ①
Probably the easiest way to create a pdf with Python3
Experiment to make a self-catering PDF for Kindle with Python
[python] A note when trying to use numpy with Cython
Post a message to Google Hangouts Chat with a thread (Python)
How to build a python2.7 series development environment with Vagrant
[Python] The first step to making a game with Pyxel
Create a message corresponding to localization with python translation string
[Python] What is a with statement?
How to write a Python class
Solve ABC163 A ~ C with Python
Python: How to use async with
Operate a receipt printer with python
A python graphing manual with Matplotlib.
Link to get started with python
[Python] Write to csv file with Python
Let's make a GUI with python.
Create folders from '01' to '12' with python
Try to operate Facebook with Python
Solve ABC166 A ~ D with Python
Output to csv file with Python
Create a virtual environment with Python!
I made a fortune with Python.
Convert list to DataFrame with python
MP3 to WAV conversion with Python
To do tail recursion with Python2
Building a virtual environment with Python 3