[GRASS-SVN] r70868 - in grass-addons/grass7/raster/r.pi: r.pi.energy r.pi.energy.pr r.pi.nlm r.pi.nlm.stats r.pi.searchtime r.pi.searchtime.pr
svn_grass at osgeo.org
svn_grass at osgeo.org
Wed Apr 12 05:26:41 PDT 2017
Author: mmetz
Date: 2017-04-12 05:26:41 -0700 (Wed, 12 Apr 2017)
New Revision: 70868
Modified:
grass-addons/grass7/raster/r.pi/r.pi.energy.pr/search.c
grass-addons/grass7/raster/r.pi/r.pi.energy/search.c
grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c
grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c
grass-addons/grass7/raster/r.pi/r.pi.searchtime.pr/search.c
grass-addons/grass7/raster/r.pi/r.pi.searchtime/search.c
Log:
r.pi: sync search.c
Modified: grass-addons/grass7/raster/r.pi/r.pi.energy/search.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.energy/search.c 2017-04-12 10:51:24 UTC (rev 70867)
+++ grass-addons/grass7/raster/r.pi/r.pi.energy/search.c 2017-04-12 12:26:41 UTC (rev 70868)
@@ -42,7 +42,7 @@
x = indi->x;
y = indi->y;
- /* fprintf(stderr, "indi%d: (%d, %d)\n", i, x, y); */
+ /* fprintf(stderr, "indi%d: (%d, %d)\n", i, x, y); */
if (!indi->lost) {
outmap[x + y * sx]++;
}
@@ -117,13 +117,13 @@
int y = frag->y;
int count = 0;
- if (x < sx - 1 && map[x + 1 + y * sx] == TYPE_NOTHING)
+ if (x >= sx - 1 || map[x + 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.0;
- if (y < sy - 1 && map[x + (y + 1) * sx] == TYPE_NOTHING)
+ if (y >= sy - 1 || map[x + (y + 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.25;
- if (x > 0 && map[x - 1 + y * sx] == TYPE_NOTHING)
+ if (x <= 0 || map[x - 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.5;
- if (y > 0 && map[x + (y - 1) * sx] == TYPE_NOTHING)
+ if (y <= 0 || map[x + (y - 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.75;
pick = count * Randomf();
@@ -357,6 +357,9 @@
/* make a step in the current direction */
int dir_index = Round(individual->dir * 8.0 * (double)step_length);
+ /* test output */
+ /* fprintf(stderr, "actpos: x = %0.2f, y = %0.2f\n", individual->x, individual->y); */
+
newx = individual->x + displacements[dir_index].x;
newy = individual->y + displacements[dir_index].y;
@@ -375,6 +378,9 @@
return;
}
+ /* test output */
+ /* fprintf(stderr, "pick: x = %0.2f, y = %0.2f\n\n", newx, newy); */
+
/* set new position, which is now approved */
individual->x = newx;
individual->y = newy;
@@ -429,6 +435,13 @@
/* write an array with possible next positions */
pick_nextpos(pos_arr, indi, map, costmap, frag, sx, sy);
+ /* test output */
+ /* fprintf(stderr, "Nextpos array:\n");
+ for(i = 0; i < pickpos_count; i++) {
+ fprintf(stderr, "(x=%d,y=%d,dir=%0.2f,weight=%0.2f)\n",
+ pos_arr[i].x, pos_arr[i].y, pos_arr[i].dir, pos_arr[i].weight);
+ } */
+
/* if no next position is possible, then set back */
sum = 0;
for (i = 0; i < pickpos_count; i++) {
@@ -471,7 +484,7 @@
int i, j;
int step_cnt = 0;
int finished_cnt = 0;
- int limit = 0.01 * percent * n;
+ int limit = ceil(n * percent / 100);
/* fprintf(stderr, "\nstarting run:\n"); */
/* fprintf(stderr, "limit = %d\n", limit); */
@@ -534,7 +547,6 @@
void perform_search(int *map, DCELL * costmap, int n, int fragcount, int sx, int sy)
{
int fragment;
- f_statmethod func;
/* allocate paths array */
indi_steps = (DCELL *) G_malloc(n * sizeof(DCELL));
@@ -563,6 +575,12 @@
memcpy(perception + 8 * perception_range, perception,
8 * perception_range * sizeof(Displacement));
+ /* fprintf(stderr, "Displacements:\n");
+ for(i = 0; i < pickpos_count; i++) {
+ fprintf(stderr, " (%d, %d)", displacements[i].x, displacements[i].y);
+ }
+ fprintf(stderr, "\n"); */
+
/* allocate patch_registry */
patch_registry = (int *)G_malloc(fragcount * n * sizeof(int));
@@ -572,11 +590,6 @@
mig_matrix = (int *)G_malloc(fragcount * fragcount * sizeof(int));
memset(mig_matrix, 0, fragcount * fragcount * sizeof(int));
- /* fprintf(stderr, "Displacements:");
- for(i = 0; i < pickpos_count; i++) {
- fprintf(stderr, " (%d, %d)", displacements[i].x, displacements[i].y);
- } */
-
/* perform a search run for each fragment */
for (fragment = 0; fragment < fragcount; fragment++) {
frag_run(map, costmap, fragment, n, fragcount, sx, sy);
Modified: grass-addons/grass7/raster/r.pi/r.pi.energy.pr/search.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.energy.pr/search.c 2017-04-12 10:51:24 UTC (rev 70867)
+++ grass-addons/grass7/raster/r.pi/r.pi.energy.pr/search.c 2017-04-12 12:26:41 UTC (rev 70868)
@@ -117,13 +117,13 @@
int y = frag->y;
int count = 0;
- if (x < sx - 1 && map[x + 1 + y * sx] == TYPE_NOTHING)
+ if (x >= sx - 1 || map[x + 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.0;
- if (y < sy - 1 && map[x + (y + 1) * sx] == TYPE_NOTHING)
+ if (y >= sy - 1 || map[x + (y + 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.25;
- if (x > 0 && map[x - 1 + y * sx] == TYPE_NOTHING)
+ if (x <= 0 || map[x - 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.5;
- if (y > 0 && map[x + (y - 1) * sx] == TYPE_NOTHING)
+ if (y <= 0 || map[x + (y - 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.75;
pick = count * Randomf();
@@ -358,6 +358,9 @@
/* make a step in the current direction */
int dir_index = Round(individual->dir * 8.0 * (double)step_length);
+ /* test output */
+ /* fprintf(stderr, "actpos: x = %0.2f, y = %0.2f\n", individual->x, individual->y); */
+
newx = individual->x + displacements[dir_index].x;
newy = individual->y + displacements[dir_index].y;
@@ -376,6 +379,9 @@
return;
}
+ /* test output */
+ /* fprintf(stderr, "pick: x = %0.2f, y = %0.2f\n\n", newx, newy); */
+
/* set new position, which is now approved */
individual->x = newx;
individual->y = newy;
@@ -432,6 +438,13 @@
/* write an array with possible next positions */
pick_nextpos(pos_arr, indi, map, costmap, frag, sx, sy);
+ /* test output */
+ /* fprintf(stderr, "Nextpos array:\n");
+ for(i = 0; i < pickpos_count; i++) {
+ fprintf(stderr, "(x=%d,y=%d,dir=%0.2f,weight=%0.2f)\n",
+ pos_arr[i].x, pos_arr[i].y, pos_arr[i].dir, pos_arr[i].weight);
+ } */
+
/* if no next position is possible, then set back */
sum = 0;
for (i = 0; i < pickpos_count; i++) {
@@ -461,7 +474,6 @@
if (pos_arr[i].weight > rnd)
break;
}
-
individual->dir = pos_arr[i].dir;
return;
@@ -475,7 +487,7 @@
int i, j;
int step_cnt = 0;
int finished_cnt = 0;
- int limit = 0.01 * percent * n;
+ int limit = ceil(n * percent / 100);
/* fprintf(stderr, "\nstarting run:\n"); */
/* fprintf(stderr, "limit = %d\n", limit); */
@@ -539,7 +551,6 @@
void perform_search(int *map, DCELL * costmap, int remove_indi, int n, int fragcount, int sx, int sy)
{
int fragment;
- f_statmethod func;
/* allocate paths array */
indi_steps = (DCELL *) G_malloc(n * sizeof(DCELL));
@@ -568,6 +579,12 @@
memcpy(perception + 8 * perception_range, perception,
8 * perception_range * sizeof(Displacement));
+ /* fprintf(stderr, "Displacements:\n");
+ for(i = 0; i < pickpos_count; i++) {
+ fprintf(stderr, " (%d, %d)", displacements[i].x, displacements[i].y);
+ }
+ fprintf(stderr, "\n"); */
+
/* allocate patch_registry */
patch_registry = (int *)G_malloc(fragcount * n * sizeof(int));
@@ -578,11 +595,6 @@
memset(emigrants, 0, fragcount * sizeof(int));
memset(lost, 0, fragcount * sizeof(int));
- /* fprintf(stderr, "Displacements:");
- for(i = 0; i < pickpos_count; i++) {
- fprintf(stderr, " (%d, %d)", displacements[i].x, displacements[i].y);
- } */
-
/* perform a search run for each fragment */
for (fragment = 0; fragment < fragcount; fragment++) {
if (!(remove_indi && deleted_arr[fragment])) {
Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c 2017-04-12 10:51:24 UTC (rev 70867)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm/main.c 2017-04-12 12:26:41 UTC (rev 70868)
@@ -177,7 +177,7 @@
landcover /= 100;
}
else {
- if (!oldname)
+ if (!oldname || !parm.keyval->answer)
G_fatal_error("Specify either landcover or an input file with key value for landcover to be acquired!");
}
@@ -219,9 +219,11 @@
}
}
- /* count pixels for landcover */
- if (result[col] == keyval)
- pixel_count++;
+ if (parm.keyval->answer) {
+ /* count pixels for landcover */
+ if (result[col] == keyval)
+ pixel_count++;
+ }
}
}
Rast_close(in_fd);
Modified: grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c 2017-04-12 10:51:24 UTC (rev 70867)
+++ grass-addons/grass7/raster/r.pi/r.pi.nlm.stats/main.c 2017-04-12 12:26:41 UTC (rev 70868)
@@ -350,18 +350,27 @@
Rast_get_c_row(in_fd, result, row);
for (col = 0; col < sx; col++) {
if (parm.nullval->answers) {
+ real_landscape[row * sx + col] = 1;
for (i = 0; parm.nullval->answers[i] != NULL; i++) {
sscanf(parm.nullval->answers[i], "%d", &nullval);
- if (result[col] == nullval)
+ if (result[col] == nullval) {
buffer[row * sx + col] = 1;
+ real_landscape[row * sx + col] = 0;
+ }
}
}
- /* count pixels for landcover */
- if (result[col] == keyval) {
- pixel_count++;
- real_landscape[row * sx + col] = 1;
+ if (parm.keyval->answer) {
+ /* count pixels for landcover */
+ if (result[col] == keyval) {
+ pixel_count++;
+ real_landscape[row * sx + col] = 1;
+ }
+ else
+ real_landscape[row * sx + col] = 0;
}
+ if (Rast_is_c_null_value(&result[col]))
+ real_landscape[row * sx + col] = 0;
}
}
Rast_close(in_fd);
Modified: grass-addons/grass7/raster/r.pi/r.pi.searchtime/search.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.searchtime/search.c 2017-04-12 10:51:24 UTC (rev 70867)
+++ grass-addons/grass7/raster/r.pi/r.pi.searchtime/search.c 2017-04-12 12:26:41 UTC (rev 70868)
@@ -116,17 +116,15 @@
int y = frag->y;
int count = 0;
- if (x < sx - 1 && map[x + 1 + y * sx] == TYPE_NOTHING)
+ if (x >= sx - 1 || map[x + 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.0;
- if (y < sy - 1 && map[x + (y + 1) * sx] == TYPE_NOTHING)
+ if (y >= sy - 1 || map[x + (y + 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.25;
- if (x > 0 && map[x - 1 + y * sx] == TYPE_NOTHING)
+ if (x <= 0 || map[x - 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.5;
- if (y > 0 && map[x + (y - 1) * sx] == TYPE_NOTHING)
+ if (y <= 0 || map[x + (y - 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.75;
- /* G_message("Picks for (%d, %d): %0.2f, %0.2f, %0.2f, %0.2f, cnt=%d", x, y, dirs[0], dirs[1], dirs[2], dirs[3], count); */
-
pick = count * Randomf();
for (i = count - 1; i >= 0; i--) {
@@ -137,7 +135,6 @@
res++;
}
/* res = res < 0 ? 2 * M_PI + res : res; */
- /* G_message("dir = %0.2f", res); */
return res;
}
}
@@ -155,7 +152,7 @@
border_count = sort_frag(frag, size);
- /* G_message("Initializing"); */
+ /* G_message("Initializing individuals"); */
for (i = 0; i < n; i++) {
/* G_message("border_count = %d", border_count); */
@@ -351,12 +348,12 @@
double newx, newy;
int act_cell;
+ /* make a step in the current direction */
+ int dir_index = Round(individual->dir * 8.0 * (double)step_length);
+
/* test output */
/* fprintf(stderr, "actpos: x = %0.2f, y = %0.2f\n", individual->x, individual->y); */
- /* make a step in the current direction */
- int dir_index = Round(individual->dir * 8.0 * (double)step_length);
-
newx = individual->x + displacements[dir_index].x;
newy = individual->y + displacements[dir_index].y;
@@ -367,7 +364,10 @@
return;
}
- /* set new position */
+ /* test output */
+ /* fprintf(stderr, "pick: x = %0.2f, y = %0.2f\n\n", newx, newy); */
+
+ /* set new position, which is now approved */
individual->x = newx;
individual->y = newy;
@@ -386,15 +386,13 @@
patch_imi[act_cell]++;
immi_matrix[frag * fragcount + act_cell]++;
individual->finished = 1;
-
- return;
}
/* write an array with possible next positions */
pick_nextpos(pos_arr, indi, map, costmap, frag, sx, sy);
/* test output */
- /* G_message("Nextpos array:\n");
+ /* fprintf(stderr, "Nextpos array:\n");
for(i = 0; i < pickpos_count; i++) {
fprintf(stderr, "(x=%d,y=%d,dir=%0.2f,weight=%0.2f)\n",
pos_arr[i].x, pos_arr[i].y, pos_arr[i].dir, pos_arr[i].weight);
@@ -423,9 +421,6 @@
}
individual->dir = pos_arr[i].dir;
- /* test output */
- /* fprintf(stderr, "pick: x = %0.2f, y = %0.2f\n\n", newx, newy); */
-
return;
}
@@ -472,8 +467,6 @@
step_cnt++;
}
- G_percent(frag + 1, fragcount, 1);
-
if (out_freq > 0 && (step_cnt % out_freq == 0)) {
test_output(map, frag, step_cnt, n, sx, sy);
}
@@ -519,11 +512,13 @@
memcpy(perception + 8 * perception_range, perception,
8 * perception_range * sizeof(Displacement));
- /* fprintf(stderr, "Displacements:");
+ /* fprintf(stderr, "Displacements:\n");
for(i = 0; i < pickpos_count; i++) {
fprintf(stderr, " (%d, %d)", displacements[i].x, displacements[i].y);
- } */
+ }
+ fprintf(stderr, "\n"); */
+ /* initialize patch imigrants array */
memset(patch_imi, 0, fragcount * sizeof(int));
/* allocate immi_matrix and mig_matrix */
@@ -546,6 +541,12 @@
}
}
+ /*fprintf(stderr, "Values\n");
+ for(i = 0; i < fragcount * stat_count; i++) {
+ fprintf(stderr, "%0.2f ", values[i]);
+ }
+ fprintf(stderr, "\n"); */
+
G_free(indi_paths);
G_free(indi_array);
G_free(pos_arr);
Modified: grass-addons/grass7/raster/r.pi/r.pi.searchtime.pr/search.c
===================================================================
--- grass-addons/grass7/raster/r.pi/r.pi.searchtime.pr/search.c 2017-04-12 10:51:24 UTC (rev 70867)
+++ grass-addons/grass7/raster/r.pi/r.pi.searchtime.pr/search.c 2017-04-12 12:26:41 UTC (rev 70868)
@@ -116,17 +116,15 @@
int y = frag->y;
int count = 0;
- if (x < sx - 1 && map[x + 1 + y * sx] == TYPE_NOTHING)
+ if (x >= sx - 1 || map[x + 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.0;
- if (y < sy - 1 && map[x + (y + 1) * sx] == TYPE_NOTHING)
+ if (y >= sy - 1 || map[x + (y + 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.25;
- if (x > 0 && map[x - 1 + y * sx] == TYPE_NOTHING)
+ if (x <= 0 || map[x - 1 + y * sx] == TYPE_NOTHING)
dirs[count++] = 0.5;
- if (y > 0 && map[x + (y - 1) * sx] == TYPE_NOTHING)
+ if (y <= 0 || map[x + (y - 1) * sx] == TYPE_NOTHING)
dirs[count++] = 0.75;
- /* G_message("Picks for (%d, %d): %0.2f, %0.2f, %0.2f, %0.2f, cnt=%d", x, y, dirs[0], dirs[1], dirs[2], dirs[3], count); */
-
pick = count * Randomf();
for (i = count - 1; i >= 0; i--) {
@@ -137,7 +135,6 @@
res++;
}
/* res = res < 0 ? 2 * M_PI + res : res; */
- /* G_message("dir = %0.2f", res); */
return res;
}
}
@@ -349,17 +346,54 @@
double sum;
Individual *individual = indi_array + indi;
double rnd;
- double newx, newy, newdir;
+ double newx, newy;
int act_cell;
+ /* make a step in the current direction */
+ int dir_index = Round(individual->dir * 8.0 * (double)step_length);
+
/* test output */
/* fprintf(stderr, "actpos: x = %0.2f, y = %0.2f\n", individual->x, individual->y); */
+ newx = individual->x + displacements[dir_index].x;
+ newy = individual->y + displacements[dir_index].y;
+
+ /* if new position is out of limits, then set back */
+ if (newx < 0 || newx >= sx || newy < 0 || newy >= sy) {
+ set_back(map, indi, frag, sx, sy);
+
+ return;
+ }
+
+ /* test output */
+ /* fprintf(stderr, "pick: x = %0.2f, y = %0.2f\n\n", newx, newy); */
+
+ /* set new position, which is now approved */
+ individual->x = newx;
+ individual->y = newy;
+
+ /* count path of the individuum */
+ if (include_cost) {
+ individual->path += 100 / costmap[(int)newy * sx + (int)newx];
+ }
+ else {
+ individual->path++;
+ }
+
+ /* if new position is another patch and patch is not deleted, then set finished = true */
+ act_cell = map[(int)newy * sx + (int)newx];
+ if (act_cell > -1 && act_cell != frag && !deleted_arr[act_cell]) {
+ /* count patch immigrants for this patch */
+ patch_imi[act_cell]++;
+ immi_matrix[frag * fragcount + act_cell]++;
+ individual->finished = 1;
+ }
+
/* write an array with possible next positions */
pick_nextpos(pos_arr, indi, map, costmap, frag, sx, sy);
/* test output */
- /* G_message("Nextpos array:\n");
+ /* fprintf(stderr, "Nextpos array:\n");
for(i = 0; i < pickpos_count; i++) {
fprintf(stderr, "(x=%d,y=%d,dir=%0.2f,weight=%0.2f)\n",
pos_arr[i].x, pos_arr[i].y, pos_arr[i].dir, pos_arr[i].weight);
@@ -386,41 +420,8 @@
if (pos_arr[i].weight > rnd)
break;
}
- newx = pos_arr[i].x;
- newy = pos_arr[i].y;
- newdir = pos_arr[i].dir;
+ individual->dir = pos_arr[i].dir;
- /* test output */
- /* fprintf(stderr, "pick: x = %0.2f, y = %0.2f\n\n", newx, newy); */
-
- /* if new position is out of limits, then set back */
- if (newx < 0 || newx >= sx || newy < 0 || newy >= sy) {
- set_back(map, indi, frag, sx, sy);
-
- return;
- }
-
- /* set new position */
- individual->x = newx;
- individual->y = newy;
-
- /* count path of the individuum */
- if (include_cost) {
- individual->path += 100 / costmap[(int)newy * sx + (int)newx];
- }
- else {
- individual->path++;
- }
- individual->dir = newdir;
-
- /* if new position is an other patch and patch is not deleted, then set finished = true */
- act_cell = map[(int)newy * sx + (int)newx];
- if (act_cell > -1 && act_cell != frag && !deleted_arr[act_cell]) {
- /* count patch immigrants for this patch */
- patch_imi[act_cell]++;
- individual->finished = 1;
- }
-
return;
}
@@ -515,10 +516,11 @@
memcpy(perception + 8 * perception_range, perception,
8 * perception_range * sizeof(Displacement));
- /* fprintf(stderr, "Displacements:");
+ /* fprintf(stderr, "Displacements:\n");
for(i = 0; i < pickpos_count; i++) {
fprintf(stderr, " (%d, %d)", displacements[i].x, displacements[i].y);
- } */
+ }
+ fprintf(stderr, "\n"); */
/* initialize patch imigrants array */
memset(patch_imi, 0, fragcount * sizeof(int));
@@ -537,11 +539,11 @@
}
}
- /*G_message("Values");
+ /*fprintf(stderr, "Values\n");
for(i = 0; i < fragcount * stat_count; i++) {
fprintf(stderr, "%0.2f ", values[i]);
}
- G_message(""); */
+ fprintf(stderr, "\n"); */
G_free(indi_paths);
G_free(indi_array);
More information about the grass-commit
mailing list