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

Added getMinAOO which uses Simulated annealing to determine the optim… #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Data/Victoria mangrove pixels.tif
Binary file not shown.
5 changes: 5 additions & 0 deletions Data/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Victoria mangrove pixels.tif is a 2017 Victorian mangrove map created by using XGBoost.

I've used the extent: extent(c(329000, 375000, -4250000, -4215000)) as my sample window for now.
The minimum AOO ('correct answer') I got us 2, though 4 seems to be the median. Any method that
gets us to 2 quickest should be our target!
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export(getArea)
export(getAreaChange)
export(getAreaEOO)
export(getDeclineStats)
export(getMinAOO)
export(gridUncertainty)
export(gridUncertaintyBase)
export(gridUncertaintyRandom)
Expand Down
82 changes: 73 additions & 9 deletions R/AOO_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ makeAOOGrid <- function (ecosystem.data, grid.size, min.percent.rule = TRUE, per
x <- rasterize(xy, grid, fun='count') # returns a 10 * 10 raster where cell value is the number of points in the cell
names(x) <- 'count'
grid.shp <- rasterToPolygons(x, dissolve=FALSE)
if (min.percent.rule == FALSE){
outGrid <- grid.shp
}
if (min.percent.rule == TRUE){

if (min.percent.rule){
cell.res <- res(ecosystem.data)
area <- cell.res[1] * cell.res[2]
one.pc.grid <- grid.size * grid.size / 100 # 1pc of grid cell
threshold <- one.pc.grid * percent / area
outGrid <- grid.shp[grid.shp$count > threshold,] # select only grids that meet one percent threshol
} else {
outGrid <- grid.shp
}
return (outGrid)
}
Expand Down Expand Up @@ -107,7 +107,6 @@ makeAOOGrid <- function (ecosystem.data, grid.size, min.percent.rule = TRUE, per
#' AOO <- getAOO(r1, n, one.percent.rule = F)
#' AOO # number of grid cells occupied by an ecosystem or species
#' @export

getAOO <- function (ecosystem.data, grid.size, min.percent.rule = TRUE, percent = 1){
# Computes the number of 10x10km grid cells that are >1% covered by an ecosystem
AOO.number = length(makeAOOGrid(ecosystem.data, grid.size, min.percent.rule, percent))
Expand Down Expand Up @@ -140,19 +139,84 @@ getAOOSilent <- function (ecosystem.data, grid, min.percent.rule = TRUE, percent
x <- rasterize(xy, grid, fun='count') # returns a 10 * 10 raster where cell value is the number of points in the cell
names(x) <- 'count'
grid.shp <- rasterToPolygons(x, dissolve=FALSE)
if (min.percent.rule == FALSE){
outGrid <- grid.shp
}
if (min.percent.rule == TRUE){

if (min.percent.rule){
cell.res <- res(ecosystem.data)
area <- cell.res[1] * cell.res[2]
one.pc.grid <- grid.size[1] * grid.size[2] / 100 # 1pc of grid cell
threshold <- one.pc.grid * percent / area
outGrid <- grid.shp[grid.shp$count > threshold,] # select only grids that meet one percent threshol
} else {
outGrid <- grid.shp
}
# end getAOO

AOO.number = length(outGrid) ## different from getAOO

return (AOO.number)
}


#' Compute the Minimum Area of Occupancy (AOO)
#'
#' \code{getAOO} determines the number of area of occupancy (AOO) grid cells
#' occupied by a species or ecosystem. It includes capability for specifying
#' whether at least one percent of the grid cell needs to be occupied before it
#' is counted in the AOO. This functionality is important for assessing the IUCN
#' Red List of Ecosystems Criteria B.
#'
#' \code{getMinAOO} optimises the placement of the grid so the AOO is the
#' smalest possible. It does this using \code{constrOptim} with method SANN.
#'
#' @inheritParams makeAOOGrid
#' @param trace If TRUE, tracing information on the progress of the optimisation
#' is produced.
#' @return The number of grid cells occupied by the ecosystem or species
#' @author Nicholas Murray \email{murr.nick@@gmail.com}, Calvin Lee
#' \email{calvinkflee@@gmail.com}, John Wilshire
#' \email{john.h.wilshire@@gmail.com}
#' @family AOO functions
#' @references Bland, L.M., Keith, D.A., Miller, R.M., Murray, N.J. and
#' Rodriguez, J.P. (eds.) 2016. Guidelines for the application of IUCN Red
#' List of Ecosystems Categories and Criteria, Version 1.0. Gland,
#' Switzerland: IUCN. ix + 94pp. Available at the following web site:
#' \url{iucnrle.org/}
#' @examples
#' crs.UTM55N <- '+proj=utm +zone=55 +ellps=WGS84 +datum=WGS84 +units=m +no_defs'
#' r1 <- raster(ifelse((volcano<130), NA, 1), crs = crs.UTM55N) #t1 = 1990
#' ext <- extent(0, 6100, 0, 8700) # set the extent of raster r1, 100m resolution
#' extent(r1) <- ext
#' (getMinAOO(r1, grid.size = 1000) -> AOO) # number of grid cells occupied by an ecosystem or species
#' @export

getMinAOO <- function(ecosystem.data, grid.size,
min.percent.rule = TRUE, percent = 1, trace = TRUE) {
grid <- createGrid(ecosystem.data, grid.size) # create the inital grid
objective_fun <- function(x) {
getAOOSilent(ecosystem.data,
grid = shift(grid, x = x[1], y = x[2]),
min.percent.rule = min.percent.rule,
percent = percent)
}

ub <- grid.size
lb <- 0
ci <- c(lb, lb, -ub, -ub)
ui <- matrix(c(1, 0, -1, 0,
0, 1, 0, -1), ncol = 2)
start <- runif(2, min = 0, max = grid.size)
constrOptim(start,
objective_fun,
method = "SANN",
ui = ui,
ci = ci,
grad = NULL,
control = list(
temp = grid.size,
tmax = 50,
maxit = 500,
parscale = c(grid.size, grid.size),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand of the parscale parameter, doesn't this mean that the adjustments moved will be too big (the whole grid we're working at)?

trace = trace)
) -> o
o$value
}
3 changes: 2 additions & 1 deletion man/createGrid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/getAOO.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/getAOOSilent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions man/getMinAOO.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/makeAOOGrid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions redlistr.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Version: 1.0

RestoreWorkspace: No
SaveWorkspace: No
AlwaysSaveHistory: Default
AlwaysSaveHistory: No

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
Expand All @@ -17,5 +17,5 @@ StripTrailingWhitespace: Yes

BuildType: Package
PackageUseDevtools: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageInstallArgs: --no-multiarch --with-keep.source --preclean
PackageRoxygenize: rd,collate,namespace