Spaces:
Sleeping
Sleeping
Commit ·
4c3db36
1
Parent(s): 805e26d
add new function
Browse files- .DS_Store +0 -0
- app.R +4 -0
- function.R +111 -0
- utils_function.R +217 -0
.DS_Store
CHANGED
|
Binary files a/.DS_Store and b/.DS_Store differ
|
|
|
app.R
CHANGED
|
@@ -2,6 +2,10 @@ library(plumber)
|
|
| 2 |
library(tidyverse)
|
| 3 |
# 'plumber.R' is the location of the file shown above
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
pr() %>%
|
| 6 |
pr_mount("/a", pr("services/a_services.R")) %>%
|
| 7 |
pr_mount("/b", pr("services/b_services.R")) %>%
|
|
|
|
| 2 |
library(tidyverse)
|
| 3 |
# 'plumber.R' is the location of the file shown above
|
| 4 |
|
| 5 |
+
message("---- load utils function ----")
|
| 6 |
+
source(file = "utils_function.R")
|
| 7 |
+
|
| 8 |
+
message("---- run plumber ----")
|
| 9 |
pr() %>%
|
| 10 |
pr_mount("/a", pr("services/a_services.R")) %>%
|
| 11 |
pr_mount("/b", pr("services/b_services.R")) %>%
|
function.R
CHANGED
|
@@ -118,3 +118,114 @@ function(req) {
|
|
| 118 |
}
|
| 119 |
|
| 120 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
}
|
| 119 |
|
| 120 |
|
| 121 |
+
#* One Cycle Feeding Generator
|
| 122 |
+
#* @serializer unboxedJSON
|
| 123 |
+
#* @post /one-cycle-generator
|
| 124 |
+
function(req) {
|
| 125 |
+
data_from_user <- req$body
|
| 126 |
+
|
| 127 |
+
shrimp_stock_data <- data_from_user$shrimp_stock_data
|
| 128 |
+
harvest_setting <- data_from_user$harvest_setting
|
| 129 |
+
max_doc <- data_from_user$max_doc
|
| 130 |
+
growth_scenario_table <- data_from_user$growth_scenario_table
|
| 131 |
+
survival_setting <- data_from_user$survival_setting
|
| 132 |
+
blind_feeding_day_1_setting <- data_from_user$blind_feeding_day_1_setting
|
| 133 |
+
blind_feeding_day_2_setting <- data_from_user$blind_feeding_day_2_setting
|
| 134 |
+
date_data <- data_from_user$date_data
|
| 135 |
+
fi_multiplier <- data_from_user$fi_multiplier
|
| 136 |
+
|
| 137 |
+
message("---- generate feeding for one cycle ----")
|
| 138 |
+
|
| 139 |
+
result <- all_feeding_generator_function_v2(
|
| 140 |
+
shrimp_stock_data = feeding_setting_variables$ponds_setting,
|
| 141 |
+
max_doc = input$feed_plan_max_doc,
|
| 142 |
+
harvest_setting = partial_harvest_setting,
|
| 143 |
+
growth_scenario_table = feeding_setting_variables$growth_simulation_result_table,
|
| 144 |
+
survival_setting = feeding_setting_variables$survival_setting,
|
| 145 |
+
blind_feeding_day_1_setting = feeding_setting_variables$blind_feeding_day_1_setting,
|
| 146 |
+
blind_feeding_day_2_setting = feeding_setting_variables$blind_feeding_day_2_setting,
|
| 147 |
+
date_data = input$general_setting_date_input,
|
| 148 |
+
fi_multiplier = feeding_setting_variables$fi_multiplier_setting
|
| 149 |
+
)
|
| 150 |
+
|
| 151 |
+
message("---- check result ----")
|
| 152 |
+
print(result)
|
| 153 |
+
|
| 154 |
+
### blind feed table
|
| 155 |
+
result$blind_feed_table -> blind_feed_table
|
| 156 |
+
|
| 157 |
+
### demand feeding table
|
| 158 |
+
result$demand_feeding_table -> demand_feeding_table
|
| 159 |
+
|
| 160 |
+
### survival table
|
| 161 |
+
result$survival_table -> survival_table
|
| 162 |
+
|
| 163 |
+
### stocking data
|
| 164 |
+
shrimp_stock_data -> stocking_data
|
| 165 |
+
|
| 166 |
+
### partial harvest setting
|
| 167 |
+
harvest_setting -> partial_harvest_setting
|
| 168 |
+
|
| 169 |
+
### feeding plan generation
|
| 170 |
+
message("---- blind feeding generation ----")
|
| 171 |
+
blind_feed_table %>%
|
| 172 |
+
pivot_wider(names_from = type, values_from = total_feed) %>%
|
| 173 |
+
mutate(
|
| 174 |
+
feed_per_day_fr = blind_feeding,
|
| 175 |
+
feed_per_day_indeks = blind_feeding
|
| 176 |
+
) %>%
|
| 177 |
+
select(-blind_feeding) -> blind_feed_table
|
| 178 |
+
|
| 179 |
+
message("---- demand feeding generation ----")
|
| 180 |
+
blind_feed_table %>%
|
| 181 |
+
bind_rows(
|
| 182 |
+
demand_feeding_table %>%
|
| 183 |
+
select(doc, pond_code, feed_per_day_indeks, feed_per_day_fr)
|
| 184 |
+
) %>%
|
| 185 |
+
mutate(feed_type = ifelse(doc <= 30, "blind_feeding", "demand_feeding")) %>%
|
| 186 |
+
left_join(growth_scenario_table()) %>%
|
| 187 |
+
left_join(survival_table) %>%
|
| 188 |
+
# left_join(partial_harvest_setting) %>%
|
| 189 |
+
left_join(stocking_data) %>%
|
| 190 |
+
arrange(pond_code, doc) %>%
|
| 191 |
+
# replace_na(list(harvest_percentage = 0)) %>%
|
| 192 |
+
# mutate(harvested = round((population * harvest_percentage/100))) %>%
|
| 193 |
+
group_by(pond_code) %>%
|
| 194 |
+
mutate(cumulative_harvested = cumsum(harvested)) %>%
|
| 195 |
+
ungroup() %>%
|
| 196 |
+
mutate(biomass = mbw * population_left/1000) %>%
|
| 197 |
+
mutate(biomass_total = biomass + (cumulative_harvested*mbw)/1000) %>%
|
| 198 |
+
group_by(pond_code) %>%
|
| 199 |
+
mutate(
|
| 200 |
+
cumulative_feed_indeks = cumsum(feed_per_day_indeks),
|
| 201 |
+
cumulative_feed_fr = cumsum(feed_per_day_fr),
|
| 202 |
+
) %>%
|
| 203 |
+
ungroup() %>%
|
| 204 |
+
mutate(
|
| 205 |
+
fcr_indeks = cumulative_feed_indeks/biomass_total,
|
| 206 |
+
fcr_fr = cumulative_feed_fr/biomass_total
|
| 207 |
+
) %>%
|
| 208 |
+
mutate(
|
| 209 |
+
data_type = "planning"
|
| 210 |
+
) -> result
|
| 211 |
+
|
| 212 |
+
# variable setting
|
| 213 |
+
## datetime information
|
| 214 |
+
datetime_info <- lubridate::now(tzone = "UTC")
|
| 215 |
+
|
| 216 |
+
## eeding_strategy_planning_datatable
|
| 217 |
+
message("---- Save Feeding Plan for One Cycle ----")
|
| 218 |
+
result %>%
|
| 219 |
+
mutate(
|
| 220 |
+
created_date = datetime_info
|
| 221 |
+
) %>%
|
| 222 |
+
select(
|
| 223 |
+
doc, date, pond_code, pond_area, mbw, sr_est,
|
| 224 |
+
population, harvest_percentage, harvested, cumulative_harvested,
|
| 225 |
+
population_left, biomass, biomass_total, feed_type,
|
| 226 |
+
feed_per_day_fr, feed_per_day_indeks,
|
| 227 |
+
cumulative_feed_fr, cumulative_feed_indeks,
|
| 228 |
+
fcr_fr, fcr_indeks,
|
| 229 |
+
data_type, created_date
|
| 230 |
+
) -> result
|
| 231 |
+
}
|
utils_function.R
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# survival function v2 ----
|
| 2 |
+
## function ----
|
| 3 |
+
survival_function_v2 <- function(
|
| 4 |
+
initial_population, target_survival, max_doc, harvest_setting
|
| 5 |
+
) {
|
| 6 |
+
natural_mort <- (1-(target_survival/100))*100/(max_doc)
|
| 7 |
+
sr_ = 100 - cumsum(rep(natural_mort, max_doc))
|
| 8 |
+
|
| 9 |
+
tibble(
|
| 10 |
+
doc = 1:max_doc,
|
| 11 |
+
sr_est = sr_,
|
| 12 |
+
population = initial_population
|
| 13 |
+
) %>%
|
| 14 |
+
left_join(
|
| 15 |
+
harvest_setting %>%
|
| 16 |
+
mutate(harvest_percentage_cumm = cumsum(harvest_percentage))
|
| 17 |
+
) %>%
|
| 18 |
+
fill(harvest_percentage_cumm, .direction = "down") %>%
|
| 19 |
+
replace_na(
|
| 20 |
+
list(
|
| 21 |
+
harvest_percentage_cumm = 0,
|
| 22 |
+
harvest_percentage = 0
|
| 23 |
+
)
|
| 24 |
+
) %>%
|
| 25 |
+
mutate(lag_harvest_percentage_cumm = lag(harvest_percentage_cumm)) %>%
|
| 26 |
+
mutate(sr_est = sr_est - ifelse(is.na(lag_harvest_percentage_cumm), 0, lag_harvest_percentage_cumm)) %>%
|
| 27 |
+
mutate(
|
| 28 |
+
population_left = population * sr_est/100,
|
| 29 |
+
harvested = population_left * harvest_percentage/100
|
| 30 |
+
) %>%
|
| 31 |
+
select(doc, sr_est, population, harvest_percentage, harvested, population_left) -> result
|
| 32 |
+
|
| 33 |
+
return(result)
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
## testing ----
|
| 37 |
+
# phs <- dbGetQuery(mydb, "select doc, harvest_percentage from feed_module.partial_harvest_setting where feeding_plan_id = 'DLA-20250106-03-Hadi';")
|
| 38 |
+
#
|
| 39 |
+
# survival_function_v2(
|
| 40 |
+
# initial_population = 414252,
|
| 41 |
+
# target_survival = 80,
|
| 42 |
+
# max_doc = 120,
|
| 43 |
+
# harvest_setting = phs
|
| 44 |
+
# ) -> survival_result
|
| 45 |
+
# survival_result %>%
|
| 46 |
+
# print(n = 120)
|
| 47 |
+
|
| 48 |
+
blind_feeding_function_v2 <- function(
|
| 49 |
+
total_feed_day_1,
|
| 50 |
+
blind_feeding_rules_table,
|
| 51 |
+
survival_table
|
| 52 |
+
) {
|
| 53 |
+
print(survival_table)
|
| 54 |
+
# init farm multiplier
|
| 55 |
+
farm_multiplier <- c(total_feed_day_1, blind_feeding_rules_table$feed_increment)
|
| 56 |
+
|
| 57 |
+
# check if farm_multiplier is empty
|
| 58 |
+
if (length(farm_multiplier) == 0) {
|
| 59 |
+
farm_multiplier <- c(3, 0.2, 0.4, 0.6)
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
blind_feeding_rules_table[1,] %>%
|
| 63 |
+
mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_1
|
| 64 |
+
|
| 65 |
+
blind_feeding_rules_table[2,] %>%
|
| 66 |
+
mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_2
|
| 67 |
+
|
| 68 |
+
blind_feeding_rules_table[3,] %>%
|
| 69 |
+
mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_3
|
| 70 |
+
|
| 71 |
+
session_1 <- c(farm_multiplier[1], farm_multiplier[1] + cumsum(rep(farm_multiplier[2], range_1)))
|
| 72 |
+
|
| 73 |
+
session_2 <- c(max(session_1) + cumsum(rep(farm_multiplier[3], range_2)))
|
| 74 |
+
|
| 75 |
+
session_3 <- c(max(session_2) + cumsum(rep(farm_multiplier[4], range_3)))
|
| 76 |
+
|
| 77 |
+
end_doc <- max(blind_feeding_rules_table$doc_end)
|
| 78 |
+
|
| 79 |
+
tibble(
|
| 80 |
+
doc = 1:end_doc,
|
| 81 |
+
bf_coef = c(session_1, session_2, session_3)
|
| 82 |
+
) %>%
|
| 83 |
+
inner_join(
|
| 84 |
+
survival_table %>%
|
| 85 |
+
select(doc, population_left)
|
| 86 |
+
) %>%
|
| 87 |
+
mutate(
|
| 88 |
+
blind_feeding = bf_coef * population_left/100000
|
| 89 |
+
) %>%
|
| 90 |
+
select(doc, blind_feeding) -> feed_table
|
| 91 |
+
|
| 92 |
+
return(feed_table)
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
## testing ----
|
| 96 |
+
|
| 97 |
+
# blind_feeding_settings_day_2 <- tibble(
|
| 98 |
+
# doc_start = c(2, 11, 21),
|
| 99 |
+
# doc_end = c(10, 20, 30),
|
| 100 |
+
# feed_increment = c(0.2, 0.4, 0.6),
|
| 101 |
+
# unit = "kg/100K shrimps"
|
| 102 |
+
# )
|
| 103 |
+
#
|
| 104 |
+
# blind_feeding_function_v2(
|
| 105 |
+
# total_feed_day_1 = 3,
|
| 106 |
+
# blind_feeding_rules_table = blind_feeding_settings_day_2,
|
| 107 |
+
# survival_table = survival_result
|
| 108 |
+
# )
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# All Feeding Generator V2 ----
|
| 112 |
+
## function ----
|
| 113 |
+
all_feeding_generator_function_v2 <- function(
|
| 114 |
+
shrimp_stock_data,
|
| 115 |
+
harvest_setting,
|
| 116 |
+
max_doc,
|
| 117 |
+
growth_scenario_table,
|
| 118 |
+
survival_setting,
|
| 119 |
+
blind_feeding_day_1_setting,
|
| 120 |
+
blind_feeding_day_2_setting,
|
| 121 |
+
date_data,
|
| 122 |
+
fi_multiplier
|
| 123 |
+
) {
|
| 124 |
+
survival_table_container <- tibble(
|
| 125 |
+
date = as.Date(character()),
|
| 126 |
+
doc = as.integer(),
|
| 127 |
+
sr_est = as.numeric(),
|
| 128 |
+
pond_code = as.character(),
|
| 129 |
+
population = as.numeric(),
|
| 130 |
+
harvest_percentage = as.numeric(),
|
| 131 |
+
harvested = as.numeric(),
|
| 132 |
+
population_left = as.numeric()
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
demand_feeding_container <- tibble(
|
| 136 |
+
doc = as.integer(),
|
| 137 |
+
pond_code = as.character(),
|
| 138 |
+
adg_plan = as.numeric(),
|
| 139 |
+
indeks_plan = as.numeric(),
|
| 140 |
+
fr_plan = as.numeric(),
|
| 141 |
+
feed_per_day_fr = as.numeric(),
|
| 142 |
+
feed_per_day_indeks = as.numeric()
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
blind_feeding_container <- tibble(
|
| 146 |
+
doc = as.integer(),
|
| 147 |
+
total_feed = as.integer(),
|
| 148 |
+
type = as.character(),
|
| 149 |
+
pond_code = as.character()
|
| 150 |
+
)
|
| 151 |
+
|
| 152 |
+
doc_vec <- 1:max_doc
|
| 153 |
+
|
| 154 |
+
for (pond_name in shrimp_stock_data$pond_code) {
|
| 155 |
+
|
| 156 |
+
date_table <- tibble(
|
| 157 |
+
date = seq(from = as.Date(date_data), by = "day", length.out = length(doc_vec))
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
shrimp_stock_data %>%
|
| 161 |
+
filter(pond_code == pond_name) -> pond_stock_stats
|
| 162 |
+
|
| 163 |
+
survival_table <- survival_function_v2(
|
| 164 |
+
initial_population = pond_stock_stats$stocking_actual,
|
| 165 |
+
target_survival = survival_setting,
|
| 166 |
+
max_doc = max_doc,
|
| 167 |
+
harvest_setting = harvest_setting
|
| 168 |
+
) %>%
|
| 169 |
+
mutate(pond_code = pond_stock_stats$pond_code) %>%
|
| 170 |
+
bind_cols(date_table)
|
| 171 |
+
|
| 172 |
+
survival_table_container %>%
|
| 173 |
+
bind_rows(survival_table) -> survival_table_container
|
| 174 |
+
|
| 175 |
+
demand_feeding_table <- growth_scenario_table %>%
|
| 176 |
+
inner_join(survival_table) %>%
|
| 177 |
+
mutate(
|
| 178 |
+
adg_plan = mbw - lag(mbw)
|
| 179 |
+
) %>%
|
| 180 |
+
mutate(
|
| 181 |
+
indeks_plan = adg_plan * fi_multiplier,
|
| 182 |
+
fr_plan = 10^(-0.899-(0.561*log10(mbw)))
|
| 183 |
+
) %>%
|
| 184 |
+
mutate(
|
| 185 |
+
feed_per_day_indeks = doc * indeks_plan * population_left/100000,
|
| 186 |
+
feed_per_day_fr = mbw * (population_left/1000) * fr_plan
|
| 187 |
+
) %>%
|
| 188 |
+
filter(doc >= 31) %>%
|
| 189 |
+
select(
|
| 190 |
+
doc, pond_code, adg_plan, indeks_plan, fr_plan, feed_per_day_fr,
|
| 191 |
+
contains("_indeks"),
|
| 192 |
+
contains("_fr")
|
| 193 |
+
)
|
| 194 |
+
|
| 195 |
+
demand_feeding_container %>%
|
| 196 |
+
bind_rows(demand_feeding_table) -> demand_feeding_container
|
| 197 |
+
|
| 198 |
+
blind_feed_table <- blind_feeding_function_v2(
|
| 199 |
+
survival_table = survival_table,
|
| 200 |
+
total_feed_day_1 = blind_feeding_day_1_setting,
|
| 201 |
+
blind_feeding_rules_table = blind_feeding_day_2_setting
|
| 202 |
+
) %>%
|
| 203 |
+
rename(total_feed = blind_feeding) %>%
|
| 204 |
+
mutate(type = 'blind_feeding', pond_code = pond_name)
|
| 205 |
+
|
| 206 |
+
blind_feeding_container %>%
|
| 207 |
+
bind_rows(blind_feed_table) -> blind_feeding_container
|
| 208 |
+
}
|
| 209 |
+
|
| 210 |
+
return(
|
| 211 |
+
list(
|
| 212 |
+
survival_table = survival_table_container,
|
| 213 |
+
blind_feed_table = blind_feeding_container,
|
| 214 |
+
demand_feeding_table = demand_feeding_container
|
| 215 |
+
)
|
| 216 |
+
)
|
| 217 |
+
}
|