Skip to content
Snippets Groups Projects
Commit 84e7261c authored by Paul A. Rubin's avatar Paul A. Rubin
Browse files

Tweaked the routine that reports MIP solutions and added a method to report CP solutions.

parent 3aefc109
No related branches found
No related tags found
No related merge requests found
......@@ -56,17 +56,55 @@ public final class Problem {
int nTrips = (int) Math.round(obj);
// Add each trip to the report.
for (int t = 1; t <= nTrips; t++) {
sb.append("Time ").append(t).append(":\t");
if (t % 2 == 0) {
// Even numbered trips are right to left.
sb.append("Time ").append(t).append(":\t")
.append(format(what(near[t - 1]), what(transit[t]), what(far[t]), false))
.append("\n");
sb.append(format(what(near[t - 1]), what(transit[t]),
what(far[t]), false));
} else {
// Odd numbered trips are left to right.
sb.append("Time ").append(t).append(":\t")
.append(format(what(near[t]), what(transit[t]), what(far[t - 1]), true))
.append("\n");
sb.append(format(what(near[t]), what(transit[t]),
what(far[t - 1]), true));
}
sb.append("\n");
}
return sb.toString();
}
/**
* Generates a string summary of a solution.
*
* The first index in the matrix argument is time, the second is item.
*
* @param nTrips
* @param far
* @param carried
* @return
*/
public static String report(final int nTrips, final double[][] far,
final double[] carried) {
StringBuilder sb = new StringBuilder();
sb.append("""
The farmer arrives at the left bank at time 0.
""");
// Add each trip to the report.
for (int t = 1; t <= nTrips; t++) {
sb.append("Time ").append(t).append(":\t");
String boat;
int c = (int) Math.round(carried[t]);
if (c < ITEMCOUNT) {
boat = ITEMS[c];
} else {
boat = "nothing";
}
if (t % 2 == 0) {
// Trip is far to near.
sb.append(format(what(flip(far[t - 1])), boat, what(far[t]), false));
} else {
// Trip is near to far.
sb.append(format(what(flip(far[t])), boat, what(far[t - 1]), true));
}
sb.append("\n");
}
return sb.toString();
}
......@@ -121,4 +159,17 @@ public final class Problem {
sb.append(String.format("%-" + WIDTH + "s", right));
return sb.toString();
}
/**
* Reverses the 0-1 values in a vector.
* @param x a vector of 0-1 values
* @return the logical negation of x
*/
private static double[] flip(final double[] x) {
double[] y = new double[x.length];
for (int i = 0; i < x.length; i++) {
y[i] = 1 - x[i];
}
return y;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment