import Functions as Fun
import CalculateCostFunc as Cal
import os
from statistics import mean

# modified parameters
file_1 = "0-RCtype.txt"      # each node's coordinates in Solomon instances
fold = "typeRC/"             # detailed solutions of each Solomon instance

# fixed parameters
PriNum = 5       # number of available prices
iteration = 20   # 20 random instances

all_files = os.listdir("./SolomonInstance/" + fold)
if '.DS_Store' in all_files:
  all_files.remove('.DS_Store')

Coor = Fun.readfile_1(file_1)      #  read coordinates of each node in Solomon instances

"""
We evaluate the basic and the generalized information models (only one period)
"""

# distance-based bidding scheme
def Distance_solveModels():

    for file2 in all_files:

        JobNum, Route, Dist, price, priceRatio = Fun.readfile_2(fold+file2, PriNum, Coor)
        K_t = 2 * JobNum       # the number of ad-hoc drivers

        for BidPrice in range(1, PriNum+1):

            OptVal_f = []
            OptVal_g = []
            OptVal_h = []
            gap_f_g = []
            gap_f_h = []

            # write the data to the result file
            outfile = open("./NewResult_Dist/" + file2, "a")
            outfile.write("BidPrice=" + str(BidPrice) + '\t' + "priceRatio=" + str(priceRatio) + '\n')
            outfile.write('SchNum' + '\t' + 'Ite' + '\t' + 'obj_f' + '\t' + 'obj_g' + '\t' + 'obj_h'
                          + '\t' + 'gap_g' + '\t' + 'gap_h' + '\n')

            # write drivers' data
            outfile_driver = open("./SolomonInstance/driverData/" + file2, "a")
            outfile_driver.write("BidPrice=" + str(BidPrice) + '\t' + "priceRatio=" + str(priceRatio) + '\n')

            for SchNum in range(JobNum):           # SchNum = (a^T)x  number of hired scheduled drivers
                outfile_driver.write('Hired scheduled drivers = ' + str(SchNum) + '\n')
                for ite in range(iteration):
                    # coordinates of drivers, distance couriers travel for performing each job
                    CoorCourier, DistCourier = Fun.CourierData(Coor, JobNum, K_t, Dist)
                    outfile_driver.write('Iteration = ' + str(ite) + '\n')
                    for ind_k in range(K_t):
                        outfile_driver.write(str(CoorCourier[ind_k,0]) + '\t' + str(CoorCourier[ind_k,1]) + '\n')

                    driver_job_price = Fun.CourierSelectJobs(K_t, JobNum, PriNum, BidPrice, DistCourier, price)

                    objVal_f = Cal.compute_function_f(K_t, JobNum, driver_job_price, SchNum)
                    OptVal_f.append(objVal_f)

                    objVal_g, z_value = Cal.compute_function_g(K_t, JobNum, driver_job_price, SchNum, PriNum, price)
                    OptVal_g.append(objVal_g)
                    PerDiff1 = (objVal_g - objVal_f)/objVal_f * 100
                    PerDiff1 = round(PerDiff1, 3)
                    gap_f_g.append(PerDiff1)
                    # outfile.write(str(objVal_f) + '\t' + str(objVal_g) + '\t' + str(PerDiff1) + '\n')

                    objVal_h = Cal.compute_function_h(z_value, K_t, JobNum, driver_job_price, SchNum, PriNum, price)
                    OptVal_h.append(objVal_h)
                    PerDiff2 = (objVal_h - objVal_f)/objVal_f * 100
                    PerDiff2 = round(PerDiff2, 3)
                    gap_f_h.append(PerDiff2)
                    outfile.write(str(SchNum) + '\t' + str(ite) + '\t' + str(objVal_f) + '\t' + str(objVal_g) + '\t'
                                  + str(objVal_h) + '\t' + str(PerDiff1)  + '\t' + str(PerDiff2) + '\n')

            ave_f = round(mean(OptVal_f), 2)
            ave_g = round(mean(OptVal_g), 2)
            ave_h = round(mean(OptVal_h), 2)
            ava_gap_g = round(mean(gap_f_g), 2)
            ava_gap_h = round(mean(gap_f_h), 2)

            outfile.write('Average Results are as follows:' + '\n')
            outfile.write(str(ave_f) + '\t' + str(ave_g) + '\t' + str(ave_h) + '\t' + str(ava_gap_g) + '\t' +
                          str(ava_gap_h) + '\n')
            outfile.write('--------------------------------------------------------------------------------------------'+ '\n')

            outfile.close()
            outfile_driver.close()

# randomly select one price from the given list
def Random_solveModels():
    for file2 in all_files:

        JobNum, Route, Dist, price, priceRatio = Fun.readfile_2(fold+file2, PriNum, Coor)
        K_t = JobNum       # the number of ad-hoc drivers

        for BidPrice in range(2, PriNum+1):

            OptVal_f = []
            OptVal_g = []
            OptVal_h = []
            gap_f_g = []
            gap_f_h = []

            # write the data to the result file
            outfile = open("./NewResult_RandBid/" + file2, "a")
            outfile.write("BidPrice=" + str(BidPrice) + '\t' + "priceRatio=" + str(priceRatio) + '\n')
            outfile.write('SchNum' + '\t' + 'Ite' + '\t' + 'obj_f' + '\t' + 'obj_g' + '\t' + 'obj_h'
                          + '\t' + 'gap_g' + '\t' + 'gap_h' + '\n')

            for SchNum in range(JobNum):           # SchNum = (a^T)x  number of hired scheduled drivers
                for ite in range(iteration):
                    driver_job_price = Fun.CourierSelectJobs_Random(K_t, JobNum, PriNum, BidPrice, price)

                    objVal_f = Cal.compute_function_f(K_t, JobNum, driver_job_price, SchNum)
                    OptVal_f.append(objVal_f)

                    objVal_g, z_value = Cal.compute_function_g(K_t, JobNum, driver_job_price, SchNum, PriNum, price)
                    OptVal_g.append(objVal_g)
                    PerDiff1 = (objVal_g - objVal_f)/objVal_f * 100
                    PerDiff1 = round(PerDiff1, 3)
                    gap_f_g.append(PerDiff1)
                    # outfile.write(str(objVal_f) + '\t' + str(objVal_g) + '\t' + str(PerDiff1) + '\n')

                    objVal_h = Cal.compute_function_h(z_value, K_t, JobNum, driver_job_price, SchNum, PriNum, price)
                    OptVal_h.append(objVal_h)
                    PerDiff2 = (objVal_h - objVal_f)/objVal_f * 100
                    PerDiff2 = round(PerDiff2, 3)
                    gap_f_h.append(PerDiff2)
                    outfile.write(str(SchNum) + '\t' + str(ite) + '\t' + str(objVal_f) + '\t' + str(objVal_g) + '\t'
                                  + str(objVal_h) + '\t' + str(PerDiff1)  + '\t' + str(PerDiff2) + '\n')

            ave_f = round(mean(OptVal_f), 2)
            ave_g = round(mean(OptVal_g), 2)
            ave_h = round(mean(OptVal_h), 2)
            ava_gap_g = round(mean(gap_f_g), 2)
            ava_gap_h = round(mean(gap_f_h), 2)

            outfile.write('Average Results are as follows:' + '\n')
            outfile.write(str(ave_f) + '\t' + str(ave_g) + '\t' + str(ave_h) + '\t' + str(ava_gap_g) + '\t' +
                          str(ava_gap_h) + '\n')
            outfile.write('--------------------------------------------------------------------------------------------'+ '\n')

            outfile.close()

            outfile_ave=open("./NewResult_RandBid/" + file2 + 'Average', "a")
            outfile_ave.write("BidPrice=" + str(BidPrice) + '\t' + "priceRatio=" + str(priceRatio) + '\t'
                              + str(ave_f) + '\t' + str(ave_g) + '\t' + str(ave_h) + '\t' + str(ava_gap_g) + '\t'
                              + str(ava_gap_h) + '\n')
            outfile_ave.close()

Random_solveModels()