    001package org.cpsolver.studentsct.heuristics.selection;
    002
    003import java.util.ArrayList;
    004import java.util.Collections;
    005import java.util.LinkedList;
    006import java.util.List;
    007import java.util.Queue;
    008
    009import org.cpsolver.ifs.heuristics.VariableSelection;
    010import org.cpsolver.ifs.solution.Solution;
    011import org.cpsolver.ifs.solver.Solver;
    012import org.cpsolver.studentsct.model.Enrollment;
    013import org.cpsolver.studentsct.model.Request;
    014import org.cpsolver.studentsct.model.Request.RequestPriority;
    015
    016public class UnassignedCriticalCourseRequestSelection implements VariableSelection<Request, Enrollment>{
    017    protected Queue<Request> iRequests = null;
    018    private RequestPriority iPriority = null;
    019
    020    UnassignedCriticalCourseRequestSelection(RequestPriority priority) {
    021        iPriority = priority;
    022    }
    023
    024
    025    @Override
    026    public void init(Solver<Request, Enrollment> solver) {
    027        iRequests = new LinkedList<Request>();
    028    }
    029
    030    @Override
    031    public Request selectVariable(Solution<Request, Enrollment> solution) {
    032        return nextRequest(solution);
    033    }
    034
    035    protected synchronized Request nextRequest(Solution<Request, Enrollment> solution) {
    036        Request ret = iRequests.poll();
    037        if (ret == null) {
    038            List<Request> variables = new ArrayList<Request>();
    039            for (Request r: solution.getModel().unassignedVariables(solution.getAssignment()))
    040                if (iPriority.isCritical(r)) variables.add(r);
    041            Collections.shuffle(variables);
    042            iRequests.addAll(variables);
    043            ret = iRequests.poll();
    044        }
    045        return ret;
    046    }
    047}