Coverage Control Library
Loading...
Searching...
No Matches
near_optimal_cvt_algorithm.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
35#ifndef CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_NEAR_OPTIMAL_CVT_ALGORITHM_H_
36#define CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_NEAR_OPTIMAL_CVT_ALGORITHM_H_
37
38#define EIGEN_NO_CUDA // Don't use eigen's cuda facility
39#include <omp.h>
40#include <time.h>
41
42#include <Eigen/Dense> // Eigen is used for maps
43#include <random>
44#include <vector>
45
46#include "CoverageControl/extern/lsap/Hungarian.h"
49
50namespace CoverageControl {
51
57inline auto NearOptimalCVTAlgorithm(int const num_tries,
58 int const max_iterations,
59 int const num_sites, MapType const &map,
60 int const map_size, double const res) {
61 std::random_device
62 rd_; // Will be used to obtain a seed for the random number engine
63 std::mt19937 gen_;
64 std::srand(time(NULL));
65 gen_ = std::mt19937(
66 rd_()); // Standard mersenne_twister_engine seeded with rd_()
67 std::vector<Voronoi> all_voronoi_cells;
68 /* all_voronoi_cells.resize(num_tries, std::vector<VoronoiCell>(num_sites));
69 */
70 all_voronoi_cells.resize(num_tries);
71 std::vector<double> obj_values;
72 obj_values.resize(num_tries, 0);
73 std::uniform_real_distribution<> distrib_pts(0.001, map_size * res - 0.001);
74
75#pragma omp parallel for
76 for (int iter = 0; iter < num_tries; ++iter) {
77 PointVector sites;
78 sites.resize(num_sites);
79 for (int iSite = 0; iSite < num_sites; ++iSite) {
80 sites[iSite] = Point2(distrib_pts(gen_), distrib_pts(gen_));
81 }
82 bool cont_flag = true;
83 /* std::cout << "voronoi start" << std::endl; */
84 Voronoi voronoi(sites, map, Point2(map_size, map_size), res);
85 /* std::cout << "voronoi end" << std::endl; */
86 auto voronoi_cells = voronoi.GetVoronoiCells();
87 int iSteps = 0;
88 for (iSteps = 0; iSteps < max_iterations and cont_flag == true; ++iSteps) {
89 cont_flag = false;
90 voronoi_cells = voronoi.GetVoronoiCells();
91 for (int iSite = 0; iSite < num_sites; ++iSite) {
92 Point2 diff =
93 voronoi_cells[iSite].centroid() - voronoi_cells[iSite].site;
94 if (diff.norm() < res) {
95 continue;
96 }
97 cont_flag = true;
98 sites[iSite] = voronoi_cells[iSite].centroid();
99 }
100 voronoi.UpdateSites(sites);
101 }
102 /* std::cout << "No. of voronoi steps: " << iSteps << std::endl; */
103 all_voronoi_cells[iter] = voronoi;
104 obj_values[iter] = voronoi.GetSumIDFGoalDistSqr();
105 }
106 int best_vornoi_idx = 0;
107 double min = obj_values[0];
108 for (int iter = 1; iter < num_tries; ++iter) {
109 if (obj_values[iter] < min) {
110 min = obj_values[iter];
111 best_vornoi_idx = iter;
112 }
113 }
114 return all_voronoi_cells[best_vornoi_idx];
115}
116
117inline auto NearOptimalCVTAlgorithm(int const num_tries,
118 int const max_iterations,
119 int const num_sites, MapType const &map,
120 int const map_size, double const res,
121 PointVector const &positions,
122 Voronoi &voronoi) {
123 voronoi = NearOptimalCVTAlgorithm(num_tries, max_iterations, num_sites, map,
124 map_size, res);
125 auto voronoi_cells = voronoi.GetVoronoiCells();
126 std::vector<std::vector<double>> cost_matrix;
127 cost_matrix.resize(num_sites, std::vector<double>(num_sites));
128#pragma omp parallel for num_threads(num_sites)
129 for (int iRobot = 0; iRobot < num_sites; ++iRobot) {
130 for (int jCentroid = 0; jCentroid < num_sites; ++jCentroid) {
131 cost_matrix[iRobot][jCentroid] =
132 (positions[iRobot] - voronoi_cells[jCentroid].centroid()).norm();
133 }
134 }
135 HungarianAlgorithm HungAlgo;
136 std::vector<int> assignment;
137 HungAlgo.Solve(cost_matrix, assignment);
138
139 PointVector goals;
140 goals.resize(num_sites);
141 for (int iRobot = 0; iRobot < num_sites; ++iRobot) {
142 goals[iRobot] = voronoi_cells[assignment[iRobot]].centroid();
143 }
144
145 return goals;
146}
147
148inline auto NearOptimalCVTAlgorithm(int const num_tries,
149 int const max_iterations,
150 int const num_sites, MapType const &map,
151 int const map_size, double const res,
152 PointVector const &positions) {
153 Voronoi voronoi;
154 return NearOptimalCVTAlgorithm(num_tries, max_iterations, num_sites, map,
155 map_size, res, positions, voronoi);
156}
157
161} /* namespace CoverageControl */
162#endif // CPPSRC_CORE_INCLUDE_COVERAGECONTROL_ALGORITHMS_NEAR_OPTIMAL_CVT_ALGORITHM_H_
Class for computing Voronoi cells.
Definition voronoi.h:116
auto GetVoronoiCells() const
Definition voronoi.h:185
void UpdateSites(PointVector const &sites)
Definition voronoi.h:178
double GetSumIDFGoalDistSqr()
Definition voronoi.h:195
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MapType
Definition typedefs.h:48
std::vector< Point2 > PointVector
Definition typedefs.h:51
Eigen::Vector2d Point2
Definition typedefs.h:44
auto NearOptimalCVTAlgorithm(int const num_tries, int const max_iterations, int const num_sites, MapType const &map, int const map_size, double const res)
Namespace for the CoverageControl library.
Contains typedefs for the library.
Class for computing Voronoi cells.