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

Merge pull request #168 from balage1551/master

SolutionPrinter able to print into any PrintWriter stream
This commit is contained in:
Stefan Schröder 2015-07-22 22:52:37 +02:00
commit 543d1c2ace

View file

@ -16,6 +16,8 @@
******************************************************************************/
package jsprit.core.reporting;
import java.io.PrintWriter;
import jsprit.core.problem.VehicleRoutingProblem;
import jsprit.core.problem.job.Job;
import jsprit.core.problem.job.Service;
@ -34,33 +36,27 @@ import jsprit.core.problem.solution.route.activity.TourActivity.JobActivity;
*/
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.
*
* <p> Print.CONCISE and Print.VERBOSE are available.
* <p>
* Print.CONCISE and Print.VERBOSE are available.
*
* @author stefan schroeder
*
*/
public enum Print {
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() + "]");
CONCISE, VERBOSE
}
private static class Jobs {
int nServices;
int nShipments;
public Jobs(int nServices, int nShipments) {
super();
this.nServices = nServices;
@ -68,77 +64,139 @@ public class SolutionPrinter {
}
}
public static void print(VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution, Print print){
/**
* 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) {
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";
System.out.format("+--------------------------+%n");
System.out.printf("| problem |%n");
System.out.format("+---------------+----------+%n");
System.out.printf("| indicator | value |%n");
System.out.format("+---------------+----------+%n");
out.format("+--------------------------+%n");
out.printf("| problem |%n");
out.format("+---------------+----------+%n");
out.printf("| indicator | value |%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);
System.out.format(leftAlign, "noServices",jobs.nServices);
System.out.format(leftAlign, "noShipments",jobs.nShipments);
System.out.format(leftAlign, "fleetsize",problem.getFleetSize().toString());
System.out.format("+--------------------------+%n");
out.format(leftAlign, "noServices", jobs.nServices);
out.format(leftAlign, "noShipments", jobs.nShipments);
out.format(leftAlign, "fleetsize", problem.getFleetSize().toString());
out.format("+--------------------------+%n");
String leftAlignSolution = "| %-13s | %-40s | %n";
System.out.format("+----------------------------------------------------------+%n");
System.out.printf("| solution |%n");
System.out.format("+---------------+------------------------------------------+%n");
System.out.printf("| indicator | value |%n");
System.out.format("+---------------+------------------------------------------+%n");
System.out.format(leftAlignSolution, "costs",solution.getCost());
System.out.format(leftAlignSolution, "noVehicles",solution.getRoutes().size());
System.out.format(leftAlignSolution, "unassgndJobs", solution.getUnassignedJobs().size());
System.out.format("+----------------------------------------------------------+%n");
out.format("+----------------------------------------------------------+%n");
out.printf("| solution |%n");
out.format("+---------------+------------------------------------------+%n");
out.printf("| indicator | value |%n");
out.format("+---------------+------------------------------------------+%n");
out.format(leftAlignSolution, "costs", solution.getCost());
out.format(leftAlignSolution, "noVehicles", solution.getRoutes().size());
out.format(leftAlignSolution, "unassgndJobs", solution.getUnassignedJobs().size());
out.format("+----------------------------------------------------------+%n");
if(print.equals(Print.VERBOSE)){
printVerbose(problem,solution);
if (print.equals(Print.VERBOSE)) {
printVerbose(out, problem, solution);
}
}
private static void printVerbose(VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
String leftAlgin = "| %-7s | %-20s | %-21s | %-15s | %-15s | %-15s | %-15s |%n";
System.out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n");
System.out.printf("| detailed solution |%n");
System.out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
System.out.printf("| route | vehicle | activity | job | arrTime | endTime | costs |%n");
int routeNu = 1;
for(VehicleRoute route : solution.getRoutes()){
System.out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
double costs = 0;
System.out.format(leftAlgin, routeNu, getVehicleString(route), route.getStart().getName(), "-", "undef", Math.round(route.getStart().getEndTime()),Math.round(costs));
TourActivity prevAct = route.getStart();
for(TourActivity act : route.getActivities()){
String jobId;
if(act instanceof JobActivity) jobId = ((JobActivity)act).getJob().getId();
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());
costs+=c;
System.out.format(leftAlgin, routeNu, getVehicleString(route), act.getName(), jobId, Math.round(act.getArrTime()), Math.round(act.getEndTime()),Math.round(costs));
prevAct=act;
printVerbose(SYSTEM_OUT_AS_PRINT_WRITER, problem, solution);
SYSTEM_OUT_AS_PRINT_WRITER.flush();
}
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());
costs+=c;
System.out.format(leftAlgin, routeNu, getVehicleString(route), route.getEnd().getName(), "-", Math.round(route.getEnd().getArrTime()), "undef", Math.round(costs));
private static void printVerbose(PrintWriter out, VehicleRoutingProblem problem, VehicleRoutingProblemSolution solution) {
String leftAlgin = "| %-7s | %-20s | %-21s | %-15s | %-15s | %-15s | %-15s |%n";
out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n");
out.printf("| detailed solution |%n");
out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
out.printf("| route | vehicle | activity | job | arrTime | endTime | costs |%n");
int routeNu = 1;
for (VehicleRoute route : solution.getRoutes()) {
out.format("+---------+----------------------+-----------------------+-----------------+-----------------+-----------------+-----------------+%n");
double costs = 0;
out.format(leftAlgin, routeNu, getVehicleString(route), route.getStart().getName(), "-", "undef", Math.round(route.getStart().getEndTime()),
Math.round(costs));
TourActivity prevAct = route.getStart();
for (TourActivity act : route.getActivities()) {
String jobId;
if (act instanceof JobActivity) {
jobId = ((JobActivity) act).getJob().getId();
} 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());
costs += c;
out.format(leftAlgin, routeNu, getVehicleString(route), act.getName(), jobId, Math.round(act.getArrTime()),
Math.round(act.getEndTime()), Math.round(costs));
prevAct = act;
}
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());
costs += c;
out.format(leftAlgin, routeNu, getVehicleString(route), route.getEnd().getName(), "-", Math.round(route.getEnd().getArrTime()), "undef",
Math.round(costs));
routeNu++;
}
System.out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n");
if(!solution.getUnassignedJobs().isEmpty()) {
System.out.format("+----------------+%n");
System.out.format("| unassignedJobs |%n");
System.out.format("+----------------+%n");
out.format("+--------------------------------------------------------------------------------------------------------------------------------+%n");
if (!solution.getUnassignedJobs().isEmpty()) {
out.format("+----------------+%n");
out.format("| unassignedJobs |%n");
out.format("+----------------+%n");
String unassignedJobAlgin = "| %-14s |%n";
for (Job j : solution.getUnassignedJobs()) {
System.out.format(unassignedJobAlgin, j.getId());
out.format(unassignedJobAlgin, j.getId());
}
System.out.format("+----------------+%n");
out.format("+----------------+%n");
}
}
@ -149,11 +207,15 @@ public class SolutionPrinter {
private static Jobs getNuOfJobs(VehicleRoutingProblem problem) {
int nShipments = 0;
int nServices = 0;
for(Job j : problem.getJobs().values()){
if(j instanceof Shipment) nShipments++;
if(j instanceof Service) nServices++;
for (Job j : problem.getJobs().values()) {
if (j instanceof Shipment) {
nShipments++;
}
return new Jobs(nServices,nShipments);
if (j instanceof Service) {
nServices++;
}
}
return new Jobs(nServices, nShipments);
}
}