Skip to content
Draft
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
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

## Added
* Adds `apus` object with the functions `addField`, `trainModel` and `optimizeFertilizerChoice`
* Adds for the cost function module 1,5 and 6
* Adds for the cost function module 1,4, 5 and 6
* Adds default table for `cultivations`, `fertilizers`, `parameters` and `fines`
35 changes: 34 additions & 1 deletion R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ calculateCost <- function(doses, fields, fertilizers, fines, reduce_batches = TR
module1 <- calculateCostModule1(doses, fields, fertilizers)


# Module 4: Cost of applying fertilizers ----------------------------------
module4 <- calculateCostModule4(doses, fields, fertilizers)


# Module 5: Revenue from harvested crops ----------------------------------
module5 <- calculateRevenueModule5(doses, fields, fertilizers)

Expand All @@ -177,7 +181,7 @@ calculateCost <- function(doses, fields, fertilizers, fines, reduce_batches = TR


# Combine the modules -----------------------------------------------------
cost <- torch::torch_zeros(dim(module1)) + module1 - module5 + module6
cost <- torch::torch_zeros(dim(module1)) + module1 + module4 - module5 + module6


# Convert to € / ha -------------------------------------------------------
Expand Down Expand Up @@ -215,6 +219,35 @@ calculateCostModule1 <- function(doses, fields, fertilizers) {
return(module1)
}

# Module 4: Cost of applying fertilizers -----------------------------------
calculateCostModule4 <- function(doses, fields, fertilizers) {

# Sum dose per fertilizer per field --------------------------------------
fields.b_area <- torch::torch_unsqueeze(fields[,,1], -1)
fields.fertilizers.dose <- fields.b_area * doses


# Calculate number of applications per field ------------------------------
fertilizers.p_app_capacity <- fertilizers[,,10]
fields.fertilizers.p_app_capacity <- torch::torch_unsqueeze(fertilizers.p_app_capacity, 2)
fields.fertilizers.p_app_capacity <- torch::torch_repeat_interleave(fields.fertilizers.p_app_capacity, repeats = dim(fields.fertilizers.dose)[2], dim =2)
fields.fertilizers.applications <- torch::torch_ceil(fields.fertilizers.dose / fields.fertilizers.p_app_capacity)


# Calculate cost of applications -------------------------------------------
fertilizers.p_app_cost <- fertilizers[,,9]
fields.fertilizers.p_app_cost <- torch::torch_unsqueeze(fertilizers.p_app_cost, 2)
fields.fertilizers.p_app_cost <- torch::torch_repeat_interleave(fields.fertilizers.p_app_cost, repeats = dim(fields.fertilizers.dose)[2], dim =2)
fields.fertilizers.cost <- fields.fertilizers.applications * fields.fertilizers.p_app_cost


# Sum cost for farm -------------------------------------------------------
fields.cost <- torch::torch_sum(fields.fertilizers.cost, dim = 3L)
module4 <- torch::torch_sum(fields.cost, dim = 2L)

return(module4)
}

# Module 5: Revenue from harvested crops ------------------------------------
calculateRevenueModule5 <- function(doses, fields, fertilizers) {

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ For the v1 version of `apus` we plan to develop to following features:
* [x] Include cost function for module 1: Purchase of fertilizers
* [ ] Include cost function for module 2: Disposal of manure
* [ ] Include cost function for module 3: Cost of storing fertilizers
* [ ] Include cost function for module 4: Cost of applying fertilizers
* [x] Include cost function for module 4: Cost of applying fertilizers
* [x] Include cost function for module 5: Revenue of harvest
* [x] Include cost function for module 6: Penalties in case of exceeding legal limits
* [ ] Include cost function for module 7: Cost of greenhouse gas emissions
Expand Down
3 changes: 3 additions & 0 deletions data-raw/fertilizers.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ fertilizers[p_type_compost == TRUE, p_p_wcl := 0.25]
fertilizers[p_name_nl == 'Champost', p_p_wcl := 0.75]
fertilizers[p_name_nl == 'Rundvee vaste mest', p_p_wcl := 0.75]

fertilizers[, p_app_cost := 100]
fertilizers[, p_app_capacity := 15000]
fertilizers[p_type_artificial ==TRUE, p_app_capacity := 1000]

# Export table ------------------------------------------------------------
usethis::use_data(fertilizers, overwrite = TRUE, version = 3, compress = 'xz')
Binary file modified data/fertilizers.rda
Binary file not shown.
2 changes: 1 addition & 1 deletion man/fertilizers.Rd

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

8 changes: 8 additions & 0 deletions tests/testthat/test-002-model.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ test_that("Calculate cost for module 1: Purchase of fertilizers", {
expect_length(as.numeric(module1), farms_count)
})


module4 <- calculateCostModule4(doses, fields, fertilizers)

test_that("Calculate cost for module 4: Cost of applying fertilizers", {
expect_contains(class(module4), 'torch_tensor')
expect_length(as.numeric(module4), farms_count)
})

module5 <- calculateRevenueModule5(doses, fields, fertilizers)

test_that("Calculate revenue for module 5: Revenue of harvested crops", {
Expand Down