    001package org.cpsolver.studentsct.heuristics.selection;
    002
    003import java.text.DecimalFormat;
    004import java.util.ArrayList;
    005import java.util.Collection;
    006import java.util.Collections;
    007import java.util.Comparator;
    008import java.util.ConcurrentModificationException;
    009import java.util.LinkedList;
    010import java.util.List;
    011import java.util.ListIterator;
    012import java.util.Map;
    013
    014import org.cpsolver.ifs.assignment.Assignment;
    015import org.cpsolver.ifs.heuristics.BacktrackNeighbourSelection;
    016import org.cpsolver.ifs.heuristics.NeighbourSelection;
    017import org.cpsolver.ifs.model.InfoProvider;
    018import org.cpsolver.ifs.model.Neighbour;
    019import org.cpsolver.ifs.solution.Solution;
    020import org.cpsolver.ifs.solver.Solver;
    021import org.cpsolver.ifs.solver.SolverListener;
    022import org.cpsolver.ifs.util.DataProperties;
    023import org.cpsolver.ifs.util.Progress;
    024import org.cpsolver.studentsct.filter.StudentFilter;
    025import org.cpsolver.studentsct.heuristics.RandomizedBacktrackNeighbourSelection;
    026import org.cpsolver.studentsct.model.CourseRequest;
    027import org.cpsolver.studentsct.model.Enrollment;
    028import org.cpsolver.studentsct.model.FreeTimeRequest;
    029import org.cpsolver.studentsct.model.Request;
    030import org.cpsolver.studentsct.model.Request.RequestPriority;
    031import org.cpsolver.studentsct.model.Student.StudentPriority;
    032
    033
    034/**
    035 * Use backtrack neighbour selection. For all unassigned variables (in a random
    036 * order), {@link RandomizedBacktrackNeighbourSelection} is being used.
    037 *
    038 * <br>
    039 * <br>
    040 *
    041 * @author  Tomáš Müller
    042 * @version StudentSct 1.3 (Student Sectioning)<br>
    043 *          Copyright (C) 2007 - 2014 Tomáš Müller<br>
    044 *          <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
    045 *          <a href="http://muller.unitime.org">http://muller.unitime.org</a><br>
    046 * <br>
    047 *          This library is free software; you can redistribute it and/or modify
    048 *          it under the terms of the GNU Lesser General Public License as
    049 *          published by the Free Software Foundation; either version 3 of the
    050 *          License, or (at your option) any later version. <br>
    051 * <br>
    052 *          This library is distributed in the hope that it will be useful, but
    053 *          WITHOUT ANY WARRANTY; without even the implied warranty of
    054 *          MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    055 *          Lesser General Public License for more details. <br>
    056 * <br>
    057 *          You should have received a copy of the GNU Lesser General Public
    058 *          License along with this library; if not see
    059 *          <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>.
    060 */
    061
    062public class BacktrackSelection implements NeighbourSelection<Request, Enrollment>, InfoProvider<Request, Enrollment>, SolverListener<Request, Enrollment> {
    063    private static DecimalFormat sDF = new DecimalFormat("0.00");
    064    protected RandomizedBacktrackNeighbourSelection iRBtNSel = null;
    065    protected LinkedList<Request> iRequests = null;
    066    protected boolean iIncludeAssignedRequests = false;
    067
    068    protected long iNbrIterations = 0;
    069    protected long iMaxIterations = 0;
    070    protected long iTotalTime = 0;
    071    protected long iNbrTimeoutReached = 0;
    072    protected long iNbrNoSolution = 0;
    073    protected StudentFilter iFilter = null;
    074    protected RequestComparator iRequestComparator = null;
    075
    076    public BacktrackSelection(DataProperties properties) {
    077        iIncludeAssignedRequests = properties.getPropertyBoolean("Neighbour.IncludeAssignedRequests", iIncludeAssignedRequests);
    078        iRequestComparator = new RequestComparator(properties);
    079    }
    080
    081    public void init(Solver<Request, Enrollment> solver, String name) {
    082        List<Request> variables = new ArrayList<Request>(iIncludeAssignedRequests ? solver.currentSolution().getModel().variables() : solver.currentSolution().getModel().unassignedVariables(solver.currentSolution().getAssignment()));
    083        Collections.shuffle(variables);
    084        Collections.sort(variables, iRequestComparator);
    085        iRequests = new LinkedList<Request>(variables);
    086        if (iRBtNSel == null) {
    087            try {
    088                iRBtNSel = new RandomizedBacktrackNeighbourSelection(solver.getProperties());
    089                iRBtNSel.init(solver);
    090            } catch (Exception e) {
    091                throw new RuntimeException(e.getMessage(), e);
    092            }
    093        }
    094        Progress.getInstance(solver.currentSolution().getModel()).setPhase(name, variables.size());
    095    }
    096
    097    @Override
    098    public void init(Solver<Request, Enrollment> solver) {
    099        init(solver, "Backtracking" + (iFilter == null ? "" : " (" + iFilter.getName().toLowerCase() + " students)") + "...");
    100
    101        iNbrIterations = 0;
    102        iNbrTimeoutReached = 0;
    103        iNbrNoSolution = 0;
    104        iTotalTime = 0;
    105        iMaxIterations = Math.max(10,  2 * iRequests.size());
    106    }
    107
    108    protected synchronized Request nextRequest() {
    109        while (true) {
    110            Request request = iRequests.poll();
    111            if (request == null) return null;
    112            if (iFilter == null || iFilter.accept(request.getStudent())) return request;
    113        }
    114    }
    115
    116    public synchronized void addRequest(Request request) {
    117        if (iRequests != null && request != null && !request.getStudent().isDummy()) {
    118            if (request.getStudent().getPriority().ordinal() < StudentPriority.Normal.ordinal() || request.getRequestPriority().ordinal() < RequestPriority.Normal.ordinal()) {
    119                for (ListIterator<Request> i = iRequests.listIterator(); i.hasNext();) {
    120                    Request r = i.next();
    121                    if (iRequestComparator.compare(r, request) > 0) {
    122                        i.previous(); // go one back
    123                        i.add(request);
    124                        return;
    125                    }
    126                }
    127            }
    128            iRequests.add(request);
    129        }
    130    }
    131
    132    @Override
    133    public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) {
    134        Request request = null;
    135        if (iNbrIterations > iMaxIterations) return null;
    136        while ((request = nextRequest()) != null) {
    137            Progress.getInstance(solution.getModel()).incProgress();
    138            Enrollment e = request.getAssignment(solution.getAssignment());
    139            if (e != null && request instanceof FreeTimeRequest) continue;
    140            if (e != null && e.getPriority() == 0 && ((CourseRequest)request).getSelectedChoices().isEmpty()) continue;
    141            for (int i = 0; i < 5; i++) {
    142                try {
    143                    Neighbour<Request, Enrollment> n = iRBtNSel.selectNeighbour(solution, request);
    144                    if (iRBtNSel.getContext() != null) {
    145                        iNbrIterations ++;
    146                        iTotalTime += iRBtNSel.getContext().getTime();
    147                        if (iRBtNSel.getContext().isTimeoutReached()) iNbrTimeoutReached ++;
    148                        if (n == null) iNbrNoSolution ++;
    149                    }
    150                    if (n != null && n.value(solution.getAssignment()) <= 0.0)
    151                        return n;
    152                    break;
    153                } catch (ConcurrentModificationException ex) {}
    154            }
    155        }
    156        return null;
    157    }
    158
    159    @Override
    160    public void getInfo(Assignment<Request, Enrollment> assignment, Map<String, String> info) {
    161        if (iNbrIterations > 0)
    162            info.put("Timing of " + getClass().getSimpleName(), sDF.format(((double)iTotalTime) / iNbrIterations) + " ms/it (" +
    163                    iNbrIterations + " iterations, " +
    164                    (iNbrNoSolution == 0 ? "" : sDF.format(100.0 * iNbrNoSolution / iNbrIterations) + "% no solution, ") +
    165                    sDF.format(100.0 * iNbrTimeoutReached / iNbrIterations) + "% time limit of " + sDF.format(iRBtNSel.getTimeout() / 1000.0) + " seconds reached" +
    166                    (iNbrIterations > iMaxIterations ? ", stopped after " + iNbrIterations + " iterations" : "") +
    167                    ")");
    168    }
    169
    170    @Override
    171    public void getInfo(Assignment<Request, Enrollment> assignment, Map<String, String> info, Collection<Request> variables) {
    172    }
    173
    174    /**
    175     * Only consider students meeting the given filter.
    176     */
    177    public StudentFilter getFilter() { return iFilter; }
    178
    179    /**
    180     * Only consider students meeting the given filter.
    181     */
    182    public BacktrackSelection withFilter(StudentFilter filter) { iFilter = filter; return this; }
    183
    184    @Override
    185    public boolean variableSelected(Assignment<Request, Enrollment> assignment, long iteration, Request variable) {
    186        return false;
    187    }
    188    @Override
    189    public boolean valueSelected(Assignment<Request, Enrollment> assignment, long iteration, Request variable, Enrollment value) {
    190        return false;
    191    }
    192    @Override
    193    public boolean neighbourSelected(Assignment<Request, Enrollment> assignment, long iteration, Neighbour<Request, Enrollment> neighbour) {
    194        return false;
    195    }
    196    @Override
    197    public void neighbourFailed(Assignment<Request, Enrollment> assignment, long iteration, Neighbour<Request, Enrollment> neighbour) {
    198        if (neighbour instanceof BacktrackNeighbourSelection.BackTrackNeighbour)
    199            addRequest(((BacktrackNeighbourSelection<Request, Enrollment>.BackTrackNeighbour)neighbour).getAssignments().get(0).getRequest());
    200    }
    201
    202    public static class RequestComparator implements Comparator<Request> {
    203        protected boolean iPreferPriorityStudents = true;
    204        protected RequestComparator(DataProperties properties) {
    205            iPreferPriorityStudents = properties.getPropertyBoolean("Sectioning.PriorityStudentsFirstSelection.AllIn", true);
    206        }
    207
    208        @Override
    209        public int compare(Request r1, Request r2) {
    210            if (iPreferPriorityStudents) {
    211                if (r1.getStudent().getPriority() != r2.getStudent().getPriority())
    212                    return r1.getStudent().getPriority().compareTo(r2.getStudent().getPriority());
    213                if (r1.getRequestPriority() != r2.getRequestPriority())
    214                    return r1.getRequestPriority().compareTo(r2.getRequestPriority());
    215            } else {
    216                if (r1.getRequestPriority() != r2.getRequestPriority())
    217                    return r1.getRequestPriority().compareTo(r2.getRequestPriority());
    218                if (r1.getRequestPriority() != r2.getRequestPriority())
    219                    return r1.getStudent().getPriority().compareTo(r2.getStudent().getPriority());
    220            }
    221            if (r1.isAlternative() != r2.isAlternative())
    222                return r2.isAlternative() ? -1 : 1;
    223            if (r1.getPriority() != r2.getPriority())
    224                return r1.getPriority() < r2.getPriority() ? -1 : 1;
    225            return 0;
    226        }
    227    }
    228}