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