001package org.cpsolver.studentsct.heuristics.selection; 002 003import java.util.HashMap; 004import java.util.List; 005import java.util.Map; 006 007import org.cpsolver.ifs.assignment.Assignment; 008import org.cpsolver.ifs.heuristics.NeighbourSelection; 009import org.cpsolver.ifs.model.Neighbour; 010import org.cpsolver.ifs.solution.Solution; 011import org.cpsolver.ifs.solver.Solver; 012import org.cpsolver.ifs.util.DataProperties; 013import org.cpsolver.ifs.util.Progress; 014import org.cpsolver.ifs.util.ToolBox; 015import org.cpsolver.studentsct.StudentSectioningModel; 016import org.cpsolver.studentsct.model.Enrollment; 017import org.cpsolver.studentsct.model.Request; 018import org.cpsolver.studentsct.model.Student; 019 020 021/** 022 * Random unassignment of some (randomly selected) students. 023 * 024 * <br> 025 * <br> 026 * In each step a student is randomly selected with the given probabilty. Null 027 * is returned otherwise (controll is passed to the following 028 * {@link NeighbourSelection}). 029 * 030 * <br> 031 * <br> 032 * Parameters: <br> 033 * <table border='1' summary='Related Solver Parameters'> 034 * <tr> 035 * <th>Parameter</th> 036 * <th>Type</th> 037 * <th>Comment</th> 038 * </tr> 039 * <tr> 040 * <td>Neighbour.RandomUnassignmentProb</td> 041 * <td>{@link Double}</td> 042 * <td>Probability of a random selection of a student.</td> 043 * </tr> 044 * </table> 045 * <br> 046 * <br> 047 * 048 * @version StudentSct 1.3 (Student Sectioning)<br> 049 * Copyright (C) 2007 - 2014 Tomas Muller<br> 050 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 051 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 052 * <br> 053 * This library is free software; you can redistribute it and/or modify 054 * it under the terms of the GNU Lesser General Public License as 055 * published by the Free Software Foundation; either version 3 of the 056 * License, or (at your option) any later version. <br> 057 * <br> 058 * This library is distributed in the hope that it will be useful, but 059 * WITHOUT ANY WARRANTY; without even the implied warranty of 060 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 061 * Lesser General Public License for more details. <br> 062 * <br> 063 * You should have received a copy of the GNU Lesser General Public 064 * License along with this library; if not see 065 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 066 */ 067public class RandomUnassignmentSelection implements NeighbourSelection<Request, Enrollment> { 068 private List<Student> iStudents = null; 069 protected double iRandom = 0.5; 070 071 /** 072 * Constructor 073 * 074 * @param properties 075 * configuration 076 */ 077 public RandomUnassignmentSelection(DataProperties properties) { 078 iRandom = properties.getPropertyDouble("Neighbour.RandomUnassignmentProb", iRandom); 079 } 080 081 /** 082 * Initialization 083 */ 084 @Override 085 public void init(Solver<Request, Enrollment> solver) { 086 iStudents = ((StudentSectioningModel) solver.currentSolution().getModel()).getStudents(); 087 Progress.getInstance(solver.currentSolution().getModel()).setPhase("Random unassignment...", 1); 088 } 089 090 /** 091 * With the given probabilty, a student is randomly selected to be 092 * unassigned. Null is returned otherwise. 093 */ 094 @Override 095 public Neighbour<Request, Enrollment> selectNeighbour(Solution<Request, Enrollment> solution) { 096 if (Math.random() < iRandom) { 097 Student student = ToolBox.random(iStudents); 098 return new UnassignStudentNeighbour(student); 099 } 100 Progress.getInstance(solution.getModel()).incProgress(); 101 return null; 102 } 103 104 /** Unassignment of all requests of a student */ 105 public static class UnassignStudentNeighbour implements Neighbour<Request, Enrollment> { 106 private Student iStudent = null; 107 108 /** 109 * Constructor 110 * 111 * @param student 112 * a student to be unassigned 113 */ 114 public UnassignStudentNeighbour(Student student) { 115 iStudent = student; 116 } 117 118 @Override 119 public double value(Assignment<Request, Enrollment> assignment) { 120 double val = 0; 121 for (Request request : iStudent.getRequests()) { 122 Enrollment enrollment = assignment.getValue(request); 123 if (enrollment != null) 124 val -= enrollment.toDouble(assignment); 125 } 126 return val; 127 } 128 129 /** All requests of the given student are unassigned */ 130 @Override 131 public void assign(Assignment<Request, Enrollment> assignment, long iteration) { 132 for (Request request : iStudent.getRequests()) 133 assignment.unassign(iteration, request); 134 } 135 136 @Override 137 public String toString() { 138 StringBuffer sb = new StringBuffer("Un{"); 139 sb.append(" " + iStudent); 140 sb.append(" }"); 141 return sb.toString(); 142 } 143 144 @Override 145 public Map<Request, Enrollment> assignments() { 146 Map<Request, Enrollment> ret = new HashMap<Request, Enrollment>(); 147 for (Request request : iStudent.getRequests()) 148 ret.put(request, null); 149 return ret; 150 } 151 152 } 153 154}