+/EE360C
algorithms
git clone https://git.julia.blue/+/EE360C
15a3727
master
1import java .util .ArrayList ; 2 3/** 4* A Matching represents a candidate solution to the Stable Marriage problem. A 5* Matching may or may not be stable. 6*/ 7public class Matching { 8/** 9* Number of internships. 10*/ 11private Integer m ; 12 13/** 14* Number of students. 15*/ 16private Integer n ; 17 18/** 19* A list containing each internship's preference list. 20*/ 21private ArrayList <ArrayList <Integer >>internship_preference ; 22 23/** 24* A list containing each student's preference list. 25*/ 26private ArrayList <ArrayList <Integer >>student_preference ; 27 28/** 29* Number of slots available in each internship. 30*/ 31private ArrayList <Integer >internship_slots ; 32private ArrayList <ArrayList <Integer >>internship_weights ; 33private ArrayList <Double >student_GPA ; 34private ArrayList <Integer >student_months ; 35private ArrayList <Integer >student_projects ; 36/** 37* Matching information representing the index of internship a student is 38* matched to, -1 if not matched. 39* 40* <p>An empty matching is represented by a null value for this field. 41*/ 42private ArrayList <Integer >student_matching ; 43 44public Matching (Integer m ,Integer n , 45ArrayList <ArrayList <Integer >>internship_weights , 46ArrayList <ArrayList <Integer >>student_preference , 47ArrayList <Integer >internship_slots , 48ArrayList <Double >student_GPA , 49ArrayList <Integer >student_months , 50ArrayList <Integer >student_projects ) { 51this .m =m ; 52this .n =n ; 53this .internship_weights =internship_weights ; 54this .student_preference =student_preference ; 55this .internship_slots =internship_slots ; 56this .student_matching =null ; 57this .student_GPA =student_GPA ; 58this .student_months =student_months ; 59this .student_projects =student_projects ; 60internship_preference =Program1 .computeInternshipPreferences ( 61getInternshipCount (),getStudentCount (),internship_weights , 62student_GPA ,student_months ,student_projects ); 63 } 64 65public Matching (Integer m ,Integer n , 66ArrayList <ArrayList <Integer >>internship_weights , 67ArrayList <ArrayList <Integer >>internship_preference , 68ArrayList <ArrayList <Integer >>student_preference , 69ArrayList <Integer >internship_slots , 70ArrayList <Integer >student_matching , 71ArrayList <Double >student_GPA , 72ArrayList <Integer >student_months , 73ArrayList <Integer >student_projects ) { 74this .m =m ; 75this .n =n ; 76this .internship_preference =internship_preference ; 77this .internship_weights =internship_weights ; 78this .student_preference =student_preference ; 79this .internship_slots =internship_slots ; 80this .student_matching =student_matching ; 81this .student_GPA =student_GPA ; 82this .student_months =student_months ; 83this .student_projects =student_projects ; 84 } 85 86/** 87* Constructs a solution to the stable marriage problem, given the problem 88* as a Matching. Take a Matching which represents the problem data with no 89* solution, and a student_matching which solves the problem given in data. 90* 91* @param data The given problem to solve. 92* @param student_matching The solution to the problem. 93*/ 94public Matching (Matching data ,ArrayList <Integer >student_matching ) { 95this (data .m ,data .n ,data .getInternshipWeights (), 96data .internship_preference ,data .student_preference , 97data .internship_slots ,student_matching ,data .getStudentGPA (), 98data .getStudentMonths (),data .getStudentProjects ()); 99 } 100 101/** 102* Creates a Matching from data which includes an empty solution. 103* 104* @param data The Matching containing the problem to solve. 105*/ 106public Matching (Matching data ) { 107this (data .m ,data .n ,data .getInternshipWeights (), 108data .internship_preference ,data .student_preference , 109data .internship_slots ,new ArrayList <Integer >(0 ), 110data .getStudentGPA (),data .getStudentMonths (), 111data .getStudentProjects ()); 112 } 113 114public void setStudentMatching (ArrayList <Integer >student_matching ) { 115this .student_matching =student_matching ; 116 } 117 118public Integer getInternshipCount () {return m ; } 119 120public Integer getStudentCount () {return n ; } 121public void setInternshipPreference (ArrayList <ArrayList <Integer >>pref ) { 122internship_preference =pref ; 123 } 124 125public ArrayList <ArrayList <Integer >>getInternshipWeights () { 126return internship_weights ; 127 } 128 129public ArrayList <ArrayList <Integer >>getInternshipPreference () { 130return internship_preference ; 131 } 132 133public ArrayList <ArrayList <Integer >>getStudentPreference () { 134return student_preference ; 135 } 136 137public ArrayList <Double >getStudentGPA () {return student_GPA ; } 138 139public ArrayList <Integer >getStudentMonths () {return student_months ; } 140 141public ArrayList <Integer >getStudentProjects () {return student_projects ; } 142 143public ArrayList <Integer >getInternshipSlots () {return internship_slots ; } 144 145public ArrayList <Integer >getStudentMatching () {return student_matching ; } 146 147public int totalInternshipSlots () { 148int slots =0 ; 149for (int i =0 ;i <m ;i ++) {slots +=internship_slots .get (i ); } 150return slots ; 151 } 152 153public String getInputSizeString () { 154return String .format ("m=%d n=%d\n" ,m ,n ); 155 } 156 157public String getSolutionString () { 158if (student_matching ==null ) { 159return "" ; 160 } 161 162StringBuilder s =new StringBuilder (); 163for (int i =0 ;i <student_matching .size ();i ++) { 164String str =String .format ("student %d internship %d" ,i , 165student_matching .get (i )); 166s .append (str ); 167if (i !=student_matching .size () -1 ) { 168s .append ("\n" ); 169 } 170 } 171return s .toString (); 172 } 173 174public String toString () { 175return getInputSizeString () +getSolutionString (); 176 } 177}