computer = unlist(strsplit(getwd(), "/"))[3]
wd.path = getwd()
shell(paste("cd", wd.path))
suppressPackageStartupMessages(library(dplyr))
prob.weights = c(64, 16, 4, 1)
cooling.ratio = 0.99
reaction.factor = 0.90
initial.d.factor = 0.4
min.prob = 0.01
source(paste0(wd.path, "/funcs.R"))
source(paste0(wd.path, "/feasibility2.R"))

### numerical experiment parameters
method = "alnsk"

nstart = 5
time.limit = 1
iteration.limit = 20000
include.drone.initial = F
save.sols = F
destroy.connected = F
vehicle.capacity = 10
min.temp = 400


errs = 0
wars = 0
chck = 0

all.results = numeric(nstart)
custnodes = rep(c(10,12,16), each=4)
dtsets = paste(custnodes, 1:4, sep = "-")
dtlen = length(dtsets)
resultsdf = matrix(0, dtlen, nstart*5+1)
movesdf = matrix(0, dtlen, max.move*4+1)
get.data.from = paste(wd.path, gams.folder, sep = "/")
save.name = paste0("resultsdf-", method, "-", start.method, "-",
                   time.limit, "mins-", nstart, "trials-",
                   gams.folder,".xlsx")
for(data.ind in 1:dtlen){
  dtset = dtsets[data.ind]
  print(method)
  print(dtset)
  vehicle.capacity = custnodes[data.ind]/vehicle.number.initial
  source("parameters.R")
  data.folder = paste(get.data.from, paste0("gams-", dtset), sep = "/")
  df = readxl::read_excel(paste(data.folder, "dataR.xlsx", sep = "/"))
  read_gams_sol_kvehicle(paste0(gams.folder,"/gams-",dtset))
  opt = read_optimal(dtset, gams.folder, "final", gams.file)
  # read_gams_sol_kvehicle()
  # opt = 1639.5612
  nodes = nrow(df)
  ncust = nodes-2
  dist.mat = calc_dist_mat(df[,c("x","y")])
  truck.time = round(dist.mat/truck.velocity,4)
  drone.time = round(dist.mat/drone.velocity,4)
  tw.df = find_timeloc_mat()
  
  ninitials = numeric(nstart)
  nresults = rep(NA, nstart)
  ntimes = numeric(nstart)
  seeds = numeric(nstart)
  ndevs = rep(NA, nstart)
  nvrp = numeric(nstart)

  for(ns in 1:nstart){
    print(paste0(dtset, ", run: ", ns))
    print(paste("optimal cost:         ", opt))
    starting.time = Sys.time()
    seed = as.numeric(starting.time)
    set.seed(seed)
    seeds[ns] = seed
    schedule = data.frame()
    candidate.schedule = data.frame()
    include.drone = include.drone.initial
    vehicle.number = vehicle.number.initial
    truck.routes = vector("list", vehicle.number)
    drone.routes = vector("list", vehicle.number)
    generate_edd_routes(vehicle.number)
    # generate_random_routes(k = vehicle.number)
    # st_dbscan(df, show.steps = TRUE)
    # clustered_initial_solution(df2)
    # generate_random_routes(vehicle.number)
    # plot_dronetruck_kvehicle(truck.routes, drone.routes)

    source(paste0(method, ".R"))

    plot_dronetruck_kvehicle(best.truck.routes, best.drone.routes)
    title(main = paste("heuristic cost:", round(best.cost,2)))
    plot_costvector()
    
    nresults[ns] = best.cost
    ninitials[ns] = initial.cost
    ntimes[ns] = last.solution.time
    nvrp[ns] = vrp.cost
    ndevs[ns] = round(best.cost/opt-1,4)*100
    trs = best.truck.routes
    drs = best.drone.routes
    if(g==1){
      save_solution(best.truck.routes, best.drone.routes, wd.path,
                    data.folder, TRUE, gams.folder, TRUE)
    }

    
    print(paste("last solution time:     ", last.solution.time))
    print(paste("last temperature:       ", temperature))
    print(paste("last temperature length:", temperature.length))
    print(paste("last cost:              ", best.cost))
    print("deviations from optimal:")
    print(ndevs)
    print(paste("algorithm errors:       ", algo.errors))
    print(paste(rep("-", 40), collapse = "-"))
    # Sys.sleep(50)
  }
  opt.found = any(ndevs == 0)
  print(paste("is optimal solution found?:                ", opt.found))
  print(paste("minimum deviation from optimal solution:   ", min(ndevs)))
  print(paste("average deviation from optimal solution:   ", mean(ndevs)))
  print(paste("average time last incumbent solution found:", round(mean(ntimes))))
  chck = chck + checks
  wars = wars + warns
  errs = errs + errors
  print(paste("errors perc:", round(errors/checks,4)))
  print(paste(rep("-", 40), collapse = "-"))
  
  update_matrices(data.ind)
  plot(nresults, type="b")
  abline(h = mean(nresults, na.rm = T))
  xlsx::write.xlsx(resultsdf, save.name,
                   row.names = F, showNA = F, sheetName = "data")
  save.name2 = unlist(strsplit(save.name,"-"))
  save.name2[1] = "movesdf"
  xlsx::write.xlsx(movesdf, paste0(save.name2, collapse = "-"),
                   row.names = F, showNA = F, sheetName = "all")
  
  #Sys.sleep(10)
}
opt = sapply(dtsets, read_optimal, where = gams.folder, sol = "final", file.name = gams.file)
save_stats()
warnings()
