001package org.cpsolver.studentsct.heuristics.selection;
002
003import org.cpsolver.ifs.assignment.Assignment;
004import org.cpsolver.ifs.model.Neighbour;
005import org.cpsolver.ifs.solution.Solution;
006import org.cpsolver.ifs.solver.Solver;
007import org.cpsolver.ifs.util.DataProperties;
008import org.cpsolver.ifs.util.JProf;
009import org.cpsolver.ifs.util.Progress;
010import org.cpsolver.studentsct.model.Enrollment;
011import org.cpsolver.studentsct.model.Request;
012import org.cpsolver.studentsct.model.Student;
013
014/**
015 * Assign initial enrollments. An extension of {@link BranchBoundSelection},
016 * where only initial enrollments (see {@link Request#getInitialAssignment()}) can 
017 * be assigned. Students that already has a schedule
018 * ({@link Student#nrAssignedRequests(Assignment)} is greater then zero) are ignored.
019 * 
020 * <br>
021 * <br>
022 * 
023 * @author  Tomáš Müller
024 * @version StudentSct 1.3 (Student Sectioning)<br>
025 *          Copyright (C) 2007 - 2015 Tomáš Müller<br>
026 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
027 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
028 * <br>
029 *          This library is free software; you can redistribute it and/or modify
030 *          it under the terms of the GNU Lesser General Public License as
031 *          published by the Free Software Foundation; either version 3 of the
032 *          License, or (at your option) any later version. <br>
033 * <br>
034 *          This library is distributed in the hope that it will be useful, but
035 *          WITHOUT ANY WARRANTY; without even the implied warranty of
036 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
037 *          Lesser General Public License for more details. <br>
038 * <br>
039 *          You should have received a copy of the GNU Lesser General Public
040 *          License along with this library; if not see
041 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
042 */
043public class AssignInitialSelection extends BranchBoundSelection {
044    
045    public AssignInitialSelection(DataProperties properties) {
046        super(properties);
047    }
048    
049    @Override
050    public void init(Solver<Request, Enrollment> solver) {
051        init(solver, "Assign initial enrollments...");
052    }
053    
054    @Override
055    public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
056        Student student = null;
057        while ((student = nextStudent()) != null) {
058            Progress.getInstance(solution.getModel()).incProgress();
059            if (student.nrAssignedRequests(solution.getAssignment()) > 0) continue;
060            Neighbour<Request, Enrollment> neighbour = new InitialSelection(student, solution.getAssignment()).select();
061            if (neighbour != null)
062                return neighbour;
063        }
064        return null;
065    }
066    
067    public class InitialSelection extends Selection {
068        public InitialSelection(Student student, Assignment<Request, Enrollment> assignment) {
069            super(student, assignment);
070        }
071        
072        @Override
073        public void backTrack(int idx) {
074            if (iTimeout > 0 && (JProf.currentTimeMillis() - iT0) > iTimeout) {
075                iTimeoutReached = true;
076                return;
077            }
078            if (getBestAssignment() != null && getBound(idx) >= getBestValue()) {
079                return;
080            }
081            if (idx == iAssignment.length) {
082                if (getBestAssignment() == null || getValue() < getBestValue()) {
083                    saveBest();
084                }
085                return;
086            }
087            Request request = iStudent.getRequests().get(idx);
088            if (!canAssign(request, idx)) {
089                backTrack(idx + 1);
090                return;
091            }
092            if (request.isMPP()) {
093                Enrollment initial = request.getInitialAssignment();
094                if (initial != null) {
095                    if (!inConflict(idx, initial)) {
096                        iAssignment[idx] = initial;
097                        backTrack(idx + 1);
098                        iAssignment[idx] = null;
099                    }
100                } else {
101                    backTrack(idx + 1);
102                }
103            } else {
104                backTrack(idx + 1);
105            }
106        }
107    }
108}