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.assignment.Assignment; 010import org.cpsolver.ifs.heuristics.ValueSelection; 011import org.cpsolver.ifs.heuristics.VariableSelection; 012import org.cpsolver.ifs.solution.Solution; 013import org.cpsolver.ifs.solver.Solver; 014import org.cpsolver.ifs.util.DataProperties; 015import org.cpsolver.studentsct.filter.StudentFilter; 016import org.cpsolver.studentsct.model.Enrollment; 017import org.cpsolver.studentsct.model.Request; 018import org.cpsolver.studentsct.model.Request.RequestPriority; 019 020/** 021 * Use the standard IFS search for the unassigned critical course requests. 022 * This selection is based on {@link StandardSelection} using 023 * {@link UnassignedCriticalCourseRequestSelection} as variable selection. 024 * Unlike {@link StandardSelection}, it allows for a critical course request to be 025 * unassigned. 026 * 027 * @version StudentSct 1.3 (Student Sectioning)<br> 028 * Copyright (C) 2007 - 2014 Tomáš Müller<br> 029 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 030 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 031 * <br> 032 * This library is free software; you can redistribute it and/or modify 033 * it under the terms of the GNU Lesser General Public License as 034 * published by the Free Software Foundation; either version 3 of the 035 * License, or (at your option) any later version. <br> 036 * <br> 037 * This library is distributed in the hope that it will be useful, but 038 * WITHOUT ANY WARRANTY; without even the implied warranty of 039 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 040 * Lesser General Public License for more details. <br> 041 * <br> 042 * You should have received a copy of the GNU Lesser General Public 043 * License along with this library; if not see 044 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 045 */ 046public class CriticalStandardSelection extends StandardSelection { 047 private RequestPriority iPriority; 048 private boolean iAllowCriticalUnassignment = false; 049 050 public CriticalStandardSelection(DataProperties properties, VariableSelection<Request, Enrollment> variableSelection, ValueSelection<Request, Enrollment> valueSelection, RequestPriority priority) { 051 super(properties, variableSelection, valueSelection); 052 iPriority = priority; 053 iAllowCriticalUnassignment = properties.getPropertyBoolean("Neighbour.AllowCriticalUnassignment", iAllowCriticalUnassignment); 054 } 055 056 public CriticalStandardSelection(DataProperties properties, ValueSelection<Request, Enrollment> valueSelection, RequestPriority priority) { 057 this(properties, new UnassignedCriticalCourseRequestSelection(priority), valueSelection, priority); 058 } 059 060 public CriticalStandardSelection(DataProperties properties, ValueSelection<Request, Enrollment> valueSelection) { 061 this(properties, valueSelection, RequestPriority.Critical); 062 } 063 064 @Override 065 public void init(Solver<Request, Enrollment> solver) { 066 StudentFilter filter = null; 067 if (iVariableSelection instanceof UnassignedRequestSelection) 068 filter = ((UnassignedRequestSelection)iVariableSelection).getFilter(); 069 init(solver, iPriority.name() + " Ifs" + (filter == null ? "" : " (" + filter.getName().toLowerCase() + " students)") + "..."); 070 } 071 072 @Override 073 public boolean canUnassign(Enrollment enrollment, Enrollment conflict, Assignment<Request, Enrollment> assignment) { 074 if (!iAllowCriticalUnassignment) return super.canUnassign(enrollment, conflict, assignment); 075 if (!iCanConflict) return false; 076 if (!iCanHigherPriorityConflict && conflict.getRequest().getPriority() < enrollment.getRequest().getPriority()) return false; 077 if (conflict.getRequest().isMPP() && conflict.equals(conflict.getRequest().getInitialAssignment())) return false; 078 if (iPreferPriorityStudents || conflict.getRequest().getRequestPriority().isSame(enrollment.getRequest())) { 079 if (conflict.getStudent().getPriority().isHigher(enrollment.getStudent())) return false; 080 } 081 // Override to allow unassignment of critical course requests 082 return true; 083 } 084 085 /** 086 * Returns the unassigned critical course requests in a random order. 087 */ 088 static class UnassignedCriticalCourseRequestSelection implements VariableSelection<Request, Enrollment>{ 089 protected int iNrRounds = 0; 090 protected Queue<Request> iRequests = null; 091 private RequestPriority iPriority; 092 093 public UnassignedCriticalCourseRequestSelection(RequestPriority priority) { 094 iPriority = priority; 095 } 096 097 @Override 098 public void init(Solver<Request, Enrollment> solver) { 099 iRequests = new LinkedList<Request>(); 100 iNrRounds = solver.getProperties().getPropertyInt("Neighbour.CriticalRounds", 10); 101 } 102 103 @Override 104 public Request selectVariable(Solution<Request, Enrollment> solution) { 105 return nextRequest(solution); 106 } 107 108 protected synchronized Request nextRequest(Solution<Request, Enrollment> solution) { 109 if (iRequests.isEmpty() && iNrRounds > 0) { 110 iNrRounds --; 111 List<Request> variables = new ArrayList<Request>(); 112 for (Request r: solution.getModel().unassignedVariables(solution.getAssignment())) 113 if (iPriority.isCritical(r)) variables.add(r); 114 Collections.shuffle(variables); 115 iRequests.addAll(variables); 116 } 117 return iRequests.poll(); 118 } 119 } 120}