Skip to content

Commit

Permalink
Merge pull request #4 from AgroCares/use-fixed-colnames
Browse files Browse the repository at this point in the history
Use fixed colnames
  • Loading branch information
SvenVw authored Oct 18, 2024
2 parents 7e43cb0 + c579208 commit e98a60a
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 18 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ BugReports: https://github.com/AgroCares/apus/issues
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Imports:
checkmate,
cli,
Expand Down
10 changes: 10 additions & 0 deletions R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@
#'
#' A data.table with the parameters used in this package and their definition
"parameters"

#' colnames fields
#'
#' bla bla
"cols.fields"

#' colnames fertilizers
#'
#' bla bla
"cols.fertilizers"
6 changes: 2 additions & 4 deletions R/dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ createApusDataset <- function(farms = NULL, cultivations, fertilizers, fines, fi
fertilizers[, p_stored := 0]
fertilizers[, p_price := 1]

fertilizers <- fertilizers[, c('p_stored', 'p_price', 'p_n_rt', 'p_n_wc', 'p_p_rt', 'p_k_rt', 'p_type_manure', 'p_p_wcl')]

fertilizers <- fertilizers[, mget(apus::cols.fertilizers)]
self$fertilizers <- torch::torch_tensor(as.matrix(fertilizers), device = device)
},

Expand Down Expand Up @@ -86,8 +85,7 @@ createApusDataset <- function(farms = NULL, cultivations, fertilizers, fines, fi
transformFieldsToTensor = function(fields, device) {

# Select only relevant columns and define column order --------------------
col.fields <- c('b_area', 'd_n_req', 'd_p_req', 'd_k_req', 'd_n_norm', 'd_n_norm_man', 'd_p_norm', 'b_lu_yield', 'b_lu_price')
fields <- fields[, mget(col.fields)]
fields <- fields[, mget(apus::cols.fields)]


# Create torch_tensor from array ------------------------------------------
Expand Down
20 changes: 10 additions & 10 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -219,46 +219,46 @@ calculateCostModule1 <- function(doses, fields, fertilizers) {
calculateRevenueModule5 <- function(doses, fields, fertilizers) {

# Calculate N dose per fields
fertilizers.p_n_rt <- fertilizers[,,3]
fertilizers.p_n_wc <- fertilizers[,,4]
fertilizers.p_n_rt <- fertilizers[,,which(apus::cols.fertilizers == 'p_n_rt')]
fertilizers.p_n_wc <- fertilizers[,,which(apus::cols.fertilizers == 'p_n_wc')]
fertilizers.p_n_workable <- fertilizers.p_n_rt * fertilizers.p_n_wc
fertilizers.p_n_workable <- torch::torch_unsqueeze(fertilizers.p_n_workable, 2)
fertilizers.p_n_workable <- torch::torch_repeat_interleave(fertilizers.p_n_workable, repeats = dim(doses)[2], dim =2)
fields.fertilizers.dose.n_workable <- doses * fertilizers.p_n_workable
fields.dose.n_workable <- torch::torch_sum(fields.fertilizers.dose.n_workable, dim = 3)

# Calculate N requirement realization
fields.d_n_req <- fields[,,2]
fields.d_n_req <- fields[,,which(apus::cols.fields == 'd_n_req')]
fields.d_n_gap <- fields.d_n_req - fields.dose.n_workable
fields.d_n_gap <- torch::torch_relu(fields.d_n_gap)
fields.d_n_realized <- torch::torch_ones(dim(fields.d_n_gap)) - (fields.d_n_gap / fields.d_n_req)
fields.d_n_realized <- torch::torch_unsqueeze(fields.d_n_realized, -1)


# Calculate P dose per fields
fertilizers.p_p_rt <- fertilizers[,,5]
fertilizers.p_p_rt <- fertilizers[,,which(apus::cols.fertilizers == 'p_p_rt')]
fertilizers.p_p_rt <- torch::torch_unsqueeze(fertilizers.p_p_rt, 2)
fertilizers.p_p_rt <- torch::torch_repeat_interleave(fertilizers.p_p_rt, repeats = dim(doses)[2], dim =2)
fields.fertilizers.dose.p <- doses * fertilizers.p_p_rt
fields.dose.p <- torch::torch_sum(fields.fertilizers.dose.p, dim = 3)

# Calculate P requirement realization
fields.d_p_req <- fields[,,3]
fields.d_p_req <- fields[,,which(apus::cols.fields == 'd_p_req')]
fields.d_p_gap <- fields.d_p_req - fields.dose.p
fields.d_p_gap <- torch::torch_relu(fields.d_p_gap)
fields.d_p_realized <- torch::torch_ones(dim(fields.d_p_gap)) - (fields.d_p_gap / fields.d_p_req)
fields.d_p_realized <- torch::torch_unsqueeze(fields.d_p_realized, -1)


# Calculate K dose per fields
fertilizers.p_k_rt <- fertilizers[,,6]
fertilizers.p_k_rt <- fertilizers[,,which(apus::cols.fertilizers == 'p_k_rt')]
fertilizers.p_k_rt <- torch::torch_unsqueeze(fertilizers.p_k_rt, 2)
fertilizers.p_k_rt <- torch::torch_repeat_interleave(fertilizers.p_k_rt, repeats = dim(doses)[2], dim =2)
fields.fertilizers.dose.k <- doses * fertilizers.p_k_rt
fields.dose.k <- torch::torch_sum(fields.fertilizers.dose.k, dim = 3)

# Calculate K requirement realization
fields.d_k_req <- fields[,,4]
fields.d_k_req <- fields[,,which(apus::cols.fields == 'd_k_req')]
fields.d_k_gap <- fields.d_k_req - fields.dose.k
fields.d_k_gap <- torch::torch_relu(fields.d_k_gap)
fields.d_k_realized <- torch::torch_ones(dim(fields.d_k_gap)) - (fields.d_k_gap / fields.d_k_req)
Expand All @@ -269,9 +269,9 @@ calculateRevenueModule5 <- function(doses, fields, fertilizers) {
fields.d_realized <- torch::torch_cat(list(fields.d_n_realized, fields.d_p_realized, fields.d_k_realized), dim=3L)
fields.d_realized <- torch::torch_mean(fields.d_realized, dim = 3, keepdim = TRUE) # TODO This should be converted to minimum
fields.d_realized <- torch::torch_squeeze(fields.d_realized, dim = -1)
fields.b_area <- fields[,,1]
fields.b_lu_yield <- fields[,,8]
fields.b_lu_price <- fields[,,9]
fields.b_area <- fields[,,which(apus::cols.fields == 'b_area')]
fields.b_lu_yield <- fields[,,which(apus::cols.fields == 'b_lu_yield')]
fields.b_lu_price <- fields[,,which(apus::cols.fields == 'b_lu_price')]
module5 <- fields.b_area * fields.b_lu_yield * fields.b_lu_price * fields.d_realized
module5 <- torch::torch_sum(module5, dim = 2L)

Expand Down
11 changes: 11 additions & 0 deletions data-raw/colnames.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

# Set colnames for fertilizers --------------------------------------------

cols.fertilizers <- c('p_stored', 'p_price', 'p_n_rt', 'p_n_wc', 'p_p_rt', 'p_k_rt', 'p_type_manure', 'p_p_wcl')
usethis::use_data(cols.fertilizers, overwrite = TRUE, version = 3)


# Set the column names for fields -----------------------------------------

cols.fields <- c('b_area', 'd_n_req', 'd_p_req', 'd_k_req', 'd_n_norm', 'd_n_norm_man', 'd_p_norm', 'b_lu_yield', 'b_lu_price')
usethis::use_data(cols.fields, overwrite = TRUE, version = 3)
Binary file added data/cols.fertilizers.rda
Binary file not shown.
Binary file added data/cols.fields.rda
Binary file not shown.
16 changes: 16 additions & 0 deletions man/cols.fertilizers.Rd

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

16 changes: 16 additions & 0 deletions man/cols.fields.Rd

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

8 changes: 5 additions & 3 deletions tests/testthat/test-001-dataset.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("Create training dataset", {
expect_equal(dataset$.length(), dataset$farms_count)
expect_contains(class(dataset$.getitem(1)$fields), 'torch_tensor')
expect_setequal(names(dataset$.getitem(1)), c('fields', 'fertilizers', 'fines'))
expect_equal(dim(dataset$.getitem(1)$fields), c(dataset$fields_max, 9))
expect_equal(dim(dataset$.getitem(1)$fields), c(dataset$fields_max, length(apus::cols.fields)))
expect_false(identical(dataset$.getitem(1), dataset$.getitem(2)))
})

Expand All @@ -22,15 +22,17 @@ test_that("Create validation/test dataset", {
expect_equal(dataset.valid$.length(), dataset.valid$farms_count)
expect_contains(class(dataset.valid$.getitem(1)$fields), 'torch_tensor')
expect_setequal(names(dataset.valid$.getitem(3)), c('fields', 'fertilizers', 'fines'))
expect_equal(dim(dataset.valid$.getitem(1)$fields), c(fields_max, length(apus::cols.fields)))
expect_setequal(names(dataset.valid$.getitem(3)), c('fields', 'fertilizers', 'fines'))
expect_equal(dim(dataset.valid$.getitem(1)$fields), c(fields_max, 9))

dl <- torch::dataloader(dataset.valid, batch_size = farms_count)
batch <- dl$.iter()
batch <- batch$.next()

expect_contains(class(batch$fields), 'torch_tensor')
expect_equal(dim(batch$fields), c(farms_count, fields_max, 9))
expect_equal(dim(batch$fertilizers), c(farms_count, nrow(apus::fertilizers), 8))
expect_equal(dim(batch$fields), c(farms_count, fields_max, length(apus::cols.fields)))
expect_equal(dim(batch$fertilizers), c(farms_count, nrow(apus::fertilizers), length(apus::cols.fertilizers)))
})


Expand Down

0 comments on commit e98a60a

Please sign in to comment.