+/EE360C
algorithms
git clone https://git.julia.blue/+/EE360C
15a3727
master
1import java .io .File ; 2import java .util .ArrayList ; 3import java .util .Scanner ; 4 5public class Driver { 6public static String filename ; 7public static boolean testBruteForce_r ; 8public static boolean testGS_r ; 9 10public static void main (String []args )throws Exception { 11parseArgs (args ); 12 13Matching problem =parseMatchingProblem (filename ); 14 15testRun (problem ); 16 } 17 18private static void usage () { 19System .err .println ("usage: java Driver [-g] [-b] <filename>" ); 20System .err .println ("\t-b\tTest brute force implementation" ); 21System .err .println ( 22"\t-gr\tTest Gale-Shapley student optimal implementation" ); 23System .exit (1 ); 24 } 25 26public static void parseArgs (String []args ) { 27if (args .length ==0 ) { 28usage (); 29 } 30filename ="" ; 31testBruteForce_r =false ; 32testGS_r =false ; 33for (String s :args ) { 34if (s .equals ("-g" )) { 35testGS_r =true ; 36 }else if (s .equals ("-b" )) { 37testBruteForce_r =true ; 38 }else if (!s .startsWith ("-" )) { 39filename =s ; 40 } 41 } 42 } 43 44public static Matching parseMatchingProblem (String inputFile ) 45throws Exception { 46int m =0 ; 47int n =0 ;// num students 48ArrayList <ArrayList <Integer >>internshipWeights ,studentPrefs ; 49ArrayList <Integer >internshipSlots ; 50ArrayList <Double >studentGPA ; 51ArrayList <Integer >studentMonths ; 52ArrayList <Integer >studentProjects ; 53 54Scanner sc =new Scanner (new File (inputFile )); 55String []inputSizes =sc .nextLine ().split (" " ); 56 57m =Integer .parseInt (inputSizes [0 ]); 58n =Integer .parseInt (inputSizes [1 ]); 59internshipSlots =readSlotsList (sc ,m ); 60studentGPA =readGPAList (sc ,n ); 61studentMonths =readMonthsList (sc ,n ); 62studentProjects =readProjectList (sc ,n ); 63internshipWeights =readPreferenceLists (sc ,m ); 64studentPrefs =readPreferenceLists (sc ,n ); 65 66Matching problem = 67new Matching (m ,n ,internshipWeights ,studentPrefs ,internshipSlots , 68studentGPA ,studentMonths ,studentProjects ); 69return problem ; 70 } 71 72public static ArrayList <Double >readGPAList (Scanner sc ,int n ) { 73ArrayList <Double >gpa =new ArrayList <Double >(0 ); 74 75String []slots =sc .nextLine ().split (" " ); 76for (int i =0 ;i <n ;i ++) {gpa .add (Double .parseDouble (slots [i ])); } 77return gpa ; 78 } 79public static ArrayList <Integer >readMonthsList (Scanner sc ,int n ) { 80ArrayList <Integer >months =new ArrayList <Integer >(0 ); 81 82String []slots =sc .nextLine ().split (" " ); 83for (int i =0 ;i <n ;i ++) {months .add (Integer .parseInt (slots [i ])); } 84return months ; 85 } 86public static ArrayList <Integer >readProjectList (Scanner sc ,int n ) { 87ArrayList <Integer >proj =new ArrayList <Integer >(0 ); 88 89String []slots =sc .nextLine ().split (" " ); 90for (int i =0 ;i <n ;i ++) {proj .add (Integer .parseInt (slots [i ])); } 91return proj ; 92 } 93public static ArrayList <Integer >readSlotsList (Scanner sc ,int m ) { 94ArrayList <Integer >internshipSlots =new ArrayList <Integer >(0 ); 95 96String []slots =sc .nextLine ().split (" " ); 97for (int i =0 ;i <m ;i ++) { 98internshipSlots .add (Integer .parseInt (slots [i ])); 99 } 100 101return internshipSlots ; 102 } 103 104public static ArrayList <ArrayList <Integer >>readPreferenceLists (Scanner sc , 105int m ) { 106ArrayList <ArrayList <Integer >>preferenceLists ; 107preferenceLists =new ArrayList <ArrayList <Integer >>(0 ); 108 109for (int i =0 ;i <m ;i ++) { 110String line =sc .nextLine (); 111String []preferences =line .split (" " ); 112ArrayList <Integer >preferenceList =new ArrayList <Integer >(0 ); 113for (Integer j =0 ;j <preferences .length ;j ++) { 114preferenceList .add (Integer .parseInt (preferences [j ])); 115 } 116preferenceLists .add (preferenceList ); 117 } 118 119return preferenceLists ; 120 } 121 122public static void testRun (Matching problem ) { 123Program1 program =new Program1 (); 124boolean isStable ; 125if (testGS_r ) { 126Matching GSMatching = 127program .stableMarriageGaleShapley_studentoptimal (problem ); 128System .out .println (GSMatching ); 129isStable =program .isStableMatching (GSMatching ); 130System .out .printf ("%s: stable? %s\n" , 131"Gale-Shapley Student Optimal" ,isStable ); 132System .out .println (); 133 } 134if (testBruteForce_r ) { 135Matching BFMatching = 136program .stableMarriageBruteForce_studentoptimal (problem ); 137System .out .println (BFMatching ); 138isStable =program .isStableMatching (BFMatching ); 139System .out .printf ("%s: stable? %s\n" ,"Brute Force Student Optimal" , 140isStable ); 141System .out .println (); 142 } 143 } 144}