Skip to content

Commit

Permalink
Merge pull request #3230 from matsim-org/addCarrierAttributeForCalcul…
Browse files Browse the repository at this point in the history
…ationTime

Add carrier attribute for calculation time
  • Loading branch information
rewertvsp authored Apr 23, 2024
2 parents 5af546b + 8e06660 commit 107a34d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class CarriersUtils {

private static final String ATTR_SKILLS = "skills";
private static final String ATTR_JSPRIT_SCORE = "jspritScore";
private static final String ATTR_JSPRIT_Time = "jspritComputationTime";

public static Carrier createCarrier( Id<Carrier> id ){
return new CarrierImpl(id);
Expand Down Expand Up @@ -243,11 +244,13 @@ else if (!carrier.getShipments().isEmpty())
log.info("routing plan for carrier {}", carrier.getId());
NetworkRouter.routePlan(newPlan, netBasedCosts);
solvedVRPCounter.incrementAndGet();
double timeForPlanningAndRouting = (System.currentTimeMillis() - start) / 1000;
log.info("routing for carrier {} finished. Tour planning plus routing took {} seconds.", carrier.getId(),
(System.currentTimeMillis() - start) / 1000);
timeForPlanningAndRouting);
log.info("solved {} out of {} carriers.", solvedVRPCounter.get(), carriers.getCarriers().size());

carrier.setSelectedPlan(newPlan);
setJspritComputationTime(carrier, timeForPlanningAndRouting);
})).get();

}
Expand Down Expand Up @@ -617,6 +620,18 @@ public static Double getJspritScore (CarrierPlan plan) {
return (Double) plan.getAttributes().getAttribute(ATTR_JSPRIT_SCORE);
}

public static double getJspritComputationTime(Carrier carrier){
try {
return (double) carrier.getAttributes().getAttribute(ATTR_JSPRIT_Time);
} catch (Exception e) {
log.error("Requested attribute jspritComputationTime does not exists. Will return " + Integer.MIN_VALUE);
return Integer.MIN_VALUE;
}
}
public static void setJspritComputationTime(Carrier carrier, double time){
carrier.getAttributes().putAttribute(ATTR_JSPRIT_Time, time);
}

public static void writeCarriers(Carriers carriers, String filename ) {
new CarrierPlanWriter( carriers ).write( filename );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,14 @@ void testGetSetJspritIteration(){
Assertions.assertEquals(125, CarriersUtils.getJspritIterations(carrier) );
}

@Test
void testGetSetJspritComputationTime(){
Carrier carrier = new CarrierImpl(Id.create("carrier", Carrier.class));
//Computation time is not set. should return Integer.Min_Value (null is not possible because returning (int)
Assertions.assertEquals(Integer.MIN_VALUE, CarriersUtils.getJspritComputationTime(carrier) );

CarriersUtils.setJspritComputationTime(carrier, 125);
Assertions.assertEquals(125, CarriersUtils.getJspritComputationTime(carrier) );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.api.core.v01.Id;
import org.matsim.core.utils.misc.Time;
import org.matsim.freight.carriers.Carrier;
import org.matsim.freight.carriers.Carriers;
import org.matsim.freight.carriers.CarriersUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
Expand Down Expand Up @@ -61,7 +63,7 @@ public void runAnalysisAndWriteStats(String analysisOutputDirectory) throws IOEx
BufferedWriter bw1 = new BufferedWriter(new FileWriter(fileName));

//Write headline:
bw1.write("carrierId \t MATSimScoreSelectedPlan \t jSpritScoreSelectedPlan \t nuOfTours \t nuOfShipments(input) \t nuOfServices(input) ");
bw1.write("carrierId \t MATSimScoreSelectedPlan \t jSpritScoreSelectedPlan \t nuOfTours \t nuOfShipments(input) \t nuOfServices(input) \t jspritComputationTime[HH:mm:ss]");
bw1.newLine();

final TreeMap<Id<Carrier>, Carrier> sortedCarrierMap = new TreeMap<>(carriers.getCarriers());
Expand All @@ -73,6 +75,11 @@ public void runAnalysisAndWriteStats(String analysisOutputDirectory) throws IOEx
bw1.write("\t" + carrier.getSelectedPlan().getScheduledTours().size());
bw1.write("\t" + carrier.getShipments().size());
bw1.write("\t" + carrier.getServices().size());
if (CarriersUtils.getJspritComputationTime(carrier) != Integer.MIN_VALUE)
bw1.write("\t" + Time.writeTime(CarriersUtils.getJspritComputationTime(carrier), Time.TIMEFORMAT_HHMMSS));
else
bw1.write("\t" + "null");

bw1.newLine();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
carrierId MATSimScoreSelectedPlan jSpritScoreSelectedPlan nuOfTours nuOfShipments(input) nuOfServices(input)
carrier1 -263.4 null 2 0 7
carrierId MATSimScoreSelectedPlan jSpritScoreSelectedPlan nuOfTours nuOfShipments(input) nuOfServices(input) jspritComputationTime[HH:mm:ss]
carrier1 -263.4 null 2 0 7 null

0 comments on commit 107a34d

Please sign in to comment.