1

Is there a way to limit the number of assignments for a specific value of a variable?

I'm writing a schedule problem using Choco [4.0.1] with courses, teachers and timeslots. The same teacher can teach many courses and I need to define the amount of courses he/she could teach. I was thinking about counting the number of timeslots, like Teacher B can work 12 hours (12 timeslots), so I could post a constraint like arithm() to ensure that. Any ideas?

[Updated]

Here is some code:

IntVar [] teachers;
IntVar [][] teacherTimeslots;
IntVar [] courses;

For each course there is a teacher and Ntimeslots, so Teacher A can teach {1,2,3,4} and teacher B {3,4,5} and each course has 4 hours per week. Now imagine that A can work 12 hours. I'd like to limit A to teach only 12 hours (3 courses) while B would teach the remaining courses;

I cannot say how many courses has a teacher but how many he/she could teach, so I'm using

Tuples tuples = new Tuples(true);
tuples.add(1, 1);
...
tuples.add(2, 5);
model.table(teacher, course, tuples).post();

constraint to ensure that he/she is able to teach certain courses.

teacherTimeslots is filled with all possible timeslots and I use model.allDifferent(teacherTimeslots[teacher]).post() to keep each teacher timeslot unique.

My courses are fixed so

course[0] = 1;
...
course[4] = 5;

I thought about getting all timeslots for a teacher but when I'm building my model there is no timeslot selected, so I' get all possibilities.

Sean Bright
  • 118,630
  • 17
  • 138
  • 146
psi_
  • 65
  • 5
  • 1
    Can you share any code with us? If not, then your question might be too broad and you should attempt something first. – Tim Biegeleisen Jan 12 '17 at 16:11
  • Just assign the courses to each time slot and if anything had been assigned previously act accordingly, i.e. allow only one course per timeslot and teacher and you should be good to go. – Thomas Jan 12 '17 at 16:15
  • @TimBiegeleisen I've put some code on my question, could you take a look? – psi_ Jan 12 '17 at 18:40

1 Answers1

0

Well, here is my solution. I've created a Timeslot class to represent a lecture, containing 1 teacher, 1 course, m rooms and n slots (m=n), so in my model there is a collection of timeslot objects for all courses. I used model.count(...) to check how many timeslots has a teacher and I put zero as lower bound because a teacher can be chosen or not and twelve times as upper bound.

List<IntVar> teachersTimeslotList = new ArrayList<IntVar>();

List<IntVar> teachersList = new ArrayList<IntVar>();

for (int i = 0; i < timeslots.size(); i++) {

    for (int j = 0; j < timeslots.get(i).getTimeslots().size(); j++) {

        IntVar timeslot = timeslots.get(i).getTimeslots().get(j);
        IntVar teacher = timeslots.get(i).getTeacher();

        IntVar sumTeacher = model.intVar("sumTeacher", 0, 100000);

        teachersList.add(teacher);

        model.sum(new IntVar[]{model.intScaleView(timeslot, 1000), teacher}, "=", sumTeacher).post();

        teachersTimeslotList.add(sumTeacher);
    }
}

for (int i = 0; i < teacheresId.length; i++) {
    model.count(teacheresId[i], teachersList.toArray(new IntVar[teachersList.size()]), model.intVar(0, 12)).post();
}

model.allDifferent(teachersTimeslotList.toArray(new IntVar[teachersTimeslotList.size()]), "NEQS").post();

I don't know if this is an optimized solution but at the moment it's working for me and even resolution time was improved by coding this. Thanks!

psi_
  • 65
  • 5