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

model and test priorities - #242

This commit is contained in:
oblonski 2016-05-24 21:03:14 +02:00
parent ee3b1589ba
commit 6cf2ac2d56
6 changed files with 184 additions and 16 deletions

View file

@ -39,7 +39,6 @@ import java.util.Collection;
*/ */
public class Service extends AbstractJob { public class Service extends AbstractJob {
/** /**
* Builder that builds a service. * Builder that builds a service.
* *
@ -48,6 +47,8 @@ public class Service extends AbstractJob {
public static class Builder<T extends Service> { public static class Builder<T extends Service> {
/** /**
* Returns a new instance of builder that builds a service. * Returns a new instance of builder that builds a service.
* *
@ -86,6 +87,8 @@ public class Service extends AbstractJob {
private boolean twAdded = false; private boolean twAdded = false;
private int priority = 2;
Builder(String id){ Builder(String id){
this.id = id; this.id = id;
timeWindows = new TimeWindowsImpl(); timeWindows = new TimeWindowsImpl();
@ -206,6 +209,20 @@ public class Service extends AbstractJob {
} }
return this; return this;
} }
/**
* Set priority to service. Only 1 = high priority, 2 = medium and 3 = low are allowed.
* <p>
* Default is 2 = medium.
*
* @param priority
* @return builder
*/
public Builder<T> setPriority(int priority) {
if(priority < 1 || priority > 3) throw new IllegalStateException("incorrect priority. only 1 = high, 2 = medium and 3 = low is allowed");
this.priority = priority;
return this;
}
} }
private final String id; private final String id;
@ -226,6 +243,8 @@ public class Service extends AbstractJob {
private final TimeWindows timeWindowManager; private final TimeWindows timeWindowManager;
private final int priority;
Service(Builder builder) { Service(Builder builder) {
id = builder.id; id = builder.id;
serviceTime = builder.serviceTime; serviceTime = builder.serviceTime;
@ -236,6 +255,7 @@ public class Service extends AbstractJob {
name = builder.name; name = builder.name;
location = builder.location; location = builder.location;
timeWindowManager = builder.timeWindows; timeWindowManager = builder.timeWindows;
priority = builder.priority;
} }
public Collection<TimeWindow> getTimeWindows(){ public Collection<TimeWindow> getTimeWindows(){
@ -338,4 +358,15 @@ public class Service extends AbstractJob {
return name; return name;
} }
/**
* Get priority of service. Only 1 = high priority, 2 = medium and 3 = low are allowed.
* <p>
* Default is 2 = medium.
*
* @return priority
*/
public int getPriority() {
return priority;
}
} }

View file

@ -46,6 +46,7 @@ public class Shipment extends AbstractJob {
/** /**
* Builder that builds the shipment. * Builder that builds the shipment.
* *
@ -85,6 +86,8 @@ public class Shipment extends AbstractJob {
private TimeWindowsImpl pickupTimeWindows; private TimeWindowsImpl pickupTimeWindows;
private int priority = 2;
/** /**
* Returns new instance of this builder. * Returns new instance of this builder.
* *
@ -263,6 +266,20 @@ public class Shipment extends AbstractJob {
public Builder addPickupTimeWindow(double earliest, double latest) { public Builder addPickupTimeWindow(double earliest, double latest) {
return addPickupTimeWindow(TimeWindow.newInstance(earliest, latest)); return addPickupTimeWindow(TimeWindow.newInstance(earliest, latest));
} }
/**
* Set priority to shipment. Only 1 = high priority, 2 = medium and 3 = low are allowed.
* <p>
* Default is 2 = medium.
*
* @param priority
* @return builder
*/
public Builder setPriority(int priority) {
if(priority < 1 || priority > 3) throw new IllegalStateException("incorrect priority. only 1 = high, 2 = medium and 3 = low is allowed");
this.priority = priority;
return this;
}
} }
private final String id; private final String id;
@ -289,6 +306,8 @@ public class Shipment extends AbstractJob {
private final TimeWindowsImpl pickupTimeWindows; private final TimeWindowsImpl pickupTimeWindows;
private final int priority;
Shipment(Builder builder) { Shipment(Builder builder) {
this.id = builder.id; this.id = builder.id;
this.pickupServiceTime = builder.pickupServiceTime; this.pickupServiceTime = builder.pickupServiceTime;
@ -302,6 +321,7 @@ public class Shipment extends AbstractJob {
this.deliveryLocation_ = builder.deliveryLocation_; this.deliveryLocation_ = builder.deliveryLocation_;
this.deliveryTimeWindows = builder.deliveryTimeWindows; this.deliveryTimeWindows = builder.deliveryTimeWindows;
this.pickupTimeWindows = builder.pickupTimeWindows; this.pickupTimeWindows = builder.pickupTimeWindows;
this.priority = builder.priority;
} }
@Override @Override
@ -409,5 +429,14 @@ public class Shipment extends AbstractJob {
return name; return name;
} }
/**
* Get priority of shipment. Only 1 = high priority, 2 = medium and 3 = low are allowed.
* <p>
* Default is 2 = medium.
*
* @return priority
*/
public int getPriority() {
return priority;
}
} }

View file

@ -17,6 +17,7 @@
package com.graphhopper.jsprit.core.problem.job; package com.graphhopper.jsprit.core.problem.job;
import com.graphhopper.jsprit.core.problem.Location; import com.graphhopper.jsprit.core.problem.Location;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -30,7 +31,7 @@ public class DeliveryTest {
@Test @Test
public void whenAddingTwoCapDimension_nuOfDimsShouldBeTwo() { public void whenAddingTwoCapDimension_nuOfDimsShouldBeTwo() {
Delivery one = (Delivery) Delivery.Builder.newInstance("s").setLocation(Location.newInstance("foofoo")) Delivery one = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("foofoo"))
.addSizeDimension(0, 2) .addSizeDimension(0, 2)
.addSizeDimension(1, 4) .addSizeDimension(1, 4)
.build(); .build();
@ -42,7 +43,7 @@ public class DeliveryTest {
@Test @Test
public void whenPickupIsBuiltWithoutSpecifyingCapacity_itShouldHvCapWithOneDimAndDimValOfZero() { public void whenPickupIsBuiltWithoutSpecifyingCapacity_itShouldHvCapWithOneDimAndDimValOfZero() {
Delivery one = (Delivery) Delivery.Builder.newInstance("s").setLocation(Location.newInstance("foofoo")) Delivery one = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("foofoo"))
.build(); .build();
assertEquals(1, one.getSize().getNuOfDimensions()); assertEquals(1, one.getSize().getNuOfDimensions());
assertEquals(0, one.getSize().get(0)); assertEquals(0, one.getSize().get(0));
@ -50,7 +51,7 @@ public class DeliveryTest {
@Test @Test
public void whenPickupIsBuiltWithConstructorWhereSizeIsSpecified_capacityShouldBeSetCorrectly() { public void whenPickupIsBuiltWithConstructorWhereSizeIsSpecified_capacityShouldBeSetCorrectly() {
Delivery one = (Delivery) Delivery.Builder.newInstance("s").addSizeDimension(0, 1).setLocation(Location.newInstance("foofoo")) Delivery one = Delivery.Builder.newInstance("s").addSizeDimension(0, 1).setLocation(Location.newInstance("foofoo"))
.build(); .build();
assertEquals(1, one.getSize().getNuOfDimensions()); assertEquals(1, one.getSize().getNuOfDimensions());
assertEquals(1, one.getSize().get(0)); assertEquals(1, one.getSize().get(0));
@ -58,7 +59,7 @@ public class DeliveryTest {
@Test @Test
public void whenAddingSkills_theyShouldBeAddedCorrectly() { public void whenAddingSkills_theyShouldBeAddedCorrectly() {
Delivery s = (Delivery) Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.addRequiredSkill("drill").addRequiredSkill("screwdriver").build(); .addRequiredSkill("drill").addRequiredSkill("screwdriver").build();
assertTrue(s.getRequiredSkills().containsSkill("drill")); assertTrue(s.getRequiredSkills().containsSkill("drill"));
assertTrue(s.getRequiredSkills().containsSkill("ScrewDriver")); assertTrue(s.getRequiredSkills().containsSkill("ScrewDriver"));
@ -66,7 +67,7 @@ public class DeliveryTest {
@Test @Test
public void whenAddingSkillsCaseSens_theyShouldBeAddedCorrectly() { public void whenAddingSkillsCaseSens_theyShouldBeAddedCorrectly() {
Delivery s = (Delivery) Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.addRequiredSkill("DriLl").addRequiredSkill("screwDriver").build(); .addRequiredSkill("DriLl").addRequiredSkill("screwDriver").build();
assertTrue(s.getRequiredSkills().containsSkill("drill")); assertTrue(s.getRequiredSkills().containsSkill("drill"));
assertTrue(s.getRequiredSkills().containsSkill("drilL")); assertTrue(s.getRequiredSkills().containsSkill("drilL"));
@ -74,7 +75,7 @@ public class DeliveryTest {
@Test @Test
public void whenAddingSkillsCaseSensV2_theyShouldBeAddedCorrectly() { public void whenAddingSkillsCaseSensV2_theyShouldBeAddedCorrectly() {
Delivery s = (Delivery) Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.addRequiredSkill("screwDriver").build(); .addRequiredSkill("screwDriver").build();
assertFalse(s.getRequiredSkills().containsSkill("drill")); assertFalse(s.getRequiredSkills().containsSkill("drill"));
assertFalse(s.getRequiredSkills().containsSkill("drilL")); assertFalse(s.getRequiredSkills().containsSkill("drilL"));
@ -82,10 +83,24 @@ public class DeliveryTest {
@Test @Test
public void nameShouldBeAssigned() { public void nameShouldBeAssigned() {
Delivery s = (Delivery) Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setName("name").build(); .setName("name").build();
assertEquals("name", s.getName()); assertEquals("name", s.getName());
} }
@Test
public void whenSettingPriorities_itShouldBeSetCorrectly(){
Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setPriority(3).build();
Assert.assertEquals(3, s.getPriority());
}
@Test
public void whenNotSettingPriorities_defaultShouldBe(){
Delivery s = Delivery.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(2, s.getPriority());
}
} }

View file

@ -17,6 +17,7 @@
package com.graphhopper.jsprit.core.problem.job; package com.graphhopper.jsprit.core.problem.job;
import com.graphhopper.jsprit.core.problem.Location; import com.graphhopper.jsprit.core.problem.Location;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.*; import static org.junit.Assert.*;
@ -30,7 +31,7 @@ public class PickupTest {
@Test @Test
public void whenAddingTwoCapDimension_nuOfDimsShouldBeTwo() { public void whenAddingTwoCapDimension_nuOfDimsShouldBeTwo() {
Pickup one = (Pickup) Pickup.Builder.newInstance("s").setLocation(Location.newInstance("foofoo")) Pickup one = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("foofoo"))
.addSizeDimension(0, 2) .addSizeDimension(0, 2)
.addSizeDimension(1, 4) .addSizeDimension(1, 4)
.build(); .build();
@ -42,7 +43,7 @@ public class PickupTest {
@Test @Test
public void whenPickupIsBuiltWithoutSpecifyingCapacity_itShouldHvCapWithOneDimAndDimValOfZero() { public void whenPickupIsBuiltWithoutSpecifyingCapacity_itShouldHvCapWithOneDimAndDimValOfZero() {
Pickup one = (Pickup) Pickup.Builder.newInstance("s").setLocation(Location.newInstance("foofoo")) Pickup one = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("foofoo"))
.build(); .build();
assertEquals(1, one.getSize().getNuOfDimensions()); assertEquals(1, one.getSize().getNuOfDimensions());
assertEquals(0, one.getSize().get(0)); assertEquals(0, one.getSize().get(0));
@ -50,7 +51,7 @@ public class PickupTest {
@Test @Test
public void whenPickupIsBuiltWithConstructorWhereSizeIsSpecified_capacityShouldBeSetCorrectly() { public void whenPickupIsBuiltWithConstructorWhereSizeIsSpecified_capacityShouldBeSetCorrectly() {
Pickup one = (Pickup) Pickup.Builder.newInstance("s").addSizeDimension(0, 1).setLocation(Location.newInstance("foofoo")) Pickup one = Pickup.Builder.newInstance("s").addSizeDimension(0, 1).setLocation(Location.newInstance("foofoo"))
.build(); .build();
assertEquals(1, one.getSize().getNuOfDimensions()); assertEquals(1, one.getSize().getNuOfDimensions());
assertEquals(1, one.getSize().get(0)); assertEquals(1, one.getSize().get(0));
@ -58,7 +59,7 @@ public class PickupTest {
@Test @Test
public void whenAddingSkills_theyShouldBeAddedCorrectly() { public void whenAddingSkills_theyShouldBeAddedCorrectly() {
Pickup s = (Pickup) Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.addRequiredSkill("drill").addRequiredSkill("screwdriver").build(); .addRequiredSkill("drill").addRequiredSkill("screwdriver").build();
assertTrue(s.getRequiredSkills().containsSkill("drill")); assertTrue(s.getRequiredSkills().containsSkill("drill"));
assertTrue(s.getRequiredSkills().containsSkill("drill")); assertTrue(s.getRequiredSkills().containsSkill("drill"));
@ -67,7 +68,7 @@ public class PickupTest {
@Test @Test
public void whenAddingSkillsCaseSens_theyShouldBeAddedCorrectly() { public void whenAddingSkillsCaseSens_theyShouldBeAddedCorrectly() {
Pickup s = (Pickup) Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.addRequiredSkill("DriLl").addRequiredSkill("screwDriver").build(); .addRequiredSkill("DriLl").addRequiredSkill("screwDriver").build();
assertTrue(s.getRequiredSkills().containsSkill("drill")); assertTrue(s.getRequiredSkills().containsSkill("drill"));
assertTrue(s.getRequiredSkills().containsSkill("drilL")); assertTrue(s.getRequiredSkills().containsSkill("drilL"));
@ -75,7 +76,7 @@ public class PickupTest {
@Test @Test
public void whenAddingSkillsCaseSensV2_theyShouldBeAddedCorrectly() { public void whenAddingSkillsCaseSensV2_theyShouldBeAddedCorrectly() {
Pickup s = (Pickup) Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.addRequiredSkill("screwDriver").build(); .addRequiredSkill("screwDriver").build();
assertFalse(s.getRequiredSkills().containsSkill("drill")); assertFalse(s.getRequiredSkills().containsSkill("drill"));
assertFalse(s.getRequiredSkills().containsSkill("drilL")); assertFalse(s.getRequiredSkills().containsSkill("drilL"));
@ -83,10 +84,24 @@ public class PickupTest {
@Test @Test
public void nameShouldBeAssigned() { public void nameShouldBeAssigned() {
Pickup s = (Pickup) Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc")) Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setName("name").build(); .setName("name").build();
assertEquals("name", s.getName()); assertEquals("name", s.getName());
} }
@Test
public void whenSettingPriorities_itShouldBeSetCorrectly(){
Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setPriority(3).build();
Assert.assertEquals(3, s.getPriority());
}
@Test
public void whenNotSettingPriorities_defaultShouldBe(){
Pickup s = Pickup.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(2, s.getPriority());
}
} }

View file

@ -18,6 +18,7 @@ package com.graphhopper.jsprit.core.problem.job;
import com.graphhopper.jsprit.core.problem.Location; import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow; import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import java.util.HashSet; import java.util.HashSet;
@ -234,4 +235,39 @@ public class ServiceTest {
.setName("name").build(); .setName("name").build();
} }
@Test
public void whenSettingPriorities_itShouldBeSetCorrectly(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setPriority(1).build();
Assert.assertEquals(1, s.getPriority());
}
@Test
public void whenSettingPriorities_itShouldBeSetCorrectly2(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setPriority(3).build();
Assert.assertEquals(3, s.getPriority());
}
@Test
public void whenNotSettingPriorities_defaultShouldBe2(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(2, s.getPriority());
}
@Test(expected = IllegalStateException.class)
public void whenSettingIncorrectPriorities_itShouldThrowException(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setPriority(30).build();
}
@Test(expected = IllegalStateException.class)
public void whenSettingIncorrectPriorities_itShouldThrowException2(){
Service s = Service.Builder.newInstance("s").setLocation(Location.newInstance("loc"))
.setPriority(0).build();
}
} }

View file

@ -20,6 +20,7 @@ import com.graphhopper.jsprit.core.problem.Location;
import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow; import com.graphhopper.jsprit.core.problem.solution.route.activity.TimeWindow;
import com.graphhopper.jsprit.core.util.Coordinate; import com.graphhopper.jsprit.core.util.Coordinate;
import com.graphhopper.jsprit.core.util.TestUtils; import com.graphhopper.jsprit.core.util.TestUtils;
import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.core.Is.is; import static org.hamcrest.core.Is.is;
@ -382,4 +383,45 @@ public class ShipmentTest {
assertEquals("del", s.getDeliveryLocation().getId()); assertEquals("del", s.getDeliveryLocation().getId());
assertEquals("del", s.getDeliveryLocation().getId()); assertEquals("del", s.getDeliveryLocation().getId());
} }
@Test
public void whenSettingPriorities_itShouldBeSetCorrectly(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc"))
.setPriority(1).build();
Assert.assertEquals(1, s.getPriority());
}
@Test
public void whenSettingPriorities_itShouldBeSetCorrectly2(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc"))
.setPriority(3).build();
Assert.assertEquals(3, s.getPriority());
}
@Test
public void whenNotSettingPriorities_defaultShouldBe2(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc"))
.build();
Assert.assertEquals(2, s.getPriority());
}
@Test(expected = IllegalStateException.class)
public void whenSettingIncorrectPriorities_itShouldThrowException(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc"))
.setPriority(30).build();
}
@Test(expected = IllegalStateException.class)
public void whenSettingIncorrectPriorities_itShouldThrowException2(){
Shipment s = Shipment.Builder.newInstance("s").setPickupLocation(Location.newInstance("loc"))
.setDeliveryLocation(Location.newInstance("loc"))
.setPriority(0).build();
}
} }