-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_backup.R
More file actions
1515 lines (1361 loc) · 53.4 KB
/
app_backup.R
File metadata and controls
1515 lines (1361 loc) · 53.4 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##############################################################################
# Supply Chain Optimization Dashboard - Shiny MVP
# Grocery retailer: supplier → warehouse → shops
# Optimizes import & delivery schedule over 30-day horizon
##############################################################################
# ---- Load Libraries --------------------------------------------------------
library(shiny)
library(leaflet)
library(data.table)
library(DT)
library(ggplot2)
library(lpSolve)
# ---- Constants -------------------------------------------------------------
PLANNING_HORIZON <- 30
DEFAULT_SERVICE_LEVEL <- 0.99
DEFAULT_ERROR_PCT <- 20
# Weekly seasonality factors (Mon-Sun)
WEEKLY_SEASONALITY <- c(1.1, 1.0, 1.0, 1.05, 1.15, 1.3, 0.8)
# Monthly seasonality factors (Jan-Dec)
MONTHLY_SEASONALITY <- c(0.90, 0.85, 0.95, 1.00, 1.05, 1.10,
1.10, 1.05, 1.00, 0.95, 0.90, 1.00)
# Default warehouse location (Amsterdam area)
DEFAULT_WH_LAT <- 52.3676
DEFAULT_WH_LNG <- 4.9041
# Default store locations (Dutch cities)
DEFAULT_STORES <- data.table(
store_id = 1:3,
store_name = c("Store Rotterdam", "Store Utrecht", "Store Den Haag"),
lat = c(51.9225, 52.0907, 52.0705),
lng = c(4.4792, 5.1214, 4.3007),
multiplier = c(1.0, 0.8, 1.2)
)
# ---- Helper Functions ------------------------------------------------------
read_product_csv <- function(file_path) {
# Read file as text to detect delimiter
lines <- readLines(file_path, n = 2, warn = FALSE)
header <- lines[1]
# Detect delimiter: if semicolons present in header, use ;
if (grepl(";", header)) {
dt <- fread(file_path, sep = ";", dec = ",", header = TRUE,
encoding = "UTF-8")
} else {
# Comma delimiter with potential quoted decimal values
dt <- fread(file_path, sep = ",", header = TRUE, encoding = "UTF-8")
# Fix decimal commas in quoted fields
num_cols <- c("price", "inkoop", "weight_(g)", "weight (gr)",
"volume_(l)", "volume (l)")
for (col in intersect(num_cols, names(dt))) {
if (is.character(dt[[col]])) {
dt[[col]] <- as.numeric(gsub(",", ".", dt[[col]]))
}
}
}
# Normalize column names
old_names <- names(dt)
new_names <- old_names
new_names <- gsub("weight_\\(g\\)|weight \\(gr\\)", "weight_g", new_names)
new_names <- gsub("volume_\\(l\\)|volume \\(l\\)", "volume_l", new_names)
setnames(dt, old_names, new_names)
# Add missing columns with defaults
if (!"import_order_size" %in% names(dt))
dt[, import_order_size := 50]
if (!"import_delivery_time" %in% names(dt))
dt[, import_delivery_time := 2]
if (!"shelf_life" %in% names(dt))
dt[, shelf_life := 7]
if (!"gekoeld" %in% names(dt))
dt[, gekoeld := 0]
if (!"bevroren" %in% names(dt))
dt[, bevroren := 0]
if (!"weight_g" %in% names(dt))
dt[, weight_g := 0.1]
if (!"volume_l" %in% names(dt))
dt[, volume_l := 0.1]
if (!"price" %in% names(dt))
dt[, price := 1.0]
if (!"inkoop" %in% names(dt))
dt[, inkoop := 0.5]
# Ensure numeric types
num_fields <- c("price", "inkoop", "weight_g", "volume_l",
"shelf_life", "gekoeld", "bevroren",
"import_order_size", "import_delivery_time")
for (col in intersect(num_fields, names(dt))) {
if (is.character(dt[[col]])) {
dt[[col]] <- as.numeric(gsub(",", ".", dt[[col]]))
}
}
# Add product_idx
dt[, product_idx := .I]
# Add default weekly demand if not present
if (!"weekly_demand" %in% names(dt))
dt[, weekly_demand := 100]
dt
}
haversine_km <- function(lat1, lng1, lat2, lng2) {
R <- 6371
dlat <- (lat2 - lat1) * pi / 180
dlng <- (lng2 - lng1) * pi / 180
a <- sin(dlat / 2)^2 +
cos(lat1 * pi / 180) * cos(lat2 * pi / 180) * sin(dlng / 2)^2
2 * R * asin(sqrt(a))
}
generate_demand <- function(products, stores, horizon = PLANNING_HORIZON,
error_pct = DEFAULT_ERROR_PCT,
start_date = Sys.Date(),
demand_overrides = NULL) {
dates <- start_date + 0:(horizon - 1)
wday_idx <- as.integer(format(dates, "%u")) # 1=Mon, 7=Sun
month_idx <- as.integer(format(dates, "%m"))
demand_list <- list()
for (s in seq_len(nrow(stores))) {
for (p in seq_len(nrow(products))) {
# Check for per-store override
weekly_dem <- NULL
if (!is.null(demand_overrides) &&
nrow(demand_overrides) >= p &&
stores$store_name[s] %in% names(demand_overrides)) {
weekly_dem <- demand_overrides[[stores$store_name[s]]][p]
}
if (is.null(weekly_dem) || is.na(weekly_dem)) {
weekly_dem <- products$weekly_demand[p] * stores$multiplier[s]
}
daily_base <- weekly_dem / 7
daily_demand <- numeric(horizon)
for (d in seq_len(horizon)) {
seasonal <- WEEKLY_SEASONALITY[wday_idx[d]] *
MONTHLY_SEASONALITY[month_idx[d]]
err <- runif(1, 1 - error_pct / 100, 1 + error_pct / 100)
daily_demand[d] <- max(0, round(daily_base * seasonal * err, 1))
}
demand_list[[length(demand_list) + 1]] <- data.table(
store_id = stores$store_id[s],
product_idx = p,
day = 1:horizon,
date = dates,
demand = daily_demand
)
}
}
rbindlist(demand_list)
}
# ---- Optimization (LP per product with lpSolve) ---------------------------
run_optimization <- function(products, stores, demand_dt, vehicles,
distances, wh_capacity, store_capacity,
service_level) {
D <- PLANNING_HORIZON
S <- nrow(stores)
P <- nrow(products)
# Transport cost per unit weight to each store
# Use cheapest vehicle, approximate cost per kg
best_cost_per_km <- min(vehicles$cost_per_km)
best_fixed_cost <- min(vehicles$fixed_cost_per_trip)
best_max_weight <- max(vehicles$max_weight)
cost_per_kg_to_store <- numeric(S)
for (s in seq_len(S)) {
trip_cost <- best_fixed_cost + 2 * distances[s] * best_cost_per_km
cost_per_kg_to_store[s] <- trip_cost / best_max_weight
}
# Results containers
all_import <- list()
all_delivery <- list()
all_wh_inv <- list()
all_st_inv <- list()
all_stockout <- list()
for (p in seq_len(P)) {
product <- products[p, ]
margin <- product$price - product$inkoop
weight <- product$weight_g / 1000 # kg per unit
lead_time <- product$import_delivery_time
# Get demand for this product across stores
dem_p <- demand_dt[product_idx == p]
# Build demand matrix [S x D]
dem_matrix <- matrix(0, nrow = S, ncol = D)
for (s in seq_len(S)) {
dd <- dem_p[store_id == stores$store_id[s]]
if (nrow(dd) > 0) {
dem_matrix[s, ] <- dd$demand[order(dd$day)]
}
}
# --- Variables layout for LP ---
# import_qty[d] : indices 1..D
# delivery_qty[s, d] : indices D + (s-1)*D + d → D+1 .. D+S*D
# stockout[s, d] : indices D+S*D + (s-1)*D + d → D+S*D+1 .. D+2*S*D
n_vars <- D + 2 * S * D
idx_import <- function(d) d
idx_delivery <- function(s, d) D + (s - 1) * D + d
idx_stockout <- function(s, d) D + S * D + (s - 1) * D + d
# --- Objective ---
obj <- rep(0, n_vars)
for (s in seq_len(S)) {
for (d in seq_len(D)) {
# Transport cost per unit delivered (approximated)
obj[idx_delivery(s, d)] <- cost_per_kg_to_store[s] * weight
# Lost margin for stockout
obj[idx_stockout(s, d)] <- margin
}
}
# --- Constraints ---
con_mat <- list()
con_dir <- character(0)
con_rhs <- numeric(0)
# Initial inventories
total_daily_demand <- sum(dem_matrix) / D
wh_inv_0 <- min(
round(total_daily_demand * 5),
wh_capacity$max_weight / max(weight, 0.001) / P
) * 0.8
store_inv_0 <- numeric(S)
for (s in seq_len(S)) {
avg_demand_s <- sum(dem_matrix[s, ]) / D
store_inv_0[s] <- min(
round(avg_demand_s * 3),
store_capacity$max_weight / max(weight, 0.001) / P
)
}
# 1) Warehouse inventory >= 0 for each day
for (d in seq_len(D)) {
row <- rep(0, n_vars)
# wh_inv[d] = wh_inv_0 + sum(import[1..d]) - sum_s(sum(delivery[s,1..d]))
for (dd in 1:d) {
ld <- dd - lead_time
if (ld >= 1) {
row[idx_import(ld)] <- 1
}
}
for (s in seq_len(S)) {
for (dd in 1:d) {
row[idx_delivery(s, dd)] <- row[idx_delivery(s, dd)] - 1
}
}
con_mat[[length(con_mat) + 1]] <- row
con_dir <- c(con_dir, ">=")
con_rhs <- c(con_rhs, -wh_inv_0)
}
# 2) Store inventory >= 0 for each store, each day
for (s in seq_len(S)) {
cum_demand <- cumsum(dem_matrix[s, ])
for (d in seq_len(D)) {
row <- rep(0, n_vars)
for (dd in 1:d) {
row[idx_delivery(s, dd)] <- 1
row[idx_stockout(s, dd)] <- 1
}
con_mat[[length(con_mat) + 1]] <- row
con_dir <- c(con_dir, ">=")
con_rhs <- c(con_rhs, cum_demand[d] - store_inv_0[s])
}
}
# 3) Warehouse capacity constraint for each day
wh_cap_units <- wh_capacity$max_weight / max(weight, 0.001) / P
for (d in seq_len(D)) {
row <- rep(0, n_vars)
for (dd in 1:d) {
ld <- dd - lead_time
if (ld >= 1) {
row[idx_import(ld)] <- 1
}
}
for (s in seq_len(S)) {
for (dd in 1:d) {
row[idx_delivery(s, dd)] <- row[idx_delivery(s, dd)] - 1
}
}
con_mat[[length(con_mat) + 1]] <- row
con_dir <- c(con_dir, "<=")
con_rhs <- c(con_rhs, wh_cap_units - wh_inv_0)
}
# 4) Store capacity constraint
store_cap_units <- store_capacity$max_weight / max(weight, 0.001) / P
for (s in seq_len(S)) {
cum_demand <- cumsum(dem_matrix[s, ])
for (d in seq_len(D)) {
row <- rep(0, n_vars)
for (dd in 1:d) {
row[idx_delivery(s, dd)] <- 1
row[idx_stockout(s, dd)] <- 1
}
con_mat[[length(con_mat) + 1]] <- row
con_dir <- c(con_dir, "<=")
con_rhs <- c(con_rhs, store_cap_units - store_inv_0[s] + cum_demand[d])
}
}
# 5) Service level for this product:
# sum(margin * stockout) <= (1 - service_level) * total expected margin
total_expected_margin <- margin * sum(dem_matrix)
row <- rep(0, n_vars)
for (s in seq_len(S)) {
for (d in seq_len(D)) {
row[idx_stockout(s, d)] <- margin
}
}
con_mat[[length(con_mat) + 1]] <- row
con_dir <- c(con_dir, "<=")
con_rhs <- c(con_rhs, (1 - service_level) * total_expected_margin)
# 6) Import quantity must be non-negative multiple of order size
# (relax to just non-negative for LP)
# Solve LP
A <- do.call(rbind, con_mat)
sol <- tryCatch({
lp("min", obj, A, con_dir, con_rhs)
}, error = function(e) {
list(status = 1, solution = rep(0, n_vars))
})
if (sol$status != 0) {
# Infeasible - use fallback: deliver exactly demand, no stockout control
sol_vec <- rep(0, n_vars)
for (s in seq_len(S)) {
for (d in seq_len(D)) {
sol_vec[idx_delivery(s, d)] <- dem_matrix[s, d]
}
}
# Back-calculate imports needed
for (d in seq_len(D)) {
total_del <- sum(sapply(seq_len(S), function(s) sol_vec[idx_delivery(s, d)]))
import_day <- min(d + lead_time, D)
if (import_day <= D) {
sol_vec[idx_import(d)] <- total_del
}
}
} else {
sol_vec <- sol$solution
}
# Extract results
import_qty <- sapply(seq_len(D), function(d) max(0, round(sol_vec[idx_import(d)], 1)))
# Round import to order size multiples
ord_size <- product$import_order_size
if (!is.na(ord_size) && ord_size > 0) {
import_qty <- ceiling(import_qty / ord_size) * ord_size
import_qty[import_qty < ord_size & import_qty > 0] <- ord_size
}
for (d in seq_len(D)) {
if (import_qty[d] > 0) {
all_import[[length(all_import) + 1]] <- data.table(
product_idx = p,
product_name = product$name,
day = d,
import_qty = import_qty[d]
)
}
}
# Simulate forward with rounded values to get actual inventories
wh_inv <- numeric(D)
pending_imports <- rep(0, D + lead_time + 1)
for (d in seq_len(D)) {
pending_imports[d + lead_time] <- pending_imports[d + lead_time] + import_qty[d]
}
curr_wh <- wh_inv_0
for (d in seq_len(D)) {
curr_wh <- curr_wh + if (d <= length(pending_imports)) pending_imports[d] else 0
total_del_today <- 0
for (s in seq_len(S)) {
del <- max(0, round(sol_vec[idx_delivery(s, d)], 1))
so <- max(0, round(sol_vec[idx_stockout(s, d)], 1))
# Limit delivery to warehouse availability
del <- min(del, curr_wh)
curr_wh <- curr_wh - del
# Store inventory update
if (d == 1) {
prev_si <- store_inv_0[s]
} else {
prev_row <- all_st_inv[sapply(all_st_inv, function(x)
x$product_idx[1] == p & x$store_id[1] == stores$store_id[s] &
x$day[1] == (d - 1))]
if (length(prev_row) > 0) {
prev_si <- prev_row[[1]]$store_inventory
} else {
prev_si <- store_inv_0[s]
}
}
actual_inv <- prev_si + del - dem_matrix[s, d]
actual_so <- max(0, -actual_inv)
actual_inv <- max(0, actual_inv)
all_delivery[[length(all_delivery) + 1]] <- data.table(
store_id = stores$store_id[s],
product_idx = p,
product_name = product$name,
day = d,
delivery_qty = del
)
all_st_inv[[length(all_st_inv) + 1]] <- data.table(
store_id = stores$store_id[s],
product_idx = p,
product_name = product$name,
day = d,
store_inventory = actual_inv,
stockout = actual_so
)
if (actual_so > 0) {
all_stockout[[length(all_stockout) + 1]] <- data.table(
store_id = stores$store_id[s],
product_idx = p,
product_name = product$name,
day = d,
stockout_qty = actual_so,
lost_margin = actual_so * margin
)
}
total_del_today <- total_del_today + del
}
wh_inv[d] <- curr_wh
}
for (d in seq_len(D)) {
all_wh_inv[[length(all_wh_inv) + 1]] <- data.table(
product_idx = p,
product_name = product$name,
day = d,
warehouse_inventory = wh_inv[d]
)
}
}
# Combine results
import_schedule <- if (length(all_import) > 0) rbindlist(all_import) else
data.table(product_idx = integer(), product_name = character(),
day = integer(), import_qty = numeric())
delivery_schedule <- if (length(all_delivery) > 0) rbindlist(all_delivery) else
data.table(store_id = integer(), product_idx = integer(),
product_name = character(), day = integer(),
delivery_qty = numeric())
wh_inventory <- if (length(all_wh_inv) > 0) rbindlist(all_wh_inv) else
data.table(product_idx = integer(), product_name = character(),
day = integer(), warehouse_inventory = numeric())
st_inventory <- if (length(all_st_inv) > 0) rbindlist(all_st_inv) else
data.table(store_id = integer(), product_idx = integer(),
product_name = character(), day = integer(),
store_inventory = numeric(), stockout = numeric())
stockouts <- if (length(all_stockout) > 0) rbindlist(all_stockout) else
data.table(store_id = integer(), product_idx = integer(),
product_name = character(), day = integer(),
stockout_qty = numeric(), lost_margin = numeric())
# Calculate truck trips
truck_trips <- calculate_truck_trips(delivery_schedule, products, stores,
vehicles, distances)
# Cost analysis
transport_cost <- calculate_transport_cost(truck_trips, vehicles, distances)
total_lost_margin <- if (nrow(stockouts) > 0) sum(stockouts$lost_margin) else 0
total_demand_margin <- sum(demand_dt$demand *
products$price[demand_dt$product_idx] -
demand_dt$demand *
products$inkoop[demand_dt$product_idx])
achieved_sl <- if (total_demand_margin > 0) {
1 - total_lost_margin / total_demand_margin
} else 1.0
list(
import_schedule = import_schedule,
delivery_schedule = delivery_schedule,
wh_inventory = wh_inventory,
store_inventory = st_inventory,
stockouts = stockouts,
truck_trips = truck_trips,
transport_cost = transport_cost,
total_lost_margin = total_lost_margin,
total_cost = transport_cost + total_lost_margin,
service_level = achieved_sl,
demand = demand_dt
)
}
calculate_truck_trips <- function(delivery_schedule, products, stores,
vehicles, distances) {
if (nrow(delivery_schedule) == 0 || sum(delivery_schedule$delivery_qty) == 0) {
return(data.table(day = integer(), store_id = integer(),
vehicle_type = character(), trips = integer(),
weight_kg = numeric(), volume_l = numeric()))
}
# Merge product weights/volumes
del <- copy(delivery_schedule)
del <- merge(del, products[, .(product_idx, weight_g, volume_l)],
by = "product_idx", all.x = TRUE)
del[, weight_kg := delivery_qty * weight_g / 1000]
del[, total_vol := delivery_qty * volume_l]
# Aggregate per store per day
daily_store <- del[, .(total_weight = sum(weight_kg, na.rm = TRUE),
total_volume = sum(total_vol, na.rm = TRUE)),
by = .(day, store_id)]
daily_store <- daily_store[total_weight > 0 | total_volume > 0]
if (nrow(daily_store) == 0) {
return(data.table(day = integer(), store_id = integer(),
vehicle_type = character(), trips = integer(),
weight_kg = numeric(), volume_l = numeric()))
}
trips_list <- list()
for (i in seq_len(nrow(daily_store))) {
row <- daily_store[i]
s_idx <- which(stores$store_id == row$store_id)
if (length(s_idx) == 0) next
dist <- distances[s_idx]
remaining_weight <- row$total_weight
remaining_volume <- row$total_volume
# Sort vehicles: prefer truck (larger capacity)
v_order <- order(-vehicles$max_weight)
for (v in v_order) {
veh <- vehicles[v, ]
max_trips <- floor(veh$max_distance_per_day / (2 * max(dist, 1)))
max_trips <- max(max_trips, 0)
while (remaining_weight > 0 && remaining_volume > 0 && max_trips > 0) {
trips_list[[length(trips_list) + 1]] <- data.table(
day = row$day,
store_id = row$store_id,
vehicle_type = veh$type,
trips = 1L,
weight_kg = min(remaining_weight, veh$max_weight),
volume_l = min(remaining_volume, veh$max_volume)
)
remaining_weight <- remaining_weight - veh$max_weight
remaining_volume <- remaining_volume - veh$max_volume
max_trips <- max_trips - 1
}
if (remaining_weight <= 0 && remaining_volume <= 0) break
}
}
if (length(trips_list) > 0) rbindlist(trips_list) else
data.table(day = integer(), store_id = integer(),
vehicle_type = character(), trips = integer(),
weight_kg = numeric(), volume_l = numeric())
}
calculate_transport_cost <- function(truck_trips, vehicles, distances) {
if (nrow(truck_trips) == 0) return(0)
total <- 0
for (i in seq_len(nrow(truck_trips))) {
tt <- truck_trips[i]
v_idx <- which(vehicles$type == tt$vehicle_type)[1]
if (is.na(v_idx)) next
veh <- vehicles[v_idx, ]
# Find distance for this store
dist <- 50 # fallback
s_idx <- which(truck_trips$store_id == tt$store_id)[1]
if (!is.na(s_idx) && s_idx <= length(distances)) {
dist <- distances[min(s_idx, length(distances))]
}
total <- total + tt$trips * (veh$fixed_cost_per_trip + 2 * dist * veh$cost_per_km)
}
total
}
# Simple value box since we're not using shinydashboard
valueBoxUI <- function(id) {
column(3,
div(style = "background:#f8f9fa; border:1px solid #dee2e6;
border-radius:8px; padding:15px; text-align:center;
margin-bottom:10px;",
h5(textOutput(paste0(id, "_title")),
style = "margin:0; color:#6c757d;"),
h3(textOutput(paste0(id, "_value")),
style = "margin:5px 0; color:#212529;")
))
}
# ============================================================================
# UI
# ============================================================================
ui <- fluidPage(
titlePanel("Supply Chain Optimization Dashboard"),
sidebarLayout(
# ---- Sidebar ----------------------------------------------------------
sidebarPanel(
width = 3,
h4("Data Input"),
fileInput("csv_upload", "Upload Product CSV",
accept = c(".csv", ".txt")),
hr(),
h4("Stores"),
numericInput("n_stores", "Number of Stores", value = 3, min = 1, max = 10),
actionButton("reset_stores", "Reset Store Locations"),
hr(),
h4("Service Level"),
sliderInput("service_level", "OTIN Service Level (%)",
min = 80, max = 100, value = 99, step = 0.5),
hr(),
h4("Demand Settings"),
numericInput("error_pct", "Demand Error (%)", value = 20,
min = 0, max = 50),
hr(),
h4("Warehouse Capacity"),
numericInput("wh_max_weight", "Max Weight (kg)", value = 50000),
numericInput("wh_max_volume", "Max Volume (L)", value = 100000),
numericInput("wh_max_refrig_weight", "Max Refrigerated Weight (kg)",
value = 20000),
numericInput("wh_max_frozen_weight", "Max Frozen Weight (kg)",
value = 10000),
hr(),
h4("Store Capacity (per store)"),
numericInput("st_max_weight", "Max Weight (kg)", value = 5000),
numericInput("st_max_volume", "Max Volume (L)", value = 10000),
numericInput("st_max_refrig_weight", "Max Refrigerated Weight (kg)",
value = 2000),
numericInput("st_max_frozen_weight", "Max Frozen Weight (kg)",
value = 1000),
hr(),
actionButton("run_optim", "Run Optimization",
class = "btn-primary btn-lg btn-block",
style = "width:100%; margin-top:10px;"),
hr(),
textOutput("optim_status")
),
# ---- Main Panel -------------------------------------------------------
mainPanel(
width = 9,
tabsetPanel(
id = "main_tabs",
# Tab 1: Map -------------------------------------------------------
tabPanel("Map",
h3("Logistics Locations"),
p("Drag markers to reposition the warehouse and stores."),
leafletOutput("map", height = "600px"),
hr(),
h4("Store Settings"),
DTOutput("store_table")
),
# Tab 2: Products --------------------------------------------------
tabPanel("Products",
h3("Product Catalog"),
fluidRow(
column(6, actionButton("add_product", "Add Product")),
column(6, actionButton("remove_product",
"Remove Selected"))
),
br(),
DTOutput("product_table"),
hr(),
h4("Edit Product"),
uiOutput("product_edit_ui")
),
# Tab 3: Demand ----------------------------------------------------
tabPanel("Demand",
h3("Demand Forecast"),
fluidRow(
column(4, selectInput("demand_product", "Product",
choices = NULL)),
column(4, selectInput("demand_store", "Store",
choices = NULL))
),
plotOutput("demand_chart", height = "350px"),
hr(),
h4("Per-Store Product Demand (weekly units)"),
p("Edit cells to set custom weekly demand per product per store. Values override the base weekly demand multiplied by the store multiplier."),
DTOutput("demand_override_table"),
hr(),
h4("Seasonality Patterns"),
fluidRow(
column(6, plotOutput("weekly_seasonality_chart",
height = "250px")),
column(6, plotOutput("monthly_seasonality_chart",
height = "250px"))
)
),
# Tab 3b: Vehicle Fleet -----------------------------------------------
tabPanel("Vehicle Fleet",
h3("Vehicle Fleet Configuration"),
p("Edit vehicle parameters directly in the table below."),
DTOutput("vehicle_fleet_table")
),
# Tab 4: Optimization Results --------------------------------------
tabPanel("Optimization Results",
h3("Optimization Results"),
conditionalPanel(
condition = "output.has_results",
fluidRow(
valueBoxUI("vb_cost"),
valueBoxUI("vb_transport"),
valueBoxUI("vb_lost_margin"),
valueBoxUI("vb_service")
),
hr(),
tabsetPanel(
tabPanel("Import Schedule",
DTOutput("import_table")),
tabPanel("Warehouse Inventory",
plotOutput("wh_inv_chart", height = "400px"),
DTOutput("wh_inv_table")),
tabPanel("Store Inventory",
fluidRow(
column(4, selectInput("inv_store",
"Store", choices = NULL)),
column(4, selectInput("inv_product",
"Product", choices = NULL))
),
plotOutput("st_inv_chart", height = "400px")),
tabPanel("Delivery Schedule",
DTOutput("delivery_table")),
tabPanel("Vehicle Usage",
plotOutput("vehicle_chart", height = "400px"),
DTOutput("vehicle_table"))
)
),
conditionalPanel(
condition = "!output.has_results",
h4("Run the optimization to see results.",
style = "color:gray; text-align:center; margin-top:50px;")
)
),
# Tab 5: Cost Analysis ----------------------------------------------
tabPanel("Cost Analysis",
h3("Cost Analysis"),
conditionalPanel(
condition = "output.has_results",
fluidRow(
column(6, plotOutput("cost_breakdown_chart",
height = "350px")),
column(6, plotOutput("cost_daily_chart",
height = "350px"))
),
hr(),
plotOutput("service_level_chart", height = "300px")
),
conditionalPanel(
condition = "!output.has_results",
h4("Run the optimization to see cost analysis.",
style = "color:gray; text-align:center; margin-top:50px;")
)
)
)
)
)
)
# ============================================================================
# SERVER
# ============================================================================
server <- function(input, output, session) {
# ---- Reactive Values ---------------------------------------------------
rv <- reactiveValues(
products = NULL,
stores = copy(DEFAULT_STORES),
wh_lat = DEFAULT_WH_LAT,
wh_lng = DEFAULT_WH_LNG,
map_clicks = 0,
results = NULL,
demand_cache = NULL,
demand_overrides = NULL,
vehicles = {
vdt <- data.table(
type = c("Truck", "Van"),
count = c(1L, 1L),
cost_per_km = c(1.20, 0.60),
speed = c(60, 80),
fixed_cost_per_trip = c(50, 25),
max_weight = c(8000, 1500),
max_volume = c(40000, 8000),
refrigerated_capable = c(TRUE, TRUE),
frozen_capable = c(TRUE, FALSE)
)
copy(vdt)
}
)
# ---- Vehicle Data (reactive) -------------------------------------------
vehicles_dt <- reactive({
vdt <- copy(rv$vehicles)
vdt[, max_distance_per_day := speed * 16]
vdt
})
# ---- Load Default Products ---------------------------------------------
observe({
default_path <- file.path(getwd(), "store_products.csv")
if (file.exists(default_path) && is.null(rv$products)) {
rv$products <- read_product_csv(default_path)
}
})
# ---- CSV Upload --------------------------------------------------------
observeEvent(input$csv_upload, {
req(input$csv_upload)
rv$products <- read_product_csv(input$csv_upload$datapath)
rv$results <- NULL
rv$demand_overrides <- NULL # Reset demand overrides on new CSV
showNotification(paste("Loaded", nrow(rv$products), "products"),
type = "message")
})
# ---- Update Stores on n_stores Change ----------------------------------
observeEvent(input$n_stores, {
n <- input$n_stores
current <- rv$stores
if (n > nrow(current)) {
# Add new stores with random positions near Amsterdam
for (i in (nrow(current) + 1):n) {
new_store <- data.table(
store_id = i,
store_name = paste("Store", i),
lat = DEFAULT_WH_LAT + runif(1, -0.5, 0.5),
lng = DEFAULT_WH_LNG + runif(1, -0.5, 0.5),
multiplier = 1.0
)
current <- rbind(current, new_store)
}
} else if (n < nrow(current)) {
current <- current[1:n]
}
rv$stores <- current
rv$results <- NULL
rv$demand_overrides <- NULL # Reset demand overrides when stores change
})
observeEvent(input$reset_stores, {
rv$stores <- copy(DEFAULT_STORES)[1:min(input$n_stores, 3)]
if (input$n_stores > 3) {
for (i in 4:input$n_stores) {
rv$stores <- rbind(rv$stores, data.table(
store_id = i,
store_name = paste("Store", i),
lat = DEFAULT_WH_LAT + runif(1, -0.5, 0.5),
lng = DEFAULT_WH_LNG + runif(1, -0.5, 0.5),
multiplier = 1.0
))
}
}
rv$wh_lat <- DEFAULT_WH_LAT
rv$wh_lng <- DEFAULT_WH_LNG
rv$map_clicks <- 0
})
# ---- Distances (reactive) ---------------------------------------------
distances <- reactive({
stores <- rv$stores
sapply(seq_len(nrow(stores)), function(i) {
haversine_km(rv$wh_lat, rv$wh_lng,
stores$lat[i], stores$lng[i])
})
})
# ---- Map ---------------------------------------------------------------
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = DEFAULT_WH_LNG, lat = DEFAULT_WH_LAT, zoom = 8)
})
observe({
stores <- rv$stores
dists <- distances()
proxy <- leafletProxy("map") %>% clearMarkers() %>% clearShapes()
# Warehouse marker (draggable)
proxy <- proxy %>% addMarkers(
lng = rv$wh_lng, lat = rv$wh_lat,
layerId = "warehouse",
popup = "Warehouse (drag to move)",
options = markerOptions(draggable = TRUE)
)
# Store markers (draggable) and routes
for (i in seq_len(nrow(stores))) {
proxy <- proxy %>%
addMarkers(
lng = stores$lng[i], lat = stores$lat[i],
layerId = paste0("store_", stores$store_id[i]),
popup = paste0(stores$store_name[i],
"<br>Distance: ", round(dists[i], 1), " km",
"<br>Multiplier: ", stores$multiplier[i],
"<br><i>Drag to move</i>"),
icon = makeIcon(
iconUrl = "https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-green.png",
iconWidth = 25, iconHeight = 41,
iconAnchorX = 12, iconAnchorY = 41
),
options = markerOptions(draggable = TRUE)
) %>%
addPolylines(
lng = c(rv$wh_lng, stores$lng[i]),
lat = c(rv$wh_lat, stores$lat[i]),
color = "steelblue", weight = 2, opacity = 0.6,
popup = paste(round(dists[i], 1), "km")
)
}
})
# Drag-end: update warehouse location
observeEvent(input$map_marker_dragend, {
evt <- input$map_marker_dragend
if (evt$id == "warehouse") {
rv$wh_lat <- evt$lat
rv$wh_lng <- evt$lng
} else if (grepl("^store_", evt$id)) {
sid <- as.integer(sub("^store_", "", evt$id))
idx <- which(rv$stores$store_id == sid)
if (length(idx) == 1) {
rv$stores$lat[idx] <- evt$lat
rv$stores$lng[idx] <- evt$lng
}
}
})
# ---- Store Table (editable multipliers) --------------------------------
output$store_table <- renderDT({
stores <- rv$stores
dists <- distances()
display <- data.table(
Store = stores$store_name,
Latitude = round(stores$lat, 4),
Longitude = round(stores$lng, 4),
`Distance (km)` = round(dists, 1),
Multiplier = stores$multiplier
)
datatable(display, editable = list(target = "cell",
disable = list(columns = c(0, 1, 2, 3, 4))),
options = list(pageLength = 10, dom = "t"),
rownames = FALSE)
})
observeEvent(input$store_table_cell_edit, {
info <- input$store_table_cell_edit
if (info$col == 4) { # Multiplier column (0-indexed: 4)
new_val <- as.numeric(info$value)
if (!is.na(new_val) && new_val > 0) {
rv$stores$multiplier[info$row] <- new_val
}
}
})
# ---- Product Table -----------------------------------------------------
output$product_table <- renderDT({
req(rv$products)
display_cols <- intersect(
c("product_idx", "name", "price", "inkoop", "weight_g", "volume_l",
"shelf_life", "gekoeld", "bevroren", "import_order_size",
"import_delivery_time", "weekly_demand"),
names(rv$products)
)
datatable(rv$products[, ..display_cols],
selection = "single",