Skip to content

Commit

Permalink
Deepmind hard solved
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisHsu committed Nov 13, 2021
1 parent 28b824b commit afbc75b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 21 deletions.
84 changes: 73 additions & 11 deletions exec/agent1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,85 @@ int main(int argc, char* const argv[])
// }

// FIXME: Hard-coded map
std::vector<std::string> level = {
"########",
"#. # #",
"# $ #",
"# # ##",
"## # $.#",
"# $ #",
"# .# @#",
"########",
std::vector<std::vector<std::string>> levels = {
{
// Teacher
"########",
"#. # #",
"# $ #",
"# # ##",
"## # $.#",
"# $ #",
"# .# @#",
"########",
},
{
// Medium
"##########",
"# ###### #",
"# #####. #",
"# ##### #",
"# ### $ #",
"#@$ . #",
"# .# $. #",
"# $#### #",
"# #####",
"##########",
},
{
// Medium
"##########",
"# ### #",
"# @## $. #",
"# $. .$$ #",
"# # . #",
"##########",
},
{
// Medium
"##########",
"###@######",
"# .$### ##",
"# $ . $ #",
"# . $ #",
"## ## # #",
"##### # #",
"#### .# #",
"#### #",
"##########",
},
{
// Hard
"#######",
"##### #",
"#@ ## #",
"# $ #",
"## $ #",
"# #..#",
"# $$ #",
"# . .#",
"#######",
},
{
// Hard
"##########",
"###### #",
"#######$ #",
"####### .#",
"# ### $ #",
"#.$ # #",
"# @# ###",
"# $ ## ##",
"# .. #",
"##########",
},
};
std::vector<std::string> &level = levels[5];

// Create solver
Solver solver(level, prefix, extension);
// Solve
std::vector<MoveDirection> policy = solver.solve();
// Optimize policy
policy = PolicyOptimize::optimize(level, policy);
// Replay policy
std::cout << "== Replay ==" << std::endl;
std::vector<std::string> map(level);
Expand Down
1 change: 0 additions & 1 deletion lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ add_library(sokoban-agent
Solver.cpp
defines.cpp
Visualizer.cpp
PolicyOptimize.cpp
State.cpp
)
22 changes: 13 additions & 9 deletions lib/Solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ static inline std::string getBoxKey(const std::string& key){
return key.substr(key.find("B:"));
}

static void printMap(const Map &map){
// Print map
for(std::string row: map){
std::cout << row << std::endl;
}
std::cout << std::endl;
}

std::vector<MoveDirection> Solver::solve(){
clean();
// Initialize current map and current state
Expand All @@ -44,6 +52,7 @@ std::vector<MoveDirection> Solver::solve(){
// Visualize
visualize(0, curState, map);
for(unsigned int iteration=1; ; ++iteration){
// Check fresh state
if(curState->childs.empty()){
// Make actions
for(MoveDirection dir: {
Expand Down Expand Up @@ -102,8 +111,6 @@ std::vector<MoveDirection> Solver::solve(){
}
// Check if curState is dead node
if(curState->childs.empty()){
// Visualize
visualize(0, curState, map);
// Clean dead nodes
while (curState->childs.empty()){
if(curState->parent == nullptr){
Expand Down Expand Up @@ -140,14 +147,15 @@ std::vector<MoveDirection> Solver::solve(){
// Move to the next state
curState = nextState;
map = move(map, nextDir, level);
// Visualize
visualize(0, curState, map);
// Check if reach the maxIter
if(iteration >= maxIter){
// Update alpha, beta, maxIter
alpha = (Decimal)maxIter / (Decimal)(restartCount + 1);
beta = ((curState->finishTargets == 0) ? 1 : ((Decimal)maxIter / (Decimal)curState->finishTargets)) + (Decimal)boxMoveCount / (Decimal)maxIter;
maxIter += deltaIter;
std::cerr << "MaxIter: " << maxIter << std::endl;
// Visualize
visualize(0, curState, map);
curState = restart(map, iteration, curState);
}
}
Expand Down Expand Up @@ -217,18 +225,14 @@ MoveDirection Solver::decide(const State* const state){

void Solver::visualize(const unsigned int iteration, const State* const curState, const Map& map){
if(visualizer.has_value()){
// Print map
for(std::string row: map){
std::cout << row << std::endl;
}
// Copy policy to set
std::unordered_set<const State*> policy;
for(const State* cursor = curState; cursor != nullptr; cursor = cursor->parent){
policy.insert(cursor);
}

// Prologue
visualizer->next(0);
visualizer->next(1);
visualizer->out << std::string("digraph{") << std::endl;
// Print states by BFS
std::queue<State*> stateQueue;
Expand Down

0 comments on commit afbc75b

Please sign in to comment.