@@ -645,27 +645,19 @@ def _build_resource_constraints(
645645 if i .course_id == j .course_id :
646646 # when a faculty teaches two sections of the same course,
647647 # they must be next to each other
648- constraint_parts .append (
649- cast (
650- z3 .BoolRef ,
651- z3 .And (
652- lecture_next_to (i .time , j .time ),
653- lab_next_to (i .time , j .time ),
654- ),
655- )
656- )
648+ same_course_constraints = [lecture_next_to (i .time , j .time )]
649+ # Only require lab_next_to if the course has labs
650+ if i .labs :
651+ same_course_constraints .append (lab_next_to (i .time , j .time ))
652+ constraint_parts .append (cast (z3 .BoolRef , z3 .And (same_course_constraints )))
657653 else :
658654 # when a faculty teaches two sections of different courses,
659655 # they must not be next to each other
660- constraint_parts .append (
661- cast (
662- z3 .BoolRef ,
663- z3 .And (
664- z3 .Not (lecture_next_to (i .time , j .time )),
665- z3 .Not (lab_next_to (i .time , j .time )),
666- ),
667- )
668- )
656+ diff_course_constraints = [z3 .Not (lecture_next_to (i .time , j .time ))]
657+ # Only require lab_next_to constraint if both courses have labs
658+ if i .labs and j .labs :
659+ diff_course_constraints .append (z3 .Not (lab_next_to (i .time , j .time )))
660+ constraint_parts .append (cast (z3 .BoolRef , z3 .And (diff_course_constraints )))
669661
670662 if resource :
671663 # add all resource constraints (room, lab, etc.)
0 commit comments