Skip to content

Commit

Permalink
refactoring part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
markusstraub committed Nov 29, 2024
1 parent 859f8b6 commit fa3cd6e
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 388 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@
import com.google.inject.Inject;

import at.ac.ait.matsim.drs.analysis.DrsTripsInfoCollector;
import at.ac.ait.matsim.drs.optimizer.BestRequestFinder;
import at.ac.ait.matsim.drs.optimizer.BestMatchFinder;
import at.ac.ait.matsim.drs.optimizer.DrsMatch;
import at.ac.ait.matsim.drs.optimizer.DrsRequest.DrsDriverRequest;
import at.ac.ait.matsim.drs.optimizer.DrsRequest.DrsRiderRequest;
import at.ac.ait.matsim.drs.optimizer.MatchMaker;
import at.ac.ait.matsim.drs.optimizer.MatchingResult;
import at.ac.ait.matsim.drs.optimizer.RequestsCollector;
import at.ac.ait.matsim.drs.optimizer.RequestsFilter;
import at.ac.ait.matsim.drs.optimizer.PotentialMatchFinder;
import at.ac.ait.matsim.drs.optimizer.RequestsRegister;
import at.ac.ait.matsim.drs.run.Drs;
import at.ac.ait.matsim.drs.run.DrsConfigGroup;
Expand Down Expand Up @@ -119,10 +119,10 @@ private void optimizeDrs(boolean isLastIteration) {
riderRequests.size(), Drs.RIDER_MODE);

RequestsRegister requestsRegister = new RequestsRegister(drsConfig, drsData.getH3ZoneSystem());
RequestsFilter requestsFilter = new RequestsFilter(drsConfig, driverRouter);
BestRequestFinder bestRequestFinder = new BestRequestFinder(driverRouter);
PotentialMatchFinder potentialMatchFinder = new PotentialMatchFinder(drsConfig, driverRouter);
BestMatchFinder bestMatchFinder = new BestMatchFinder(driverRouter);
MatchMaker matchMaker = new MatchMaker(drsConfig, driverRequests, riderRequests, requestsRegister,
requestsFilter, bestRequestFinder);
potentialMatchFinder, bestMatchFinder);
MatchingResult result = matchMaker.match();
LOGGER.info("Found {} drs matches.", result.matches().size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@

import at.ac.ait.matsim.drs.util.DrsUtil;

public class BestRequestFinder {
public class BestMatchFinder {
private final RoutingModule router;

public BestRequestFinder(RoutingModule router) {
public BestMatchFinder(RoutingModule router) {
this.router = router;
}

/**
* @return null if no match was found
*/
public DrsMatch findBestRequest(List<DrsMatch> matches) {
public DrsMatch findBestMatch(List<DrsMatch> matches) {
if (matches.isEmpty()) {
return null;
}
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/at/ac/ait/matsim/drs/optimizer/MatchMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,19 @@ public class MatchMaker {
private final List<DrsRiderRequest> originalRiderRequests;
private final List<DrsRiderRequest> riderRequests;
private final RequestsRegister requestsRegister;
private final BestRequestFinder bestRequestFinder;
private final RequestsFilter requestsFilter;
private final BestMatchFinder bestMatchFinder;
private final PotentialMatchFinder potentialMatchFinder;

private List<DrsMatch> matches;
private List<DrsDriverRequest> unmatchedDriverRequests;
private List<DrsRiderRequest> unmatchedRiderRequests;

public MatchMaker(DrsConfigGroup drsConfig, List<DrsDriverRequest> driverRequests,
public MatchMaker(DrsConfigGroup drsConfig,
List<DrsDriverRequest> driverRequests,
List<DrsRiderRequest> riderRequests,
RequestsRegister requestsRegister,
RequestsFilter requestsFilter,
BestRequestFinder bestRequestFinder) {
PotentialMatchFinder potentialMatchFinder,
BestMatchFinder bestMatchFinder) {
this.drsConfig = drsConfig;
// mutable copies of the requests
this.driverRequests = Lists.newArrayList(driverRequests);
Expand All @@ -63,8 +64,8 @@ public MatchMaker(DrsConfigGroup drsConfig, List<DrsDriverRequest> driverRequest
this.originalRiderRequests = List.copyOf(riderRequests);

this.requestsRegister = requestsRegister;
this.requestsFilter = requestsFilter;
this.bestRequestFinder = bestRequestFinder;
this.potentialMatchFinder = potentialMatchFinder;
this.bestMatchFinder = bestMatchFinder;
}

public MatchingResult match() {
Expand All @@ -83,8 +84,8 @@ public MatchingResult match() {
driverRequest.getFromNode(), driverRequest.getToNode(),
driverRequest.getDepartureTime());

List<DrsMatch> filteredMatches = requestsFilter.filterRequests(driverRequest, potentialRiders);
DrsMatch bestMatch = bestRequestFinder.findBestRequest(filteredMatches);
List<DrsMatch> filteredMatches = potentialMatchFinder.filterRequests(driverRequest, potentialRiders);
DrsMatch bestMatch = bestMatchFinder.findBestMatch(filteredMatches);
if (bestMatch == null) {
continue;
}
Expand Down Expand Up @@ -132,8 +133,9 @@ public List<DrsRiderRequest> findPotentialRiders(Node origin, Node destination,
}

static List<DrsRiderRequest> findPotentialRiders(DrsConfigGroup drsConfig,
Stream<DrsRequest> originNearRequests, Stream<DrsRequest> destinationNearRequests,
Stream<DrsRequest> temporalNearRequests) {
Stream<? extends DrsRequest> originNearRequests,
Stream<? extends DrsRequest> destinationNearRequests,
Stream<? extends DrsRequest> temporalNearRequests) {
Stream<DrsRiderRequest> zoneRegistryIntersection = originNearRequests
.filter(destinationNearRequests.collect(Collectors.toList())::contains)
.filter(DrsRiderRequest.class::isInstance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
import at.ac.ait.matsim.drs.run.DrsConfigGroup;
import at.ac.ait.matsim.drs.util.DrsUtil;

// TODO rename? actually creates potential matches and filters them. does not filter requests per se
public class RequestsFilter {
public class PotentialMatchFinder {
private final DrsConfigGroup drsConfig;
private final RoutingModule router;

public RequestsFilter(DrsConfigGroup drsConfig, RoutingModule router) {
public PotentialMatchFinder(DrsConfigGroup drsConfig, RoutingModule router) {
this.drsConfig = drsConfig;
this.router = router;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import at.ac.ait.matsim.drs.util.DrsUtil;

/**
* Collects all DRS trips (requests) from the selected plans of the population.
* Collects all drs trips (requests) from the selected plans of the population.
*
* These trips are expected to be freshly routed / unmatched since
* we reset results from the previous iteration in PlanModificationUndoer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Main Example starting with regular plans (no drs modes yet).
* DRS gets introduced via replanning.
* Drs gets introduced via replanning.
*/
public class RunPerfectMatchExample extends RunSimpleDrsExample {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/**
* Main Example starting with regular plans (no drs modes yet).
* DRS gets introduced via replanning.
* Drs gets introduced via replanning.
*/
public class RunSimpleDrsExample {
private static final Logger LOGGER = LogManager.getLogger();
Expand Down
100 changes: 100 additions & 0 deletions src/test/java/at/ac/ait/matsim/drs/optimizer/BestMatchFinderTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package at.ac.ait.matsim.drs.optimizer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.matsim.api.core.v01.Id;
import org.matsim.api.core.v01.network.Network;
import org.matsim.api.core.v01.population.PlanElement;
import org.matsim.core.router.DefaultRoutingRequest;
import org.matsim.core.router.RoutingModule;
import org.matsim.core.router.RoutingRequest;
import org.matsim.facilities.FacilitiesUtils;

import at.ac.ait.matsim.drs.DrsTestUtil;
import at.ac.ait.matsim.drs.RoutingForTests;
import at.ac.ait.matsim.drs.optimizer.DrsRequest.DrsDriverRequest;
import at.ac.ait.matsim.drs.optimizer.DrsRequest.DrsRiderRequest;

class BestMatchFinderTest {
static Network network;
static DrsDriverRequest driverRequest;
static DrsRiderRequest request2, request3, request4, request5;
static List<? extends PlanElement> request2Route, request3Route, request4Route, request5Route;
static RoutingRequest toRequest2, toRequest3, toRequest4, toRequest5;
static BestMatchFinder bestMatchFinder;

@BeforeAll
static void setup() {
RoutingForTests routingForTests = new RoutingForTests("data/floridsdorf/network.xml");
network = routingForTests.getNetwork();
RoutingModule driverRouter = routingForTests.getDriverRouter();

bestMatchFinder = new BestMatchFinder(driverRouter);

driverRequest = DrsTestUtil.mockDriverRequest(1, 8 * 60 * 60,
network.getLinks().get(Id.createLinkId(1540)),
network.getLinks().get(Id.createLinkId(186)));

request2 = DrsTestUtil.mockRiderRequest(2, 8 * 60 * 60,
network.getLinks().get(Id.createLinkId(1541)),
network.getLinks().get(Id.createLinkId(186)));
toRequest2 = DefaultRoutingRequest.withoutAttributes(
FacilitiesUtils.wrapLink(request2.getFromLink()),
FacilitiesUtils.wrapLink(request2.getToLink()),
driverRequest.getDepartureTime(), driverRequest.getPerson());
request2Route = driverRouter.calcRoute(toRequest2);

request3 = DrsTestUtil.mockRiderRequest(3, 8 * 60 * 60,
network.getLinks().get(Id.createLinkId(1037)),
network.getLinks().get(Id.createLinkId(186)));
toRequest3 = DefaultRoutingRequest.withoutAttributes(
FacilitiesUtils.wrapLink(request3.getFromLink()),
FacilitiesUtils.wrapLink(request3.getToLink()),
driverRequest.getDepartureTime(), driverRequest.getPerson());
request3Route = driverRouter.calcRoute(toRequest3);

request4 = DrsTestUtil.mockRiderRequest(4, 8 * 60 * 60,
network.getLinks().get(Id.createLinkId(186)),
network.getLinks().get(Id.createLinkId(1037)));
toRequest4 = DefaultRoutingRequest.withoutAttributes(
FacilitiesUtils.wrapLink(request4.getFromLink()),
FacilitiesUtils.wrapLink(request4.getToLink()),
driverRequest.getDepartureTime(), driverRequest.getPerson());
request4Route = driverRouter.calcRoute(toRequest4);

request5 = DrsTestUtil.mockRiderRequest(5, 8 * 60 * 60,
network.getLinks().get(Id.createLinkId(688)),
network.getLinks().get(Id.createLinkId(1540)));
toRequest5 = DefaultRoutingRequest.withoutAttributes(
FacilitiesUtils.wrapLink(request5.getFromLink()),
FacilitiesUtils.wrapLink(request5.getToLink()),
driverRequest.getDepartureTime(), driverRequest.getPerson());
request5Route = driverRouter.calcRoute(toRequest5);
}

@Test
void noFilteredRequestsTest() {
assertNull(bestMatchFinder.findBestMatch(Collections.emptyList()));
}

@Test
void findBestRequestTest() {
List<DrsMatch> potentialMatches = new ArrayList<>();
potentialMatches.add(DrsMatch.createMinimal(driverRequest, request4, null));
potentialMatches.add(DrsMatch.createMinimal(driverRequest, request5, null));
potentialMatches.add(DrsMatch.createMinimal(driverRequest, request3, null));
potentialMatches.add(DrsMatch.createMinimal(driverRequest, request2, null));

DrsMatch bestMatch = bestMatchFinder.findBestMatch(potentialMatches);
assertNotNull(bestMatch);
assertEquals("2", bestMatch.getRider().getId().toString());
}
}

This file was deleted.

Loading

0 comments on commit fa3cd6e

Please sign in to comment.