Coverage Control Library
Loading...
Searching...
No Matches
oracle_explore_exploit.h
Go to the documentation of this file.
1/*
2 * This file is part of the CoverageControl library
3 *
4 * Author: Saurav Agarwal
5 * Contact: sauravag@seas.upenn.edu, agr.saurav1@gmail.com
6 * Repository: https://github.com/KumarRobotics/CoverageControl
7 *
8 * Copyright (c) 2024, Saurav Agarwal
9 *
10 * The CoverageControl library is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or (at your
13 * option) any later version.
14 *
15 * The CoverageControl library is distributed in the hope that it will be
16 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
18 * Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along with
21 * CoverageControl library. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_ORACLE_EXPLORE_EXPLOIT_H_
31#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_ORACLE_EXPLORE_EXPLOIT_H_
32
33#include <omp.h>
34
35#include <fstream>
36#include <iostream>
37#include <random>
38#include <vector>
39
43#include "CoverageControl/extern/lsap/Hungarian.h"
46
47namespace CoverageControl {
48
50 private:
51 Parameters const params_;
52 size_t num_robots_ = 0;
53 CoverageSystem &env_;
54 Voronoi voronoi_;
55 std::vector<VoronoiCell> voronoi_cells_;
56 PointVector robot_global_positions_;
57 PointVector goals_, actions_;
58 std::vector<std::vector<double>> cost_matrix_;
59 MapType oracle_map_;
60 MapType exploration_map_; // Binary map: true for unexplored locations
61 MapType explored_idf_map_;
62 double exploration_ratio_ = 1;
63 int recompute_goal_steps_ = 0, recompute_goal_counter_ = 0;
64 bool is_converged_ = false;
65 bool start_exploit_ = false;
66
67 public:
68 OracleExploreExploit(Parameters const &params, size_t const &num_robots,
69 CoverageSystem &env)
70 : params_{params}, num_robots_{num_robots}, env_{env} {
71 cost_matrix_.resize(num_robots_, std::vector<double>(num_robots_));
72 voronoi_cells_.resize(num_robots_);
73
74 robot_global_positions_ = env_.GetRobotPositions();
75 actions_.resize(num_robots_);
76 goals_.resize(num_robots_);
77
78 // The oracle map is designed to store the pixels seen by any robot
79 oracle_map_ =
80 MapType::Constant(params_.pWorldMapSize, params_.pWorldMapSize, 0);
81 exploration_map_ =
82 MapType::Constant(params_.pWorldMapSize, params_.pWorldMapSize, 1);
83 explored_idf_map_ =
84 MapType::Constant(params_.pWorldMapSize, params_.pWorldMapSize, 0);
87 }
88
89 PointVector GetActions() { return actions_; }
90
91 MapType const &GetOracleMap() { return oracle_map_; }
92
93 void SetGoals(PointVector const &goals) { goals_ = goals; }
94
95 auto GetGoals() { return goals_; }
96
97 std::vector<VoronoiCell> GetVoronoiCells() { return voronoi_cells_; }
98
100 robot_global_positions_ = env_.GetRobotPositions();
101 for (size_t i = 0; i < num_robots_; ++i) {
102 MapUtils::MapBounds index, offset;
103 MapUtils::ComputeOffsets(params_.pResolution, robot_global_positions_[i],
104 params_.pSensorSize, params_.pWorldMapSize,
105 index, offset);
106 explored_idf_map_.block(index.left + offset.left,
107 index.bottom + offset.bottom, offset.width,
108 offset.height) =
109 env_.GetRobotSensorView(i).block(offset.left, offset.bottom,
110 offset.width, offset.height);
111 exploration_map_.block(index.left + offset.left,
112 index.bottom + offset.bottom, offset.width,
113 offset.height) =
114 MapType::Zero(params_.pSensorSize, params_.pSensorSize);
115 }
116 exploration_ratio_ = static_cast<double>(exploration_map_.count()) /
117 (params_.pWorldMapSize * params_.pWorldMapSize);
118 oracle_map_ = explored_idf_map_ + 2 * exploration_ratio_ * exploration_map_;
119 }
120
121 double GetExplorationRatio() const { return exploration_ratio_; }
122
124 /* voronoi_ = Voronoi(robot_global_positions_, oracle_map_,
125 * params_.pWorldMapSize, */
126 /* params_.pResolution); */
127 /* for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) { */
128 /* goals_[iRobot] = voronoi_cells_[iRobot].centroid; */
129 /* } */
130 robot_global_positions_ = env_.GetRobotPositions();
131 voronoi_ = NearOptimalCVTAlgorithm(
132 params_.pLloydNumTries, params_.pLloydMaxIterations, num_robots_,
133 oracle_map_, params_.pWorldMapSize, params_.pResolution);
134 voronoi_cells_ = voronoi_.GetVoronoiCells();
135#pragma omp parallel for num_threads(num_robots_)
136 for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) {
137 for (size_t jCentroid = 0; jCentroid < voronoi_cells_.size();
138 ++jCentroid) {
139 cost_matrix_[iRobot][jCentroid] = (robot_global_positions_[iRobot] -
140 voronoi_cells_[jCentroid].centroid())
141 .norm();
142 }
143 }
144 HungarianAlgorithm HungAlgo;
145 std::vector<int> assignment;
146 HungAlgo.Solve(cost_matrix_, assignment);
147
148 goals_.resize(num_robots_);
149 auto ordered_voronoi_cells = voronoi_cells_;
150 for (size_t iRobot = 0; iRobot < num_robots_; ++iRobot) {
151 goals_[iRobot] = voronoi_cells_[assignment[iRobot]].centroid();
152 ordered_voronoi_cells[iRobot] = voronoi_cells_[assignment[iRobot]];
153 }
154 voronoi_cells_ = ordered_voronoi_cells;
155 if (exploration_ratio_ < 0.1) {
156 start_exploit_ = true;
157 } else {
158 recompute_goal_steps_ = ceil(-12.5 * exploration_ratio_ + 22.5);
159 }
160 }
161
163 if (start_exploit_ == false) {
164 if (recompute_goal_counter_ >= recompute_goal_steps_ or
165 is_converged_ == true) {
166 recompute_goal_counter_ = 0;
167 ComputeGoals();
168 }
169 ++recompute_goal_counter_;
170 }
171 is_converged_ = not env_.StepRobotsToGoals(goals_, actions_);
173 return 0;
174 }
175
176 bool IsConverged() { return is_converged_; }
177};
178
179} /* namespace CoverageControl */
180#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_ORACLE_EXPLORE_EXPLOIT_H_
Contains the abstract class for coverage control algorithms.
The CoverageSystem class is the main class for the coverage control library.
PointVector GetRobotPositions(bool force_no_noise=false)
Get the global positions of all robots.
const MapType & GetRobotSensorView(size_t const id) const
bool StepRobotsToGoals(PointVector const &goals, PointVector &actions)
void SetGoals(PointVector const &goals)
OracleExploreExploit(Parameters const &params, size_t const &num_robots, CoverageSystem &env)
std::vector< VoronoiCell > GetVoronoiCells()
Class to store parameters.
Definition parameters.h:48
Class for computing Voronoi cells.
Definition voronoi.h:116
auto GetVoronoiCells() const
Definition voronoi.h:185
The file contains the CoverageSystem class, which is the main class for the coverage control library.
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
std::vector< Point2 > PointVector
Definition typedefs.h:51
auto NearOptimalCVTAlgorithm(int const num_tries, int const max_iterations, int const num_sites, MapType const &map, int const map_size, double const res)
void ComputeOffsets(double const resolution, Point2 const &pos, int const submap_size, int const map_size, MapBounds &index, MapBounds &offset)
Definition map_utils.h:59
Namespace for the CoverageControl library.
Near Optimal Centroidal Voronoi Tessellation (CVT) algorithm Near-optimal Centroidal Voronoi Tessella...
Contains parameters.
Contains typedefs for the library.