Last active
April 21, 2021 20:04
-
-
Save ali2236/a72a868f4690256e2f58f69b01823942 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /* | |
| * Ali Ghanbari - 970216657 | |
| * online compiler: https://dartpad.dev/a72a868f4690256e2f58f69b01823942?null_safety=true | |
| */ | |
| /// | |
| /// Constants | |
| /// | |
| enum Action { Left, Right, Suck } | |
| enum RoomState { Clean, Dirty } | |
| const leftRoom = 0, rightRoom = 1; | |
| /// | |
| /// State | |
| /// | |
| class State { | |
| final int location; // 0 for left, 1 for right | |
| final Map<int, RoomState> rooms; //room location to clean | |
| State(this.location, this.rooms); // constructor | |
| @override | |
| String toString() => '(${roomName(location)}, $rooms)'; | |
| } | |
| /// | |
| /// Agent | |
| /// | |
| class Vacuum { | |
| State state; // enviroment | |
| int _actions = 0; // number of actions | |
| Vacuum(this.state); // constructor | |
| int clean() { | |
| while (!everyRoomClean) { | |
| if (currentRoomIsDirty) { | |
| doAction(Action.Suck); | |
| } else { | |
| if (state.location == leftRoom) { | |
| doAction(Action.Right); | |
| } else { | |
| doAction(Action.Left); | |
| } | |
| } | |
| } | |
| return 3 - _actions; | |
| } | |
| void doAction(Action action) { | |
| if (action == Action.Suck) { | |
| state = State( | |
| state.location, | |
| Map<int, RoomState>.from(state.rooms) | |
| ..[state.location] = RoomState.Clean, | |
| ); | |
| } else if (action == Action.Left) { | |
| state = State(leftRoom, state.rooms); | |
| } else if (action == Action.Right) { | |
| state = State(rightRoom, state.rooms); | |
| } | |
| _actions++; | |
| } | |
| bool get currentRoomIsDirty { | |
| return state.rooms[state.location] == RoomState.Dirty; | |
| } | |
| bool get everyRoomClean { | |
| return state.rooms.values.every((room) => room == RoomState.Clean); | |
| } | |
| } | |
| /// | |
| /// Main Program | |
| /// | |
| void main() { | |
| var allPossibleStates = List.generate( | |
| 8, // 8 possible states | |
| (i) => State((i ~/ 4) % 2, { | |
| leftRoom: roomState((i ~/ 2) % 2 == 0), | |
| rightRoom: roomState(i % 2 == 0), | |
| }), | |
| ); | |
| //print('All Possible States:'); | |
| //print(allPossibleStates); | |
| var allStateScores = allPossibleStates.map((state) => { | |
| 'state': state.toString(), | |
| 'score': Vacuum(state).clean(), | |
| }); | |
| print('\nAll State Scores:'); | |
| print(allStateScores.map((e) => e.toString()).join('\n')); | |
| var averageScore = allStateScores | |
| .map<int>((e) => e['score'] as int) | |
| .reduce((value, sum) => sum + value) / | |
| 8; | |
| print('\nAverage Score:'); | |
| print(averageScore); | |
| } | |
| /// | |
| /// Utility | |
| /// | |
| RoomState roomState(bool isClean) => | |
| isClean ? RoomState.Clean : RoomState.Dirty; | |
| String roomName(int location) => { | |
| leftRoom: 'left', | |
| rightRoom: 'right', | |
| }[location]!; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment