-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
754 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "elev_algo/driver"] | ||
path = elev_algo/driver | ||
url = https://github.com/TTK4145/driver-c | ||
[submodule "cost_fns/hall_request_assigner/d-json"] | ||
path = cost_fns/hall_request_assigner/d-json | ||
url = https://github.com/klasbo/d-json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
Hall request assigner | ||
===================== | ||
|
||
Made for "on-the-fly" / "dynamic" hall request assignment, ie. where all hall requests are completely re-assigned every time a new request (or other event) arrives. | ||
|
||
### JSON format: | ||
|
||
Input: | ||
``` | ||
{ | ||
"hallRequests" : | ||
[[Boolean, Boolean], ...], | ||
"states" : | ||
{ | ||
"id_1" : { | ||
"behaviour" : < "idle" | "moving" | "doorOpen" > | ||
"floor" : NonNegativeInteger | ||
"direction" : < "up" | "down" | "stop" > | ||
"cabRequests" : [Boolean, ...] | ||
}, | ||
"id_2" : {...} | ||
} | ||
} | ||
``` | ||
|
||
Output: | ||
``` | ||
{ | ||
"id_1" : [[Boolean, Boolean], ...], | ||
"id_2" : ... | ||
} | ||
``` | ||
|
||
The pairs of boolean hall requests are ordered `[[up-0, down-0], [up-1, down-1], ...]`, which is the standard in all official driver and example code. | ||
|
||
|
||
### Example JSON: | ||
|
||
Input: | ||
``` | ||
{ | ||
"hallRequests" : | ||
[[false,false],[true,false],[false,false],[false,true]], | ||
"states" : { | ||
"one" : { | ||
"behaviour":"moving", | ||
"floor":2, | ||
"direction":"up", | ||
"cabRequests":[false,false,true,true] | ||
}, | ||
"two" : { | ||
"behaviour":"idle", | ||
"floor":0, | ||
"direction":"stop", | ||
"cabRequests":[false,false,false,false] | ||
} | ||
} | ||
} | ||
``` | ||
|
||
Output: | ||
``` | ||
{ | ||
"one" : [[false,false],[false,false],[false,false],[false,true]], | ||
"two" : [[false,false],[true,false],[false,false],[false,false]] | ||
} | ||
``` | ||
|
||
|
||
Usage | ||
----- | ||
|
||
### Downloading: | ||
|
||
See [the releases tab](https://github.com/TTK4145/Project-resources/releases/latest) to find executables. | ||
|
||
To download the code in order to compile yourself, use `git clone --recursive https://github.com/TTK4145/Project-resources` | ||
|
||
#### Building: | ||
|
||
Run `build.sh`, or copy its one line of content and run that. | ||
|
||
### Command line arguments: | ||
|
||
- `-i` | `--input` : JSON input. | ||
- Example: `./hall_request_assigner --input '{"hallRequests":....}'` | ||
- `--travelDuration` : Travel time between two floors in milliseconds (default 2500) | ||
- `--doorOpenDuration` : Door open time in milliseconds (default 3000) | ||
- `--clearRequestType` : When stopping at a floor, clear either `all` requests or only those `inDirn` (default) | ||
|
||
If JSON input is not passed on the command line, the program will read the first line from stdin instead. JSON output is written to stdout. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dmd main.d config.d elevator_algorithm.d elevator_state.d optimal_hall_requests.d d-json/jsonx.d -w -g -ofhall_request_assigner; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
int doorOpenDuration = 3000; | ||
int travelDuration = 2500; | ||
|
||
enum ClearRequestType { | ||
all, | ||
inDirn, | ||
} | ||
|
||
ClearRequestType clearRequestType = ClearRequestType.all; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import std.algorithm; | ||
import std.range; | ||
import std.stdio; | ||
|
||
import elevator_state; | ||
import config; | ||
|
||
|
||
|
||
private bool requestsAbove(ElevatorState e){ | ||
return e.requests[e.floor+1..$].map!(a => a.array.any).any; | ||
} | ||
|
||
private bool requestsBelow(ElevatorState e){ | ||
return e.requests[0..e.floor].map!(a => a.array.any).any; | ||
} | ||
|
||
bool anyRequests(ElevatorState e){ | ||
return e.requests.map!(a => a.array.any).any; | ||
} | ||
|
||
bool anyRequestsAtFloor(ElevatorState e){ | ||
return e.requests[e.floor].array.any; | ||
} | ||
|
||
|
||
bool shouldStop(ElevatorState e){ | ||
final switch(e.direction) with(Dirn){ | ||
case up: | ||
return | ||
e.requests[e.floor][CallType.hallUp] || | ||
e.requests[e.floor][CallType.cab] || | ||
!e.requestsAbove || | ||
e.floor == 0 || | ||
e.floor == e.requests.length-1; | ||
case down: | ||
return | ||
e.requests[e.floor][CallType.hallDown] || | ||
e.requests[e.floor][CallType.cab] || | ||
!e.requestsBelow || | ||
e.floor == 0 || | ||
e.floor == e.requests.length-1; | ||
case stop: | ||
return true; | ||
} | ||
} | ||
|
||
Dirn chooseDirection(ElevatorState e){ | ||
final switch(e.direction) with(Dirn){ | ||
case up: | ||
return | ||
e.requestsAbove ? up : | ||
e.requestsBelow ? down : | ||
stop; | ||
case down, stop: | ||
return | ||
e.requestsBelow ? down : | ||
e.requestsAbove ? up : | ||
stop; | ||
} | ||
} | ||
|
||
ElevatorState clearReqsAtFloor(ElevatorState e, void delegate(CallType c) onClearedRequest = null){ | ||
|
||
auto e2 = e; | ||
|
||
|
||
void clear(CallType c){ | ||
if(e2.requests[e2.floor][c]){ | ||
if(&onClearedRequest){ | ||
onClearedRequest(c); | ||
} | ||
e2.requests[e2.floor][c] = false; | ||
} | ||
} | ||
|
||
|
||
final switch(clearRequestType) with(ClearRequestType){ | ||
case all: | ||
for(auto c = CallType.min; c < e2.requests[0].length; c++){ | ||
clear(c); | ||
} | ||
break; | ||
|
||
case inDirn: | ||
clear(CallType.cab); | ||
|
||
final switch(e.direction) with(Dirn){ | ||
case up: | ||
clear(CallType.hallUp); | ||
if(!e2.requestsAbove){ | ||
clear(CallType.hallDown); | ||
} | ||
break; | ||
case down: | ||
clear(CallType.hallDown); | ||
if(!e2.requestsBelow){ | ||
clear(CallType.hallUp); | ||
} | ||
break; | ||
case stop: | ||
clear(CallType.hallUp); | ||
clear(CallType.hallDown); | ||
break; | ||
} | ||
break; | ||
} | ||
|
||
return e2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
|
||
import std.algorithm; | ||
import std.array; | ||
import std.conv; | ||
import std.range; | ||
|
||
enum CallType : int { | ||
hallUp, | ||
hallDown, | ||
cab | ||
} | ||
|
||
enum HallCallType : int { | ||
up, | ||
down | ||
} | ||
|
||
enum Dirn : int { | ||
down = -1, | ||
stop = 0, | ||
up = 1 | ||
} | ||
|
||
enum ElevatorBehaviour { | ||
idle, | ||
moving, | ||
doorOpen, | ||
} | ||
|
||
|
||
struct LocalElevatorState { | ||
ElevatorBehaviour behaviour; | ||
int floor; | ||
Dirn direction; | ||
bool[] cabRequests; | ||
this(this){ | ||
cabRequests = cabRequests.dup; | ||
} | ||
} | ||
|
||
|
||
struct ElevatorState { | ||
ElevatorBehaviour behaviour; | ||
int floor; | ||
Dirn direction; | ||
bool[3][] requests; | ||
this(this){ | ||
requests = requests.dup; | ||
} | ||
} | ||
|
||
LocalElevatorState local(ElevatorState e){ | ||
return LocalElevatorState( | ||
e.behaviour, | ||
e.floor, | ||
e.direction | ||
); | ||
} | ||
|
||
ElevatorState withRequests(LocalElevatorState e, bool[2][] hallReqs){ | ||
return ElevatorState( | ||
e.behaviour, | ||
e.floor, | ||
e.direction, | ||
zip(hallReqs, e.cabRequests).map!(a => a[0] ~ a[1]).array.to!(bool[3][]), | ||
); | ||
} | ||
|
||
bool[2][] hallRequests(ElevatorState e){ | ||
return e.requests.to!(bool[][]).map!(a => a[0..2]).array.to!(bool[2][]); | ||
} | ||
|
Oops, something went wrong.