1
0
Fork 0
mirror of https://github.com/graphhopper/jsprit.git synced 2020-01-24 07:45:05 +01:00

SolutionPrinter able to print into any PrintWriter stream

This commit is contained in:
unknown 2015-07-22 20:30:41 +02:00
parent 1604a637a5
commit e74885d83b

View file

@ -16,6 +16,8 @@
******************************************************************************/ ******************************************************************************/
package jsprit.core.reporting; package jsprit.core.reporting;
import java.io.PrintWriter;
import jsprit.core.problem.VehicleRoutingProblem; import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job; import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service; import jsprit.core.problem.job.Service;
@ -34,10 +36,14 @@ import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
*/ */
public class SolutionPrinter { public class SolutionPrinter {
// Wrapping System.out into a PrintWriter
private static final PrintWriter SYSTEM_OUT_AS_PRINT_WRITER = new PrintWriter(System.out);
/** /**
* Enum to indicate verbose-level. * Enum to indicate verbose-level.
* *
* <p> Print.CONCISE and Print.VERBOSE are available. * <p>
* Print.CONCISE and Print.VERBOSE are available.
* *
* @author stefan schroeder * @author stefan schroeder
* *
@ -47,20 +53,10 @@ public class SolutionPrinter {
CONCISE, VERBOSE CONCISE, VERBOSE
} }
/**
* Prints costs and #vehicles to stdout (System.out.println).
*
* @param solution the solution to be printed
*/
public static void print(VehicleRoutingProblemSolution solution){
System.out.println("[costs="+solution.getCost() + "]");
System.out.println("[#vehicles="+solution.getRoutes().size() + "]");
}
private static class Jobs { private static class Jobs {
int nServices; int nServices;
int nShipments; int nShipments;
public Jobs(int nServices, int nShipments) { public Jobs(int nServices, int nShipments) {
super(); super();
this.nServices = nServices; this.nServices = nServices;
@ -68,77 +64,139 @@ public class SolutionPrinter {
} }
} }
/**
* Prints costs and #vehicles to stdout (out.println).
*
* @param solution
* the solution to be printed
*/
public static void print(VehicleRoutingProblemSolution solution) {
print(SYSTEM_OUT_AS_PRINT_WRITER, solution);
SYSTEM_OUT_AS_PRINT_WRITER.flush();
}
/**
* Prints costs and #vehicles to the given writer
*
* @param out
* the destination writer
*
* @param solution
* the solution to be printed
*/
public static void print(PrintWriter out, VehicleRoutingProblemSolution solution) {
out.println("[costs=" + solution.getCost() + "]");
out.println("[#vehicles=" + solution.getRoutes().size() + "]");
}
/**
* Prints costs and #vehicles to the to stdout (out.println).
*
* @param out
* the destination writer
*
* @param solution
* the solution to be printed
*/
public static void print(VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution, Print print) { public static void print(VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution, Print print) {
print(SYSTEM_OUT_AS_PRINT_WRITER, problem, solution, print);
SYSTEM_OUT_AS_PRINT_WRITER.flush();
}
/**
* Prints costs and #vehicles to the given writer
*
* @param out
* the destination writer
*
* @param solution
* the solution to be printed
*/
public static void print(PrintWriter out, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution, Print print) {
String leftAlign = "| %-13s | %-8s | %n"; String leftAlign = "| %-13s | %-8s | %n";
System.out.format("+--------------------------+%n"); out.format("+--------------------------+%n");
System.out.printf("| problem |%n"); out.printf("| problem |%n");
System.out.format("+---------------+----------+%n"); out.format("+---------------+----------+%n");
System.out.printf("| indicator | value |%n"); out.printf("| indicator | value |%n");
System.out.format("+---------------+----------+%n"); out.format("+---------------+----------+%n");
System.out.format(leftAlign, "noJobs", problem.getJobs().values().size()); out.format(leftAlign, "noJobs", problem.getJobs().values().size());
Jobs jobs = getNuOfJobs(problem); Jobs jobs = getNuOfJobs(problem);
System.out.format(leftAlign, "noServices",jobs.nServices); out.format(leftAlign, "noServices", jobs.nServices);
System.out.format(leftAlign, "noShipments",jobs.nShipments); out.format(leftAlign, "noShipments", jobs.nShipments);
System.out.format(leftAlign, "fleetsize",problem.getFleetSize().toString()); out.format(leftAlign, "fleetsize", problem.getFleetSize().toString());
System.out.format("+--------------------------+%n"); out.format("+--------------------------+%n");
String leftAlignSolution = "| %-13s | %-40s | %n"; String leftAlignSolution = "| %-13s | %-40s | %n";
System.out.format("+----------------------------------------------------------+%n"); out.format("+----------------------------------------------------------+%n");
System.out.printf("| solution |%n"); out.printf("| solution |%n");
System.out.format("+---------------+------------------------------------------+%n"); out.format("+---------------+------------------------------------------+%n");
System.out.printf("| indicator | value |%n"); out.printf("| indicator | value |%n");
System.out.format("+---------------+------------------------------------------+%n"); out.format("+---------------+------------------------------------------+%n");
System.out.format(leftAlignSolution, "costs",solution.getCost()); out.format(leftAlignSolution, "costs", solution.getCost());
System.out.format(leftAlignSolution, "noVehicles",solution.getRoutes().size()); out.format(leftAlignSolution, "noVehicles", solution.getRoutes().size());
System.out.format(leftAlignSolution, "unassgndJobs", solution.getUnassignedJobs().size()); out.format(leftAlignSolution, "unassgndJobs", solution.getUnassignedJobs().size());
System.out.format("+----------------------------------------------------------+%n"); out.format("+----------------------------------------------------------+%n");
if (print.equals(Print.VERBOSE)) { if (print.equals(Print.VERBOSE)) {
printVerbose(problem,solution); printVerbose(out, problem, solution);
} }
} }
private static void printVerbose(VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) { private static void printVerbose(VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
printVerbose(SYSTEM_OUT_AS_PRINT_WRITER, problem, solution);
SYSTEM_OUT_AS_PRINT_WRITER.flush();
}
private static void printVerbose(PrintWriter out, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
String leftAlgin = "| %-7s | %-20s | %-21s | %-15s | %-15s | %-15s | %-15s |%n"; String leftAlgin = "| %-7s | %-20s | %-21s | %-15s | %-15s | %-15s | %-15s |%n";
System.out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n"); out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n");
System.out.printf("| detailed solution |%n"); out.printf("| detailed solution |%n");
System.out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n"); out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
System.out.printf("| route | vehicle | activity | job | arrTime | endTime | costs |%n"); out.printf("| route | vehicle | activity | job | arrTime | endTime | costs |%n");
int routeNu = 1; int routeNu = 1;
for (VehicleRoute route : solution.getRoutes()) { for (VehicleRoute route : solution.getRoutes()) {
System.out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n"); out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
double costs = 0; double costs = 0;
System.out.format(leftAlgin, routeNu, getVehicleString(route), route.getStart().getName(), "-", "undef", Math.round(route.getStart().getEndTime()),Math.round(costs)); out.format(leftAlgin, routeNu, getVehicleString(route), route.getStart().getName(), "-", "undef", Math.round(route.getStart().getEndTime()),
Math.round(costs));
TourActivity prevAct = route.getStart(); TourActivity prevAct = route.getStart();
for (TourActivity act : route.getActivities()) { for (TourActivity act : route.getActivities()) {
String jobId; String jobId;
if(act instanceof JobActivity) jobId = ((JobActivity)act).getJob().getId(); if (act instanceof JobActivity) {
else jobId = "-"; jobId = ((JobActivity) act).getJob().getId();
double c = problem.getTransportCosts().getTransportCost(prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle()); } else {
jobId = "-";
}
double c = problem.getTransportCosts().getTransportCost(prevAct.getLocation(), act.getLocation(), prevAct.getEndTime(), route.getDriver(),
route.getVehicle());
c += problem.getActivityCosts().getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle()); c += problem.getActivityCosts().getActivityCost(act, act.getArrTime(), route.getDriver(), route.getVehicle());
costs += c; costs += c;
System.out.format(leftAlgin, routeNu, getVehicleString(route), act.getName(), jobId, Math.round(act.getArrTime()), Math.round(act.getEndTime()),Math.round(costs)); out.format(leftAlgin, routeNu, getVehicleString(route), act.getName(), jobId, Math.round(act.getArrTime()),
Math.round(act.getEndTime()), Math.round(costs));
prevAct = act; prevAct = act;
} }
double c = problem.getTransportCosts().getTransportCost(prevAct.getLocation(), route.getEnd().getLocation(), prevAct.getEndTime(), route.getDriver(), route.getVehicle()); double c = problem.getTransportCosts().getTransportCost(prevAct.getLocation(), route.getEnd().getLocation(), prevAct.getEndTime(),
route.getDriver(), route.getVehicle());
c += problem.getActivityCosts().getActivityCost(route.getEnd(), route.getEnd().getArrTime(), route.getDriver(), route.getVehicle()); c += problem.getActivityCosts().getActivityCost(route.getEnd(), route.getEnd().getArrTime(), route.getDriver(), route.getVehicle());
costs += c; costs += c;
System.out.format(leftAlgin, routeNu, getVehicleString(route), route.getEnd().getName(), "-", Math.round(route.getEnd().getArrTime()), "undef", Math.round(costs)); out.format(leftAlgin, routeNu, getVehicleString(route), route.getEnd().getName(), "-", Math.round(route.getEnd().getArrTime()), "undef",
Math.round(costs));
routeNu++; routeNu++;
} }
System.out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n"); out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n");
if (!solution.getUnassignedJobs().isEmpty()) { if (!solution.getUnassignedJobs().isEmpty()) {
System.out.format("+----------------+%n"); out.format("+----------------+%n");
System.out.format("| unassignedJobs |%n"); out.format("| unassignedJobs |%n");
System.out.format("+----------------+%n"); out.format("+----------------+%n");
String unassignedJobAlgin = "| %-14s |%n"; String unassignedJobAlgin = "| %-14s |%n";
for (Job j : solution.getUnassignedJobs()) { for (Job j : solution.getUnassignedJobs()) {
System.out.format(unassignedJobAlgin, j.getId()); out.format(unassignedJobAlgin, j.getId());
} }
System.out.format("+----------------+%n"); out.format("+----------------+%n");
} }
} }
@ -150,8 +208,12 @@ public class SolutionPrinter {
int nShipments = 0; int nShipments = 0;
int nServices = 0; int nServices = 0;
for (Job j : problem.getJobs().values()) { for (Job j : problem.getJobs().values()) {
if(j instanceof Shipment) nShipments++; if (j instanceof Shipment) {
if(j instanceof Service) nServices++; nShipments++;
}
if (j instanceof Service) {
nServices++;
}
} }
return new Jobs(nServices, nShipments); return new Jobs(nServices, nShipments);
} }