-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemoteRebalancingStrategy.java
141 lines (106 loc) · 6.17 KB
/
RemoteRebalancingStrategy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.matsim.contrib.drt.optimizer.rebalancing.remoteBalancing;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.matsim.contrib.drt.analysis.zonal.DrtZonalSystem;
import org.matsim.contrib.drt.analysis.zonal.DrtZone;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingParams;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingStrategy;
import org.matsim.contrib.drt.optimizer.rebalancing.RebalancingUtils;
import org.matsim.contrib.drt.optimizer.rebalancing.mincostflow.AggregatedMinCostRelocationCalculator;
import org.matsim.contrib.drt.optimizer.rebalancing.mincostflow.ZonalRelocationCalculator;
import org.matsim.contrib.drt.optimizer.rebalancing.remoteBalancing.server.Rebalancer;
import org.matsim.contrib.drt.optimizer.rebalancing.targetcalculator.RebalancingTargetCalculator;
import org.matsim.contrib.dvrp.fleet.DvrpVehicle;
import org.matsim.contrib.dvrp.fleet.Fleet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.ToDoubleFunction;
import java.util.stream.Stream;
import static java.util.stream.Collectors.toList;
/**
* Implements the remote balancing strategy, which will receive rebalancing instructions from an external service.
*/
public class RemoteRebalancingStrategy implements RebalancingStrategy {
private static final Logger log = LogManager.getLogger(RemoteRebalancingStrategy.class);
private final ConnectionManager server;
private final DrtZonalSystem zonalSystem;
private final Fleet fleet;
private final RebalancingTargetCalculator rebalancingTargetCalculator;
private final ZonalRelocationCalculator relocationCalculator;
private final RebalancingParams params;
private final RemoteRebalancingParams remoteParams;
public RemoteRebalancingStrategy(ConnectionManager server, DrtZonalSystem zonalSystem, Fleet fleet,
RebalancingTargetCalculator rebalancingTargetCalculator,
ZonalRelocationCalculator relocationCalculator,
RebalancingParams params, RemoteRebalancingParams remoteParams) {
this.server = server;
this.zonalSystem = zonalSystem;
this.fleet = fleet;
this.rebalancingTargetCalculator = rebalancingTargetCalculator;
this.relocationCalculator = relocationCalculator;
this.params = params;
this.remoteParams = remoteParams;
}
@Override
public List<Relocation> calcRelocations(Stream<? extends DvrpVehicle> rebalancableVehicles, double time) {
Map<DrtZone, List<DvrpVehicle>> rebalancableVehiclesPerZone = RebalancingUtils.groupRebalancableVehicles(
zonalSystem, params, rebalancableVehicles, time);
Map<DrtZone, List<DvrpVehicle>> soonIdleVehiclesPerZone = RebalancingUtils.groupSoonIdleVehicles(zonalSystem,
params, fleet, time);
// No rebalancing before start and after end
if (time < remoteParams.startRebalancing || time > remoteParams.endRebalancing || server.skipTimestep(time))
return calculateMinCostRelocations(Rebalancer.RebalancingInstructions.MinCostFlow.newBuilder()
.setAlpha(0.5)
.setBeta(0.5)
.build(), time, rebalancableVehiclesPerZone, soonIdleVehiclesPerZone);
Rebalancer.RebalancingState state = server.setCurrentState(time, rebalancableVehiclesPerZone, soonIdleVehiclesPerZone);
log.info("Time step {}, expected demand per zone: {}", state.getSimulationTime(), state.getExpectedDemandList());
// Check if last time step was signaled
if (state.getSimulationEnded())
return List.of();
Rebalancer.RebalancingInstructions cmd = server.waitForInstructions(time);
if (cmd.hasZoneTargets()) {
return calculateRebalanceTargets(cmd.getZoneTargets(), rebalancableVehiclesPerZone, soonIdleVehiclesPerZone);
} else if (cmd.hasMinCostFlow()) {
return calculateMinCostRelocations(cmd.getMinCostFlow(), time, rebalancableVehiclesPerZone, soonIdleVehiclesPerZone);
}
log.warn("No rebalancing method was given.");
return List.of();
}
private List<Relocation> calculateRebalanceTargets(Rebalancer.RebalancingInstructions.ZoneTargets targets,
Map<DrtZone, List<DvrpVehicle>> rebalancableVehiclesPerZone,
Map<DrtZone, List<DvrpVehicle>> soonIdleVehiclesPerZone) {
if (targets.getVehiclesCount() != zonalSystem.getZones().size()) {
log.error("Invalid number of targets in rebalancing instruction: {}", targets.getVehiclesCount());
return List.of();
}
log.info("Received targets={}", targets.getVehiclesList());
List<AggregatedMinCostRelocationCalculator.DrtZoneVehicleSurplus> surpluses = new ArrayList<>();
int i = 0;
for (DrtZone z : zonalSystem.getZones().values()) {
long target = Math.round(targets.getVehicles(i++));
int rebalancable = rebalancableVehiclesPerZone.getOrDefault(z, List.of()).size();
int soonIdle = soonIdleVehiclesPerZone.getOrDefault(z, List.of()).size();
long surplus = Math.min(rebalancable + soonIdle - target, rebalancable);
surpluses.add(new AggregatedMinCostRelocationCalculator.DrtZoneVehicleSurplus(z, (int) surplus));
}
return relocationCalculator.calcRelocations(surpluses, rebalancableVehiclesPerZone);
}
private List<Relocation> calculateMinCostRelocations(Rebalancer.RebalancingInstructions.MinCostFlow minCostFlow, double time,
Map<DrtZone, List<DvrpVehicle>> rebalancableVehiclesPerZone,
Map<DrtZone, List<DvrpVehicle>> soonIdleVehiclesPerZone) {
ToDoubleFunction<DrtZone> targetFunction = rebalancingTargetCalculator.calculate(time, rebalancableVehiclesPerZone);
double alpha = minCostFlow.getAlpha();
double beta = minCostFlow.getBeta();
log.info("Received alpha={}, beta={}", alpha, beta);
List<AggregatedMinCostRelocationCalculator.DrtZoneVehicleSurplus> vehicleSurpluses = zonalSystem.getZones().values().stream().map(z -> {
int rebalancable = rebalancableVehiclesPerZone.getOrDefault(z, List.of()).size();
int soonIdle = soonIdleVehiclesPerZone.getOrDefault(z, List.of()).size();
int target = (int) Math.floor(alpha * targetFunction.applyAsDouble(z) + beta);
int surplus = Math.min(rebalancable + soonIdle - target, rebalancable);
return new AggregatedMinCostRelocationCalculator.DrtZoneVehicleSurplus(z, surplus);
}).collect(toList());
return relocationCalculator.calcRelocations(vehicleSurpluses, rebalancableVehiclesPerZone);
}
}