From d1368284acc025e3325688c374b4737011928944 Mon Sep 17 00:00:00 2001 From: kandelirina Date: Sun, 11 Nov 2018 08:48:42 +0200 Subject: [PATCH] add_user_data_to_vehicle_type_id (#71) * add_user_data_to_vehicle_type_id * add in to string * tetss --- .../core/problem/vehicle/VehicleImpl.java | 2 +- .../core/problem/vehicle/VehicleTypeKey.java | 16 +++++++++++++++- .../problem/vehicle/VehicleTypeKeyTest.java | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleImpl.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleImpl.java index 41107525..f6121eaf 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleImpl.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleImpl.java @@ -364,7 +364,7 @@ public class VehicleImpl extends AbstractVehicle { aBreak = builder.aBreak; prohibitedTasks = builder.prohibitedTasks; // setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(),startLocation.getId(),endLocation.getId(),earliestDeparture,latestArrival,skills)); - setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(), startLocation.getId(), endLocation.getId(), earliestDeparture, latestArrival, skills, returnToDepot)); + setVehicleIdentifier(new VehicleTypeKey(type.getTypeId(), startLocation.getId(), endLocation.getId(), earliestDeparture, latestArrival, skills, returnToDepot, getUserData())); } /** diff --git a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKey.java b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKey.java index 4f8bdc44..d66be173 100644 --- a/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKey.java +++ b/jsprit-core/src/main/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKey.java @@ -20,6 +20,8 @@ package com.graphhopper.jsprit.core.problem.vehicle; import com.graphhopper.jsprit.core.problem.AbstractVehicle; import com.graphhopper.jsprit.core.problem.Skills; +import java.util.Objects; + /** * Key to identify similar vehicles *

@@ -36,8 +38,9 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey { public final double latestEnd; public final Skills skills; public final boolean returnToDepot; + public final Object userData; - public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills, boolean returnToDepot) { + public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills, boolean returnToDepot, Object userData) { super(); this.type = typeId; this.startLocationId = startLocationId; @@ -46,6 +49,11 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey { this.latestEnd = latestEnd; this.skills = skills; this.returnToDepot = returnToDepot; + this.userData = userData; + } + + public VehicleTypeKey(String typeId, String startLocationId, String endLocationId, double earliestStart, double latestEnd, Skills skills, boolean returnToDepot) { + this(typeId, startLocationId, endLocationId, earliestStart, latestEnd, skills, returnToDepot, null); } @Override @@ -62,6 +70,7 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey { if (!skills.equals(that.skills)) return false; if (!startLocationId.equals(that.startLocationId)) return false; if (!type.equals(that.type)) return false; + if (!Objects.equals(that.userData, this.userData)) return false; return true; } @@ -79,6 +88,9 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey { result = 31 * result + (int) (temp ^ (temp >>> 32)); result = 31 * result + skills.hashCode(); result = 31 * result + (returnToDepot ? 1 : 0); + if (userData != null) + result = 31 * result + userData.hashCode(); + return result; } @@ -87,6 +99,8 @@ public class VehicleTypeKey extends AbstractVehicle.AbstractTypeKey { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(type).append("_").append(startLocationId).append("_").append(endLocationId) .append("_").append(Double.toString(earliestStart)).append("_").append(Double.toString(latestEnd)); + if (userData != null) + stringBuilder.append("_").append(userData.toString()); return stringBuilder.toString(); } diff --git a/jsprit-core/src/test/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKeyTest.java b/jsprit-core/src/test/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKeyTest.java index 9f195433..2d85694b 100644 --- a/jsprit-core/src/test/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKeyTest.java +++ b/jsprit-core/src/test/java/com/graphhopper/jsprit/core/problem/vehicle/VehicleTypeKeyTest.java @@ -44,4 +44,22 @@ public class VehicleTypeKeyTest { .addSkill("skill3").build(); assertFalse(v1.getVehicleTypeIdentifier().equals(v2.getVehicleTypeIdentifier())); } + + @Test + public void typeIdentifierShouldBeEqual2() { + Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocation(Location.newInstance("start")).addSkill("skill1").addSkill("skill2") + .setUserData(new String("it's just a test")).build(); + Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocation(Location.newInstance("start")).addSkill("skill1").addSkill("skill2") + .setUserData(new String("it's just a test")).build(); + assertTrue(v1.getVehicleTypeIdentifier().equals(v2.getVehicleTypeIdentifier())); + } + + @Test + public void typeIdentifierShouldNotBeEqual2() { + Vehicle v1 = VehicleImpl.Builder.newInstance("v1").setStartLocation(Location.newInstance("start")).addSkill("skill1").addSkill("skill2") + .setUserData(new String("it's just a test")).build(); + Vehicle v2 = VehicleImpl.Builder.newInstance("v2").setStartLocation(Location.newInstance("start")).addSkill("skill1").addSkill("skill2") + .setUserData(new String("it's just stupid test")).build(); + assertFalse(v1.getVehicleTypeIdentifier().equals(v2.getVehicleTypeIdentifier())); + } }