Skip to content

Commit

Permalink
Merge pull request #477 from einride/master
Browse files Browse the repository at this point in the history
enhancements in TourActivities
  • Loading branch information
oblonski authored Sep 12, 2019
2 parents da0bff3 + 8076d46 commit 74c3c68
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ matrix:
fast_finish: true
include:
- jdk: oraclejdk8
dist: trusty
# Java 9 needs to be manually installed/upgraded
# see: https://github.com/travis-ci/travis-ci/issues/2968#issuecomment-149164058
- jdk: oraclejdk9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public class BreakScheduling implements InsertionStartsListener,JobInsertedListe

private final EventListeners eventListeners;

private Set<VehicleRoute> modifiedRoutes = new HashSet<VehicleRoute>();

public BreakScheduling(VehicleRoutingProblem vrp, StateManager stateManager, ConstraintManager constraintManager) {
this.stateManager = stateManager;
this.breakInsertionCalculator = new BreakInsertionCalculator(vrp.getTransportCosts(), vrp.getActivityCosts(), new LocalActivityInsertionCostsCalculator(vrp.getTransportCosts(), vrp.getActivityCosts(), stateManager), constraintManager, vrp.getJobActivityFactory());
Expand Down Expand Up @@ -78,7 +76,6 @@ public void informJobInserted(Job job2insert, VehicleRoute inRoute, double addit

@Override
public void ruinStarts(Collection<VehicleRoute> routes) {

}

@Override
Expand All @@ -88,7 +85,7 @@ public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassigned
boolean removed = route.getTourActivities().removeJob(aBreak);
if(removed) logger.trace("ruin: {}", aBreak.getId());
}
List<Break> breaks = new ArrayList<Break>();
List<Break> breaks = new ArrayList<>();
for (Job j : unassignedJobs) {
if (j instanceof Break) {
breaks.add((Break) j);
Expand All @@ -99,7 +96,6 @@ public void ruinEnds(Collection<VehicleRoute> routes, Collection<Job> unassigned

@Override
public void removed(Job job, VehicleRoute fromRoute) {
if(fromRoute.getVehicle().getBreak() != null) modifiedRoutes.add(fromRoute);
}

@Override
Expand All @@ -119,6 +115,5 @@ public void informInsertionStarts(Collection<VehicleRoute> vehicleRoutes, Collec
}
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,37 @@ private TourActivities(TourActivities tour2copy) {
}

public TourActivities() {

}

public List<TourActivity> getActivities() {
return Collections.unmodifiableList(tourActivities);
}

public Iterator<TourActivity> iterator() {
return tourActivities.iterator();
final Iterator<TourActivity> iterator = tourActivities.iterator();
return new Iterator<TourActivity>() {
private TourActivity lastReturned = null;

@Override
public boolean hasNext() {
return iterator.hasNext();
}

@Override
public TourActivity next() {
return lastReturned = iterator.next();
}

@Override
public void remove() {
if (lastReturned instanceof JobActivity) {
throw new IllegalStateException("Cannot remove JobActivities via iterator. "
+ "Use TourActivities.removeActivity(), or alternatively, consider TourActivities.removeJob()");
} else {
iterator.remove();
}
}
};
}

public boolean isEmpty() {
Expand Down Expand Up @@ -154,26 +176,35 @@ public boolean removeJob(Job job) {
* @return true if activity has been removed, false otherwise
*/
public boolean removeActivity(TourActivity activity) {
Job job = null;
if (activity instanceof JobActivity) {
job = ((JobActivity) activity).getJob();
if (!(activity instanceof JobActivity)) {
//assumes that an activity can be added only once to tourActivities
return tourActivities.remove(activity);
}

Job job = ((JobActivity) activity).getJob();
boolean jobIsAlsoAssociateToOtherActs = false;
boolean actRemoved = false;
List<TourActivity> acts = new ArrayList<>(tourActivities);
for (TourActivity act : acts) {
for (TourActivity act : new ArrayList<>(tourActivities)) {
if (act == activity) {
tourActivities.remove(act);
if (jobIsAlsoAssociateToOtherActs) {
// other activities also refer to job --> do not remove job
// thus no need to iterate any further
return true;
}
actRemoved = true;
} else {
if (act instanceof JobActivity && job != null) {
if (((JobActivity) act).getJob().equals(job)) {
jobIsAlsoAssociateToOtherActs = true;
if (act instanceof JobActivity && ((JobActivity) act).getJob().equals(job)) {
if (actRemoved) {
// other activities also refer to job --> do not remove job
// thus no need to iterate any further
return true;
}
jobIsAlsoAssociateToOtherActs = true;
}
}
}
if (!jobIsAlsoAssociateToOtherActs && actRemoved) {
if (actRemoved) {
jobs.remove(job);
}
return actRemoved;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.graphhopper.jsprit.core.problem.job.Shipment;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import static org.junit.Assert.*;

Expand Down Expand Up @@ -118,6 +119,19 @@ public void whenRemovingShipment_theirCorrespondingActivitiesShouldBeRemoved() {
assertEquals(0, tour.getActivities().size());
}

@Test
public void removingNonJobActivityShouldWork() {
TourActivity nonJobAct = Mockito.mock(TourActivity.class);

tour.addActivity(nonJobAct);
assertTrue(tour.getActivities().contains(nonJobAct));

tour.removeActivity(nonJobAct);

assertTrue(tour.isEmpty());
assertFalse(tour.getActivities().contains(nonJobAct));
}

@Test
public void removingActivityShouldWork() {
tour.addActivity(act);
Expand Down

0 comments on commit 74c3c68

Please sign in to comment.