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})
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()
def main(): DIRNAME = os.path.dirname(__file__) trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data', 'evaluateHierarchyPlanningEnvMADDPG', 'trajectories') if not os.path.exists(trajectoryDirectory): os.makedirs(trajectoryDirectory) maxRunningSteps = 101 trajectoryFixedParameters = {'maxRunningStepsToSample': maxRunningSteps} trajectoryExtension = '.pickle' getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters) # Trajectories loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle) numWolves = 3 numSheep = 2 wolfType = 'individualReward' sheepConcern = 'selfSheep' trajectoryParameters = {'numWolves': numWolves, 'numSheep': numSheep, 'wolfType': wolfType, 'sheepConcern': sheepConcern} trajectories = loadTrajectories(trajectoryParameters) # generate demo image screenWidth = 700 screenHeight = 700 screen = pg.display.set_mode((screenWidth, screenHeight)) screenColor = THECOLORS['black'] xBoundary = [0, 700] yBoundary = [0, 700] lineColor = THECOLORS['white'] lineWidth = 4 drawBackground = DrawBackground(screen, screenColor, xBoundary, yBoundary, lineColor, lineWidth) FPS = 10 numBlocks = 2 wolfColor = [255, 255, 255] sheepColor = [0, 250, 0] blockColor = [200, 200, 200] circleColorSpace = [wolfColor] * numWolves + [sheepColor]*numSheep + [blockColor] * numBlocks viewRatio = 1.5 sheepSize = int(0.05 * screenWidth / (2 * viewRatio)) wolfSize = int(0.075 * screenWidth / (3 * viewRatio)) blockSize = int(0.2 * screenWidth / (3 * viewRatio)) circleSizeSpace = [wolfSize] * numWolves + [sheepSize] * numSheep + [blockSize] * numBlocks positionIndex = [0, 1] agentIdsToDraw = list(range(numWolves + numSheep + numBlocks)) saveImage = False imageSavePath = os.path.join(trajectoryDirectory, 'picMovingSheep') if not os.path.exists(imageSavePath): os.makedirs(imageSavePath) imageFolderName = str('forDemo') saveImageDir = os.path.join(os.path.join(imageSavePath, imageFolderName)) if not os.path.exists(saveImageDir): os.makedirs(saveImageDir) imaginedWeIdsForInferenceSubject = list(range(numWolves)) updateColorSpaceByPosterior = None outsideCircleAgentIds = imaginedWeIdsForInferenceSubject outsideCircleColor = np.array([[255, 0, 0]] * numWolves) outsideCircleSize = int(wolfSize * 1.5) drawCircleOutside = DrawCircleOutsideEnvMADDPG(screen, viewRatio, outsideCircleAgentIds, positionIndex, outsideCircleColor, outsideCircleSize) drawState = DrawStateEnvMADDPG(FPS, screen, viewRatio, circleColorSpace, circleSizeSpace, agentIdsToDraw, positionIndex, saveImage, saveImageDir, drawBackground, updateColorSpaceByPosterior, drawCircleOutside) # MDP Env interpolateState = None stateIndexInTimeStep = 0 actionIndexInTimeStep = 1 posteriorIndexInTimeStep = None chaseTrial = ChaseTrialWithTraj(stateIndexInTimeStep, drawState, interpolateState, actionIndexInTimeStep, posteriorIndexInTimeStep) maxWolfPositions = np.array([max([max([max(abs(timeStep[0][wolfId][0]), abs(timeStep[0][wolfId][1])) for wolfId in range(numWolves)]) for timeStep in trajectory]) for trajectory in trajectories]) flags = maxWolfPositions < 1.3 * viewRatio index = flags.nonzero()[0] print(trajectories[0][1]) [chaseTrial(trajectory) for trajectory in np.array(trajectories)[index[[0, 2, 3]]]]
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()
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(): 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 = 61 softParameterInPlanningForSheep = 2.0 softParameterInPlanning = 2.0 trajectoryFixedParameters = { 'sheepPolicySoft': softParameterInPlanningForSheep, 'wolfPolicySoft': softParameterInPlanning, 'maxRunningSteps': maxRunningSteps, 'NNNumSimulations': NNNumSimulations } trajectoryExtension = '.pickle' getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters) # Compute Statistics on the Trajectories loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle) numWolves = 2 numSheep = 1 competePolicy = 'heatseeking' heatseekingPrecesion = 1.83 otherCompeteRate = 1.0 competeDetectionRate = 0.5 inferenceSoft = 0.05 trajectoryParameters = { 'heatseekingPrecesion': heatseekingPrecesion, 'inferenceSoft': inferenceSoft, 'numWolves': numWolves, 'numSheep': numSheep, 'competePolicy': competePolicy, 'otherCompeteRate': otherCompeteRate, 'competeDetectionRate': competeDetectionRate } trajectories = loadTrajectories(trajectoryParameters) # generate demo image screenWidth = 600 screenHeight = 600 screen = pg.display.set_mode((screenWidth, screenHeight)) screenColor = THECOLORS['black'] xBoundary = [0, 600] yBoundary = [0, 600] lineColor = THECOLORS['white'] lineWidth = 4 drawBackground = DrawBackground(screen, screenColor, xBoundary, yBoundary, lineColor, lineWidth) FPS = 24 circleColorSpace = [[0, 255, 0]] * numSheep + [[255, 0, 0]] * numWolves circleSize = 10 positionIndex = [0, 1] agentIdsToDraw = list(range(numSheep + numWolves)) #saveImage = False saveImage = True imageSavePath = os.path.join(trajectoryDirectory, 'picMovingSheep') if not os.path.exists(imageSavePath): os.makedirs(imageSavePath) imageFolderName = str('forDemo') saveImageDir = os.path.join(os.path.join(imageSavePath, imageFolderName)) if not os.path.exists(saveImageDir): os.makedirs(saveImageDir) goalSpace = list(range(numSheep)) imaginedWeIdsForInferenceSubject = list( range(numSheep, numWolves + numSheep)) softParameter = 1.1 softFunction = SoftDistribution(softParameter) selfPosteriorIndex = 0 concernedAgentId = 2 competeIntention = (0, ()) cooperateIntention = (0, tuple(range(numSheep, numSheep + numWolves))) updateColorSpaceByPosterior = lambda colorSpace, posterior: updateColorSpace( colorSpace, [ softFunction(individualPosterior) for individualPosterior in posterior ], selfPosteriorIndex, concernedAgentId, competeIntention, cooperateIntention) #updateColorSpaceByPosterior = lambda originalColorSpace, posterior : originalColorSpace outsideCircleAgentIds = imaginedWeIdsForInferenceSubject outsideCircleColor = np.array([[0, 0, 0]] * numWolves) outsideCircleSize = 15 drawCircleOutside = DrawCircleOutside(screen, outsideCircleAgentIds, positionIndex, outsideCircleColor, outsideCircleSize) drawState = DrawState(FPS, screen, circleColorSpace, circleSize, agentIdsToDraw, positionIndex, saveImage, saveImageDir, drawBackground, updateColorSpaceByPosterior, drawCircleOutside) # MDP Env xBoundary = [0, 600] yBoundary = [0, 600] stayInBoundaryByReflectVelocity = StayInBoundaryByReflectVelocity( xBoundary, yBoundary) transit = InterpolateOneFrame(stayInBoundaryByReflectVelocity) numFramesToInterpolate = 5 interpolateState = InterpolateState(numFramesToInterpolate, transit) stateIndexInTimeStep = 0 actionIndexInTimeStep = 1 posteriorIndexInTimeStep = 4 chaseTrial = ChaseTrialWithTraj(stateIndexInTimeStep, drawState, interpolateState, actionIndexInTimeStep, posteriorIndexInTimeStep) print(len(trajectories)) lens = [len(trajectory) for trajectory in trajectories] index = np.argsort(-np.array(lens)) print(index) print(trajectories[0][1]) #[chaseTrial(trajectory) for trajectory in np.array(trajectories)[index[0:10]]] [ chaseTrial(trajectory) for trajectory in np.array(trajectories)[index[9:10]] ]
def main(): 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 = 101 deviationFor2DAction = 9.0 trajectoryFixedParameters = { 'maxRunningSteps': maxRunningSteps, 'valuePriorSoftMaxBeta': valuePriorSoftMaxBeta, 'deviationFor2DAction': deviationFor2DAction } trajectoryExtension = '.pickle' getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters) # Trajectories loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle) numWolves = 3 numSheep = 4 rationalityBetaInInference = 0.5 valuePriorEndTime = -100 valuePriorSoftMaxBeta = 0.0 sheepConcern = 'selfSheep' wolfType = 'sharedAgencyByIndividualRewardWolf' trajectoryParameters = { 'numWolves': numWolves, 'numSheep': numSheep, 'valuePriorEndTime': valuePriorEndTime, 'valuePriorSoftMaxBeta': valuePriorSoftMaxBeta, 'rationalityBetaInInference': rationalityBetaInInference, 'sheepConcern': sheepConcern, 'wolfType': wolfType } trajectories = loadTrajectories(trajectoryParameters) # generate demo image screenWidth = 700 screenHeight = 700 screen = pg.display.set_mode((screenWidth, screenHeight)) screenColor = THECOLORS['black'] xBoundary = [0, 700] yBoundary = [0, 700] lineColor = THECOLORS['white'] lineWidth = 4 drawBackground = DrawBackground(screen, screenColor, xBoundary, yBoundary, lineColor, lineWidth) FPS = 10 numBlocks = 2 wolfColor = [255, 255, 255] sheepColor = [80, 80, 80] blockColor = [200, 200, 200] circleColorSpace = [wolfColor] * numWolves + [sheepColor] * numSheep + [ blockColor ] * numBlocks viewRatio = 1.5 sheepSize = int(0.05 * screenWidth / (2 * viewRatio)) wolfSize = int(0.075 * screenWidth / (3 * viewRatio)) blockSize = int(0.2 * screenWidth / (3 * viewRatio)) circleSizeSpace = [wolfSize] * numWolves + [sheepSize] * numSheep + [ blockSize ] * numBlocks positionIndex = [0, 1] agentIdsToDraw = list(range(numWolves + numSheep + numBlocks)) saveImage = True #saveImage = False imageSavePath = os.path.join(trajectoryDirectory, 'picMovingSheep') if not os.path.exists(imageSavePath): os.makedirs(imageSavePath) imageFolderName = str('forDemo') saveImageDir = os.path.join(os.path.join(imageSavePath, imageFolderName)) if not os.path.exists(saveImageDir): os.makedirs(saveImageDir) goalSpace = list(range(numWolves, numWolves + numSheep)) imaginedWeIdsForInferenceSubject = list(range(numWolves)) softParameter = 0.9 softFunction = SoftDistribution(softParameter) updateColorSpaceByPosterior = lambda colorSpace, posterior: updateColorSpace( colorSpace, [ softFunction(individualPosterior) for individualPosterior in posterior ], goalSpace, imaginedWeIdsForInferenceSubject) #updateColorSpaceByPosterior = lambda originalColorSpace, posterior : originalColorSpace outsideCircleAgentIds = imaginedWeIdsForInferenceSubject outsideCircleColor = np.array([[255, 0, 0]] * numWolves) outsideCircleSize = int(wolfSize * 1.5) drawCircleOutside = DrawCircleOutsideEnvMADDPG(screen, viewRatio, outsideCircleAgentIds, positionIndex, outsideCircleColor, outsideCircleSize) drawState = DrawStateEnvMADDPG(FPS, screen, viewRatio, circleColorSpace, circleSizeSpace, agentIdsToDraw, positionIndex, saveImage, saveImageDir, drawBackground, updateColorSpaceByPosterior, drawCircleOutside) # MDP Env interpolateState = None stateIndexInTimeStep = 0 actionIndexInTimeStep = 1 posteriorIndexInTimeStep = 4 chaseTrial = ChaseTrialWithTraj(stateIndexInTimeStep, drawState, interpolateState, actionIndexInTimeStep, posteriorIndexInTimeStep) print(len(trajectories)) lens = [len(trajectory) for trajectory in trajectories] index = np.argsort(-np.array(lens)) print(index) print(trajectories[0][1]) #[chaseTrial(trajectory) for trajectory in np.array(trajectories)[index[16:20]]]#3v4 rational0.5, no 3, 16, 18 print([len(trajectory) for trajectory in np.array(trajectories)[index[:]]]) [ chaseTrial(trajectory) for trajectory in np.array(trajectories)[index[[3, 16, 18]]] ]
def main(): 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 = 52 softParameterInPlanningForSheep = 2.5 softParameterInPlanning = 2.5 hierarchy = 0 trajectoryFixedParameters = {'sheepPolicySoft': softParameterInPlanningForSheep, 'wolfPolicySoft': softParameterInPlanning, 'maxRunningSteps': maxRunningSteps, 'hierarchy': hierarchy, 'NNNumSimulations':NNNumSimulations} trajectoryExtension = '.pickle' getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters) # Compute Statistics on the Trajectories loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle) numWolves = 2 numSheep = 2 valuePriorEndTime = -100 valuePriorSoftMaxBeta = 0.0 trajectoryParameters = {'numWolves': numWolves, 'numSheep': numSheep, 'valuePriorEndTime': valuePriorEndTime, 'valuePriorSoftMaxBeta': valuePriorSoftMaxBeta} wolfType = 'sharedReward' #trajectoryParameters.update({'wolfType': wolfType}) trajectories = loadTrajectories(trajectoryParameters) # generate demo image screenWidth = 600 screenHeight = 600 screen = pg.display.set_mode((screenWidth, screenHeight)) screenColor = THECOLORS['black'] xBoundary = [0, 600] yBoundary = [0, 600] lineColor = THECOLORS['white'] lineWidth = 4 drawBackground = DrawBackground(screen, screenColor, xBoundary, yBoundary, lineColor, lineWidth) FPS = 32 circleColorSpace = [[100, 100, 100]]*numSheep + [[255, 255, 255]] * numWolves circleSize = 10 positionIndex = [0, 1] agentIdsToDraw = list(range(numSheep + numWolves)) saveImage = True imageSavePath = os.path.join(trajectoryDirectory, 'picMovingSheep') if not os.path.exists(imageSavePath): os.makedirs(imageSavePath) imageFolderName = str('forDemo') saveImageDir = os.path.join(os.path.join(imageSavePath, imageFolderName)) if not os.path.exists(saveImageDir): os.makedirs(saveImageDir) goalSpace = list(range(numSheep)) imaginedWeIdsForInferenceSubject = list(range(numSheep, numWolves + numSheep)) softParameter = 1 softFunction = SoftDistribution(softParameter) updateColorSpaceByPosterior = lambda colorSpace, posterior : updateColorSpace( colorSpace, [softFunction(individualPosterior) for individualPosterior in posterior], goalSpace, imaginedWeIdsForInferenceSubject) #updateColorSpaceByPosterior = lambda originalColorSpace, posterior : originalColorSpace outsideCircleAgentIds = imaginedWeIdsForInferenceSubject outsideCircleColor = np.array([[255, 0, 0]] * numWolves) outsideCircleSize = 15 drawCircleOutside = DrawCircleOutside(screen, outsideCircleAgentIds, positionIndex, outsideCircleColor, outsideCircleSize) drawState = DrawState(FPS, screen, circleColorSpace, circleSize, agentIdsToDraw, positionIndex, saveImage, saveImageDir, drawBackground, updateColorSpaceByPosterior, drawCircleOutside) # MDP Env xBoundary = [0,600] yBoundary = [0,600] stayInBoundaryByReflectVelocity = StayInBoundaryByReflectVelocity(xBoundary, yBoundary) transit = InterpolateOneFrame(stayInBoundaryByReflectVelocity) numFramesToInterpolate = 7 interpolateState = InterpolateState(numFramesToInterpolate, transit) stateIndexInTimeStep = 0 actionIndexInTimeStep = 1 posteriorIndexInTimeStep = 4 chaseTrial = ChaseTrialWithTraj(stateIndexInTimeStep, drawState, interpolateState, actionIndexInTimeStep, posteriorIndexInTimeStep) print(len(trajectories)) lens = [len(trajectory) for trajectory in trajectories] index = np.argsort(-np.array(lens)) print(index) print(trajectories[0][1]) [chaseTrial(trajectory) for trajectory in np.array(trajectories)[index[0:10]]] print([len(trajectory) for trajectory in np.array(trajectories)[index[:]]])
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(): DIRNAME = os.path.dirname(__file__) trajectoryDirectory = os.path.join(DIRNAME, '..', '..', 'data', 'evaluateGoalPerturbationHighLevel', 'trajectories') if not os.path.exists(trajectoryDirectory): os.makedirs(trajectoryDirectory) maxRunningSteps = 101 trajectoryFixedParameters = {'maxRunningStepsToSample': maxRunningSteps} trajectoryExtension = '.pickle' getTrajectorySavePath = GetSavePath(trajectoryDirectory, trajectoryExtension, trajectoryFixedParameters) # Compute Statistics on the Trajectories loadTrajectories = LoadTrajectories(getTrajectorySavePath, loadFromPickle) numWolves = 3 numSheep = 2 wolfType = 'sharedAgencyByIndividualRewardWolf' # sharedReward, sharedAgencyByIndividualRewardWolf perturbedWolfID = 0 perturbedWolfGoalID = 0 perturbed = 1 if perturbed: trajectoryParameters = {'numWolves': numWolves, 'numSheep': numSheep, 'wolfType': wolfType, 'perturbedWolfID': perturbedWolfID, 'perturbedWolfGoalID': perturbedWolfGoalID} else: trajectoryParameters = {'numWolves': numWolves, 'numSheep': numSheep, 'wolfType': wolfType} trajectories = loadTrajectories(trajectoryParameters) # generate demo image screenWidth = 700 screenHeight = 700 screen = pg.display.set_mode((screenWidth, screenHeight)) screenColor = THECOLORS['black'] xBoundary = [0, 700] yBoundary = [0, 700] lineColor = THECOLORS['white'] lineWidth = 4 drawBackground = DrawBackground(screen, screenColor, xBoundary, yBoundary, lineColor, lineWidth) FPS = 10 numBlocks = 2 wolfColor = white perturbedWolfColor = blue sheepColor = green perturbedSheepColor = blue blockColor = grey wolvesColor = [wolfColor] * numWolves wolvesColor[perturbedWolfID] = perturbedWolfColor if perturbed else wolfColor sheepColorList = [sheepColor] * numSheep sheepColorList[perturbedWolfGoalID] = perturbedSheepColor if perturbed else sheepColor circleColorSpace = wolvesColor + sheepColorList + [blockColor] * numBlocks sheepSize = int(0.05 * screenWidth/2) wolfSize = int(0.075 * screenWidth/2) blockSize = int(0.2 * screenWidth/2) circleSizeSpace = [wolfSize] * numWolves + [sheepSize] * numSheep + [blockSize] * numBlocks positionIndex = [0, 1] agentIdsToDraw = list(range(numWolves + numSheep + numBlocks)) saveImage = False imageSavePath = os.path.join(trajectoryDirectory, 'picMovingSheep') if not os.path.exists(imageSavePath): os.makedirs(imageSavePath) imageFolderName = str('forDemo') saveImageDir = os.path.join(os.path.join(imageSavePath, imageFolderName)) if not os.path.exists(saveImageDir): os.makedirs(saveImageDir) softParameter = 0.05 softFunction = SoftPolicy(softParameter) updateColorSpace = UpdateColorSpace(perturbedWolfID, list(range(numWolves)), circleColorSpace) updateColorSpaceByPosterior = lambda posterior: updateColorSpace([softFunction(individualPosterior) for individualPosterior in posterior]) outsideCircleAgentIds = list(range(numWolves)) outsideCircleColor = np.array([red] * numWolves) outsideCircleSize = int(wolfSize * 1.5) viewRatio = 1 drawCircleOutside = DrawCircleOutsideEnvMADDPG(screen, viewRatio, outsideCircleAgentIds, positionIndex, outsideCircleColor, outsideCircleSize) drawState = DrawStateEnvMADDPG(FPS, screen, viewRatio, circleColorSpace, circleSizeSpace, agentIdsToDraw, positionIndex, saveImage, saveImageDir, drawBackground, updateColorSpaceByPosterior, drawCircleOutside) # MDP Env interpolateState = None stateIndexInTimeStep = 0 actionIndexInTimeStep = 1 posteriorIndexInTimeStep = 4 chaseTrial = ChaseTrialWithTraj(stateIndexInTimeStep, drawState, interpolateState, actionIndexInTimeStep, posteriorIndexInTimeStep) [chaseTrial(trajectory) for trajectory in np.array(trajectories)[list(range(5))]]
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()