001 package net.sf.cpsolver.studentsct.heuristics.selection;
002
003 import java.util.Enumeration;
004 import java.util.Vector;
005
006 import net.sf.cpsolver.ifs.heuristics.NeighbourSelection;
007 import net.sf.cpsolver.ifs.model.Neighbour;
008 import net.sf.cpsolver.ifs.solution.Solution;
009 import net.sf.cpsolver.ifs.solver.Solver;
010 import net.sf.cpsolver.ifs.util.DataProperties;
011 import net.sf.cpsolver.ifs.util.Progress;
012 import net.sf.cpsolver.ifs.util.ToolBox;
013 import net.sf.cpsolver.studentsct.StudentSectioningModel;
014 import net.sf.cpsolver.studentsct.model.Request;
015 import net.sf.cpsolver.studentsct.model.Student;
016
017 /**
018 * Random unassignment of some (randomly selected) students.
019 *
020 * <br><br>
021 * In each step a student is randomly selected with the given probabilty.
022 * Null is returned otherwise (controll is passed to the following {@link NeighbourSelection}).
023 *
024 * <br><br>
025 * Parameters:
026 * <br>
027 * <table border='1'><tr><th>Parameter</th><th>Type</th><th>Comment</th></tr>
028 * <tr><td>Neighbour.RandomUnassignmentProb</td><td>{@link Double}</td><td>Probability of a random selection of a student.</td></tr>
029 * </table>
030 * <br><br>
031 *
032 * @version
033 * StudentSct 1.1 (Student Sectioning)<br>
034 * Copyright (C) 2007 Tomáš Müller<br>
035 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br>
036 * Lazenska 391, 76314 Zlin, Czech Republic<br>
037 * <br>
038 * This library is free software; you can redistribute it and/or
039 * modify it under the terms of the GNU Lesser General Public
040 * License as published by the Free Software Foundation; either
041 * version 2.1 of the License, or (at your option) any later version.
042 * <br><br>
043 * This library is distributed in the hope that it will be useful,
044 * but WITHOUT ANY WARRANTY; without even the implied warranty of
045 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
046 * Lesser General Public License for more details.
047 * <br><br>
048 * You should have received a copy of the GNU Lesser General Public
049 * License along with this library; if not, write to the Free Software
050 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
051 */
052 public class RandomUnassignmentSelection implements NeighbourSelection {
053 private Vector iStudents = null;
054 protected double iRandom = 0.5;
055
056 /**
057 * Constructor
058 * @param properties configuration
059 */
060 public RandomUnassignmentSelection(DataProperties properties) {
061 iRandom = properties.getPropertyDouble("Neighbour.RandomUnassignmentProb", iRandom);
062 }
063
064 /**
065 * Initialization
066 */
067 public void init(Solver solver) {
068 iStudents = ((StudentSectioningModel)solver.currentSolution().getModel()).getStudents();
069 Progress.getInstance(solver.currentSolution().getModel()).setPhase("Random unassignment...", 1);
070 }
071
072 /**
073 * With the given probabilty, a student is randomly selected to be unassigned.
074 * Null is returned otherwise.
075 */
076 public Neighbour selectNeighbour(Solution solution) {
077 if (Math.random()<iRandom) {
078 Student student = (Student)ToolBox.random(iStudents);
079 return new UnassignStudentNeighbour(student);
080 }
081 Progress.getInstance(solution.getModel()).incProgress();
082 return null;
083 }
084
085 /** Unassignment of all requests of a student */
086 public static class UnassignStudentNeighbour extends Neighbour {
087 private Student iStudent = null;
088
089 /**
090 * Constructor
091 * @param student a student to be unassigned
092 */
093 public UnassignStudentNeighbour(Student student) {
094 iStudent = student;
095 }
096
097 public double value() {
098 double val = 0;
099 for (Enumeration e=iStudent.getRequests().elements();e.hasMoreElements();) {
100 Request request = (Request)e.nextElement();
101 if (request.getAssignment()!=null)
102 val -= request.getAssignment().toDouble();
103 }
104 return val;
105 }
106
107 /** All requests of the given student are unassigned */
108 public void assign(long iteration) {
109 for (Enumeration e=iStudent.getRequests().elements();e.hasMoreElements();) {
110 Request request = (Request)e.nextElement();
111 if (request.getAssignment()!=null)
112 request.unassign(iteration);
113 }
114 }
115
116 public String toString() {
117 StringBuffer sb = new StringBuffer("Un{");
118 sb.append(" "+iStudent);
119 sb.append(" }");
120 return sb.toString();
121 }
122
123 }
124
125 }