1
  1. [These are the outputs] [1]: https://i.stack.imgur.com/aVepW.png
  1. constraints.py
from optapy import constraint_provider, get_class
from optapy.types import Joiners, HardSoftScore
from domain import TimeTable, Lesson, Room
from datetime import datetime, date, timedelta

TimeTableClass = get_class(TimeTable)
LessonClass = get_class(Lesson)
RoomClass = get_class(Room)

today = date.today()


def within30Mins(lesson1, lesson2):
    between = datetime.combine(today, lesson1.timeslot.endTime) - datetime.combine(today, lesson2.timeslot.startTime)
    return timedelta(minutes=0) <= between <= timedelta(minutes=30)
  1. 1-Hard constraints and Soft constraints

    @constraint_provider def defineConstraints(constraintFactory): return [

        roomConflict(constraintFactory),
        teacherConflict(constraintFactory),
        #studentGroupConflict(constraintFactory),
    
    
        2- Soft constraints
        teacherRoomStability(constraintFactory),
        teacherTimeEfficiency(constraintFactory),
        #studentGroupSubjectVariety(constraintFactory),
    
       # curriculum_needs_to_be_met(constraintFactory),
    ]
    
  2. A room can accommodate at most one lesson at the same time.

    def roomConflict(constraintFactory):

    return constraintFactory \
            .fromUniquePair(LessonClass,
            # ... in the same timeslot ...
                [Joiners.equal(lambda lesson: lesson.timeslot),
            # ... in the same room ...
                Joiners.equal(lambda lesson: lesson.room), 
                Joiners.equal(lambda lesson: lesson.timeslot.dayOfWeek)])\
            .penalize("Room conflict", HardSoftScore.ONE_HARD)
    
  3. A teacher can teach at most one lesson at the same time.

    def teacherConflict(constraintFactory):

    return constraintFactory \
                .fromUniquePair(LessonClass,
                        [Joiners.equal(lambda lesson: lesson.timeslot),
                        Joiners.equal(lambda lesson: lesson.teacher)]) \
                .penalize("Teacher conflict", HardSoftScore.ONE_HARD)
    
  4. A student can attend at most one lesson at the same time.

    def studentGroupConflict(constraintFactory):

    return constraintFactory \
            .fromUniquePair(LessonClass,
                [Joiners.equal(lambda lesson: lesson.timeslot),
                Joiners.equal(lambda lesson: lesson.studentGroup)]) \
            .penalize("Student group conflict", HardSoftScore.ONE_HARD)
    
  5. A teacher prefers to teach in a single room.

    def teacherRoomStability(constraintFactory):

    return constraintFactory \
                .fromUniquePair(LessonClass,
                        [Joiners.equal(lambda lesson: lesson.teacher)]) \
                .filter(lambda lesson1, lesson2: lesson1.room != lesson2.room) \
                .penalize("Teacher room stability", HardSoftScore.ONE_SOFT)
    
  6. ** A teacher prefers to teach sequential lessons and dislikes gaps between lessons.**

    def teacherTimeEfficiency(constraintFactory):

    return constraintFactory.from_(LessonClass)\
                .join(LessonClass, [Joiners.equal(lambda lesson: lesson.teacher),
                        Joiners.equal(lambda lesson: lesson.timeslot.dayOfWeek)]) \
                .filter(within30Mins) \
                .reward("Teacher time efficiency", HardSoftScore.ONE_SOFT)
    
  7. A student group dislikes sequential lessons on the same subject.

    def studentGroupSubjectVariety(constraintFactory):

    return constraintFactory.from_(LessonClass) \
        .join(LessonClass,
                        [Joiners.equal(lambda lesson: lesson.subject),
                        Joiners.equal(lambda lesson: lesson.studentGroup),
                        Joiners.equal(lambda lesson: lesson.timeslot.dayOfWeek)]) \
        .filter(within30Mins) \
        .penalize("Student group subject variety", HardSoftScore.ONE_SOFT)
    
mery
  • 11
  • 2
  • What is your question? – Geoffrey De Smet Sep 20 '22 at 13:54
  • I want to fill all the hours and classes with teachers. Thank you for your reply – mery Sep 21 '22 at 04:49
  • Do you have enough lessons to fill all the rooms and hours with teachers? If the number of lessons is less than `num_timeslots * num_rooms` (where `num_timeslots` and `num_rooms` are the number of unique timeslots and rooms respectively), then it is impossible to have all rooms filled in all timeslots. Conversely, if the number of lessons is greater than the product `num_timeslots * num_rooms`, there will be hard conflicts (as two lessons cannot share a room in the same timeslot) unless teacher is set to nullable. – Christopher Chianelli Sep 21 '22 at 17:43
  • If we make the professor at times give the material for two hours of continuous teaching, as well as repeat the teachers in all classes in a way that the teacher is not in two classes at the same time. Thank you – mery Sep 21 '22 at 19:54

0 Answers0