Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Derive per county patch library #10

Merged
merged 10 commits into from
Jun 25, 2020
Next Next commit
Patch library updates (#9)
Co-authored-by: MargaretLawrimore <margaret.lawrimore@gmail.com>
  • Loading branch information
petrasovaa and malawrim committed May 20, 2020
commit d55de2b784839e7c073e432c756f18b51944b603
Binary file added .DS_Store
Binary file not shown.
Binary file added r.futures/.DS_Store
Binary file not shown.
75 changes: 53 additions & 22 deletions r.futures/r.futures.pga/inputs.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <grass/gis.h>
Expand Down Expand Up @@ -413,41 +415,70 @@ void read_patch_sizes(struct PatchSizes *patch_info, double discount_factor)
{
FILE *fin;
char *size_buffer;
int n_max_patches;
int line = 0;
int patch;


char** tokens;
patch_info->max_patches = 0;
patch_info->max_patch_size = 0;

fin = fopen(patch_info->filename, "rb");
// essentially number of columns in file
patch_info->area_count = 0;
fin = fopen(patch_info->filename, "rb");
if (fin) {
size_buffer = (char *) G_malloc(100 * sizeof(char));
size_buffer = (char *) G_malloc(1054 * sizeof(char));
if (size_buffer) {
/* just scan the file twice */
n_max_patches = 0;
while (fgets(size_buffer, 100, fin)) {
n_max_patches++;
// TODO need to look at region_map and update indexing
// scan in the header line
fgets(size_buffer, 1054, fin);
tokens = G_tokenize(size_buffer, ",")
patch_info->area_count = G_number_of_tokens(tokens);
// initialize patch_info->patch_count to all zero
patch_info->patch_count = (int*) G_calloc(patch_info->area_count, sizeof(int));
// take one line
while (fgets(size_buffer, 1054, fin)) {
// process each column in row
tokens = G_tokenize(size_buffer, ",")
int s = G_number_of_tokens(tokens);
for ( int i = 0; i < s; i++) {
// increment the count of the patches for that area
if (stcmp(tokens[i], "") != 0 ) {
patch_info->patch_count[i]++;
}
}
}
rewind(fin);
if (n_max_patches) {
patch_info->patch_sizes =
(int *) G_malloc(sizeof(int) * n_max_patches);
if (patch_info->area_count) {
// flipping rows and columns so each area is a row
// in a 2D array
patch_info->patch_sizes = (int **) G_malloc(sizeof(int * ) * patch_info->area_count);
// malloc appropriate size for each area
for(int i = 0; i < patch_info->area_count; i++) {
patch_info->patch_sizes[i] =
(int *) G_malloc(patch_info->patch_count[i] * sizeof(int));
}
if (patch_info->patch_sizes) {
while (fgets(size_buffer, 100, fin)) {
patch = atoi(size_buffer) * discount_factor;
if (patch > 0) {
if (patch_info->max_patch_size < patch)
patch_info->max_patch_size = patch;
patch_info->patch_sizes[patch_info->max_patches] = patch;
patch_info->max_patches++;
}
}
while (fgets(size_buffer, 1054, fin)) {
tokens = G_tokenize(size_buffer, ",");
int s = G_number_of_tokens(tokens);
for ( int i = 0; i < s; i++) {
patch = atoi(tokens[i]) * discount_factor;
if (patch > 0) {
if (patch_info->max_patch_size < patch)
patch_info->max_patch_size = patch;
// TODO check order
patch_info->patch_sizes[i][line] = patch;
patch_info->max_patches++;
}
}
line++;
}
}
}
free(size_buffer);
}
fclose(fin);
fclose(fin);
}
}


11 changes: 9 additions & 2 deletions r.futures/r.futures.pga/inputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,17 @@ struct Potential
struct PatchSizes
{
const char *filename;
// max number of patches // total number of patches?
int max_patches;
int *patch_sizes;
// array of patches
int **patch_sizes;
// array of number of patches per area
int *patch_count;
// maximum patch size
int max_patch_size;

// count of number of areas
int area_count;

};

struct SegmentMemory
Expand Down
4 changes: 2 additions & 2 deletions r.futures/r.futures.pga/patch.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ static float get_alpha(struct PatchInfo *patch_info)
* \param patch_sizes patch sizes
* \return number of cells
*/
int get_patch_size(struct PatchSizes *patch_sizes)
int get_patch_size(struct PatchSizes *patch_sizes, int region)
{
return patch_sizes->patch_sizes[(int)(G_drand48() * patch_sizes->max_patches)];
return patch_info->patch_sizes[region][(int)(G_drand48() * patch_info->patch_count[region])];
}
/*!
* \brief Decides if to add a cell to a candidate list for patch growing
Expand Down
2 changes: 1 addition & 1 deletion r.futures/r.futures.pga/simulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ void compute_step(struct Undeveloped *undev_cells, struct Demand *demand,
/* challenge probability unless we need to convert all */
if(force_convert_all || G_drand48() < prob) {
/* ger random patch size */
patch_size = get_patch_size(patch_sizes);
patch_size = get_patch_size(patch_sizes, region);
/* last year: we shouldn't grow bigger patches than we have space for */
if (!overgrow && patch_size + n_done > n_to_convert)
patch_size = n_to_convert - n_done;
Expand Down