Spaces:
Sleeping
Sleeping
| # growth function v2 ---- | |
| growth_function_training <- function( | |
| data, | |
| init_size = 0.01, | |
| max_doc = 120, | |
| method = "ADG" | |
| ) { | |
| # set maximum doc | |
| doc_vec <- 1:max_doc | |
| if (method == "Growth Model") { | |
| message("---- use Growth Model ----") | |
| model_res <-nls( | |
| mbw ~ asymptotic_growth*(1-(1-(init_size/asymptotic_growth)^(1/allometric_scale))*exp(-growth_rate*doc))^allometric_scale, | |
| data = data, | |
| start = list( | |
| asymptotic_growth = 40, | |
| allometric_scale = 3, | |
| growth_rate = 0.015 | |
| ) | |
| ) | |
| pred_res <- predict( | |
| model_res, | |
| list( | |
| doc = doc_vec | |
| ) | |
| ) | |
| tibble( | |
| doc = doc_vec, | |
| mbw = round(pred_res,3) | |
| ) -> growth_res | |
| } else if(method == "ABW Interpolation") { | |
| message("---- use ABW Interpolation ----") | |
| min_doc <- min(data$doc) | |
| max_doc <- max(data$doc) | |
| growth_res <- data.frame( | |
| with(data, | |
| approx(doc, mbw, xout = seq(min_doc, max_doc, by = 1), method = "linear") | |
| ) | |
| ) %>% | |
| rename(doc = x, mbw = y) | |
| } else if(method == "Genetics - Balanced") { | |
| message("---- use Genetics - Balanced Data ----") | |
| growth_res <- growth_by_genetic_data %>% | |
| select(DOC, Balanced) %>% | |
| rename( | |
| doc = DOC, | |
| mbw = Balanced | |
| ) %>% | |
| filter(doc <= max_doc) | |
| } else if(method == "Genetics - Fast Growth") { | |
| message("---- use Genetics - Fast Growth Data ----") | |
| growth_res <- growth_by_genetic_data %>% | |
| select(DOC, Fast.Growth) %>% | |
| rename( | |
| mbw = Fast.Growth, | |
| doc = DOC | |
| ) %>% | |
| filter(doc <= max_doc) | |
| } else { | |
| message("---- use ADG Interpolation ----") | |
| initial_weight <- init_size | |
| final_doc <- max_doc | |
| adg_data <- data | |
| abw_container <- list() | |
| for (indeks in 1:nrow(adg_data)) { | |
| if (indeks == 1) { | |
| abw_container[[indeks]] <- c(initial_weight, initial_weight + cumsum(rep(adg_data$adg[indeks], adg_data$doc[indeks + 1] - adg_data$doc[indeks] - 1))) | |
| } else if(indeks == nrow(adg_data)) { | |
| initial_weight <- max(abw_container[[indeks-1]]) | |
| abw_container[[indeks]] <- c(initial_weight + cumsum(rep(adg_data$adg[indeks], final_doc - adg_data$doc[indeks] + 1))) | |
| } else { | |
| initial_weight <- max(abw_container[[indeks-1]]) | |
| abw_container[[indeks]] <- c(initial_weight + cumsum(rep(adg_data$adg[indeks], adg_data$doc[indeks + 1] - adg_data$doc[indeks]))) | |
| } | |
| } | |
| mbw_ <- unlist(abw_container) | |
| tibble( | |
| doc = 1:final_doc, | |
| mbw = mbw_ | |
| ) -> growth_res | |
| } | |
| return(growth_res) | |
| } | |
| # survival function v2 ---- | |
| ## function ---- | |
| survival_function_v2 <- function( | |
| method, model, initial_population, target_survival, max_doc, harvest_setting | |
| ) { | |
| if (method == "Target Based") { | |
| message("---- Calculate Survival using Target Based Method ----") | |
| natural_mort <- (1-(target_survival))*100/(max_doc) | |
| sr_ = 100 - cumsum(rep(natural_mort, max_doc)) | |
| tibble( | |
| doc = 1:max_doc, | |
| sr_est = sr_, | |
| population = initial_population | |
| ) -> temp_table | |
| } else { | |
| message("---- Calculate Survival using Model Based Method ----") | |
| if(model == "Model Survival Logistik") { | |
| message("---- Use Survival Logistic Model ----") | |
| doc <- 1:max_doc | |
| sr_ <- (100 - 2.86*log2(doc))/100 | |
| tibble( | |
| doc = 1:max_doc, | |
| sr_est = sr_, | |
| population = initial_population | |
| ) -> temp_table | |
| } | |
| } | |
| temp_table %>% | |
| left_join( | |
| harvest_setting %>% | |
| mutate(harvest_percentage_cumm = cumsum(harvest_percentage)) | |
| ) %>% | |
| fill(harvest_percentage_cumm, .direction = "down") %>% | |
| replace_na( | |
| list( | |
| harvest_percentage_cumm = 0, | |
| harvest_percentage = 0 | |
| ) | |
| ) %>% | |
| mutate(lag_harvest_percentage_cumm = lag(harvest_percentage_cumm)) %>% | |
| mutate(sr_est = sr_est - ifelse(is.na(lag_harvest_percentage_cumm), 0, lag_harvest_percentage_cumm)) %>% | |
| mutate( | |
| population_left = population * sr_est/100, | |
| harvested = population_left * harvest_percentage/100 | |
| ) %>% | |
| select(doc, sr_est, population, harvest_percentage, harvested, population_left) -> result | |
| return(result) | |
| } | |
| ## testing ---- | |
| # survival_function_v2( | |
| # initial_population = 414252, | |
| # target_survival = 0.8, | |
| # max_doc = 120, | |
| # harvest_setting = harvest_setting, | |
| # method = survival_calculation_method, | |
| # model = survival_model | |
| # ) -> survival_result | |
| # survival_result %>% | |
| # print(n = 120) | |
| blind_feeding_function_v2 <- function( | |
| total_feed_day_1, | |
| blind_feeding_rules_table, | |
| survival_table | |
| ) { | |
| # init farm multiplier | |
| farm_multiplier <- c(total_feed_day_1, blind_feeding_rules_table$feed_increment) | |
| # check if farm_multiplier is empty | |
| if (length(farm_multiplier) == 0) { | |
| farm_multiplier <- c(3, 0.2, 0.4, 0.6) | |
| } | |
| blind_feeding_rules_table[1,] %>% | |
| mutate(range = doc_end - doc_start) %>% pull(range) -> range_1 | |
| blind_feeding_rules_table[2,] %>% | |
| mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_2 | |
| blind_feeding_rules_table[3,] %>% | |
| mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_3 | |
| session_1 <- c(farm_multiplier[1], farm_multiplier[1] + cumsum(rep(farm_multiplier[2], range_1))) | |
| session_2 <- c(max(session_1) + cumsum(rep(farm_multiplier[3], range_2))) | |
| session_3 <- c(max(session_2) + cumsum(rep(farm_multiplier[4], range_3))) | |
| end_doc <- max(blind_feeding_rules_table$doc_end) | |
| tibble( | |
| doc = 1:end_doc, | |
| bf_coef = c(session_1, session_2, session_3) | |
| ) %>% | |
| inner_join( | |
| survival_table %>% | |
| select(doc, population_left) | |
| ) %>% | |
| mutate( | |
| blind_feeding = bf_coef * population_left/100000 | |
| ) %>% | |
| select(doc, blind_feeding) -> feed_table | |
| return(feed_table) | |
| } | |
| ## testing ---- | |
| # blind_feeding_settings_day_2 <- tibble( | |
| # doc_start = c(1, 11, 21), | |
| # doc_end = c(10, 20, 30), | |
| # feed_increment = c(0.2, 0.4, 0.6), | |
| # unit = "kg/100K shrimps" | |
| # ) | |
| # | |
| # blind_feeding_function_v2( | |
| # total_feed_day_1 = 3, | |
| # blind_feeding_rules_table = blind_feeding_settings_day_2, | |
| # survival_table = survival_result | |
| # ) | |
| fi_generator <- function( | |
| total_feed_day_1, | |
| blind_feeding_rules_table | |
| ) { | |
| # init farm multiplier | |
| farm_multiplier <- c(total_feed_day_1, blind_feeding_rules_table$feed_increment) | |
| # check if farm_multiplier is empty | |
| if (length(farm_multiplier) == 0) { | |
| farm_multiplier <- c(3, 0.2, 0.4, 0.6) | |
| } | |
| blind_feeding_rules_table[1,] %>% | |
| mutate(range = doc_end - doc_start) %>% pull(range) -> range_1 | |
| blind_feeding_rules_table[2,] %>% | |
| mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_2 | |
| blind_feeding_rules_table[3,] %>% | |
| mutate(range = doc_end - doc_start + 1) %>% pull(range) -> range_3 | |
| session_1 <- c(farm_multiplier[1], farm_multiplier[1] + cumsum(rep(farm_multiplier[2], range_1))) | |
| session_2 <- c(max(session_1) + cumsum(rep(farm_multiplier[3], range_2))) | |
| session_3 <- c(max(session_2) + cumsum(rep(farm_multiplier[4], range_3))) | |
| end_doc <- max(blind_feeding_rules_table$doc_end) | |
| tibble( | |
| doc = 1:end_doc, | |
| feed_index = c(session_1, session_2, session_3) | |
| ) -> result | |
| return(result) | |
| } | |
| # fi_generator( | |
| # total_feed_day_1 = 3, | |
| # blind_feeding_rules_table = blind_feeding_increment_setting | |
| # ) | |
| # All Feeding Generator V2 ---- | |
| ## function ---- | |
| all_feeding_generator_function_v2 <- function( | |
| date_data, | |
| max_doc, | |
| shrimp_stock_data, | |
| harvest_setting, | |
| survival_calculation_method, | |
| target_survival, | |
| survival_model, | |
| blind_feeding_day_1_setting, | |
| blind_feeding_day_2_setting, | |
| fi_multiplier, | |
| fr_calculation_method, | |
| fr_coef_x, | |
| fr_coef_y, | |
| growth_scenario_table | |
| ) { | |
| survival_table_container <- tibble( | |
| date = as.Date(character()), | |
| doc = as.integer(), | |
| sr_est = as.numeric(), | |
| pond_code = as.character(), | |
| population = as.numeric(), | |
| harvest_percentage = as.numeric(), | |
| harvested = as.numeric(), | |
| population_left = as.numeric() | |
| ) | |
| demand_feeding_container <- tibble( | |
| doc = as.integer(), | |
| pond_code = as.character(), | |
| adg_plan = as.numeric(), | |
| indeks_plan = as.numeric(), | |
| fr_plan = as.numeric(), | |
| feed_per_day_fr = as.numeric(), | |
| feed_per_day_indeks = as.numeric() | |
| ) | |
| blind_feeding_container <- tibble( | |
| doc = as.integer(), | |
| total_feed = as.integer(), | |
| type = as.character(), | |
| pond_code = as.character() | |
| ) | |
| doc_vec <- 1:max_doc | |
| for (pond_name in shrimp_stock_data$pond_code) { | |
| date_table <- tibble( | |
| date = seq(from = date_data, by = "day", length.out = length(doc_vec)) | |
| ) | |
| shrimp_stock_data %>% | |
| filter(pond_code == pond_name) -> pond_stock_stats | |
| survival_table <- survival_function_v2( | |
| method = survival_calculation_method, | |
| model = survival_model, | |
| initial_population = pond_stock_stats$stocking_actual, | |
| target_survival = target_survival, | |
| max_doc = max_doc, | |
| harvest_setting = harvest_setting | |
| ) %>% | |
| mutate(pond_code = pond_stock_stats$pond_code) %>% | |
| bind_cols(date_table) | |
| survival_table_container %>% | |
| bind_rows(survival_table) -> survival_table_container | |
| demand_feeding_table <- growth_scenario_table %>% | |
| inner_join(survival_table) %>% | |
| mutate( | |
| adg_plan = mbw - lag(mbw) | |
| ) %>% | |
| mutate( | |
| indeks_plan = adg_plan * fi_multiplier, | |
| ) | |
| if (fr_calculation_method == "FR Type 1") { | |
| demand_feeding_table <- demand_feeding_table %>% | |
| mutate( | |
| fr_plan = 10^(fr_coef_x-(fr_coef_y*log10(mbw))) | |
| ) | |
| } else if(fr_calculation_method == "FR Type 2") { | |
| demand_feeding_table <- demand_feeding_table %>% | |
| mutate( | |
| fr_plan = 10^(fr_coef_x-(fr_coef_y*log10(mbw))) * 100 | |
| ) | |
| } else { | |
| demand_feeding_table <- demand_feeding_table %>% | |
| mutate( | |
| fr_plan = 10^(fr_coef_x-(fr_coef_y*log10(mbw))) | |
| ) | |
| } | |
| demand_feeding_table <- demand_feeding_table %>% | |
| mutate( | |
| feed_per_day_indeks = doc * indeks_plan * population_left/100000, | |
| feed_per_day_fr = mbw * (population_left/1000) * fr_plan | |
| ) %>% | |
| filter(doc >= 31) %>% | |
| select( | |
| doc, pond_code, adg_plan, indeks_plan, fr_plan, feed_per_day_fr, | |
| contains("_indeks"), | |
| contains("_fr") | |
| ) | |
| demand_feeding_container %>% | |
| bind_rows(demand_feeding_table) -> demand_feeding_container | |
| blind_feed_table <- blind_feeding_function_v2( | |
| survival_table = survival_table, | |
| total_feed_day_1 = blind_feeding_day_1_setting, | |
| blind_feeding_rules_table = blind_feeding_day_2_setting | |
| ) %>% | |
| rename(total_feed = blind_feeding) %>% | |
| mutate(type = 'blind_feeding', pond_code = pond_name) | |
| blind_feeding_container %>% | |
| bind_rows(blind_feed_table) -> blind_feeding_container | |
| } | |
| return( | |
| list( | |
| survival_table = survival_table_container, | |
| blind_feed_table = blind_feeding_container, | |
| demand_feeding_table = demand_feeding_container | |
| ) | |
| ) | |
| } | |