def plot(file, legend):
    with open(join(path, file)) as data_file:
        data = json.load(data_file)

        output = """
            \\begin{tikzpicture}
                \\begin{axis}[
                    xlabel={Laps},
                    xmin=1, xmax=%d,
                    ymin=0, ymax=100,
                    ytick={0,20,40,60,80,100},
                    legend pos=north east,
                    ymajorgrids=true,
                    grid style=dashed,
                    smooth,
                    axis lines=left
                ]
""" % len(data['executions'])

        countDecision = 0;
        countPassedExecution = 0;
        usedDecisions = {};
        index = 1
        outputPassed = ""
        outputSearch = ""
        outputLean = ""
        for execution in data['executions']:
            if 'decisions' not in execution:
                continue
            if execution['result']['success']:
                countPassedExecution += 1
            for decision in execution['decisions']:
                dID = decisionID(decision)
                if dID not in usedDecisions:
                    usedDecisions[dID] = 0
                usedDecisions[dID] += 1
            outputPassed += "(%d,%d) " % (index, countPassedExecution*100/(len(data['executions'])))
            outputSearch += "(%d,%d) " % (index, len(usedDecisions)*100/(len(data['searchSpace'])))
            outputLean += "(%d,%d) " % (index, countPassedExecution*100/(index))
            index += 1
        output += "\n               \\addplot[color=primary] coordinates {%s};" % outputPassed
        if legend:
            output +=" \\addlegendentry{Nb correct Laps}"
        output += "\n               \\addplot[color=secondary] coordinates {%s};" % outputSearch
        if legend:
            output +=" \\addlegendentry{\\% Explored Decision}"
        output += "\n               \\addplot[color=tertiary] coordinates {%s};" % outputLean
        if legend:
            output +=" \\addlegendentry{\\% Correct Laps}"
        output += """
                \end{axis}
            \end{tikzpicture}"""
    return output
			sizeCorrectSeqDecisions = []
			sizeFailureSeqDecisions = []

			for execution in data['executions']:
				if 'decisions' not in execution:
					continue
				if execution['result']['success']:
					countPassedLaps += 1
				sID = ""
				for decision in execution['decisions']:
					lID = locationID(decision['location'])
					if lID not in exploredLocations:
						exploredLocations[lID] = 0
					exploredLocations[lID] += 1

					dID = decisionID(decision)
					sID += dID
					if dID not in exploredDecisions:
						exploredDecisions[dID] = 0
					exploredDecisions[dID] += 1

				if sID not in exploredSeqDecisions:
					exploredSeqDecisions[sID] = 0
					if execution['result']['success']:
						nbPassedSeqLaps += 1
				exploredSeqDecisions[sID] += 1

				if execution['result']['success']:
					sizeCorrectSeqDecisions += [len(execution['decisions'])]
				else:
					sizeFailureSeqDecisions += [len(execution['decisions'])]
def plot(bugName, bug):
    output = """        \\begin{tikzpicture}
            \\begin{axis}[
                enlargelimits=false,
                xmin=1,xmax=200,
                ymin=0,ymax=100,
                ytick={0,10,20,30,40,50,60,70,80,90,100},
                ymajorgrids=true,
                grid style=dashed,
                smooth,
                axis lines=left,
                legend pos=outer north east,
                ylabel={\\% Correct Laps},
                xlabel={Nb Laps Before Max Explored Decision},
                ]
"""
    #ylabel={Nb Laps Before Stabilization},
    epsilons = sorted(bug)
    for epsilon in epsilons:
        if "%.1f" % float(epsilon) == "0.0":
            continue
        coordinates = ""
        seeds = sorted(bug[epsilon])
        for seed in seeds:            
            executionsPath = join(path, bugName, str(bug[epsilon][seed]) + ".json")
            with open(executionsPath) as data_file:    
                data = json.load(data_file)

                index = 0

                countPassedLaps = 0
                nbBeforeStabilization = -1 
                exploredDecisions = {}
                exploredSeqDecisions = {}
                exploredLocations = {}
                learns = []
                sizeCorrectSeqDecisions = []
                sizeFailureSeqDecisions = []

                maxExplredDecision = 0
                nbLapsBeforeMaxDecision = 0
                for execution in data['executions']:
                    if 'decisions' not in execution:
                        continue
                    if execution['result']['success']:
                        countPassedLaps += 1

                    sID = ""
                    for decision in execution['decisions']:
                        lID = locationID(decision['location'])
                        if lID not in exploredLocations:
                            exploredLocations[lID] = 0
                        exploredLocations[lID] += 1

                        dID = decisionID(decision)
                        sID += dID
                        if dID not in exploredDecisions:
                            exploredDecisions[dID] = 0
                        exploredDecisions[dID] += 1

                    # learning curve
                    learn = countPassedLaps*100/(index + 1)
                    learns += [learn]
                    if len(learns) >= 10 and nbBeforeStabilization == -1 and learn != 0:
                        minLearn = 100
                        maxLearn = 0
                        for learn in learns[index - 9::]:
                            if learn > maxLearn:
                                maxLearn = learn
                            if learn < minLearn:
                                minLearn = learn
                        diffLearn = maxLearn - minLearn
                        if diffLearn <= 4:
                            nbBeforeStabilization = index - 9

                    if len(exploredDecisions) > maxExplredDecision:
                        maxExplredDecision = len(exploredDecisions)
                        nbLapsBeforeMaxDecision = index + 1

                    index += 1
                #print seed, epsilon, nbLapsBeforeMaxDecision, nbBeforeStabilization
                coordinates += "(%d,%d) " % (nbLapsBeforeMaxDecision, countPassedLaps*100/len(data['executions']))
        output += "                \\addplot+[only marks] coordinates {%s};" % coordinates
        output +="    \\addlegendentry{\\exploitationCoefficient %.1f}\n" % (1 - float(epsilon))
    output += """           \end{axis}
        \end{tikzpicture}"""
    return output