import pandas as pd

with open('Results/sol9_2.log', 'r') as file:
  data = file.read()

rows_list = []
for dline in data.split('\n\n'):
  if dline.startswith('n = '):
    lines=dline.split('\n')
    for line in lines:
      if line.startswith('n = '):
        words=line.split(', ')
        for word in words:
          if word.startswith('n = '):
            n=int(word.strip('n = '))
          if word.startswith('m = '):
            k=int(word.strip('m = '))
          if word.startswith('instance = '):
            instance=word.strip('instance = ')
      
      if line.startswith('Classical cost from best known solution: '):
        classical_cost=f"{float(line.strip('Classical cost from best known solution: ')):.5f}"

      if line.startswith('RAS: '):
        words=line.split(' \t ')
        for word in words:
          if word.startswith('quantum cost = '):
            ras_quantum_cost=f"{float(word.strip('quantum cost = ')):.5f}" if word.strip('quantum cost = ') != 'None' else None
          if word.startswith('approximation ratio = '):
            ras_approximation_ratio=f"{float(word.strip('approximation ratio = ')):.5f}" if word.strip('approximation ratio = ') != 'None' else None
          if word.startswith('number of variables = '):
            ras_no_of_variables=int(word.strip('number of variables = '))
          if word.startswith('runtime = '):
            ras_runtime=f"{float(word.strip('runtime = ')):.5f}"
      
      if line.startswith('FQS: '):
        words=line.split(' \t ')
        for word in words:
          if word.startswith('quantum cost = '):
            fqs_quantum_cost=f"{float(word.strip('quantum cost = ')):.5f}" if word.strip('quantum cost = ') != 'None' else None
          if word.startswith('approximation ratio = '):
            fqs_approximation_ratio=f"{float(word.strip('approximation ratio = ')):.5f}" if word.strip('approximation ratio = ') != 'None' else None
          if word.startswith('number of variables = '):
            fqs_no_of_variables=int(word.strip('number of variables = '))
          if word.startswith('runtime = '):
            fqs_runtime=f"{float(word.strip('runtime = ')):.5f}"
      
      if line.startswith('GPS: '):
        words=line.split(' \t ')
        for word in words:
          if word.startswith('quantum cost = '):
            gps_quantum_cost=f"{float(word.strip('quantum cost = ')):.5f}" if word.strip('quantum cost = ') != 'None' else None
          if word.startswith('approximation ratio = '):
            gps_approximation_ratio=f"{float(word.strip('approximation ratio = ')):.5f}" if word.strip('approximation ratio = ') != 'None' else None
          if word.startswith('number of variables = '):
            gps_no_of_variables=int(word.strip('number of variables = '))
          if word.startswith('runtime = '):
            gps_runtime=f"{float(word.strip('runtime = ')):.5f}"
    
    rows_list.append({'n': n, 'k': k, 'instance': instance, 'Classical Cost': classical_cost, 'RAS: Quantum Cost': ras_quantum_cost, 'RAS: Approximation Ratio': ras_approximation_ratio, 'RAS: No. of Variables': ras_no_of_variables, 'RAS: Runtime': ras_runtime, 'FQS: Quantum Cost': fqs_quantum_cost, 'FQS: Approximation Ratio': fqs_approximation_ratio, 'FQS: No. of Variables': fqs_no_of_variables, 'FQS: Runtime': fqs_runtime, 'GPS: Quantum Cost': gps_quantum_cost, 'GPS: Approximation Ratio': gps_approximation_ratio, 'GPS: No. of Variables': gps_no_of_variables, 'GPS: Runtime': gps_runtime})

df = pd.DataFrame(rows_list)

df.sort_values(by=['n', 'k', 'instance'], inplace=True)
df.head()

df.to_csv('Results/sol9_2.csv', index=False)