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5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d0ec8f
 
ea61af3
3d0ec8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee22a2f
ce04399
ee22a2f
3d0ec8f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# plumber.R

#* Echo back the input
#* @get /
function() {
  message("---- hello world triggered ----")
  list(msg = paste0("Hello, World!"))
}


#* Echo back the input
#* @get /iris
function() {
  message("---- call iris data ----")
  list(data = janitor::clean_names(head(iris)))
}

#* Echo back the input
#* @param msg The message to echo
#* @get /echo
function(msg="") {
  message("---- echo ----")
  list(msg = paste0("The message is: '", msg, "'"))
}

#* Return the sum of two numbers
#* @param a The first number to add
#* @param b The second number to add
#* @post /sum
function(a, b) {
  message("---- sum two number ----")
  as.numeric(a) + as.numeric(b)
}

#* Growth Calculation
#* #* @serializer unboxedJSON
#* @post /growth-calculation
function(req) {
  
  # data, 
  # init_size = 0.01, 
  # max_doc = 120, 
  # method = "gm"
  
  message("---- call growth function ----")

  data_from_user <- req$body
  
  data <- data_from_user$data
  init_size <- data_from_user$init_size
  max_doc <- data_from_user$max_doc
  method <- data_from_user$model_method
  
  # set maximum doc
  doc_vec <- 1:max_doc

  if (method == "gm") {
    print("use gm")
    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 == "interp_abw") {
    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 {
    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)
}


#* One Cycle Feeding Generator
#* @serializer unboxedJSON
#* @post /one-cycle-generator
function(req) {
  data_from_user <- req$body

  start_date <-  data_from_user$start_date
  maximum_doc <-  data_from_user$maximum_doc
  pond_setting <-  data_from_user$pond_setting %>% 
    janitor::clean_names()
  partial_harvest_setting <-  data_from_user$partial_harvest_setting %>% 
    janitor::clean_names()
  survival_calculation_method <-  data_from_user$survival_calculation_method
  target_survival <-  data_from_user$target_survival
  survival_model <-  data_from_user$survival_model
  fi_multiplier_setting <-  data_from_user$fi_multiplier_setting
  fr_type_setting <- data_from_user$fr_type_setting
  fr_coef_x <-  data_from_user$fr_coef_x
  fr_coef_y <-  data_from_user$fr_coef_y
  blind_feeding_start_feeding_setting <-  data_from_user$blind_feeding_start_feeding_setting
  blind_feeding_increment_setting <-  data_from_user$blind_feeding_increment_setting %>% 
    janitor::clean_names()
  growth_calculation_method <- data_from_user$growth_calculation_method
  initial_abw <-  data_from_user$initial_abw
  initial_growth_data <-  data_from_user$initial_growth_data %>% 
    janitor::clean_names()
  
  message("---- generate growth scenario table ----")
  growth_scenario_table <- growth_function_training(
    data = initial_growth_data,
    init_size = initial_abw,
    max_doc = maximum_doc,
    method = growth_calculation_method
  )
  
  message("---- generate feeding for one cycle ----")
  result <- all_feeding_generator_function_v2(
    date_data = as.Date(start_date),
    max_doc = maximum_doc,
    shrimp_stock_data = pond_setting,
    harvest_setting = partial_harvest_setting,
    survival_calculation_method = survival_calculation_method,
    target_survival = target_survival,
    survival_model = survival_model,
    blind_feeding_day_1_setting = blind_feeding_start_feeding_setting,
    blind_feeding_day_2_setting = blind_feeding_increment_setting,
    fi_multiplier = fi_multiplier_setting,
    fr_calculation_method = fr_type_setting,
    fr_coef_x = fr_coef_x,
    fr_coef_y = fr_coef_y,
    growth_scenario_table = growth_scenario_table
  )
  
  message("---- check result ----")

  ### blind feed table
  result$blind_feed_table -> blind_feed_table

  ### demand feeding table
  result$demand_feeding_table -> demand_feeding_table

  ### survival table
  result$survival_table -> survival_table

  ### stocking data
  pond_setting -> stocking_data

  ### partial harvest setting
  partial_harvest_setting

  ### feeding plan generation
  message("---- blind feeding generation ----")
  blind_feed_table %>%
    pivot_wider(names_from = type, values_from = total_feed) %>%
    mutate(
      feed_per_day_fr = blind_feeding,
      feed_per_day_indeks = blind_feeding
    ) %>%
    select(-blind_feeding) -> blind_feed_table

  message("---- demand feeding generation ----")
  blind_feed_table %>%
    bind_rows(
      demand_feeding_table %>%
        select(doc, pond_code, feed_per_day_indeks, feed_per_day_fr)
    ) %>%
    mutate(feed_type = ifelse(doc <= 30, "blind_feeding", "demand_feeding")) %>%
    left_join(growth_scenario_table) %>%
    left_join(survival_table) %>%
    left_join(stocking_data) %>%
    arrange(pond_code, doc) %>%
    group_by(pond_code) %>%
    mutate(cumulative_harvested = cumsum(harvested)) %>%
    ungroup() %>%
    mutate(biomass = mbw * population_left/1000) %>%
    mutate(biomass_total = biomass + (cumulative_harvested*mbw)/1000) %>%
    group_by(pond_code) %>%
    mutate(
      cumulative_feed_indeks = cumsum(feed_per_day_indeks),
      cumulative_feed_fr = cumsum(feed_per_day_fr),
    ) %>%
    ungroup() %>%
    mutate(
      fcr_indeks = cumulative_feed_indeks/biomass_total,
      fcr_fr = cumulative_feed_fr/biomass_total
    ) %>%
    mutate(
      data_type = "planning"
    ) -> result

  # variable setting
  ## datetime information
  datetime_info <- lubridate::now(tzone = "UTC")
  
  tibble(
    pond_code = as.character(),
    doc = as.numeric(),
    feed_index = as.numeric()
  ) -> feed_indeks_table
  
  for (pond_code in pond_setting$pond_code) {
    
    feed_indeks_table <- feed_indeks_table %>% 
      bind_rows(
        fi_generator(
          total_feed_day_1 = blind_feeding_start_feeding_setting,
          blind_feeding_rules_table = blind_feeding_increment_setting
        ) %>% 
          mutate(
            pond_code = pond_code
          )
      )
  }
  
  feed_indeks_table <- feed_indeks_table %>% 
    bind_rows(
      demand_feeding_table %>% 
        select(pond_code, doc, indeks_plan) %>% 
        rename(feed_index = indeks_plan)
    ) %>% 
    arrange(pond_code, doc)
  
  ## feeding_strategy_planning_datatable
  message("---- Save Feeding Plan for One Cycle ----")
  result %>%
    mutate(
      created_date = datetime_info
    ) %>%
    group_by(pond_code) %>% 
    mutate(adg = mbw-lag(mbw)) %>% 
    ungroup() -> result
  
  if (fr_type_setting == "FR Type 1") {
    result <- result %>% 
      mutate(
        fr_plan = 10^(fr_coef_x-(fr_coef_y*log10(mbw)))
      )
  } else if(fr_calculation_method == "FR Type 2") {
    result <- result %>% 
      mutate(
        fr_plan = 10^(fr_coef_x-(fr_coef_y*log10(mbw))) * 100
      )
  } else {
    result <- result %>% 
      mutate(
        fr_plan = 10^(fr_coef_x-(fr_coef_y*log10(mbw)))
      )
  }
  
  result <- result %>% 
    group_by(pond_code) %>% 
    mutate(adg = round(mbw-lag(mbw), 2)) %>% 
    ungroup() %>%
    mutate(
      carrying_capacity = biomass/pond_area,
      partial_biomass = population_left * harvest_percentage * mbw
    ) %>% 
    left_join(
      feed_indeks_table
    ) %>% 
    select(
      pond_code, doc, date, mbw, adg, fr_plan, sr_est,
      harvest_percentage, biomass, carrying_capacity, partial_biomass,
      biomass_total, population_left, feed_index, feed_per_day_fr, feed_per_day_indeks,
      cumulative_feed_fr, cumulative_feed_indeks, fcr_fr, fcr_indeks
    )

  return(result)
}

#* One Cycle Feeding Generator
#* @serializer unboxedJSON
#* @post /one-week-generator
function(req) {
  
  data_from_user <- req$body
  
  # Weekly pond setting ----
  result_data <- data_from_user$data %>% 
    janitor::clean_names()
  
  # date input ----
  weekly_feeding_date_input <- as.Date(data_from_user$date_input)
  
  # sr setting ----
  sr_method <- data_from_user$sr_method
  sr_model <- data_from_user$sr_model
  
  # fr setting ----
  fr_method <- data_from_user$fr_method
  fr_x_coef <- data_from_user$fr_x_coef
  fr_y_coef <- data_from_user$fr_y_coef
  
  # growth method ----
  growth_method <- data_from_user$growth_method
  
  feed_data_container <- tibble(
    pond_code = as.character(),
    doc_data = as.numeric(),
    mbw_data = as.numeric(),
    sr_data = as.numeric(),
    fi_data = as.numeric(),
    fr_data = as.numeric(),
    feed_fi = as.numeric(),
    feed_fr = as.numeric(),
    biomass_data = as.numeric(),
    population_data = as.numeric(),
    date_data = as.Date(character())
  )
  
  result_data %>% 
    select(pond_code, stocking_actual, fi_multiplier) -> ponds_setting
  
  for (i in 1:nrow(result_data)) {
    pond_data <- result_data[i,]
    
    stock_data <- ponds_setting %>%
      filter(pond_code == pond_data$pond) %>% 
      pull(stocking_actual)
    
    fi_mult <- ponds_setting %>%
      filter(pond_code == pond_data$pond) %>% 
      pull(fi_multiplier)
    
    doc_data <- cumsum(c(pond_data$doc, rep(1, 7)))[2:8]
    mbw_data <- cumsum(c(pond_data$mbw_sampling, rep(pond_data$adg_target, 7)))[2:8]
    fi_data <- rep(pond_data$adg_target* fi_mult, 7)
    
    if (fr_method == "FR Tipe 1") {
      fr_data <- 10^(fr_x_coef-(fr_y_coef*log10(mbw_data))) * 100
    } else if(fr_method == "FR Tipe 2") {
      fr_data <- 10^(fr_x_coef-(fr_y_coef*log10(mbw_data)))
    } else {
      fr_data <- 10^(fr_x_coef-(fr_y_coef*log10(mbw_data))) * 100
    }
    
    if (sr_method == "Nilai SR Konstan") {
      message("---- Use Constant SR Model ----")
      sr_est <- pond_data$sr_input
    } else if(sr_method == "Berdasarkan Target") {
      message("---- Use Target Based SR ----")
      survival_input <- pond_data$sr_input
      survival_target <- pond_data$sr_target
      sr_est <- seq(from = survival_input, to = survival_target, length.out = 7)
    } else {
      if(model == "Model Survival Logistik") {
        message("---- Use Survival Logistic Model ----")
        sr_est <- (100 - 2.86*log2(doc_data))/100
      }
    }
    
    feed_fi <- (stock_data * (sr_est) * doc_data * fi_data)/100000
    feed_fr <- (stock_data * (sr_est) * mbw_data * fr_data)/1000
    
    population_data <- stock_data * (sr_est)
    biomass <- population_data * mbw_data
    
    date_data <- seq(weekly_feeding_date_input, by = "day", length.out = 7)
    
    tibble(
      pond_code = pond_data$pond,
      doc_data = doc_data,
      mbw_data = mbw_data,
      sr_data = sr_est,
      fi_data = fi_data,
      fr_data = fr_data,
      feed_fi = feed_fi,
      feed_fr = feed_fr,
      fi_fr_ratio = feed_fi/feed_fr,
      biomass_data = biomass,
      population_data = population_data,
      date_data = date_data
    ) %>% 
      bind_rows(feed_data_container) -> feed_data_container
  }
  
  feed_data_container <- feed_data_container %>% 
    arrange(date_data, pond_code)
  
  return(feed_data_container)
}