Skip to content

Commit

Permalink
Bug fixes: Fix unordered assignment with multiple identical elevator …
Browse files Browse the repository at this point in the history
…states

Bug fixes:
 * Fixed a bug where a single order would not always be given to the elevator with the lowest ID in cases where there were multiple elevators with the exact same state.
  • Loading branch information
klasbo committed Apr 16, 2018
1 parent 7e48074 commit 4a2128e
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions cost_fns/hall_request_assigner/optimal_hall_requests.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ bool[][][string] optimalHallRequests(
}

while(true){
states.sort!("a.time < b.time")();

debug(optimal_hall_requests) writeln;
debug(optimal_hall_requests) writefln("states:\n %(%s,\n %)", states);
debug(optimal_hall_requests) writefln("reqs:\n%( %(%s, %)\n%)", reqs);
Expand All @@ -47,8 +49,6 @@ bool[][][string] optimalHallRequests(
break;
}


states.sort!("a.time < b.time")();
performSingleMove(states[0], reqs);
}

Expand Down Expand Up @@ -227,8 +227,8 @@ bool unvisitedAreImmediatelyAssignable(Req[2][] reqs, State[] states){
void assignImmediate(ref Req[2][] reqs, ref State[] states){
foreach(f, ref reqsAtFloor; reqs){
foreach(c, ref req; reqsAtFloor){
if(req.active && req.assignedTo == string.init){
foreach(ref s; states){
foreach(ref s; states){
if(req.active && req.assignedTo == string.init){
if(s.state.floor == f && !s.state.cabRequests.any){
req.assignedTo = s.id;
s.time += doorOpenDuration.msecs;
Expand Down Expand Up @@ -370,3 +370,27 @@ unittest {
]);
}


unittest {
LocalElevatorState[string] states = [
"1" : LocalElevatorState(ElevatorBehaviour.moving, 1, Dirn.up, [1, 0, 0, 0].to!(bool[])),
"2" : LocalElevatorState(ElevatorBehaviour.idle, 1, Dirn.stop, [1, 0, 0, 0].to!(bool[])),
"3" : LocalElevatorState(ElevatorBehaviour.idle, 1, Dirn.stop, [1, 0, 0, 0].to!(bool[])),
];

bool[2][] hallreqs = [
[true, false],
[false, false],
[false, false],
[false, true],
];


auto optimal = optimalHallRequests(hallreqs, states);

assert(optimal == [
"1" : [[0,0], [0,0], [0,0], [0,1]].to!(bool[][]),
"2" : [[1,0], [0,0], [0,0], [0,0]].to!(bool[][]),
"3" : [[0,0], [0,0], [0,0], [0,0]].to!(bool[][]),
]);
}

0 comments on commit 4a2128e

Please sign in to comment.