简体   繁体   English

限制分配值 - Choco

[英]Limiting assignment value - Choco

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. 我正在使用Choco [4.0.1]编写课程,教师和时段的课程安排问题。 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. 我正在考虑计算时间段的数量,比如B老师可以工作12 hours (12 timeslots) ,所以我可以发布像arithm()这样的约束来确保这一点。 Any ideas? 有任何想法吗?

[Updated] [更新]

Here is some code: 这是一些代码:

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

For each course there is a teacher and N timeslots, so Teacher A can teach {1,2,3,4} and teacher B {3,4,5} and each course has 4 hours per week. 每个课程都有一个教师和N时间段,因此A老师可以教{1,2,3,4}和老师B {3,4,5},每个课程每周有4个小时。 Now imagine that A can work 12 hours. 现在想象A可以工作12个小时。 I'd like to limit A to teach only 12 hours (3 courses) while B would teach the remaining courses; 我想限制A只教12小时(3门课程)而B教授其余课程;

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. teacherTimeslots充满了所有可能的时间段,我使用model.allDifferent(teacherTimeslots[teacher]).post()来保持每个教师时段的独特性。

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. 我想为老师获得所有时间段但是当我建立我的模型时没有选择时间段,所以我得到了所有可能性。

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. 我创建了一个Timeslot课来代表一个讲座,包含1个老师,1个课程, m房间和n个插槽(m=n) ,所以在我的模型中有一系列所有课程的timeslot对象。 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. 我使用model.count(...)来检查有多少个时隙有一个老师,我把零作为下限,因为教师可以选择或不选择,12倍作为上限。

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! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM