    001package org.cpsolver.coursett.sectioning;
    002
    003import java.util.ArrayList;
    004import java.util.Collection;
    005import java.util.Iterator;
    006import java.util.LinkedList;
    007import java.util.List;
    008import java.util.Queue;
    009
    010import org.cpsolver.coursett.model.Configuration;
    011import org.cpsolver.coursett.model.Lecture;
    012import org.cpsolver.coursett.model.Placement;
    013import org.cpsolver.coursett.model.Student;
    014import org.cpsolver.coursett.model.TimetableModel;
    015import org.cpsolver.ifs.assignment.Assignment;
    016import org.cpsolver.ifs.heuristics.NeighbourSelection;
    017import org.cpsolver.ifs.model.Neighbour;
    018import org.cpsolver.ifs.solution.Solution;
    019import org.cpsolver.ifs.solver.Solver;
    020import org.cpsolver.ifs.util.ToolBox;
    021
    022/**
    023 * Generate student swaps. Rather than generating swaps at random, the class iterates between
    024 * all classes and all conflicting students of each class and generate a random swap for
    025 * each such student.
    026 *
    027 * @author  Tomáš Müller
    028 * @version CourseTT 1.3 (University Course Timetabling)<br>
    029 *          Copyright (C) 2017 Tomáš Müller<br>
    030 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    031 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    032 * <br>
    033 *          This library is free software; you can redistribute it and/or modify
    034 *          it under the terms of the GNU Lesser General Public License as
    035 *          published by the Free Software Foundation; either version 3 of the
    036 *          License, or (at your option) any later version. <br>
    037 * <br>
    038 *          This library is distributed in the hope that it will be useful, but
    039 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    040 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    041 *          Lesser General Public License for more details. <br>
    042 * <br>
    043 *          You should have received a copy of the GNU Lesser General Public
    044 *          License along with this library; if not see
    045 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    046 */
    047public class StudentSwapGenerator implements NeighbourSelection<Lecture, Placement> {
    048    Iterator<Lecture> iLectures = null;
    049    Lecture iLecture = null;
    050    Iterator<Student> iStudents = null;
    051
    052    @Override
    053    public void init(Solver<Lecture, Placement> solver) {
    054    }
    055
    056    @Override
    057    public Neighbour<Lecture, Placement> selectNeighbour(Solution<Lecture, Placement> solution) {
    058        TimetableModel model = (TimetableModel)solution.getModel();
    059        if (model.getAllStudents().isEmpty()) return null;
    060
    061        boolean next = false;
    062        while (iLecture == null || iStudents == null || !iStudents.hasNext()) {
    063            if (iLectures == null || !iLectures.hasNext()) {
    064                if (next) return null;
    065                iLectures = model.variables().iterator();
    066                next = true;
    067            }
    068            iLecture = iLectures.next();
    069            if (iLecture.getConfiguration() == null || (iLecture.getConfiguration().getAltConfigurations().isEmpty() && iLecture.isSingleSection())) continue;
    070            iStudents = iLecture.conflictStudents(solution.getAssignment()).iterator();
    071        }
    072        return generateSwap(model, solution.getAssignment(), iStudents.next(), iLecture.getConfiguration());
    073    }
    074
    075    public Neighbour<Lecture, Placement> selectNeighbour(Assignment<Lecture, Placement> assignment, Lecture lecture) {
    076        return generateSwap((TimetableModel)lecture.getModel(), assignment, ToolBox.random(lecture.students()), lecture.getConfiguration());
    077
    078    }
    079
    080    public Neighbour<Lecture, Placement> generateSwap(TimetableModel model, Assignment<Lecture, Placement> assignment, Student student, Configuration config) {
    081        if (student == null || config == null) return null;
    082        if (!config.getAltConfigurations().isEmpty()) {
    083            int idx = ToolBox.random(config.getAltConfigurations().size() + 2);
    084            if (idx > 1) config = config.getAltConfigurations().get(idx - 2);
    085        }
    086        Long subpartId = ToolBox.random(config.getTopSubpartIds());
    087        int nrAttempts = ToolBox.random(10);
    088        for (int i = 0; i < nrAttempts; i++) {
    089            Lecture lecture = ToolBox.random(config.getTopLectures(subpartId));
    090            Student other = ToolBox.random(lecture.students());
    091            if (other != null && !student.equals(other)) {
    092                StudentSwap swap = new StudentSwap(model, assignment, student, other, config.getOfferingId());
    093                if (swap.isAllowed()) return swap;
    094            }
    095        }
    096        List<Lecture> lectures = new ArrayList<Lecture>();
    097        Queue<Collection<Lecture>> queue = new LinkedList<Collection<Lecture>>(config.getTopLectures().values());
    098        while (!queue.isEmpty()) {
    099            List<Lecture> adepts = new ArrayList<Lecture>();
    100            for (Lecture adept: queue.poll())
    101                if (student.canEnroll(adept)) adepts.add(adept);
    102            if (adepts.isEmpty()) return null;
    103            Lecture lect = ToolBox.random(adepts);
    104            lectures.add(lect);
    105            if (lect.hasAnyChildren())
    106                queue.addAll(lect.getChildren().values());
    107        }
    108        StudentSwap swap = new StudentSwap(model, assignment, student, lectures);
    109        return (swap.isAllowed() ? swap : null);
    110    }
    111
    112}