Esempio n. 1
0
def evalPerturbationPercent(df):
    interestedDim = df.index.get_level_values('interestedDim')[0]
    interestedAgentID = df.index.get_level_values('interestedAgentID')[0]
    varsNotInTraj = ['interestedDim', 'interestedAgentID']


    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data', 'evaluatePerturbation', 'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    trajectoryFixedParameters = {'numWolves': 3, 'maxRunningStepsToSample': 101}
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters)

    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)

    trajVariables = readParametersFromDf(df)
    [trajVariables.pop(var) for var in varsNotInTraj]
    allTrajectories = loadTrajectories(trajVariables)

    calculatePercentageActionChange = CalculatePercentageActionChange(interestedDim, interestedAgentID)
    allMeasurements = np.array([calculatePercentageActionChange(trajectory) for trajectory in allTrajectories])
    measurementMean = np.mean(allMeasurements, axis=0)
    measurementSe = np.std(allMeasurements, axis=0) / np.sqrt(len(allTrajectories) - 1)

    return pd.Series({'mean': measurementMean, 'se': measurementSe})
Esempio n. 2
0
def main():
    # manipulated variables
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numSheep'] = [2, 4]
    manipulatedVariables['shared'] = ['no', 'reward', 'agencyByIndividualWolf']#['no', 'reward', 'agencyBySharedWolf', 'agencyByIndividualWolf']
    manipulatedVariables['sheepType'] = ['sheepWithIndividualRewardWolf']#['sheepWithSharedRewardWolf', 'copySheepWithSharedRewardWolf', 'sheepWithIndividualRewardWolf', 'copySheepWithIndividualRewardWolf']
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)
    productedValues = it.product(*[[(key, value) for value in values] for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [dict(list(specificValueParameter)) for specificValueParameter in productedValues]


    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data', 'evaluateHierarchyPlanningEnvMADDPG',
                                    'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)
   
    maxRunningSteps = 103
    numWolves = 3
    trajectoryFixedParameters = {'numWolves': numWolves, 'maxRunningSteps': maxRunningSteps}
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters)
     
    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    #loadTrajectoriesFromDf = lambda df: [trajectory for trajectory in loadTrajectories(readParametersFromDf(df)) if len(trajectory) < readParametersFromDf(df)['maxRunningSteps']]
    loadTrajectoriesFromDf = lambda df: loadTrajectories(readParametersFromDf(df)) 
    
    measureIntentionArcheivement = lambda df: lambda trajectory: np.sum(np.array(trajectory)[:, 3])
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf, measureIntentionArcheivement)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)
    print(statisticsDf) 
    #__import__('ipdb').set_trace() 
    fig = plt.figure()
    fig.set_dpi(120)
    numColumns = len(manipulatedVariables['sheepType'])
    numRows = 1#len(manipulatedVariables['sheepType'])
    plotCounter = 1
    
    sheepTypeTable = {'sheepWithSharedRewardWolf': 'No Copy Shared',
            'copySheepWithSharedRewardWolf': 'Copy Shared', 
            'sheepWithIndividualRewardWolf': 'No Copy Individual', 
            'copySheepWithIndividualRewardWolf': 'Copy Individual'}
    wolfTypeTable = {'no': 'Individual Reward', 'reward': 'Shared Reward', 
            'agencyBySharedWolf': 'Shared Agency',
            'agencyByIndividualWolf': 'Shared Agency'}

    for key, group in statisticsDf.groupby(['sheepType']):
        group.index = group.index.droplevel(['sheepType'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)
        if (plotCounter) % max(numColumns, 2) == 1:
            axForDraw.set_ylabel('Average Bite')
        
        #if plotCounter <= numColumns:
        #    axForDraw.set_title(str(sheepTypeTable[key]))
        
        numWolvesLabels = ['2', '3']
        for wolfType, grp in group.groupby('shared'):
            grp['mean'] = grp['mean']-5
            grp.index = grp.index.droplevel('shared')
            grp.plot.line(ax = axForDraw, y = 'mean', yerr = 'se', label = wolfTypeTable[wolfType], ylim = (0, 15), marker = 'o', rot = 0 )
            if int((plotCounter - 1) / numColumns) == numRows - 1:
                axForDraw.xaxis.set_label_text('Number of Sheep') 
            else:
                xAxis = axForDraw.get_xaxis()
                xLabel = xAxis.get_label()
                xLabel.set_visible(False)

        plt.xticks([2, 4]) 
        plotCounter = plotCounter + 1

    plt.suptitle('3 Wolves')
    #fig.text(x = 0.5, y = 0.92, s = 'Wolf Type', ha = 'center', va = 'center')
    #fig.text(x = 0.05, y = 0.5, s = 'Sheep Type', ha = 'center', va = 'center', rotation=90)
    plt.show()
def main():
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numSheep'] = [2, 4, 8]
    manipulatedVariables['hierarchy'] = [0, 1, 2]
    manipulatedVariables['valuePriorEndTime'] = [-100, 0, 100]
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)
    productedValues = it.product(
        *[[(key, value) for value in values]
          for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [
        dict(list(specificValueParameter))
        for specificValueParameter in productedValues
    ]

    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data',
                                       'evaluateHierarchyPlanning',
                                       'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    NNNumSimulations = 200
    maxRunningSteps = 50
    wolfPolicySoft = 2.5
    sheepPolicySoft = 2.5
    numWolves = 3
    trajectoryFixedParameters = {
        'numWolves': numWolves,
        'wolfPolicySoft': wolfPolicySoft,
        'sheepPolicySoft': sheepPolicySoft,
        'maxRunningSteps': maxRunningSteps,
        'NNNumSimulations': NNNumSimulations
    }
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory,
                                        trajectoryExtension,
                                        trajectoryFixedParameters)

    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    #loadTrajectoriesFromDf = lambda df: [trajectory for trajectory in loadTrajectories(readParametersFromDf(df)) if len(trajectory) < readParametersFromDf(df)['maxRunningSteps']]
    loadTrajectoriesFromDf = lambda df: loadTrajectories(
        readParametersFromDf(df))

    priorIndexinTimestep = 4
    composeInterpolateFunction = lambda df: Interpolate1dData(maxRunningSteps)
    #getImaginedWeIdsForCrossEntropy = lambda df: [readParametersFromDf(df)['numSheep'], list(range(readParametersFromDf(df)['numSheep'] + 1, readParametersFromDf(df)['numSheep'] +
    #    readParametersFromDf(df)['numWolves']))]
    #composeMeasureCrossEntropy = lambda df: MeasureCrossEntropy(getImaginedWeIdsForCrossEntropy(df), priorIndexinTimestep)
    #measureFunction = lambda df: lambda trajectory: composeInterpolateFunction(df)(composeMeasureCrossEntropy(df)(trajectory))
    getImaginedWeIdsForConvergeRate = lambda df: list(
        range(
            readParametersFromDf(df)['numSheep'],
            readParametersFromDf(df)['numSheep'] + numWolves))
    measureConvergeRate = lambda df: MeasureConvergeOnGoal(
        getImaginedWeIdsForConvergeRate(df), priorIndexinTimestep,
        sampleFromDistribution)
    measureFunction = lambda df: lambda trajectory: composeInterpolateFunction(
        df)(measureConvergeRate(df)(trajectory))
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf,
                                          measureFunction)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)

    fig = plt.figure()
    numColumns = len(manipulatedVariables['hierarchy'])
    numRows = len(manipulatedVariables['valuePriorEndTime'])
    plotCounter = 1

    hierarchyLabels = ['noHierarchy9', 'noHierarchy5', 'Hierarchy9']

    for key, group in statisticsDf.groupby(['valuePriorEndTime', 'hierarchy']):
        group.index = group.index.droplevel(['valuePriorEndTime', 'hierarchy'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)
        #if plotCounter % numColumns == 1:
        #axForDraw.set_ylabel('Cross Entropy')
        axForDraw.set_ylabel('Converge Rate')
        if plotCounter <= numColumns:
            axForDraw.set_title(hierarchyLabels[key[1]])
        for numSheep, grp in group.groupby('numSheep'):
            df = pd.DataFrame(grp.values[0].tolist(),
                              columns=list(range(maxRunningSteps)),
                              index=['mean', 'se']).T
            #df.plot.line(ax = axForDraw, label = 'Set Size of Intentions = {}'.format(numSheep), y = 'mean', yerr = 'se', ylim = (0, 3), rot = 0)
            df.plot.line(ax=axForDraw,
                         label='numSheep = {}'.format(numSheep),
                         y='mean',
                         yerr='se',
                         ylim=(0, 1),
                         rot=0)

        plotCounter = plotCounter + 1

    plt.suptitle(str(numWolves) + 'Wolves')
    #plt.legend(loc='best')
    fig.text(x=0.5, y=0.04, s='hierarchy', ha='center', va='center')
    fig.text(x=0.05,
             y=0.5,
             s='valuePriorEndTime',
             ha='center',
             va='center',
             rotation=90)
    plt.show()
Esempio n. 4
0
def main():
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numWolves'] = [2]  # temp just 2
    manipulatedVariables['numSheep'] = [1]  # temp just 1
    manipulatedVariables['inferenceSoft'] = [1.0]
    manipulatedVariables['wolfPolicySoft'] = [2.5]
    manipulatedVariables['otherCompeteRate'] = [
        0.0, 0.5, 1.0
    ]  # 0 never compete, 1 always compete
    manipulatedVariables['competeDetectionRate'] = [
        0.0, 0.5
    ]  # 0 never detect compete, 1 only detect compete
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)
    productedValues = it.product(
        *[[(key, value) for value in values]
          for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [
        dict(list(specificValueParameter))
        for specificValueParameter in productedValues
    ]

    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data',
                                       'evaluateCompeteDetection',
                                       'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    NNNumSimulations = 250
    maxRunningSteps = 40
    sheepPolicySoft = 1.0
    competePolicy = 'heatseeking'
    trajectoryFixedParameters = {
        'competePolicy': competePolicy,
        'sheepPolicySoft': sheepPolicySoft,
        'maxRunningSteps': maxRunningSteps,
        'NNNumSimulations': NNNumSimulations
    }
    heatseekingPrecesion = 50
    trajectoryFixedParameters.update(
        {'heatseekingPrecesion': heatseekingPrecesion})
    print(trajectoryFixedParameters)
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory,
                                        trajectoryExtension,
                                        trajectoryFixedParameters)

    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    loadTrajectoriesFromDf = lambda df: loadTrajectories(
        readParametersFromDf(df))

    maxSteps = 50
    measureFunction = lambda df: lambda trajectory: int(
        np.linalg.norm(trajectory[-1][0][0] - trajectory[-1][0][1]) <= 50
    ) - 0.00 * min(len(trajectory), maxSteps)

    def measureSelfReward(trajectory):
        stateLastTimeStep = trajectory[-1][0]
        goalState, selfState, otherState = stateLastTimeStep
        selfReward = int(np.linalg.norm(goalState - selfState) <= 50)
        otherReward = int(np.linalg.norm(goalState - otherState) <= 50)

        intentionLastTimeStep = trajectory[-1][4]
        selfIntention, otherIntention = intentionLastTimeStep

        selfShare = False
        if (0, (1, 2)) in list(selfIntention):
            if selfIntention[(0, (1, 2))] >= 0.8:
                selfShare = True

        otherShare = False
        if (0, (1, 2)) in list(otherIntention):
            if otherIntention[(0, (1, 2))] >= 0.8:
                otherShare = True

        if selfReward == 1 and selfShare:
            selfReward = 0.5
        if selfReward == 0 and otherShare:
            selfReward = 0.5

        selfCost = -0.02 * len(trajectory)
        selfFinalGot = selfReward + selfCost
        return selfFinalGot

    measureFunction = lambda df: lambda trajectory: measureSelfReward(
        trajectory)
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf,
                                          measureFunction)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)
    print(statisticsDf)

    fig = plt.figure()
    numColumns = len(manipulatedVariables['inferenceSoft'])
    numRows = len(manipulatedVariables['wolfPolicySoft'])
    plotCounter = 1

    for key, group in statisticsDf.groupby(
        ['numWolves', 'numSheep', 'wolfPolicySoft', 'inferenceSoft']):
        group.index = group.index.droplevel(
            ['numWolves', 'numSheep', 'wolfPolicySoft', 'inferenceSoft'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)
        axForDraw.set_ylabel('Accumulated Reward')
        #axForDraw.set_ylabel(str(numWolves))

        #if plotCounter <= numColumns:
        #    axForDraw.set_title(str(key[3]) + 'inferenceSoft')
        competeDetectionLabels = [
            'Coorperation Only', 'Coorperation Or Competetion Detection',
            'Competetion Only'
        ]
        for competeDetection, grp in group.groupby('competeDetectionRate'):
            grp.index = grp.index.droplevel('competeDetectionRate')
            grp.plot.line(ax=axForDraw,
                          y='mean',
                          yerr='se',
                          label=competeDetectionLabels[int(competeDetection *
                                                           2)],
                          ylim=(-0.3, 0.4),
                          marker='o',
                          rot=0)
        axForDraw.xaxis.set_label_text('Other Wolf Competing Rate')
        plotCounter = plotCounter + 1

    #plt.suptitle(str(competePolicy))
    plt.show()
def main():
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numWolves'] = [2]
    manipulatedVariables['numSheep'] = [2, 4, 8]#, 4, 8]
    manipulatedVariables['hierarchy'] = [0]
    manipulatedVariables['valuePriorSoftMaxBeta'] = [0.0, 1.0, 2.0, 3.0, 4.0]
    manipulatedVariables['valuePriorEndTime'] = [0]#[-100, 0, 100]
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)
    productedValues = it.product(*[[(key, value) for value in values] for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [dict(list(specificValueParameter)) for specificValueParameter in productedValues]


    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data', 'evaluateHierarchyPlanning',
                                    'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)
   
    NNNumSimulations = 250
    maxRunningSteps = 51
    wolfPolicySoft = 2.5
    sheepPolicySoft = 2.5
    trajectoryFixedParameters = {'wolfPolicySoft': wolfPolicySoft, 'sheepPolicySoft': sheepPolicySoft, 
            'maxRunningSteps': maxRunningSteps, 'NNNumSimulations': NNNumSimulations}
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters)
    
    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    loadTrajectoriesFromDf = lambda df: loadTrajectories(readParametersFromDf(df))
    
    maxSteps = maxRunningSteps-26
    measureIntentionArcheivement = lambda df: lambda trajectory: int(len(trajectory) < maxSteps) - 1 / maxSteps * min(len(trajectory), maxSteps)
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf, measureIntentionArcheivement)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)
    print(statisticsDf) 
    
    fig = plt.figure()
    numColumns = len(manipulatedVariables['numWolves'])
    numRows = len(manipulatedVariables['hierarchy'])
    plotCounter = 1

    for key, group in statisticsDf.groupby(['valuePriorEndTime', 'hierarchy', 'numWolves']):
        group.index = group.index.droplevel(['valuePriorEndTime', 'hierarchy', 'numWolves'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)
        axForDraw.set_ylabel('Accumulated Reward')
        #axForDraw.set_ylabel(str(numWolves))
        
        #if plotCounter <= numColumns:
        #    axForDraw.set_title(str(key[1]) + 'Wolves')
        #valuePriorLabels = ['No Value Prior', 'Value Prior']# First Time Step', 'Value Prior Every Time Step ']
        for beta, grp in group.groupby('valuePriorSoftMaxBeta'):
            grp.index = grp.index.droplevel('valuePriorSoftMaxBeta')
            grp.plot.line(ax = axForDraw, y = 'mean', yerr = 'se', label = 'Value SofMax Beta = '+str(beta/2), ylim = (0.2, 0.7), marker = 'o', rot = 0 )
        axForDraw.xaxis.set_label_text('Number of Sheep') 
        plotCounter = plotCounter + 1

    #plt.suptitle('3 Wolves')
    #fig.text(x = 0.5, y = 0.04, s = 'numWolves', ha = 'center', va = 'center')
    #fig.text(x = 0.05, y = 0.5, s = 'valuePriorEndTime', ha = 'center', va = 'center', rotation=90)
    plt.show()
Esempio n. 6
0
def main():
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numSheep'] = [1, 2, 4]
    manipulatedVariables['wolfType'] = [
        'individualReward', 'sharedReward',
        'sharedAgencyByIndividualRewardWolf'
    ]
    manipulatedVariables['sheepConcern'] = ['selfSheep']
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)

    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data',
                                       'evaluateHierarchyPlanningEnvMADDPG',
                                       'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    maxRunningSteps = 101
    numWolves = 3
    trajectoryFixedParameters = {
        'numWolves': numWolves,
        'maxRunningStepsToSample': maxRunningSteps
    }
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory,
                                        trajectoryExtension,
                                        trajectoryFixedParameters)

    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    loadTrajectoriesFromDf = lambda df: loadTrajectories(
        readParametersFromDf(df))

    measureIntentionArcheivement = lambda df: lambda trajectory: np.sum(
        np.array(trajectory)[:, 3])
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf,
                                          measureIntentionArcheivement)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)
    print(statisticsDf)

    fig = plt.figure()
    fig.set_dpi(120)
    numColumns = len(manipulatedVariables['sheepConcern'])
    numRows = 1
    plotCounter = 1

    wolfTypeTable = {
        'individualReward': 'Individual Reward',
        'sharedReward': 'Shared Reward',
        'sharedAgencyBySharedRewardWolf':
        'Shared Agency By Shared Reward Wolf',
        'sharedAgencyByIndividualRewardWolf': 'Shared Agency'
    }

    for key, group in statisticsDf.groupby(['sheepConcern']):
        group.index = group.index.droplevel(['sheepConcern'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)

        for wolfType, grp in group.groupby('wolfType'):
            grp.index = grp.index.droplevel('wolfType')
            grp.plot.line(ax=axForDraw,
                          y='mean',
                          yerr='se',
                          label=wolfTypeTable[wolfType],
                          ylim=(0, 18),
                          marker='o',
                          rot=0)
            axForDraw.set_xlabel('Number of Biting Sheep')
            if plotCounter % numColumns == 1:
                axForDraw.set_ylabel('Number of Wolves = ' + str(key))
        plt.xticks(manipulatedVariables['numSheep'])
        plotCounter = plotCounter + 1
    fig.text(x=0.03,
             y=0.5,
             s='Mean Episode Bite',
             ha='center',
             va='center',
             rotation=90)

    plt.suptitle('Compare Shared Agency with Individual/Sharing Rewards')
    resultPath = os.path.join(DIRNAME, '..', 'evalResult')
    if not os.path.exists(resultPath):
        os.makedirs(resultPath)
    plt.savefig(
        os.path.join(
            resultPath,
            'RewardMADDPG3WolfSharedAgencyVSIndividualRewardVSSharedReward'))
    plt.show()
def main():
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numSheep'] = [2, 4]
    #manipulatedVariables['rationalityBetaInInference'] = [0.0, 0.125, 0.25, 0.5, 1.0]
    manipulatedVariables['rationalityBetaInInference'] = [
        1.0
    ]  #[0.0, 0.1, 0.2, 1.0]#[0.0, 0.025, 0.05, 0.1, 1.0]
    manipulatedVariables['deviationFor2DAction'] = [3.0]  #[0.3, 1.0, 3.0, 9.0]
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)
    productedValues = it.product(
        *[[(key, value) for value in values]
          for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [
        dict(list(specificValueParameter))
        for specificValueParameter in productedValues
    ]

    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data',
                                       'evaluateHierarchyPlanningEnvMADDPG',
                                       'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)

    numWolves = 3
    valuePriorSoftMaxBeta = 0.0
    maxRunningSteps = 102
    valuePriorEndTime = -100
    trajectoryFixedParameters = {
        'numWolves': numWolves,
        'maxRunningSteps': maxRunningSteps,
        'valuePriorEndTime': valuePriorEndTime,
        'valuePriorSoftMaxBeta': valuePriorSoftMaxBeta
    }
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory,
                                        trajectoryExtension,
                                        trajectoryFixedParameters)

    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    #loadTrajectoriesFromDf = lambda df: [trajectory for trajectory in loadTrajectories(readParametersFromDf(df)) if len(trajectory) < readParametersFromDf(df)['maxRunningSteps']]
    loadTrajectoriesFromDf = lambda df: loadTrajectories(
        readParametersFromDf(df))

    priorIndexinTimestep = 4
    composeInterpolateFunction = lambda df: Interpolate1dData(maxRunningSteps)
    #getImaginedWeIdsForCrossEntropy = lambda df: [readParametersFromDf(df)['numSheep'], list(range(readParametersFromDf(df)['numSheep'] + 1, readParametersFromDf(df)['numSheep'] +
    #    readParametersFromDf(df)['numWolves']))]
    #composeMeasureCrossEntropy = lambda df: MeasureCrossEntropy(getImaginedWeIdsForCrossEntropy(df), priorIndexinTimestep)
    #measureFunction = lambda df: lambda trajectory: composeInterpolateFunction(df)(composeMeasureCrossEntropy(df)(trajectory))
    getImaginedWeIdsForConvergeRate = lambda df: list(
        range(
            readParametersFromDf(df)['numSheep'],
            readParametersFromDf(df)['numSheep'] + numWolves))
    measureConvergeRate = lambda df: MeasureConvergeOnGoal(
        getImaginedWeIdsForConvergeRate(df), priorIndexinTimestep,
        sampleFromDistribution)
    measureFunction = lambda df: lambda trajectory: composeInterpolateFunction(
        df)(measureConvergeRate(df)(trajectory))
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf,
                                          measureFunction)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)

    fig = plt.figure()
    fig.set_dpi(120)
    numColumns = len(manipulatedVariables['rationalityBetaInInference'])
    numRows = len(manipulatedVariables['deviationFor2DAction'])
    plotCounter = 1

    for key, group in statisticsDf.groupby(
        ['deviationFor2DAction', 'rationalityBetaInInference']):
        group.index = group.index.droplevel(
            ['deviationFor2DAction', 'rationalityBetaInInference'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)
        #if (plotCounter) % max(numColumns, 2) == 1:
        #    axForDraw.set_ylabel('Observe Noise = '+str(key[0]))
        #if plotCounter <= numColumns:
        #    axForDraw.set_title('Rationality Beta = '+str(key[1]))
        for numSheep, grp in group.groupby('numSheep'):
            df = pd.DataFrame(grp.values[0].tolist(),
                              columns=list(range(maxRunningSteps)),
                              index=['mean', 'se'
                                     ])[list(range(int(maxRunningSteps - 3)))]
            dfTransPosed = df.T
            #df.plot.line(ax = axForDraw, label = 'Set Size of Intentions = {}'.format(numSheep), y = 'mean', yerr = 'se', ylim = (0, 3), rot = 0)
            dfTransPosed.plot.line(ax=axForDraw,
                                   label='numSheep = {}'.format(numSheep),
                                   y='mean',
                                   yerr='se',
                                   ylim=(0, 1.01),
                                   rot=0)

        plotCounter = plotCounter + 1

    plt.suptitle('Shared Agency ' + str(numWolves) + 'Wolves Convege Rate')
    #plt.legend(loc='best')
    #fig.text(x = 0.5, y = 0.04, s = 'Rationality In Inference', ha = 'center', va = 'center')
    #fig.text(x = 0.05, y = 0.5, s = 'valuePriorEndTime', ha = 'center', va = 'center', rotation=90)
    plt.show()
def main():
    # manipulated variables
    # manipulated variables
    manipulatedVariables = OrderedDict()
    manipulatedVariables['numWolves'] = [3]
    manipulatedVariables['numSheep'] = [1, 2, 4]
    manipulatedVariables['deviationFor2DAction'] = [1.0, 3.0, 9.0]
    #manipulatedVariables['rationalityBetaInInference'] = [0.0, 0.125, 0.25, 0.5, 1.0]
    manipulatedVariables['rationalityBetaInInference'] = [0.0, 0.1, 0.2, 1.0]
    levelNames = list(manipulatedVariables.keys())
    levelValues = list(manipulatedVariables.values())
    modelIndex = pd.MultiIndex.from_product(levelValues, names=levelNames)
    toSplitFrame = pd.DataFrame(index=modelIndex)
    productedValues = it.product(*[[(key, value) for value in values] for key, values in manipulatedVariables.items()])
    parametersAllCondtion = [dict(list(specificValueParameter)) for specificValueParameter in productedValues]


    DIRNAME = os.path.dirname(__file__)
    trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data', 'evaluateHierarchyPlanningEnvMADDPG',
                                    'trajectories')
    if not os.path.exists(trajectoryDirectory):
        os.makedirs(trajectoryDirectory)
   
    valuePriorSoftMaxBeta = 0.0
    maxRunningSteps = 102
    valuePriorEndTime = -100
    trajectoryFixedParameters = {'maxRunningSteps': maxRunningSteps,'valuePriorSoftMaxBeta': valuePriorSoftMaxBeta,
            'valuePriorEndTime': valuePriorEndTime}
    trajectoryExtension = '.pickle'
    getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters)
     
    # Compute Statistics on the Trajectories
    loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle)
    #loadTrajectoriesFromDf = lambda df: [trajectory for trajectory in loadTrajectories(readParametersFromDf(df)) if len(trajectory) < readParametersFromDf(df)['maxRunningSteps']]
    loadTrajectoriesFromDf = lambda df: loadTrajectories(readParametersFromDf(df)) 
    
    measureIntentionArcheivement = lambda df: lambda trajectory: np.sum(np.array(trajectory)[:, 3])
    computeStatistics = ComputeStatistics(loadTrajectoriesFromDf, measureIntentionArcheivement)
    statisticsDf = toSplitFrame.groupby(levelNames).apply(computeStatistics)
    print(statisticsDf) 
    __import__('ipdb').set_trace() 
    fig = plt.figure()
    fig.set_dpi(120)
    numColumns = len(manipulatedVariables['rationalityBetaInInference'])
    numRows = len(manipulatedVariables['deviationFor2DAction'])
    plotCounter = 1
    
    for key, group in statisticsDf.groupby(['deviationFor2DAction', 'rationalityBetaInInference']):
        group.index = group.index.droplevel(['deviationFor2DAction', 'rationalityBetaInInference'])
        axForDraw = fig.add_subplot(numRows, numColumns, plotCounter)
        if (plotCounter) % max(numColumns, 2) == 1:
            axForDraw.set_ylabel('Observe Noise = '+str(key[0]))
        
        if plotCounter <= numColumns:
            axForDraw.set_title('Rationality Beta = '+str(key[1]))
        
        numWolvesLabels = ['2', '3']
        for numWolves, grp in group.groupby('numWolves'):
            grp.index = grp.index.droplevel('numWolves')
            grp.plot.line(ax = axForDraw, y = 'mean', yerr = 'se', label = str(numWolves)+' Wolves', ylim = (0, 16), marker = 'o', rot = 0 )
            if int((plotCounter - 1) / numColumns) == numRows - 1:
                axForDraw.xaxis.set_label_text('Number of Sheep') 
            else:
                xAxis = axForDraw.get_xaxis()
                xLabel = xAxis.get_label()
                xLabel.set_visible(False)

       
        plotCounter = plotCounter + 1

    plt.suptitle('Accumulated Reward')
    #fig.text(x = 0.5, y = 0.04, s = 'numWolves', ha = 'center', va = 'center')
    #fig.text(x = 0.05, y = 0.5, s = 'valuePriorEndTime', ha = 'center', va = 'center', rotation=90)
    plt.show()