26 namespace environments {
33 registerProperty(
type);
39 type(
"type", md_.type)
42 registerProperty(
type);
96 LOG(LNOTICE) <<
"Generating an exemplary maze of digits";
148 mic::types::Position2D goal;
149 for (
size_t x = 0; x <
width; x++)
150 for (
size_t y = 0; y <
height; y++)
158 mic::types::Position2D agent;
161 agent.rand(0, width - 1, 0, height - 1);
174 optimal_path_length = abs((
int) goal.x - (
int) agent.x) + abs((
int) goal.y - (
int) agent.y);
179 LOG(LNOTICE) <<
"Generating a fully random " << width <<
"x" << height<<
" maze of digits";
181 static bool maze_generated =
false;
184 if (((
short)
type == -1) && (maze_generated)) {
194 mic::types::Position2D agent(0, width-1, 0, height-1);
200 mic::types::Position2D goal;
203 goal.rand(0, width-1, 0, height-1);
219 std::random_device rd;
220 std::mt19937_64 rng_mt19937_64(rd());
223 std::uniform_int_distribution<size_t> d_dist(0, 8);
227 for(
size_t x=0; x<
width; x++ ){
228 for(
size_t y=0; y<
height; y++ ){
231 if ((x == (
size_t)goal.x) && (y == (
size_t)goal.y))
235 size_t d = d_dist(rng_mt19937_64);
241 maze_generated =
true;
246 LOG(LNOTICE) <<
"Generating a structured random " << width <<
"x" << height<<
" maze of digits";
248 static bool maze_generated =
false;
251 if (((
short)
type == -3) && (maze_generated)) {
261 mic::types::Position2D agent(0, width-1, 0, height-1);
267 mic::types::Position2D goal;
270 goal.rand(0, width-1, 0, height-1);
286 std::random_device rd;
287 std::mt19937_64 rng_mt19937_64(rd());
291 for(
size_t x=0; x<
width; x++ ){
292 for(
size_t y=0; y<
height; y++ ){
295 if ((x == (
size_t)goal.x) && (y == (
size_t)goal.y))
299 float dist = (float)sqrt((x-goal.x)*(x-goal.x) + (y-goal.y)*(y-goal.y));
301 float scaled_dist = 10*dist/sqrt((width*height));
304 if (scaled_dist<1.1) {
307 min = 9 - ((scaled_dist >= 9) ? 9 : scaled_dist);
313 std::uniform_int_distribution<size_t> d_dist(min, max);
314 size_t d = d_dist(rng_mt19937_64);
315 LOG(LDEBUG)<<
" x = " << x <<
" goal.x = " << goal.x <<
" y = " << y <<
" goal.y = " << goal.y <<
" dist = " << dist <<
" scaled_dist = " << scaled_dist <<
" min = " << min <<
" max = " << max <<
" d = " << d;
321 maze_generated =
true;
326 LOG(LNOTICE) <<
"Generating a random patch " << width <<
"x" << height<<
" maze of digits";
328 static bool maze_generated =
false;
331 if (((
short)
type == -5) && (maze_generated)) {
342 mic::types::Position2D agent(0, width-1, 0, height-1);
348 mic::types::Position2D goal;
351 goal.rand(2, width-3, 2, height-3);
367 std::random_device rd;
368 std::mt19937_64 rng_mt19937_64(rd());
371 types::NESWAction dir_min, dir_max;
372 if (((
size_t)goal.x < width/2) && ((
size_t)goal.y < height/2)) {
379 }
else if (((
size_t)goal.x >= width/2) && ((
size_t)goal.y < height/2)) {
386 }
else if (((
size_t)goal.x >= width/2) && ((
size_t)goal.y >= height/2)) {
402 std::uniform_int_distribution<size_t> x_dist(dir_min.dx, dir_max.dx);
403 std::uniform_int_distribution<size_t> y_dist(dir_min.dy, dir_max.dy);
406 mic::types::Position2D cur = goal;
408 std::vector<mic::types::Position2D> path;
409 path.push_back(goal);
413 types::NESWAction action;
414 action.dx = x_dist(rng_mt19937_64);
415 action.dy = y_dist(rng_mt19937_64);
418 if ((cur.x < 0) || (cur.y < 0) || ((
size_t)cur.x >= width) || ((
size_t)cur.y >= height))
422 if ((cur.x == goal.x) && (cur.y == goal.y))
429 float dist = (float)sqrt((cur.x-goal.x)*(cur.x-goal.x) + (cur.y-goal.y)*(cur.y-goal.y));
431 float scaled_dist = 5*dist/sqrt((width*height));
434 if (scaled_dist<1.1) {
437 min = 9 - ((scaled_dist >= 9) ? 9 : scaled_dist);
443 std::uniform_int_distribution<size_t> d_dist(min, max);
444 size_t d = d_dist(rng_mt19937_64);
445 LOG(LDEBUG)<<
" x = " << cur.x <<
" goal.x = " << goal.x <<
" y = " << cur.y <<
" goal.y = " << goal.y <<
" dist = " << dist <<
" scaled_dist = " << scaled_dist <<
" min = " << min <<
" max = " << max <<
" d = " << d;
451 for (
auto point : path) {
455 max = (max == 9) ? 8 : max;
456 min = ((max < 1) ? 1 : max - 1);
457 std::uniform_int_distribution<size_t> d_dist(min, max);
467 for (
auto point : path) {
471 max = (max > 8) ? 7 : max;
472 min = ((max < 2) ? 1 : max - 2);
473 std::uniform_int_distribution<size_t> d2_dist(min, max);
487 for (
auto point : path) {
491 max = (max > 7) ? 6 : max;
492 min = ((max < 2) ? 1 : max - 2);
493 std::uniform_int_distribution<size_t> d3_dist(min, max);
507 for(
size_t x=0; x<
width; x++ ){
508 for(
size_t y=0; y<
height; y++ ){
511 std::uniform_int_distribution<size_t> d_dist(0, 1);
512 size_t d = d_dist(rng_mt19937_64);
514 (*
environment_grid)({(size_t)x, (
size_t)y, (size_t)MazeOfDigitsChannels::Digits}) = d;
522 maze_generated =
true;
527 if ((x_ >= width) || (y_ >= height))
531 (*
environment_grid)({(size_t)x_, (
size_t)y_, (size_t)MazeOfDigitsChannels::Digits}) = value_;
538 for (
size_t x=0; x<grid_->dim(0); x++)
542 for (
size_t y=0; y<grid_->dim(1); y++){
544 for (
size_t x=0; x<grid_->dim(0); x++) {
557 s +=
" " + std::to_string((
unsigned short)(*grid_)({x,y, (size_t)MazeOfDigitsChannels::Digits})) +
" ";
564 for (
size_t x=0; x<grid_->dim(0); x++)
589 mic::types::MatrixXfPtr encoded_grid (
new mic::types::MatrixXf(*
environment_grid));
598 LOG(LDEBUG) <<
"encodeObservation()";
608 mic::types::MatrixXfPtr encoded_obs (
new mic::types::MatrixXf(*obs));
621 LOG(LDEBUG) <<
"getObservation()";
629 for (
long oy=0, ey=(p.y-delta); oy<(
long)
roi_size; oy++, ey++){
630 for (
long ox=0, ex=(p.x-delta); ox<(
long)
roi_size; ox++, ex++) {
632 if ((ex < 0) || (ex >= (
long)width) || (ey < 0) || (ey >= (
long)height)){
639 (*observation_grid)({(size_t)ox,(
size_t)oy, (size_t)MazeOfDigitsChannels::Digits}) = (*environment_grid)({(size_t)ex,(
size_t)ey, (size_t)MazeOfDigitsChannels::Digits});
653 mic::types::MatrixXfPtr encoded_grid (
new mic::types::MatrixXf(height, width));
654 encoded_grid->setZero();
656 for (
size_t y=0; y<
height; y++){
657 for (
size_t x=0; x<
width; x++) {
661 (*encoded_grid)(y,x) = 1;
666 encoded_grid->resize(height*width, 1);
674 mic::types::Position2D position;
675 for (
size_t y=0; y<
height; y++){
676 for (
size_t x=0; x<
width; x++) {
689 LOG(LDEBUG) <<
"New agent position = " << pos_;
715 if ((pos_.x < 0) || ((
size_t)pos_.x >= width))
718 if ((pos_.y < 0) || ((
size_t)pos_.y >= height))
730 if ((pos_.x < 0) || ((
size_t)pos_.x >= width))
733 if ((pos_.y < 0) || ((
size_t)pos_.y >= height))
void initFullyRandomMaze()
virtual float getStateReward(mic::types::Position2D pos_)
virtual std::string observationToString()
virtual void initializeEnvironment()
Abstract class representing an environment.
virtual bool isStateTerminal(mic::types::Position2D pos_)
mic::environments::MazeOfDigits & operator=(const mic::environments::MazeOfDigits &md)
size_t channels
Number of channels.
virtual mic::types::Position2D getAgentPosition()
virtual bool isStateAllowed(mic::types::Position2D pos_)
mic::types::TensorXfPtr getObservation()
virtual mic::types::MatrixXfPtr encodeEnvironment()
std::string gridToString(mic::types::TensorXfPtr &grid_)
mic::configuration::Property< size_t > width
Property: width of the environment.
void initRandomStructuredMaze()
void reRandomAgentPosition()
Class emulating the maze of digits environment.
virtual void initializePropertyDependentVariables()
bool pomdp_flag
Flag related to.
Channel storing the agent position.
void setBiggerDigit(size_t x_, size_t y_, size_t value_)
void initRandomPathMaze()
virtual mic::types::MatrixXfPtr encodeAgentGrid()
Encode the current state of the reduced grid (only the agent position) as a matrix of size [1...
mic::configuration::Property< size_t > roi_size
Property: size of the ROI (region of interest).
virtual bool moveAgentToPosition(mic::types::Position2D pos_)
mic::types::TensorXfPtr observation_grid
virtual mic::types::MatrixXfPtr encodeObservation()
mic::types::TensorXfPtr environment_grid
Tensor storing the environment.
MazeOfDigits(std::string node_name_="maze_of_digits")
mic::configuration::Property< short > type
mic::types::Position2D initial_position
Property: initial position of the agent.
mic::configuration::Property< size_t > height
Property: height of the environment.
unsigned int optimal_path_length
virtual std::string environmentToString()