fp-elevators
AI.cpp
You need to implement the functions in this file for the Reach, not the Core.
We have provided stub implementations for these functions in the starter code, so that your game is playable as you implement the Core. You will need to erase the code we provided for these functions when you implement them for the Reach.
Overview
Implementing functions present in AI.cpp
will be necessary for the Reach
component of the project.
The Reach AI and Showcase page has details about how to design your AI and what you will be presenting about it at the Showcase.
Your AI will need to optimize pickups for the benchmarks listed in SatisfactionIndex.cpp
.
These optimizations will focus around some key decisions:
- Where should your elevator idle if there is no other work to do?
- Given that several floors have waiting patrons, which floor should an elevator service next?
- How will you coordinate multiple elevators?
- Who should you pick up from a given floor?
The way you decide to design your AI is completely up to you! We recommend playing the game a couple of times to look for your strategy and then use that to figure out how you want the AI to work.
There are two main components of this page:
AI.cpp
Structs
AI.cpp
/*
* Requires: buildingState is a valid state representing the current building
* Modifies: Nothing
* Effects: Returns a string that represents the decision the AI makes
* given the current state of the building, which it reads
* from buildingState.
* The string should share the exact format as a
* human player's move input.
*/
string getAIMoveString(const BuildingState& buildingState);
This function will take in a read-only
BuildingState
. Given this building state, you must return a string representing a valid move. Remember that a valid move can be any of:
- a
Pass
move (example: “”) - a
pickupMove
(example: “e1p”) - a
service
move (example: “e0f5”)
/*
* Requires: buildingState is a valid state representing the current building
* move is a pickup move that was generated by getAIMoveString()
* floorToPickup represents the floor the pickup move occurred.
* Modifies: Nothing
* Effects: Returns a string representing which people indices
* should be picked up. The string should share the exact format
* as a human player's pickup list input.
*/
string getAIPickupList(const Move& move, const BuildingState& buildingState, const Floor& floorToPickup);
If your AI had decided to perform a pickupMove
, it will then be asked for a pickup list.
Your AI must produce a valid string representing a valid pickup list.
This function is provided with a const Floor&
variable: floorToPickup
. This is a constant reference to the floor where the pickup move was chosen.
Your pickup move must pickup at least one patron from the requested pickup floor, by the elevator the pickup request is associated with.
Structs
A struct
in c++
is simply a class where each member is public by default.
We will use structs
in this project to provide a friendly API of the buildingState
to the AI component. This will allow us to quickly access state data without the worry of functions calls or whether or not the data is protected.
In this project, we will pass a struct
of the buildingState
to the AI component. It is passed by const reference
and is effectively read-only
. In order to prevent naming conflicts, we will use the _
(underscore) naming convention.
Example uses of buildingState
:
Example Read
BuildingState state;
cout << "Current turn: " << state.turn << endl;
for (int i = 0; i < NUM_FLOORS; ++i) {
_Floor floor = state.floors[i];
cout << "There are: " << floor.numPeople;
cout << " people on floor " << floor.floorNum << endl;
for (int j = 0; j < floor.numPeople; ++j) {
_Person patron = floor.people[j];
cout << "Patron " << j << " has an anger level of: ";
cout << patron.angerLevel << endl;
}
}
for (int i = 0; i < NUM_ELEVATORS; ++i) {
_Elevator elevator = state.elevators[i];
cout << "Elevator " << elevator.elevatorId;
cout << " is on floor " << elevator.currentFloor;
cout << " and going to floor " << elevator.targetFloor;
cout << endl;
cout << "This elevator is ";
if (elevator.isServicing) {
cout << "currently servicing a request";
} else {
cout << "not servicing any request";
}
cout << endl;
}
// Output:
Current turn: 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
There are: 0 people on floor 0
Elevator 0 is on floor 0 and going to floor 0
This elevator is not servicing any request
Elevator 0 is on floor 0 and going to floor 0
This elevator is not servicing any request
Elevator 0 is on floor 0 and going to floor 0
This elevator is not servicing any request
Example Read and Write
BuildingState state;
state.turn = 1;
cout << "Current turn: " << state.turn << endl;
for (int i = 0; i < NUM_FLOORS; ++i) {
_Floor floor = state.floors[i];
floor.floorNum = i;
floor.numPeople = i / 3;
cout << "There are: " << floor.numPeople;
cout << " people on floor " << floor.floorNum << endl;
for (int j = 0; j < floor.numPeople; ++j) {
_Person patron = floor.people[j];
patron.angerLevel = j;
cout << "Patron " << j << " has an anger level of: ";
cout << patron.angerLevel << endl;
}
}
for (int i = 0; i < NUM_ELEVATORS; ++i) {
_Elevator elevator = state.elevators[i];
elevator.elevatorId = i;
elevator.currentFloor = i;
elevator.targetFloor = i * 2;
elevator.isServicing = i % 2;
cout << "Elevator " << elevator.elevatorId;
cout << " is on floor " << elevator.currentFloor;
cout << " and going to floor " << elevator.targetFloor;
cout << endl;
cout << "This elevator is ";
if (elevator.isServicing) {
cout << "currently servicing a request";
} else {
cout << "not servicing any request";
}
cout << endl;
}
// Output:
Current turn: 1
There are: 0 people on floor 0
There are: 0 people on floor 1
There are: 0 people on floor 2
There are: 1 people on floor 3
Patron 0 has an anger level of: 0
There are: 1 people on floor 4
Patron 0 has an anger level of: 0
There are: 1 people on floor 5
Patron 0 has an anger level of: 0
There are: 2 people on floor 6
Patron 0 has an anger level of: 0
Patron 1 has an anger level of: 1
There are: 2 people on floor 7
Patron 0 has an anger level of: 0
Patron 1 has an anger level of: 1
There are: 2 people on floor 8
Patron 0 has an anger level of: 0
Patron 1 has an anger level of: 1
There are: 3 people on floor 9
Patron 0 has an anger level of: 0
Patron 1 has an anger level of: 1
Patron 2 has an anger level of: 2
Elevator 0 is on floor 0 and going to floor 0
This elevator is not servicing any request
Elevator 1 is on floor 1 and going to floor 2
This elevator is currently servicing a request
Elevator 2 is on floor 2 and going to floor 4
This elevator is not servicing any request