001package org.cpsolver.studentsct.heuristics.studentord; 002 003import org.cpsolver.ifs.util.DataProperties; 004import org.cpsolver.studentsct.model.CourseRequest; 005import org.cpsolver.studentsct.model.Request; 006import org.cpsolver.studentsct.model.Student; 007 008/** 009 * Return the given set of students in an order of average number of request groups of 010 * each student. If two students are involved in the same number of groups, the comparison 011 * falls down to the average number of choices of each student (students with less choices first). 012 * <br> 013 * 014 * @author Tomáš Müller 015 * @version StudentSct 1.3 (Student Sectioning)<br> 016 * Copyright (C) 2015 Tomáš Müller<br> 017 * <a href="mailto:muller@unitime.org">muller@unitime.org</a><br> 018 * <a href="http://muller.unitime.org">http://muller.unitime.org</a><br> 019 * <br> 020 * This library is free software; you can redistribute it and/or modify 021 * it under the terms of the GNU Lesser General Public License as 022 * published by the Free Software Foundation; either version 3 of the 023 * License, or (at your option) any later version. <br> 024 * <br> 025 * This library is distributed in the hope that it will be useful, but 026 * WITHOUT ANY WARRANTY; without even the implied warranty of 027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 028 * Lesser General Public License for more details. <br> 029 * <br> 030 * You should have received a copy of the GNU Lesser General Public 031 * License along with this library; if not see 032 * <a href='http://www.gnu.org/licenses/'>http://www.gnu.org/licenses/</a>. 033 */ 034public class StudentGroupsChoiceRealFirstOrder extends StudentChoiceOrder { 035 036 public StudentGroupsChoiceRealFirstOrder(DataProperties config) { 037 super(config); 038 } 039 040 public StudentGroupsChoiceRealFirstOrder() { 041 super(new DataProperties()); 042 setReverse(true); 043 } 044 045 @Override 046 public int compare(Student s1, Student s2) { 047 if (s1.getPriority() != s2.getPriority()) return (s1.getPriority().ordinal() < s2.getPriority().ordinal() ? -1 : 1); 048 if (s1.isDummy()) { 049 if (!s2.isDummy()) 050 return 1; 051 } else if (s2.isDummy()) 052 return -1; 053 054 int cmp = -Double.compare(nrGroups(s1), nrGroups(s2)); 055 if (cmp != 0) 056 return cmp; 057 return super.compare(s1, s2); 058 } 059 060 /** 061 * Average number of request groups that the student is involved in 062 * @return number of groups / number of requests 063 */ 064 public double nrGroups(Student s) { 065 double nrGroups = 0.0; 066 for (Request r: s.getRequests()) { 067 if (r instanceof CourseRequest) 068 nrGroups += ((CourseRequest)r).getRequestGroups().size(); 069 } 070 return nrGroups / s.getRequests().size(); 071 } 072}