Advertisement
patryk

KCK - Zadanie 1 na 5.0

Oct 10th, 2015
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import glob
  3. import csv
  4. import numpy as np
  5.  
  6. MARKERS_LIST = ['o', 'v', 'D', 's', 'd']
  7. COLORS_LIST = ['b', 'g', 'r', 'k', 'm']
  8. FILES_LIST = ['rsel.csv', 'cel-rs.csv', '2cel-rs.csv', 'cel.csv', '2cel.csv']
  9. HEURISTICS_LIST = ['1-Evol-RS', '1-Coev-RS', '2-Coev-RS', '1-Coev', '2-Coev']
  10.  
  11.  
  12. def str_to_float(string):
  13.     try:
  14.         string = float(string)
  15.     except ValueError:
  16.         pass
  17.     return string
  18.  
  19.  
  20.  
  21.  
  22. def read_file(filename):
  23.     arr = []
  24.     with open(filename, newline='') as csvfile:
  25.         handler = csv.reader(csvfile, delimiter=',')
  26.         for row in handler:
  27.             arr.append(row)
  28.     return arr 
  29.  
  30.  
  31.  
  32.  
  33. def prepare_array(arr):
  34.     new_arr = []
  35.     for row in arr:
  36.         temp = []
  37.         for cell in row:
  38.             temp.append(str_to_float(cell))
  39.         new_arr.append(temp)
  40.     return new_arr
  41.  
  42.  
  43.  
  44.  
  45. def left_plot(arr, ax1, ax2, marker, color, heuristic):
  46.     x1, y1, x2, y2 = [], [], [], []
  47.     xScaleFactor, yScaleFactor = 1000, 100
  48.  
  49.     for row in arr[1:]:
  50.         x1.append(row[1] / xScaleFactor)
  51.         y1.append((sum(row[2:]) / float(len(row[2:]))) * yScaleFactor)
  52.         if (row[0] % 30 == 0):
  53.             x2.append(row[1] / xScaleFactor)
  54.             y2.append((sum(row[2:]) / float(len(row[2:]))) * yScaleFactor)
  55.  
  56.     ax1.plot(x1, y1, c=color)
  57.     ax2.scatter(x2, y2, s=30, c=color, marker=marker, label=heuristic)
  58.    
  59.  
  60.  
  61.  
  62. def left_plot_info(ax1, ax2):
  63.     ax1.set_xlabel('Rozegranych gier (x1000)')
  64.     ax1.set_ylabel('Odsetek wygranych gier [%]')
  65.     ax1.set_xlim([0,500000 / 1000])
  66.     ax1.set_ylim([60, 100])
  67.     ax1.grid()
  68.  
  69.     ax2.set_xlabel('Pokolenie')
  70.     ax2.set_xlim([0, 500000 / 1000])
  71.     ax2.set_ylim([60, 100])
  72.     ax2.legend(loc=4, fontsize='medium')
  73.     ax2.set_xticks([0,100,200,300,400,500])
  74.     ax2.set_xticklabels(['0','40','80','120','160','200']);
  75.    
  76.  
  77.  
  78.  
  79. def right_plot(arr):
  80.     y = []
  81.     for row in arr[1:]:
  82.         y.append(sum(row[2:]) / float(len(row[2:])) * 100)
  83.     return y
  84.  
  85.  
  86.  
  87.  
  88. def right_plot_info(ax, data):
  89.     ax.boxplot(data, notch=True, bootstrap=10000)
  90.     ax.set_xticks([1,2,3,4,5,6])
  91.     ax.set_xticklabels(['1-Evol-RS', '1-Coev-RS', '2-Coev-RS', '1-Coev', '2-Coev'], rotation=22, fontsize=11);
  92.     ax.yaxis.tick_right()
  93.     ax.grid()
  94.  
  95.  
  96.  
  97.  
  98. def main():
  99.     #fig = plt.figure(figsize=(6.7, 6.7))        PROPONOWANY PRZEZ PANA ROZMIAR WYKRESU
  100.     ax1 = plt.subplot(1, 2, 1)
  101.     ax2 = ax1.twiny()
  102.     ax3 = plt.subplot(1, 2, 2)
  103.    
  104.     i = 0
  105.     data = []
  106.     #for file_name in glob.glob('*.csv'):         NIE KORZYSTAM Z glob.glob, PONIEWAŻ WCZYTUJE ON PLIKI W MNIEJ WYGODNEJ KOLEJNOŚCI, PRZEZ CO TRUDNIEJ DOBRAĆ, ŻEBY KOLORY WYKRESÓW BYŁY TAKIE JAK W TREŚCI
  107.  
  108.     for filename in FILES_LIST:
  109.         left_plot(prepare_array(read_file(filename)), ax1, ax2, MARKERS_LIST[i], COLORS_LIST[i], HEURISTICS_LIST[i])
  110.         i += 1
  111.     left_plot_info(ax1, ax2)
  112.  
  113.    
  114.     for filename in FILES_LIST:
  115.         data.append(right_plot(prepare_array(read_file(filename))))
  116.     right_plot_info(ax3, data)
  117.     #plt.savefig('na5.pdf')     ODKOMENTOWAĆ JEŻELI WYKRES MA ZOSTAĆ ZAPISANY NA DYSKU  
  118.     plt.show()
  119.    
  120.  
  121. if __name__ == '__main__':
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement