Skip to content

Instantly share code, notes, and snippets.

@ali2236
Last active April 22, 2021 17:50
Show Gist options
  • Save ali2236/a69b65959d0207f02adf4e78c532f87a to your computer and use it in GitHub Desktop.
Save ali2236/a69b65959d0207f02adf4e78c532f87a to your computer and use it in GitHub Desktop.
/*
* Ali Ghanbari - 970216657
* online compiler: https://dartpad.dev/a69b65959d0207f02adf4e78c532f87a?null_safety=true
*/
///
/// Constants
///
enum Action { Left, Right, Suck, Idle }
enum RoomState { Clean, Dirty }
const left = 0, right = 1;
///
/// State
///
class Environment {
final int location; // 0 for left, 1 for right
final Map<int, RoomState> rooms;
Environment({required this.location, required this.rooms}); // constructor
RoomState get currentRoom => rooms[location]!;
// check if all rooms are clean
bool get clean => rooms.entries.every((e) => e.value == RoomState.Clean);
@override
String toString() {
return '${roomName(location)}\t${rooms[left]}\t${rooms[right]}\t';
}
}
///
/// Agent
///
Action reflexVacuumAgent(Environment env) {
if (env.currentRoom == RoomState.Dirty) {
return Action.Suck;
} else if (env.location == left) {
return Action.Right;
} else if (env.location == right) {
return Action.Left;
} else {
return Action.Idle;
}
}
int simulate(Environment environment) {
int score = 0;
while (!environment.clean) {
var action = reflexVacuumAgent(environment);
environment = applyAction(environment, action);
score = updateScore(score, action);
}
return score;
}
Environment applyAction(Environment env, Action action) {
if (action == Action.Suck) {
return Environment(
location: env.location,
rooms: Map.from(env.rooms)..[env.location] = RoomState.Clean,
);
} else if (action == Action.Left) {
return Environment(location: left, rooms: env.rooms);
} else if (action == Action.Right) {
return Environment(location: right, rooms: env.rooms);
} else {
return env;
}
}
int updateScore(int score, Action action) {
if (action == Action.Suck) {
return score + 1;
} else {
return score - 1;
}
}
///
/// Main Program
///
void main() {
var allStates = List.generate(
8, // 8 possible states
(i) => Environment(
location: (i ~/ 4) % 2,
rooms: {
left: roomState((i ~/ 2) % 2 == 0),
right: roomState(i % 2 == 0),
},
),
);
print('Loc\tLeft Room\tRight Room\tScore');
print(allStates.map((env) => '$env${simulate(env)}').join('\n'));
print('Average Score: ${allStates.map(simulate).reduce(sum) / 8}');
}
///
/// Utility
///
RoomState roomState(bool isClean) {
return isClean ? RoomState.Clean : RoomState.Dirty;
}
String roomName(int location) {
if (location == left) {
return 'Left';
} else {
return 'Right';
}
}
int Function(int a, int b) sum = (a, b) => a + b;
/* Output
Loc Left Room Right Room Score
Left RoomState.Clean RoomState.Clean 0
Left RoomState.Clean RoomState.Dirty 0
Left RoomState.Dirty RoomState.Clean 1
Left RoomState.Dirty RoomState.Dirty 1
Right RoomState.Clean RoomState.Clean 0
Right RoomState.Clean RoomState.Dirty 1
Right RoomState.Dirty RoomState.Clean 0
Right RoomState.Dirty RoomState.Dirty 1
Average Score: 0.5
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment