import os
import matplotlib.pyplot as plt
results = {}

def errorfill(x, y, yerr, color=None, alpha_fill=0.3, ax=None):
    ax = ax if ax is not None else plt.gca()
    if color is None:
        color = ax._get_lines.color_cycle.next()
    ymin, ymax = yerr
    ax.plot(x, y, color=color)
    ax.fill_between(x, ymax, ymin, color=color, alpha=alpha_fill)


def generate(dim, l, la = 0.992, init = 0.01, it = 1000):
    axis = []
    resu = []
    resu_bars_mini = []
    resu_bars_maxi = []
    NB_IT = 8
    for i in range(80, 200, 25):
        results[i] = []
        axis.append(i)
        mini  =10
        maxi = 0
        for j in range(NB_IT):
            os.system('./main '+str(l)+" "+str(i)+ " " + str(dim) +" "+str(it)+" " + str(la)+ " "+ str(init)+ " > res"+str(dim))
            r = open("res"+str(dim), "r")
            res = float(r.readline())
            mini = min(res, mini)
            maxi = max(res, maxi)
            results[i].append(res)
        resu.append(sum(results[i])/NB_IT)
        resu_bars_mini.append(mini)
        resu_bars_maxi.append(maxi)

    resu_bars = [resu_bars_mini, resu_bars_maxi]
    print results
    print axis
    print resu
    print resu_bars

    errorfill(axis, resu, resu_bars)


def gen_generate(dim, l, mu = 5, la = 5, c = 0.5):
    axis = []
    resu = []
    resu_bars_mini = []
    resu_bars_maxi = []
    NB_IT = 8
    for i in range(80, 200, 25):
        results[i] = []
        axis.append(i)
        mini  =10
        maxi = 0
        for j in range(NB_IT):
            os.system('./main '+str(l)+" "+str(i)+ " " + str(dim) + " 1000 "+ str(mu)+ " "+ str(la)+ " " + str(c) + " > res_genetic_"+str(dim))
            r = open("res_genetic_"+str(dim), "r")
            res = float(r.readline())
            mini = min(res, mini)
            maxi = max(res, maxi)
            results[i].append(res)
        resu.append(sum(results[i])/NB_IT)
        resu_bars_mini.append(mini)
        resu_bars_maxi.append(maxi)

    resu_bars = [resu_bars_mini, resu_bars_maxi]
    print results
    print axis
    print resu
    print resu_bars

    errorfill(axis, resu, resu_bars)

dim = 4
generate(dim, 1, 0.99, 0.001, 1000)
generate(dim, 2, 0.99, 0.001, 1000)
gen_generate(dim, 3, 5, 5, 0.6)


plt.show()
