__author__ = 'fatma'
from os import listdir
import subprocess
from os.path import isfile, join
from subprocess import PIPE, Popen
import re

linkDir = '/home/fatma/workspace/data/java_data/alpha/0.7/Links/'
nodeDir = '/home/fatma/workspace/data/java_data/alpha/0.7/Nodes/'
pairDir = '/home/fatma/workspace/data/java_data/alpha/0.7/Pairs/'


def wrapper(*args):
    process = Popen(list(args), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    stdout, stderr = process.communicate()
    return stdout

linkFileList = [ join(linkDir,f) for f in listdir(linkDir) if isfile(join(linkDir,f)) ]
nodeFileList = [ join(nodeDir,f) for f in listdir(nodeDir) if isfile(join(nodeDir,f)) ]
pairFileList = [ join(pairDir,f) for f in listdir(pairDir) if isfile(join(pairDir,f)) ]
linkFileList.sort()
nodeFileList.sort()
pairFileList.sort()
size = len(linkFileList)
text_file = open("/home/fatma/workspace/data/results/alpha/0.7/Output.txt", "w")
#text_file2 = open("OutputFeasible.txt", "w")
numOfFeasibleCplex = 0
numOfFeasibleHeuristic = 0
avgEnergyCostHeuristic = 0
avgEnergyCostCplex = 0
avgRelayCostHeuristic = 0
avgRelayCostCplex = 0
for count in range(0, size):
    x = wrapper('java', '-jar',  '-Xmx12g', '/home/fatma/workspace/HeuristicRunnable.jar', nodeFileList[count], linkFileList[count], pairFileList[count])
    #print(x)
    y = wrapper('java', '-jar', '-Xmx12g', '-Djava.library.path=/opt/ibm/ILOG/CPLEX_Studio1251/cplex/bin/x86-64_sles10_4.1', '/home/fatma/workspace/CplexRunnable.jar', linkFileList[count], nodeFileList[count], pairFileList[count])
    #print(y)
    m = re.findall(r"Total Cost:\s?([-+]?\d+)", x)
    n = re.findall(r"Objective fonksiyon\s?([-+]?\d+)", y)
    p = re.findall(r"Relay Cost:\s?([-+]?\d+)", x)
    q = re.findall(r"Relay Cost:\s?([-+]?\d+)", y)
	
    if m[0] != "-1":
        numOfFeasibleHeuristic += 1
        avgEnergyCostHeuristic += float(m[0])
        avgRelayCostHeuristic += float(p[0])
    #concat = m[0] + ' '
    #if m[0] != "-1" and n:
        #concat2 = m[0] + ' ' + n[0] + '\n'
        #text_file2.write(concat2)
    if n:
        #concat = concat + n[0] + '\n'
        numOfFeasibleCplex += 1
        if m[0] != "-1":
            avgEnergyCostCplex += float(n[0])
            avgRelayCostCplex += float(q[0])
        if m[0] == "-1":
            print(count)
    print("Finished " + str(count))
    #else:
        #concat = concat + "-1" + '\n'
    #text_file.write(concat)
if numOfFeasibleCplex != 0:
    avgEnergyCostCplex = avgEnergyCostCplex/numOfFeasibleHeuristic
    avgRelayCostCplex = avgRelayCostCplex/numOfFeasibleHeuristic
if numOfFeasibleHeuristic != 0:
    avgEnergyCostHeuristic = avgEnergyCostHeuristic/numOfFeasibleHeuristic
    avgRelayCostHeuristic = avgRelayCostHeuristic/numOfFeasibleHeuristic
text_file.write("Average energy cost CPLEX ")
text_file.write(str(avgEnergyCostCplex))
text_file.write("\n")
text_file.write("Average energy cost Heuristic ")
text_file.write(str(avgEnergyCostHeuristic))
text_file.write("\n")
text_file.write("Average relay cost CPLEX ")
text_file.write(str(avgRelayCostCplex))
text_file.write("\n")
text_file.write("Average relay cost Heuristic ")
text_file.write(str(avgRelayCostHeuristic))
text_file.write("\n")
text_file.write("Num Of Feasible CPLEX ")
text_file.write(str(numOfFeasibleCplex))
text_file.write("\n")
text_file.write("Num Of Feasible Heuristic ")
text_file.write(str(numOfFeasibleHeuristic))

text_file.close()
#text_file2.close()
print("Num Of Feasible CPLEX ", numOfFeasibleCplex)
print("Num Of Feasible Heuristic ", numOfFeasibleHeuristic)