I need help commenting on the codes below: Code 1 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import
Question:
I need help commenting on the codes below:
Code 1
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
public class PreferenceData {
private final List
private static enum ReadState { STUDENT_MODE, PROJECT_MODE, PREFERENCE_MODE, UNKNOWN; };
public PreferenceData() { super(); this.students = new ArrayList
public void addStudent(Student s) { this.students.add(s); }
public void addStudent(String s) { this.addStudent(Student.createStudent(s)); }
public void addProject(Project p) { this.projects.add(p); } public void addProject(String p) { this.addProject(Project.createProject(p)); }
public void createPreferenceMatrix() { this.preferences = new int[this.students.size()][this.projects.size()]; }
public void setPreference(Student s, Project p, int preference) { this.preferences[this.students.indexOf(s)][this.projects.indexOf(p)] = preference; }
public void setPreference(int row, int column, int preference) { this.preferences[row][column] = preference; }
public void setPreferenceRow(int row, String[] prefValues) { for (int j = 0; j < prefValues.length; j++) { this.preferences[row][j] = Integer.parseInt(prefValues[j]); } }
public List
public List
public int[][] getPreferences() { return preferences; }
@Override public String toString() { return "PreferenceData [students=" + students + ", projects=" + projects + ", preferences=" + Arrays.toString(preferences) + "]"; }
static PreferenceData readData(String inputFile) {
PreferenceData prefs = new PreferenceData(); try(BufferedReader reader = new BufferedReader(new FileReader(inputFile))) { ReadState state = ReadState.UNKNOWN; int row = 0;
while (reader.ready()) { String line = reader.readLine(); switch (line.trim()) { case "Students:": state = ReadState.STUDENT_MODE; break; case "Projects:": state = ReadState.PROJECT_MODE; break; case "Preferences:": prefs.createPreferenceMatrix(); state = ReadState.PREFERENCE_MODE; break; default: switch (state) { case STUDENT_MODE: prefs.addStudent(line); break; case PROJECT_MODE: prefs.addProject(line); break; case PREFERENCE_MODE: prefs.setPreferenceRow(row, line.split(",")); row++; break; default: throw new PreferenceFormatException(line); } }
}
reader.close();
} catch (FileNotFoundException e) { System.err.println("Error opening preferences file. File does not exist as specified."); e.printStackTrace(); } catch (IOException e) { System.err.println("Error reading from file."); e.printStackTrace(); } catch (PreferenceFormatException e) { System.err.println("Preference file in incorrect format. I can't tell which section I'm in."); System.err.println("Line being read: " + e.getCurrentLine()); e.printStackTrace(); }
return prefs; }
public int getPreference(int i, int j) { return this.preferences[i][j]; } public int numStudents() { return this.students.size(); } public int numProjects() { return this.projects.size(); }
}
Code 2
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List;
import ilog.concert.IloException; import ilog.concert.IloIntVar; import ilog.concert.IloRange; import ilog.cplex.IloCplex;
public class Runner {
private static final String INPUT_FILE_NAME = "prefs.csv"; private static final String OUTPUT_FILE_NAME = "assignment.txt";
public static void main(String[] args) {
PreferenceData data = PreferenceData.readData(INPUT_FILE_NAME);
try(BufferedWriter writer = new BufferedWriter(new FileWriter(OUTPUT_FILE_NAME))) {
IloCplex cplex = new IloCplex(); cplex.setOut(null);
List
for (Student s : data.getStudents()) { for (Project p : data.getProjects()) { variableNames.add(s.getName() + ", " + s.getStudentNumber() + ", " + s.getEmail() + ", " + p.getSupervisor()); } } List
for (String name : variableNames) variables.add(cplex.intVar(0, 1, name));
IloIntVar[] varArray = variables.toArray(new IloIntVar[variables.size()]); IloRange[] constraints = new IloRange[data.numStudents() + data.numProjects()]; for (int k = 0; k < data.numStudents(); k++) { IloIntVar[] studentVars = Arrays.copyOfRange(varArray, k * data.numProjects(), (k+1)*data.numProjects()); constraints[k] = cplex.addEq(1, cplex.sum(studentVars)); } for (int k = data.numStudents(); k < data.numStudents() + data.numProjects(); k++) { IloIntVar[] projectVars = new IloIntVar[data.numStudents()]; int column = k - data.numStudents(); for (int i = 0; i < data.numStudents(); i++) { projectVars[i] = varArray[i*data.numProjects() + column]; } constraints[k] = cplex.addLe(cplex.sum(projectVars), data.getProjects().get(column).getCapacity()); } int[] coefficients = new int[data.numStudents() * data.numProjects()]; for (int i = 0; i < data.numStudents(); i++) { for (int j = 0; j < data.numProjects(); j++) { coefficients[i * data.numProjects() + j] = data.getPreference(i, j); } } cplex.addMinimize(cplex.scalProd(varArray, coefficients)); List
if (cplex.solve()) {
double[] values = cplex.getValues(varArray);
solution = new ArrayList
for (int i = 0; i < varArray.length; i++) {
if (1.0 - values[i] < 0.00000001) {
solution.add(variableNames.get(i));
}
}
} for (String s : solution) { writer.write(s + " "); }
cplex.clearModel();
} catch (IloException e) { e.printStackTrace(); } catch (IOException e) { System.err.println("Failed attempt to write to file: " + OUTPUT_FILE_NAME); e.printStackTrace(); }
}
}
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill