Skip to content

Commit

Permalink
設定ファイルMazeSolver_conf.hに設定パラメータを集めた
Browse files Browse the repository at this point in the history
  • Loading branch information
tunguska112 committed Oct 25, 2015
1 parent 655a6b0 commit 677794c
Show file tree
Hide file tree
Showing 11 changed files with 122 additions and 93 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Debug/*
*.cproject
*.project
/Debug/
7 changes: 4 additions & 3 deletions Agent.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <stdio.h>
#include <algorithm>

#include "MazeSolver_conf.h"
#include "Agent.h"

#define SEARCH_DEPTH 1

void Agent::reset()
{
Expand Down Expand Up @@ -97,7 +98,7 @@ void Agent::update(const IndexVec &cur, const Direction &cur_wall)
//TODO:到達不可能な壁が連続で出てくる可能性 詰みマスに囲まれた場合はどうなるのだろう
if (dist == cur || calcNextDirection(cur, dist) == 0) {
distIndexList.clear();
path.calcKShortestDistancePath(IndexVec(0,0), mazeGoalList,SEARCH_DEPTH, false);
path.calcKShortestDistancePath(IndexVec(0,0), mazeGoalList,SEARCH_DEPTH1, false);
path.calcNeedToSearchWallIndex();
distIndexList.assign(path.getNeedToSearchIndex().begin(), path.getNeedToSearchIndex().end());
if (distIndexList.empty()) {
Expand Down Expand Up @@ -130,7 +131,7 @@ void Agent::update(const IndexVec &cur, const Direction &cur_wall)
nextDir = 0;

//最終的に走る最短経路を計算
path.calcShortestTimePath(IndexVec(0,0), mazeGoalList, 20, true);
path.calcShortestTimePath(IndexVec(0,0), mazeGoalList, SEARCH_DEPTH2, true);
return;
}
nextDir = calcNextDirection(cur, dist);
Expand Down
8 changes: 4 additions & 4 deletions Agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ class Agent {
public:
Agent(Maze &_maze) :maze(&_maze), state(Agent::IDLE), path(_maze)
{
mazeGoalList.push_back(IndexVec(7,7));
mazeGoalList.push_back(IndexVec(7,8));
mazeGoalList.push_back(IndexVec(8,7));
mazeGoalList.push_back(IndexVec(8,8));
mazeGoalList.push_back(MAZE_GOAL1);
mazeGoalList.push_back(MAZE_GOAL2);
mazeGoalList.push_back(MAZE_GOAL3);
mazeGoalList.push_back(MAZE_GOAL4);
}

//状態を更新する
Expand Down
44 changes: 22 additions & 22 deletions Maze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ Maze::Maze()

void Maze::clear()
{
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
for (int i=0;i<MAZE_SIZE;i++) {
for (int j=0;j<MAZE_SIZE;j++) {
wall[i][j] = 0;
}
}
for (int i=0;i<N;i++) {
wall[N-1][i] |= NORTH | DONE_NORTH;
wall[i][N-1] |= EAST | DONE_EAST;
for (int i=0;i<MAZE_SIZE;i++) {
wall[MAZE_SIZE-1][i] |= NORTH | DONE_NORTH;
wall[i][MAZE_SIZE-1] |= EAST | DONE_EAST;
wall[0][i] |= SOUTH | DONE_SOUTH;
wall[i][0] |= WEST | DONE_WEST;
}
Expand Down Expand Up @@ -66,8 +66,8 @@ bool Maze::loadFromFile(const char *_filename)
if ('0' <= ch && ch <= '9') wall_bin = ch - '0';
else wall_bin = ch - 'a' + 10;

size_t y = N -1 -cnt/N;
size_t x = cnt%N;
size_t y = MAZE_SIZE -1 -cnt/MAZE_SIZE;
size_t x = cnt%MAZE_SIZE;
wall[y][x].byte = wall_bin | 0xf0;
cnt++;
}
Expand All @@ -78,11 +78,11 @@ bool Maze::loadFromFile(const char *_filename)
}


void Maze::loadFromArray(const char asciiData[N+1][N+1])
void Maze::loadFromArray(const char asciiData[MAZE_SIZE+1][MAZE_SIZE+1])
{
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
char ch = asciiData[N-1-i][j];
for (int i=0;i<MAZE_SIZE;i++) {
for (int j=0;j<MAZE_SIZE;j++) {
char ch = asciiData[MAZE_SIZE-1-i][j];
if ( ('0' <= ch && ch <= '9') || ('a' <= ch && ch <= 'f')) {
uint8_t wall_bin;
if ('0' <= ch && ch <= '9') wall_bin = ch - '0';
Expand All @@ -94,20 +94,20 @@ void Maze::loadFromArray(const char asciiData[N+1][N+1])
}
}

void Maze::printWall(const uint8_t value[N][N]) const
void Maze::printWall(const uint8_t value[MAZE_SIZE][MAZE_SIZE]) const
{
bool printValueOn = false;
if (value) printValueOn = true;

for (int y=N-1;y>=0;y--) {
for (int x=0;x<N;x++) {
for (int y=MAZE_SIZE-1;y>=0;y--) {
for (int x=0;x<MAZE_SIZE;x++) {
printf("+");
if(wall[y][x].bits.North) printf("----");
else printf(" ");
}
printf("+\n\r");

for (int x=0;x<N;x++) {
for (int x=0;x<MAZE_SIZE;x++) {
if (wall[y][x].bits.West) printf("|");
else printf(" ");
printf(" ");
Expand All @@ -116,28 +116,28 @@ void Maze::printWall(const uint8_t value[N][N]) const
}
printf("|\n\r");
}
for (int i=0;i<N;i++) {
for (int i=0;i<MAZE_SIZE;i++) {
printf("-----");
}
printf("+\n\r");
}



void Maze::printWall(const bool value[N][N]) const
void Maze::printWall(const bool value[MAZE_SIZE][MAZE_SIZE]) const
{
bool printValueOn = false;
if (value) printValueOn = true;

for (int y=N-1;y>=0;y--) {
for (int x=0;x<N;x++) {
for (int y=MAZE_SIZE-1;y>=0;y--) {
for (int x=0;x<MAZE_SIZE;x++) {
printf("+");
if(wall[y][x].bits.North) printf("----");
else printf(" ");
}
printf("+\n\r");

for (int x=0;x<N;x++) {
for (int x=0;x<MAZE_SIZE;x++) {
if (wall[y][x].bits.West) printf("|");
else printf(" ");
printf(" ");
Expand All @@ -149,7 +149,7 @@ void Maze::printWall(const bool value[N][N]) const
}
printf("|\n\r");
}
for (int i=0;i<N;i++) {
for (int i=0;i<MAZE_SIZE;i++) {
printf("-----");
}
printf("+\n\r");
Expand Down Expand Up @@ -181,7 +181,7 @@ void Maze::updateWall(const IndexVec &cur, const Direction& newState, bool force

void Maze::updateStepMap(const IndexVec &dist)
{
memset(&stepMap, 0xff, sizeof(uint8_t)*N*N);
memset(&stepMap, 0xff, sizeof(uint8_t)*MAZE_SIZE*MAZE_SIZE);
stepMap[dist.y][dist.x] = 0;

std::queue<IndexVec> q;
Expand Down
28 changes: 13 additions & 15 deletions Maze.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
#include <stdlib.h>
#include <sys/types.h>

//TODO:もっといい感じに設定できるようにする ゴールも
#define N 16

#include "MazeSolver_conf.h"


/* 壁の情報・ロボットがどちらに進むかの方向などを表現するのに使う
Expand Down Expand Up @@ -84,17 +82,17 @@ struct __attribute__ ((__packed__)) IndexVec {
inline bool canSum(const IndexVec &obj) const
{
const int8_t res_x = x + obj.x;
if (res_x<0 || N<=res_x) return false;
if (res_x<0 || MAZE_SIZE<=res_x) return false;
const int8_t res_y = y + obj.y;
if (res_y<0 || N<=res_y) return false;
if (res_y<0 || MAZE_SIZE<=res_y) return false;
return true;
}
inline bool canSub(const IndexVec &obj) const
{
const int8_t res_x = x - obj.x;
if (res_x<0 || N<=res_x) return false;
if (res_x<0 || MAZE_SIZE<=res_x) return false;
const int8_t res_y = y - obj.y;
if (res_y<0 || N<=res_y) return false;
if (res_y<0 || MAZE_SIZE<=res_y) return false;
return true;
}
inline uint norm() const
Expand All @@ -103,7 +101,7 @@ struct __attribute__ ((__packed__)) IndexVec {
const int8_t y_abs = y>0?y:-y;
return x_abs + y_abs;
}
inline bool isCorner(){ return x == N-1 || x == 0 || y == N-1 || y == 0; }
inline bool isCorner(){ return x == MAZE_SIZE-1 || x == 0 || y == MAZE_SIZE-1 || y == 0; }

//各方角を表すベクトル
static const IndexVec vecNorth;
Expand All @@ -121,13 +119,13 @@ struct Maze {
private:

public:
Direction wall[N][N];
uint8_t stepMap[N][N];
Direction wall[MAZE_SIZE][MAZE_SIZE];
uint8_t stepMap[MAZE_SIZE][MAZE_SIZE];
Maze();
Maze(const Maze &obj)
{
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
for (int i=0;i<MAZE_SIZE;i++) {
for (int j=0;j<MAZE_SIZE;j++) {
wall[i][j] = obj.wall[i][j];
}
}
Expand All @@ -138,10 +136,10 @@ struct Maze {
//Maze.wallは上下が逆転しているから注意
//file[i][j] = ascii[i][j] = wall[N-1-i][j]
bool loadFromFile(const char *_filename);
void loadFromArray(const char asciiData[N+1][N+1]);
void loadFromArray(const char asciiData[MAZE_SIZE+1][MAZE_SIZE+1]);

void printWall(const uint8_t value[N][N] = NULL) const;
void printWall(const bool value[N][N]) const;
void printWall(const uint8_t value[MAZE_SIZE][MAZE_SIZE] = NULL) const;
void printWall(const bool value[MAZE_SIZE][MAZE_SIZE]) const;
void printStepMap() const;

void updateWall(const IndexVec &cur, const Direction &newState, bool forceSetDone = true);
Expand Down
27 changes: 27 additions & 0 deletions MazeSolver_conf.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef MAZESOLVER_CONF_H_
#define MAZESOLVER_CONF_H_

/***************************************
* 迷路に関するパラメータ
***************************************/
//迷路の大きさ
#define MAZE_SIZE 16

//迷路のゴール座標
#define MAZE_GOAL1 IndexVec(7,7)
#define MAZE_GOAL2 IndexVec(7,8)
#define MAZE_GOAL3 IndexVec(8,7)
#define MAZE_GOAL4 IndexVec(8,8)


/****************************************
* 探索アルゴリズムに関するパラメータ
****************************************/
//一旦ゴールに到達したあとのk最短経路を計算するときのk
#define SEARCH_DEPTH1 2

//探索が終了し、最終的な走行ルートを計算するときのk
#define SEARCH_DEPTH2 20


#endif /* MAZESOLVER_CONF_H_ */
5 changes: 3 additions & 2 deletions ShortestPath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <queue>
#include <algorithm>

#include "MazeSolver_conf.h"
#include "ShortestPath.h"

int ShortestPath::calcShortestDistancePath(const IndexVec &start, const IndexVec &goal, bool onlyUseFoundWall)
Expand All @@ -16,8 +17,8 @@ int ShortestPath::calcShortestDistancePath(const IndexVec &start, const IndexVec
//Dijkstra's algorithm
int ShortestPath::calcShortestDistancePath(const IndexVec &start, const std::list<IndexVec> &goalList, bool onlyUseFoundWall)
{
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
for (int i=0;i<MAZE_SIZE;i++) {
for (int j=0;j<MAZE_SIZE;j++) {
node[i][j].index.x = j;
node[i][j].index.y = i;
node[i][j].from = 0;
Expand Down
2 changes: 1 addition & 1 deletion ShortestPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ShortestPath {
private:

Maze *maze;
Node node[N][N];
Node node[MAZE_SIZE][MAZE_SIZE];
Path shortestDistancePath;
std::vector< Path > k_shortestDistancePath;
int shortestTimePath_index;
Expand Down
Loading

0 comments on commit 677794c

Please sign in to comment.