hadimaster65555 commited on
Commit
4dfdb4e
·
1 Parent(s): 487edd3

fix script

Browse files
.DS_Store CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
 
app.R CHANGED
@@ -5,6 +5,9 @@ library(tidyverse)
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")) %>%
 
5
  message("---- load utils function ----")
6
  source(file = "utils_function.R")
7
 
8
+ message("---- load any data ----")
9
+ source(file = "data_loader_script.R")
10
+
11
  message("---- run plumber ----")
12
  pr() %>%
13
  pr_mount("/a", pr("services/a_services.R")) %>%
data/raw_data_for_feed_module - ABW Growth.csv ADDED
@@ -0,0 +1,121 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ DOC,Balanced,Fast Growth
2
+ 1,0.01,0.03
3
+ 2,0.08,0.1
4
+ 3,0.15,0.17
5
+ 4,0.22,0.24
6
+ 5,0.29,0.31
7
+ 6,0.36,0.38
8
+ 7,0.43,0.45
9
+ 8,0.5,0.52
10
+ 9,0.57,0.59
11
+ 10,0.64,0.66
12
+ 11,0.71,0.73
13
+ 12,0.78,0.8
14
+ 13,0.85,0.87
15
+ 14,0.92,0.94
16
+ 15,0.99,1.01
17
+ 16,1.06,1.08
18
+ 17,1.13,1.15
19
+ 18,1.2,1.22
20
+ 19,1.27,1.29
21
+ 20,1.34,1.36
22
+ 21,1.41,1.43
23
+ 22,1.48,1.5
24
+ 23,1.55,1.57
25
+ 24,1.62,1.64
26
+ 25,1.69,1.71
27
+ 26,1.76,1.78
28
+ 27,1.83,1.85
29
+ 28,1.9,1.92
30
+ 29,1.97,1.99
31
+ 30,2.17,2.19
32
+ 31,2.37,2.39
33
+ 32,2.57,2.59
34
+ 33,2.77,2.79
35
+ 34,2.97,2.99
36
+ 35,3.17,3.19
37
+ 36,3.37,3.39
38
+ 37,3.57,3.59
39
+ 38,3.77,3.79
40
+ 39,3.97,3.99
41
+ 40,4.17,4.19
42
+ 41,4.37,4.39
43
+ 42,4.57,4.59
44
+ 43,4.77,4.79
45
+ 44,4.97,4.99
46
+ 45,5.17,5.19
47
+ 46,5.37,5.39
48
+ 47,5.57,5.59
49
+ 48,5.77,5.79
50
+ 49,5.97,5.99
51
+ 50,6.17,6.37
52
+ 51,6.37,6.57
53
+ 52,6.57,6.77
54
+ 53,6.77,6.97
55
+ 54,6.97,7.17
56
+ 55,7.17,7.37
57
+ 56,7.37,7.57
58
+ 57,7.57,7.77
59
+ 58,7.77,7.97
60
+ 59,7.97,8.17
61
+ 60,8.17,8.37
62
+ 61,8.47,8.67
63
+ 62,8.77,8.97
64
+ 63,9.07,9.27
65
+ 64,9.37,9.57
66
+ 65,9.67,9.87
67
+ 66,9.97,10.17
68
+ 67,10.27,10.47
69
+ 68,10.57,10.77
70
+ 69,10.87,11.07
71
+ 70,11.17,11.37
72
+ 71,11.47,11.67
73
+ 72,11.77,11.97
74
+ 73,12.07,12.27
75
+ 74,12.37,12.57
76
+ 75,12.67,12.87
77
+ 76,12.97,13.17
78
+ 77,13.27,13.47
79
+ 78,13.57,13.77
80
+ 79,13.87,14.07
81
+ 80,14.17,14.67
82
+ 81,14.47,14.97
83
+ 82,14.77,15.27
84
+ 83,15.07,15.57
85
+ 84,15.37,15.87
86
+ 85,15.67,16.17
87
+ 86,15.97,16.47
88
+ 87,16.27,16.77
89
+ 88,16.57,17.07
90
+ 89,16.87,17.37
91
+ 90,17.17,18.17
92
+ 91,17.47,18.47
93
+ 92,17.82,18.82
94
+ 93,18.17,19.17
95
+ 94,18.52,19.52
96
+ 95,18.87,19.87
97
+ 96,19.22,20.22
98
+ 97,19.57,20.57
99
+ 98,19.92,20.92
100
+ 99,20.27,21.27
101
+ 100,20.62,22.12
102
+ 101,20.97,22.47
103
+ 102,21.32,22.82
104
+ 103,21.67,23.17
105
+ 104,22.02,23.52
106
+ 105,22.37,23.87
107
+ 106,22.72,24.22
108
+ 107,23.07,24.57
109
+ 108,23.42,24.92
110
+ 109,23.77,25.27
111
+ 110,24.12,25.62
112
+ 111,24.47,25.97
113
+ 112,24.82,26.32
114
+ 113,25.17,26.67
115
+ 114,25.52,27.02
116
+ 115,25.87,27.37
117
+ 116,26.22,27.72
118
+ 117,26.57,28.07
119
+ 118,26.92,28.42
120
+ 119,27.27,28.77
121
+ 120,27.62,29.12
data_loader_script.R ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # growth by genetics
2
+ growth_by_genetic_data <- read.csv(file = "data/raw_data_for_feed_module - ABW Growth.csv")
function.R CHANGED
@@ -124,113 +124,123 @@ function(req) {
124
  function(req) {
125
  data_from_user <- req$body
126
 
127
- print(data_from_user)
128
- shrimp_stock_data <- data_from_user$shrimp_stock_data %>%
 
129
  janitor::clean_names()
130
- harvest_setting <- data_from_user$harvest_setting %>%
131
  janitor::clean_names()
132
- max_doc <- data_from_user$max_doc
133
- growth_scenario_table <- data_from_user$growth_scenario_table %>%
 
 
 
 
 
 
 
 
134
  janitor::clean_names()
135
- survival_setting <- data_from_user$survival_setting
136
- blind_feeding_day_1_setting <- data_from_user$blind_feeding_day_1_setting
137
- blind_feeding_day_2_setting <- data_from_user$blind_feeding_day_2_setting %>%
138
  janitor::clean_names()
139
- date_data <- data_from_user$date_data
140
- fi_multiplier <- data_from_user$fi_multiplier_setting
141
 
142
  message("---- generate feeding for one cycle ----")
143
 
144
- result <- all_feeding_generator_function_v2(
145
- shrimp_stock_data = shrimp_stock_data,
146
- max_doc = max_doc,
147
- harvest_setting = harvest_setting,
148
- growth_scenario_table = growth_scenario_table,
149
- survival_setting = survival_setting,
150
- blind_feeding_day_1_setting = blind_feeding_day_1_setting,
151
- blind_feeding_day_2_setting = blind_feeding_day_2_setting,
152
- date_data = date_data,
153
- fi_multiplier = fi_multiplier
154
- )
155
 
156
  message("---- check result ----")
157
- print(result)
158
-
159
- ### blind feed table
160
- result$blind_feed_table -> blind_feed_table
161
-
162
- ### demand feeding table
163
- result$demand_feeding_table -> demand_feeding_table
164
-
165
- ### survival table
166
- result$survival_table -> survival_table
167
-
168
- ### stocking data
169
- shrimp_stock_data -> stocking_data
170
-
171
- ### partial harvest setting
172
- harvest_setting -> partial_harvest_setting
173
-
174
- ### feeding plan generation
175
- message("---- blind feeding generation ----")
176
- blind_feed_table %>%
177
- pivot_wider(names_from = type, values_from = total_feed) %>%
178
- mutate(
179
- feed_per_day_fr = blind_feeding,
180
- feed_per_day_indeks = blind_feeding
181
- ) %>%
182
- select(-blind_feeding) -> blind_feed_table
183
-
184
- message("---- demand feeding generation ----")
185
- blind_feed_table %>%
186
- bind_rows(
187
- demand_feeding_table %>%
188
- select(doc, pond_code, feed_per_day_indeks, feed_per_day_fr)
189
- ) %>%
190
- mutate(feed_type = ifelse(doc <= 30, "blind_feeding", "demand_feeding")) %>%
191
- left_join(growth_scenario_table) %>%
192
- left_join(survival_table) %>%
193
- # left_join(partial_harvest_setting) %>%
194
- left_join(stocking_data) %>%
195
- arrange(pond_code, doc) %>%
196
- # replace_na(list(harvest_percentage = 0)) %>%
197
- # mutate(harvested = round((population * harvest_percentage/100))) %>%
198
- group_by(pond_code) %>%
199
- mutate(cumulative_harvested = cumsum(harvested)) %>%
200
- ungroup() %>%
201
- mutate(biomass = mbw * population_left/1000) %>%
202
- mutate(biomass_total = biomass + (cumulative_harvested*mbw)/1000) %>%
203
- group_by(pond_code) %>%
204
- mutate(
205
- cumulative_feed_indeks = cumsum(feed_per_day_indeks),
206
- cumulative_feed_fr = cumsum(feed_per_day_fr),
207
- ) %>%
208
- ungroup() %>%
209
- mutate(
210
- fcr_indeks = cumulative_feed_indeks/biomass_total,
211
- fcr_fr = cumulative_feed_fr/biomass_total
212
- ) %>%
213
- mutate(
214
- data_type = "planning"
215
- ) -> result
216
-
217
- # variable setting
218
- ## datetime information
219
- datetime_info <- lubridate::now(tzone = "UTC")
220
-
221
- ## eeding_strategy_planning_datatable
222
- message("---- Save Feeding Plan for One Cycle ----")
223
- result %>%
224
- mutate(
225
- created_date = datetime_info
226
- ) %>%
227
- select(
228
- doc, date, pond_code, pond_area, mbw, sr_est,
229
- population, harvest_percentage, harvested, cumulative_harvested,
230
- population_left, biomass, biomass_total, feed_type,
231
- feed_per_day_fr, feed_per_day_indeks,
232
- cumulative_feed_fr, cumulative_feed_indeks,
233
- fcr_fr, fcr_indeks,
234
- data_type, created_date
235
- ) -> result
 
236
  }
 
124
  function(req) {
125
  data_from_user <- req$body
126
 
127
+ start_date <- data_from_user$start_date
128
+ maximum_doc <- data_from_user$maximum_doc
129
+ pond_setting <- data_from_user$pond_setting %>%
130
  janitor::clean_names()
131
+ partial_harvest_setting <- data_from_user$partial_harvest_setting %>%
132
  janitor::clean_names()
133
+ survival_calculation_method <- data_from_user$survival_calculation_method
134
+ target_survival <- data_from_user$target_survival
135
+ survival_model <- data_from_user$survival_model
136
+ feeding_session_setting <- data_from_user$feeding_session_setting
137
+ fi_multiplier_setting <- data_from_user$fi_multiplier_setting
138
+ fr_type_setting <- data_from_user$fr_type_setting
139
+ fr_coef_x <- data_from_user$fr_coef_x
140
+ fr_coef_y <- data_from_user$fr_coef_y
141
+ blind_feeding_start_feeding_setting <- data_from_user$blind_feeding_start_feeding_setting
142
+ blind_feeding_increment_setting <- data_from_user$blind_feeding_increment_setting %>%
143
  janitor::clean_names()
144
+ growth_calculation_method <- data_from_user$growth_calculation_method
145
+ initial_abw <- data_from_user$initial_abw
146
+ initial_growth_data <- data_from_user$initial_growth_data %>%
147
  janitor::clean_names()
148
+
149
+ print()
150
 
151
  message("---- generate feeding for one cycle ----")
152
 
153
+ # result <- all_feeding_generator_function_v2(
154
+ # shrimp_stock_data = shrimp_stock_data,
155
+ # max_doc = max_doc,
156
+ # harvest_setting = harvest_setting,
157
+ # growth_scenario_table = growth_scenario_table,
158
+ # survival_setting = survival_setting,
159
+ # blind_feeding_day_1_setting = blind_feeding_day_1_setting,
160
+ # blind_feeding_day_2_setting = blind_feeding_day_2_setting,
161
+ # date_data = date_data,
162
+ # fi_multiplier = fi_multiplier
163
+ # )
164
 
165
  message("---- check result ----")
166
+
167
+ # ### blind feed table
168
+ # result$blind_feed_table -> blind_feed_table
169
+ #
170
+ # ### demand feeding table
171
+ # result$demand_feeding_table -> demand_feeding_table
172
+ #
173
+ # ### survival table
174
+ # result$survival_table -> survival_table
175
+ #
176
+ # ### stocking data
177
+ # shrimp_stock_data -> stocking_data
178
+ #
179
+ # ### partial harvest setting
180
+ # harvest_setting -> partial_harvest_setting
181
+ #
182
+ # ### feeding plan generation
183
+ # message("---- blind feeding generation ----")
184
+ # blind_feed_table %>%
185
+ # pivot_wider(names_from = type, values_from = total_feed) %>%
186
+ # mutate(
187
+ # feed_per_day_fr = blind_feeding,
188
+ # feed_per_day_indeks = blind_feeding
189
+ # ) %>%
190
+ # select(-blind_feeding) -> blind_feed_table
191
+ #
192
+ # message("---- demand feeding generation ----")
193
+ # blind_feed_table %>%
194
+ # bind_rows(
195
+ # demand_feeding_table %>%
196
+ # select(doc, pond_code, feed_per_day_indeks, feed_per_day_fr)
197
+ # ) %>%
198
+ # mutate(feed_type = ifelse(doc <= 30, "blind_feeding", "demand_feeding")) %>%
199
+ # left_join(growth_scenario_table) %>%
200
+ # left_join(survival_table) %>%
201
+ # # left_join(partial_harvest_setting) %>%
202
+ # left_join(stocking_data) %>%
203
+ # arrange(pond_code, doc) %>%
204
+ # # replace_na(list(harvest_percentage = 0)) %>%
205
+ # # mutate(harvested = round((population * harvest_percentage/100))) %>%
206
+ # group_by(pond_code) %>%
207
+ # mutate(cumulative_harvested = cumsum(harvested)) %>%
208
+ # ungroup() %>%
209
+ # mutate(biomass = mbw * population_left/1000) %>%
210
+ # mutate(biomass_total = biomass + (cumulative_harvested*mbw)/1000) %>%
211
+ # group_by(pond_code) %>%
212
+ # mutate(
213
+ # cumulative_feed_indeks = cumsum(feed_per_day_indeks),
214
+ # cumulative_feed_fr = cumsum(feed_per_day_fr),
215
+ # ) %>%
216
+ # ungroup() %>%
217
+ # mutate(
218
+ # fcr_indeks = cumulative_feed_indeks/biomass_total,
219
+ # fcr_fr = cumulative_feed_fr/biomass_total
220
+ # ) %>%
221
+ # mutate(
222
+ # data_type = "planning"
223
+ # ) -> result
224
+ #
225
+ # # variable setting
226
+ # ## datetime information
227
+ # datetime_info <- lubridate::now(tzone = "UTC")
228
+ #
229
+ # ## eeding_strategy_planning_datatable
230
+ # message("---- Save Feeding Plan for One Cycle ----")
231
+ # result %>%
232
+ # mutate(
233
+ # created_date = datetime_info
234
+ # ) %>%
235
+ # select(
236
+ # doc, date, pond_code, pond_area, mbw, sr_est,
237
+ # population, harvest_percentage, harvested, cumulative_harvested,
238
+ # population_left, biomass, biomass_total, feed_type,
239
+ # feed_per_day_fr, feed_per_day_indeks,
240
+ # cumulative_feed_fr, cumulative_feed_indeks,
241
+ # fcr_fr, fcr_indeks,
242
+ # data_type, created_date
243
+ # ) -> result
244
+
245
+ return("Pass")
246
  }
utils_function.R CHANGED
@@ -1,3 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # survival function v2 ----
2
  ## function ----
3
  survival_function_v2 <- function(
 
1
+ # growth function v2 ----
2
+ growth_function_training <- function(
3
+ data,
4
+ init_size = 0.01,
5
+ max_doc = 120,
6
+ method = "ADG"
7
+ ) {
8
+ # set maximum doc
9
+ doc_vec <- 1:max_doc
10
+ if (method == "Growth Model") {
11
+ message("---- use Growth Model ----")
12
+ model_res <-nls(
13
+ mbw ~ asymptotic_growth*(1-(1-(init_size/asymptotic_growth)^(1/allometric_scale))*exp(-growth_rate*doc))^allometric_scale,
14
+ data = data,
15
+ start = list(
16
+ asymptotic_growth = 40,
17
+ allometric_scale = 3,
18
+ growth_rate = 0.015
19
+ )
20
+ )
21
+
22
+ pred_res <- predict(
23
+ model_res,
24
+ list(
25
+ doc = doc_vec
26
+ )
27
+ )
28
+
29
+ tibble(
30
+ doc = doc_vec,
31
+ mbw = round(pred_res,3)
32
+ ) -> growth_res
33
+ } else if(method == "ABW Interpolation") {
34
+ message("---- use ABW Interpolation ----")
35
+ min_doc <- min(data$doc)
36
+ max_doc <- max(data$doc)
37
+
38
+ growth_res <- data.frame(
39
+ with(data,
40
+ approx(doc, mbw, xout = seq(min_doc, max_doc, by = 1), method = "linear")
41
+ )
42
+ ) %>%
43
+ rename(doc = x, mbw = y)
44
+ } else if(method == "Genetics - Balanced") {
45
+ message("---- use Genetics - Balanced Data ----")
46
+ growth_res <- growth_by_genetic_data %>%
47
+ select(DOC, Balanced) %>%
48
+ rename(
49
+ doc = DOC,
50
+ mbw = Balanced
51
+ ) %>%
52
+ filter(doc <= max_doc)
53
+ } else if(method == "Genetics - Fast Growth") {
54
+ message("---- use Genetics - Fast Growth Data ----")
55
+ growth_res <- growth_by_genetic_data %>%
56
+ select(DOC, Fast.Growth) %>%
57
+ rename(
58
+ mbw = Fast.Growth,
59
+ doc = DOC
60
+ ) %>%
61
+ filter(doc <= max_doc)
62
+ } else {
63
+ message("---- use ADG Interpolation ----")
64
+ initial_weight <- init_size
65
+ final_doc <- max_doc
66
+ adg_data <- data
67
+
68
+ abw_container <- list()
69
+
70
+ for (indeks in 1:nrow(adg_data)) {
71
+ if (indeks == 1) {
72
+ abw_container[[indeks]] <- c(initial_weight, initial_weight + cumsum(rep(adg_data$adg[indeks], adg_data$doc[indeks + 1] - adg_data$doc[indeks] - 1)))
73
+ } else if(indeks == nrow(adg_data)) {
74
+ initial_weight <- max(abw_container[[indeks-1]])
75
+ abw_container[[indeks]] <- c(initial_weight + cumsum(rep(adg_data$adg[indeks], final_doc - adg_data$doc[indeks] + 1)))
76
+ } else {
77
+ initial_weight <- max(abw_container[[indeks-1]])
78
+ abw_container[[indeks]] <- c(initial_weight + cumsum(rep(adg_data$adg[indeks], adg_data$doc[indeks + 1] - adg_data$doc[indeks])))
79
+ }
80
+ }
81
+
82
+ mbw_ <- unlist(abw_container)
83
+
84
+ tibble(
85
+ doc = 1:final_doc,
86
+ mbw = mbw_
87
+ ) -> growth_res
88
+ }
89
+ return(growth_res)
90
+ }
91
+
92
+
93
  # survival function v2 ----
94
  ## function ----
95
  survival_function_v2 <- function(