Skip to content

Commit

Permalink
MarklovSolver works!
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisHsu committed Nov 9, 2021
1 parent 338479a commit dc916dc
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
27 changes: 27 additions & 0 deletions exec/agent1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ static std::unordered_map<std::string, std::any> parseArgs(int argc, char const
return result;
}

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

int main(int argc, char const *argv[])
{
// Parse command-line arguments
Expand Down Expand Up @@ -68,5 +74,26 @@ int main(int argc, char const *argv[])
solver.attach_Visualizer("output/out", ".dot");
// Solve
std::stack<MoveDirection> policy = solver.solve();
// Replay policy
std::cout << "== Replay ==" << std::endl;
while(!policy.empty()){
map = solver.move(map, policy.top());
switch(policy.top()){
case MoveDirection::Up:
std::cout << "Up" << std::endl;
break;
case MoveDirection::Down:
std::cout << "Down" << std::endl;
break;
case MoveDirection::Left:
std::cout << "Left" << std::endl;
break;
case MoveDirection::Right:
std::cout << "Right" << std::endl;
break;
}
printMap(map);
policy.pop();
}
return 0;
}
2 changes: 1 addition & 1 deletion include/MarklovSolver/MarklovSolver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class MarklovSolver : public Solver{
std::mt19937 random_generator;
void clean();
void visualize(unsigned int iteration, State* curState, const Map& map);
State* update(const Map &map, unsigned int &iteration);
State* update(Map &map, unsigned int &iteration);
Action* decide(const std::vector<Action*> &actions);
std::optional<Visualizer> visualizer;
};
Expand Down
2 changes: 1 addition & 1 deletion include/Solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class Solver{
public:
Solver(const Map map);
virtual std::stack<MoveDirection> solve() = 0;
Map move(const Map& map, const MoveDirection direction);
protected:
Map getMap();
Map move(const Map& map, const MoveDirection direction);
bool isWin(const Map& map);
private:
const Map map;
Expand Down
28 changes: 15 additions & 13 deletions lib/MarklovSolver/MarklovSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ std::stack<MoveDirection> MarklovSolver::solve(){
action->direction = dir;
action->pathCost = 1;
action->confidence = 0;
action->restartCost = 0;
if(getBoxKey(curState->key) != getBoxKey(nextState->key)) {
totalBoxMoved += 1;
}
action->restartCost = 0;
}
// Unsolved or solved
if(finished || curState->actions.empty()){
Expand Down Expand Up @@ -126,6 +123,9 @@ std::stack<MoveDirection> MarklovSolver::solve(){
}
// Move
Action* decision = decide(curState->actions);
if(getBoxKey(curState->key) != getBoxKey(decision->next->key)) {
totalBoxMoved += 1;
}
map = move(map, decision->direction);
decision->pathCost += 1;
// Update policy & current state
Expand All @@ -141,8 +141,6 @@ std::stack<MoveDirection> MarklovSolver::solve(){
return result;
}



void MarklovSolver::clean(){
totalBoxMoved = 0;
totalRestart = 0;
Expand Down Expand Up @@ -185,7 +183,7 @@ void MarklovSolver::visualize(unsigned int iteration, State* curState, const Map
<< "Man=(" << state->manPosition.first << ", " << state->manPosition.second << ")\\n"
<< "\"";
if(state == curState){
visualizer->out << ",color=blue";
visualizer->out << ", style=filled, fillcolor=orange";
}
visualizer->out << "]" << std::endl;
// Print actions
Expand Down Expand Up @@ -217,7 +215,7 @@ void MarklovSolver::visualize(unsigned int iteration, State* curState, const Map
}
}
// Print additional values
visualizer->out << "additional[label=\""
visualizer->out << "\n\tadditional[label=\""
<< "a=" << alpha << "\\n"
<< "b=" << beta << "\\n"
<< "r=" << gamma << "\\n"
Expand Down Expand Up @@ -254,21 +252,24 @@ Action* MarklovSolver::decide(const std::vector<Action*> &actions){

#define divide_guard(EXPR) ((EXPR > 0) ? EXPR : 0.5)

State* MarklovSolver::update(const Map &map, unsigned int &iteration){
State* MarklovSolver::update(Map &map, unsigned int &iteration){
// Check dead
State* curState = policy.top()->next;
std::vector<Position> boxPositions = curState->boxPosition;
bool isDead = false;
for(Position pos: boxPositions){
int nextToWall = 0;
int row = pos.first, col = pos.second;
int col = pos.first, row = pos.second;
// Check if box on target
if(map[row][col] == '%'){
continue;
}
if(map[row][col+1] == '#' || map[row][col-1] == '#'){
// Check horizontal
if((map[row][col+1] == '#') || (map[row][col-1] == '#')){
nextToWall += 1;
}
if(map[row+1][col] == '#' || map[row-1][col] == '#'){
// Check vertical
if((map[row+1][col] == '#') || (map[row-1][col] == '#')){
nextToWall += 1;
}
if(nextToWall == 2){
Expand Down Expand Up @@ -306,7 +307,8 @@ State* MarklovSolver::update(const Map &map, unsigned int &iteration){
totalRestart += 1;
totalBoxMoved = 0;
iteration = 0;
return allStates[State::getKey(getMap())];
map = getMap();
return allStates[State::getKey(map)];
}

void MarklovSolver::attach_Visualizer(std::string prefix, std::string extention){
Expand Down

0 comments on commit dc916dc

Please sign in to comment.