7 nh.param(this_node::getName() +
"/optimizer/iterations",
max_iter, 10);
8 nh.param(this_node::getName() +
"/optimizer/variate_weight",
variate_weight, 0.01);
9 nh.param(this_node::getName() +
"/optimizer/discrepancy",
discr, 30);
16 double fairDivision = 1.0 /
nr;
17 int effectiveSize = NoTiles -
nr -
ob;
19 if (effectiveSize %
nr !=0) {
27 vector<valarray<double>> AllDistances(
nr, valarray<double>(
rows*cols));
28 for (
int i=0; i<
rows; i++) {
29 for (
int j=0; j<
cols;j++) {
30 for (
int r=0; r<
nr; r++) {
31 AllDistances[r][i*cols+j] = hypot(
cps[r][1]-i,
cps[r][0]-j);
37 vector<valarray<double>> MetricMatrix = AllDistances;
43 double downThres = ((double)NoTiles-(
double)termThr*(
nr-1)) / (double)(NoTiles*
nr);
44 double upperThres = ((double)NoTiles+termThr) / (double)(NoTiles*nr);
53 vector<valarray<float>> ConnectedMultiplierList(nr);
54 double plainErrors[
nr];
55 double divFairError[
nr];
56 for (
int r=0; r<
nr; r++) {
57 valarray<float> ConnectedMultiplier(1, rows*cols);
72 ConnectedMultiplierList[r] = ConnectedMultiplier;
76 if (plainErrors[r] < downThres) {
77 divFairError[r] = downThres - plainErrors[r];
79 else if (plainErrors[r] > upperThres) {
80 divFairError[r] = upperThres - plainErrors[r];
90 double TotalNegPerc = 0.0, totalNegPlainErrors = 0.0;
91 double correctionMult[
nr];
92 for (
int r=0; r<
nr; r++) {
93 if (divFairError[r] < 0) {
94 TotalNegPerc += abs(divFairError[r]);
95 totalNegPlainErrors += plainErrors[r];
97 correctionMult[r] = 1.0;
101 for (
int r=0; r<
nr; r++) {
102 if (totalNegPlainErrors != 0.0) {
103 if (divFairError[r]<0.0) {
104 correctionMult[r] = 1.0 + (plainErrors[r] / totalNegPlainErrors) * (TotalNegPerc / 2.0);
107 correctionMult[r] = 1.0 - (plainErrors[r] / totalNegPlainErrors) * (TotalNegPerc / 2.0);
127 ROS_ERROR(
"Area division failed!");
132 nav_msgs::OccupancyGrid assigned;
135 for (
int i=0; i<
rows; i++) {
136 for (
int j=0; j<
cols;j++) {
138 if (
A[(rows-1-i)*cols+j] ==
uuid_map[cps]) {
139 assigned.data[i*cols+j] = 0;
144 assigned.data[i*cols+j] = 100;
149 assigned.header.stamp = Time::now();
150 assigned.info.map_load_time == Time::now();
166 for (
auto c : cpss) {
169 int y =
rows - 1 - c.second[1];
172 int idx = y * cols + x;
175 gridmap[idx] = numeric_limits<signed char>::max();
179 cps.push_back(initializer_list<int>{x,y});
199 for (
int r=0; r<
nr; r++) {
206 for (
int i=0; i<
rows; i++) {
207 for (
int j=0; j<
cols; j++) {
210 double minV = matrix[0][i*cols+j];
212 for (
int r=1; r<
nr; r++) {
213 if (matrix[r][i*cols+j] < minV) {
214 minV = matrix[r][i*cols+j];
218 A[i*cols+j] = indMin;
219 BWlist[indMin][i*cols+j] = 1;
224 else if (
gridmap[i*cols+j] < numeric_limits<signed char>::max()) {
235 for (
int i=0; i<MMnew.size(); ++i) {
236 MMnew[i] = curentONe[i] * CM * CC[i];
246 int minCellsAss = numeric_limits<int>::max();
249 for (
int r=0; r<
nr; r++) {
262 return (maxCellsAss - minCellsAss) <= thres;
269 float MinV = numeric_limits<float>::max();
270 for (
int i=0;i<
rows;i++){
271 for (
int j=0;j<
cols;j++){
272 returnM[i*cols+j] = dist1[i*cols+j] - dist2[i*cols+j];
273 if (MaxV < returnM[i*cols+j]) {MaxV = returnM[i*cols+j];}
274 if (MinV > returnM[i*cols+j]) {MinV = returnM[i*cols+j];}
278 for (
int i=0;i<
rows;i++){
279 for (
int j=0;j<
cols;j++){
280 returnM[i*cols+j] =(returnM[i*cols+j] - MinV)*((2*(
float)
variate_weight)/(MaxV-MinV))+(1-(float)variate_weight);
valarray< int > A
Area assignment matrix.
map< string, int > uuid_map
UUID mapping of CPSs.
void assign(vector< valarray< double >> matrix)
Define the assignment matrix based on a given distance metric matrix.
int getMaxLabel()
Get the max label of the labeling process which is in the range [0,max_label].
int max_iter
Maximum number of iterations of the optimization process.
void constructBinaryImages(int robotsLabel)
Create binary arrays of connected labels.
vector< bool > regions
Whether the regions of the robots are connected.
vector< vector< int > > cps
Positions of the CPSs.
int discr
Maximum difference between number of assigned grid cells to each CPS.
bool isThisAGoalState(int thres)
Check if the areas assigned to each robot are of similar size.
Use row-by-row labeling algorithm to label connected components. The algorithm makes two passes over ...
int rows
Number of rows in the grid map.
double variate_weight
Maximum variate weight of connected components.
valarray< float > NormalizedEuclideanDistanceBinary(bool RobotR)
Calculate the normalized euclidean distance transform of a binary image with foreground pixels set to...
void initialize_cps(map< string, vector< int >> cpss)
Define the CPS positions.
void divide()
Perform the area division.
valarray< int > compactLabeling()
Rearrange the labels to make the label numbers consecutive.
vector< valarray< int > > BWlist
A binary array for each CPS which indicates free space.
void initialize_map(int r, int c, vector< signed char > src)
Define the grid map.
vector< int > ArrayOfElements
Grid cells not assigned to the robots.
bool success
Whether the assignment succeeded.
area_division()
Constructor.
int ob
Number of obstacles.
valarray< double > FinalUpdateOnMetricMatrix(double CM, valarray< double > curentONe, valarray< float > CC)
Update the metric matrix.
int cols
Number of columns in the grid map.
nav_msgs::OccupancyGrid get_grid(nav_msgs::OccupancyGrid map, string cps)
Get the region assigned to a CPS.
vector< signed char > gridmap
The environment grid map. Robots are represented by 2, obstacles by 1/-2, empty cells by -1...
valarray< float > CalcConnectedMultiplier(valarray< float > dist1, valarray< float > dist2)
Calculate the connected multiplier.