Algorithm for sorting people into rooms based on age and nationality

I'm working on program for the English Language school I work for. I'm not being paid, its just a kind of a hobby to improve / automate my work flow.

It's a residential school and one aspects I'm looking at automating is the way we allocate room to students, and although I don't want a full blown solution I was hoping someone could point me in the right direction… Suggestions of the way you might approach this or by suggesting algorithms to look at etc.

Basically at the school we have a whole bunch of different rooms ranging from singles to dormitories for 8 people. We get lots of different nationalities from all over the world, and we always try to maker sure each room has a mix of nationalities. Where there is more than one nationality we try to balance them. Age is also important, we always put students of a similar age together, while still trying to mix nationalities, and its unusual for us to have students sharing with more than two years between them.

I suppose more generically speaking, I am in interested in how to sort a given set of students based on two parameters to an optimal result with a few rules attached.

I hope I've explain clearly what I am trying to achieve… in a way it sounds really simple, but I've trying to think how to do it in a simple way, ie by sorting by nationality and then by age but it just doesn't cut it and I know there must be a better way of approaching this. When I do it “by hand” on an excel sheet it does feel quite intuitive.

Thank you to anyone who offers help / advice.


This is an interesting question but it's not easy to answer. Somehow it's connected with subdivsion and bin packing or the cutting-stock problem. You may want to look for a topological sort too. You can look for Drools a business logic platform that let you define such rules.


First of all you might find this interesting: Stable Room-mates Problem (wikipedia). Unfortunately it does not answer your question.

Try a genetic algorithm.

There are three main criteria for using a genetic algorithm:

  • ability to represent a solution as a mutable array. We can have an array of integers such that a[i] is the room for the ith student.

  • mutation of the state should produce predictable results. In our case this is true. Mutating the array will predictably shuffle students between the rooms.

  • easy to write a fast fitness function. Shouldn't be too hard to write a O(n) fitness function.

  • This is an interesting problem. I'll try writing some code with this approach and we'll see what happens.


    How about, you think of a room as something that repels students of a nationality it already has, and attracts students of a close age to what it already has. The closer the age to the average age, the more it attracts it, and the more guys of X nationality are in the room, the more if repels guys of X nationality.

    Then you would, for every new student to be added, iterate through each room and see which is the one that attracts it more. I guess if the room is empty you can set all forces to 0. Also, you would have a couple of constants that multiply each of both "forces" so you can calibrate it depending on how important is to have the same age against how important is to have different nationalities.

    链接地址: http://www.djcxy.com/p/55836.html

    上一篇: 如何在iOS应用中切换外观(或设计主题)?

    下一篇: 基于年龄和国籍将人分为房间的算法