Matching Problem Question (C#)

Status
Not open for further replies.

sryan2k1

Ars Legatus Legionis
46,882
Subscriptor++
I'm writing a scheduling app for my girlfriend who is a teacher to help get the kids to do things with random students rather then the same pair every time.


To simplify it, I have four time slots, A, B, C, D. In each time slot there are 10 activities that can be occupied, and 10 kids to fill them (not all the class will be doing the activities at the same time). She wants two kids per center. So for any given time slot, half of the centers will be occupied. All of that is easy up to this point.

Now for the part I can't quite wrap my brain around, each kid needs to do every center once per week (Each student gets 2 activities per day so 2 * 5 = All 10). The entire week is going to be generated at once, and I don't need to keep track of previous weeks. When I am figuring out where to put what kid, how can I ensure they end up doing all 10 things over the course of the week?


Any questions about how it's supposed to work please ask!


Thanks!


Edit: Of course I always think of more when I ask the question, but I could build a class (C#, not school class :p) for each student that keeps track if they have been assigned that activity (1-10) for the week. As far as student pairing, that should be as random as possible at the time of matching (seeding with the time is fine), I'm just not sure how to implement the actual scheduler to get all the kids in the activities
 

sryan2k1

Ars Legatus Legionis
46,882
Subscriptor++
I'll post some code later when I clean it up (the project got a little unwieldy quick)

I call it the Clint Ecker approach (it's what secret santa does)

Shuffle everything, if you get to a dead end. Reset it all, reset the RNG and try again. I'm seeing it finish in 5000-50000 attempts, takes about 15 seconds on a core2. I'm not too worried about it.
 

Alamout

Ars Legatus Legionis
27,285
Was your OP describing the actual parameters? If so, you might have an issue: student Alice can work with 9 other children (Bob, Charlie, etc) but she's going to do 10 things in a given week. So, Alice works with one other child twice. Maybe you do want to keep track of the weeks, so that Alice works with each of her classmates exactly 18 times every 9 weeks? Or maybe that's just too much equality. :D

If those parameters are correct, then the random aspect isn't that big a deal. I would generate all the pairs you're going to use--either randomly selecting the extra pair, or keeping track of it week to week. Permute the pairs, then iterate through the stations and time slots. Stick the next pair into the station/time slot unless it violates your constraints--if a child is already doing something, or they've already done it this week, or if five slots are being used, which should violate constraint 1 anyway.

If you violate a constraint, move to the next time slot or kick the pair to the back of the list. Given that you're exhausting the combinations of children and activities, I think it should work out without a lot of back-tracking.

Now I'm re-reading your post and I don't get how you have four time slots, but kids only do two things per day. Are there empty time slots?

The only other question is whether you want activities to be spread through the week pretty much randomly, or if it's okay to be pretty regimented--if you do Activity 1 in the morning then you always do Activity 7 in the afternoon. But that's a separate, not particularly complicated problem of generating which slots are used when.
 

Bengie25

Ars Praefectus
5,506
Subscriptor
If I understand this correctly, doesn't sound that hard.

FOREACH time slot, you want to pick a random 50% of the activities
FOREACH of these activities, you want to pick two random kids. Once a kid is picked for a timeslot this day, they may not be picked again. Once a kid if picked for an activity this week, they may not be picked again. A kid may not get picked more than twice per day. ie, your main program will be two loops, one in the other.

You could make a class for timeslots, activities, and students. Each class could keep track of usage info.

Now, each of these items can be "consumed". So, once they're picked or so many are picked, the same one or no more may be picked.

eg. For a given student, you will not re-pick them for the same activity more than once per week. You may not also pick a student more than twice per day. Also, due to the logical structer of the requirements, each student will be picked twice per day even though you don't explicity make sure that they will.

foreach(x in timeslot)
{
for(i=0;i<activities/2;i++)
{
pickrandomactivity(x)
}
}

pickrandomactivity(timeslot)
{
SetRandomActivityForTimeSlotThatHasNotBeenUsedToday()

PickFirstRandomStudentWhoHasNotBeenAssignedTwiceTodayOrInThisTimeSlotOrForThisActivity()
PickSecondRandomStudentWhoHasNotBeenAssignedTwiceTodayOrInThisTimeSlotOrForThisActivity()

}
 

MrTed

Smack-Fu Master, in training
95
What you are doing is is a classic example of a constraint satisfaction problem. In this situation you have a set of constraints C = {c1, c2, ... ck} (in your case how often to do activities, how may activities need to be done, etc.) and a bunch of variables you can assign values to V = {v1, v2, ..., vl} (in your case which student should be doing which activity on which day).

There are a number of ways to solve these problems but the easiest to implement, and most straight forward is to use backtracking search. The idea is simply to explore the entire space of possible solutions in a simple manner, assigning variables and checking if the constraints are satisfied at each step. If they are return the satisfying solution otherwise backtrack in the search. The most common way to do this is via a recursive function such along the lines of the following pseudocode:

Code:
bool BuildSolution(List<Student> students, Schedule sched)
{
    if(students.Count == 0 && sched.SatisfiesAllConstraints())
        return s;
    else
    {
        Student s = PickAndRemoveSomeStudent(students);
        foreach(Activity a in sched.AllAvailableActivities())
        {
            sched.AssignStudent(s, a);
            
            bool ok = BuildSolution(students, sched);
            if(ok)
                return true;
            
            sched.UnAssignStudent(s, a);
        }
        students.Insert(s);
        
        return false;
    }
}

I think this should get you going in the right direction. Now this is not really the fastest or cleverest solution possible, but it is simple and extensible. If there is interest I am happy to elaborate on some improvements or more interesting ways to solve the problem.
 

TheEternalVortex

Ars Praefectus
3,094
Subscriptor
MrTed":2ojougbq said:
What you are doing is is a classic example of a constraint satisfaction problem. In this situation you have a set of constraints C = {c1, c2, ... ck} (in your case how often to do activities, how may activities need to be done, etc.) and a bunch of variables you can assign values to V = {v1, v2, ..., vl} (in your case which student should be doing which activity on which day).

Yes, and you can always just use some free solver like http://lpsolve.sourceforge.net/5.5/ to solve it for you :).
 
Status
Not open for further replies.