Exemple #1
0
def main():
    numOfAgent = 4

    manipulatedVariables = co.OrderedDict()
    # manipulatedVariables['damping'] = [0.0]  # [0.0, 1.0]
    # manipulatedVariables['frictionloss'] = [0.0]  # [0.0, 0.2, 0.4]
    # manipulatedVariables['masterForce'] = [0.0]
    manipulatedVariables['damping'] = [0.0, 0.5]  # [0.0, 1.0]
    manipulatedVariables['frictionloss'] = [1.0]  # [0.0, 0.2, 0.4]
    manipulatedVariables['masterForce'] = [0.0, 1.0]  # [0.0, 2.0]

    chaseTrailVariables = manipulatedVariables.copy()
    catchTrailVariables = manipulatedVariables.copy()
    chaseTrailVariables['hideId'] = [3, 4
                                     ]  #0 wolf 1 sheep 2 master 3 4 distractor
    catchTrailVariables['hideId'] = [1]
    chaseTrailconditions = [
        dict(list(specificValueParameter))
        for specificValueParameter in it.product(
            *[[(key, value) for value in values]
              for key, values in chaseTrailVariables.items()])
    ]
    # chaseTrailconditionsWithId =list (zip(range(len(conditions)),conditions ))
    catchTrailconditions = [
        dict(list(specificValueParameter))
        for specificValueParameter in it.product(
            *[[(key, value) for value in values]
              for key, values in catchTrailVariables.items()])
    ]
    # catchTrailconditionsWithId =list (zip(range(len(conditions)),conditions ))
    # print('state',chaseTrailVariables,catchTrailVariables)
    conditionsWithId = list(
        zip(range(len(chaseTrailconditions) + len(catchTrailconditions)),
            chaseTrailconditions + catchTrailconditions))
    # print(conditionsWithId)
    # conditions = [dict(list(specificValueParameter)) for specificValueParameter in it.product(*[[(key, value) for value in values] for key, values in manipulatedVariables.items()])]
    # conditionsWithId =list (zip(range(len(conditions)),conditions ))
    # print(conditionsWithId)
    conditions = chaseTrailconditions + catchTrailconditions
    conditions = [
        condition.update({'conditionId': condtionId})
        for condtionId, condition in zip(range(len(conditions)), conditions)
    ]
    # print (conditions)

    chaseTrailTrajetoryIndexList = range(2)
    chaseTrailManipulatedVariablesForExp = co.OrderedDict()
    chaseTrailManipulatedVariablesForExp['conditonId'] = range(
        len(chaseTrailconditions))
    chaseTrailManipulatedVariablesForExp[
        'trajetoryIndex'] = chaseTrailTrajetoryIndexList
    chaseTrailProductedValues = it.product(
        *[[(key, value) for value in values]
          for key, values in chaseTrailManipulatedVariablesForExp.items()])

    catchTrailTrajetoryIndexList = range(1)
    catchTrailManipulatedVariablesForExp = co.OrderedDict()
    catchTrailManipulatedVariablesForExp['conditonId'] = range(
        len(chaseTrailconditions),
        len(chaseTrailconditions) + len(catchTrailconditions))
    catchTrailManipulatedVariablesForExp[
        'trajetoryIndex'] = catchTrailTrajetoryIndexList
    catchTrailProductedValues = it.product(
        *[[(key, value) for value in values]
          for key, values in catchTrailManipulatedVariablesForExp.items()])

    # print(productedValues)
    exprimentVarableList = [
        dict(list(specificValueParameter))
        for specificValueParameter in chaseTrailProductedValues
    ] + [
        dict(list(specificValueParameter))
        for specificValueParameter in catchTrailProductedValues
    ]

    [
        exprimentVarable.update(
            {'condition': conditionsWithId[exprimentVarable['conditonId']][1]})
        for exprimentVarable in exprimentVarableList
    ]
    # exprimentVarableList = [exprimentVarable.update({'condition': conditionsWithId[exprimentVarable['conditonId']][1]}) for exprimentVarable in exprimentVarableList ]
    print(exprimentVarableList)
    print(len(exprimentVarableList))
    numOfBlock = 1
    numOfTrialsPerBlock = 1
    designValues = createDesignValues(
        exprimentVarableList * numOfTrialsPerBlock, numOfBlock)

    positionIndex = [0, 1]
    FPS = 50
    rawXRange = [-1, 1]
    rawYRange = [-1, 1]
    scaledXRange = [200, 600]
    scaledYRange = [200, 600]
    scaleTrajectoryInSpace = ScaleTrajectory(positionIndex, rawXRange,
                                             rawYRange, scaledXRange,
                                             scaledYRange)
    oldFPS = 50
    numFramesToInterpolate = int(FPS / oldFPS - 1)
    interpolateState = InterpolateState(numFramesToInterpolate)

    scaleTrajectoryInTime = ScaleTrajectoryInTime(interpolateState)

    trajectoriesSaveDirectory = '../PataData/mujoco50Fps4Agent'
    # trajectoriesSaveDirectory =os.path.join(dataFolder, 'trajectory', modelSaveName)
    trajectorySaveExtension = '.pickle'

    evaluateEpisode = 120000
    evalNum = 20
    fixedParameters = {
        'distractorNoise': 3.0,
        'evaluateEpisode': evaluateEpisode
    }
    generateTrajectoryLoadPath = GetSavePath(trajectoriesSaveDirectory,
                                             trajectorySaveExtension,
                                             fixedParameters)
    trajectoryDf = lambda condition: pd.read_pickle(
        generateTrajectoryLoadPath({
            'hideId': condition['hideId'],
            'damping': condition['damping'],
            'frictionloss': condition['frictionloss'],
            'masterForce': condition['masterForce'],
            'evalNum': evalNum,
        }))
    # trajectoryDf = lambda condition: pd.read_pickle(generateTrajectoryLoadPath(condition))
    getTrajectory = lambda trajectoryDf: scaleTrajectoryInTime(
        scaleTrajectoryInSpace(trajectoryDf))
    # getTrajectory = lambda trajectoryDf: scaleTrajectoryInSpace(trajectoryDf)

    print('loading')
    stimulus = {
        conditionId: getTrajectory(trajectoryDf(condition))
        for conditionId, condition in conditionsWithId
    }
    print(stimulus[0][0][0])
    print(stimulus[10][0][0])
    print('loding success')

    # print(stimulus[1][1])
    experimentValues = co.OrderedDict()
    experimentValues["name"] = input("Please enter your name:").capitalize()

    screenWidth = 800
    screenHeight = 800

    fullScreen = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()

    leaveEdgeSpace = 200
    circleSize = 10
    clickImageHeight = 80
    lineWidth = 3
    fontSize = 50
    xBoundary = [leaveEdgeSpace, screenWidth - leaveEdgeSpace * 2]
    yBoundary = [leaveEdgeSpace, screenHeight - leaveEdgeSpace * 2]

    screenColor = THECOLORS['black']
    lineColor = THECOLORS['white']
    textColor = THECOLORS['white']
    fixationPointColor = THECOLORS['white']

    colorSpace = [(203, 164, 4, 255), (49, 153, 0, 255), (255, 90, 16, 255),
                  (251, 7, 255, 255), (9, 204, 172, 255), (3, 28, 255, 255)]

    picturePath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'pictures')
    resultsPath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'results')

    introductionImage1 = pygame.image.load(
        os.path.join(picturePath, 'mujocoIntro1.png'))
    introductionImage2 = pygame.image.load(
        os.path.join(picturePath, 'mujocoIntro2.png'))
    finishImage = pygame.image.load(os.path.join(picturePath, 'over.jpg'))
    introductionImage1 = pygame.transform.scale(introductionImage1,
                                                (screenWidth, screenHeight))
    introductionImage2 = pygame.transform.scale(introductionImage2,
                                                (screenWidth, screenHeight))

    finishImage = pygame.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    clickWolfImage = pygame.image.load(
        os.path.join(picturePath, 'clickwolf.png'))
    clickSheepImage = pygame.image.load(
        os.path.join(picturePath, 'clicksheep.png'))
    restImage = pygame.image.load(os.path.join(picturePath, 'rest.jpg'))

    drawImage = DrawImage(screen)
    drawText = DrawText(screen, fontSize, textColor)
    drawBackGround = DrawBackGround(screen, screenColor, xBoundary, yBoundary,
                                    lineColor, lineWidth)
    drawFixationPoint = DrawFixationPoint(screen, drawBackGround,
                                          fixationPointColor)
    drawImageClick = DrawImageClick(screen, clickImageHeight, drawText)

    drawState = DrawState(screen, circleSize, numOfAgent, positionIndex,
                          drawBackGround)
    # drawStateWithRope = DrawStateWithRope(screen, circleSize, numOfAgent, positionIndex, ropeColor, drawBackground)

    writerPath = os.path.join(resultsPath, experimentValues["name"]) + '.csv'
    writer = WriteDataFrameToCSV(writerPath)

    displayFrames = 500
    keysForCheck = {'f': 0, 'j': 1}
    checkHumanResponse = CheckHumanResponse(keysForCheck)
    trial = ChaseTrialMujoco(conditionsWithId, displayFrames, drawState,
                             drawImage, stimulus, checkHumanResponse,
                             colorSpace, numOfAgent, drawFixationPoint,
                             drawText, drawImageClick, clickWolfImage,
                             clickSheepImage, FPS)

    experiment = Experiment(trial, writer, experimentValues, drawImage,
                            restImage, drawBackGround)

    restDuration = 20

    drawImage(introductionImage1)
    drawImage(introductionImage2)

    experiment(designValues, restDuration)
    # self.darwBackground()
    drawImage(finishImage)

    print("Result saved at {}".format(writerPath))
Exemple #2
0
def main():
    experimentValues = co.OrderedDict()
    experimentValues["name"] = 'test'
    # experimentValues["name"] = input("Please enter your name:").capitalize()
    fullScreen = 0 if experimentValues["name"] == 'test' else 1
    mouseVisible = 1 if experimentValues["name"] == 'test' else 0

    screenWidth = 600
    screenHeight = 600
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    pg.mouse.set_visible(mouseVisible)

    gridSize = 15
    bounds = [0, 0, gridSize - 1, gridSize - 1]

    leaveEdgeSpace = 1  # 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    picturePath = os.path.abspath(os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(os.path.join(os.path.join(os.getcwd(), os.pardir), 'resultsNoTimePressure'))
    if not os.path.exists(resultsPath):
        os.mkdir(resultsPath)

    formalTrialImage = pg.image.load(os.path.join(picturePath, 'NoTimePressureExp.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    restImage = pg.image.load(os.path.join(picturePath, 'rest.png'))

    formalTrialImage = pg.transform.scale(formalTrialImage, (screenWidth, screenHeight))
    restImage = pg.transform.scale(restImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    finishImage = pg.transform.scale(finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))

    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace, backgroundColor, lineColor, lineWidth, textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor, playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

# condition maps
    condition = namedtuple('condition', 'name decisionSteps initAgent avoidCommitPoint crossPoint targetDisToCrossPoint fixedObstacles')

    map1ObsStep0a = condition(name='expCondition1', decisionSteps=0, initAgent=(0, 2), avoidCommitPoint=(1, 2), crossPoint=(3, 3), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep0a = condition(name='expCondition2', decisionSteps=0, initAgent=(0, 2), avoidCommitPoint=(1, 2), crossPoint=(4, 4), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4), (4, 1)])

    map1ObsStep0b = condition(name='expCondition1', decisionSteps=0, initAgent=(2, 0), avoidCommitPoint=(2, 1), crossPoint=(3, 3), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep0b = condition(name='expCondition2', decisionSteps=0, initAgent=(2, 0), avoidCommitPoint=(2, 1), crossPoint=(4, 4), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4), (4, 1)])

    map1ObsStep1a = condition(name='expCondition1', decisionSteps=1, initAgent=(1, 0), avoidCommitPoint=(2, 1), crossPoint=(3, 3), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep1a = condition(name='expCondition2', decisionSteps=1, initAgent=(1, 0), avoidCommitPoint=(2, 1), crossPoint=(4, 4), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4), (4, 1)])

    map1ObsStep1b = condition(name='expCondition1', decisionSteps=1, initAgent=(0, 1), avoidCommitPoint=(1, 2), crossPoint=(3, 3), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep1b = condition(name='expCondition2', decisionSteps=1, initAgent=(0, 1), avoidCommitPoint=(1, 2), crossPoint=(4, 4), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4), (4, 1)])

    map1ObsStep2 = condition(name='expCondition1', decisionSteps=2, initAgent=(0, 0), avoidCommitPoint=(2, 2), crossPoint=(3, 3), targetDisToCrossPoint=[4, 5, 6], fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep2 = condition(name='expCondition2', decisionSteps=2, initAgent=(0, 0), avoidCommitPoint=(2, 2), crossPoint=(4, 4), targetDisToCrossPoint=[4, 5, 6], fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4), (4, 1)])

    map1ObsStep4 = condition(name='expCondition1', decisionSteps=4, initAgent=(0, 0), avoidCommitPoint=(3, 3), crossPoint=(4, 4), targetDisToCrossPoint=[4, 5, 6], fixedObstacles=[(2, 2), (2, 0), (2, 4), (0, 2), (4, 2)])

    map2ObsStep4 = condition(name='expCondition2', decisionSteps=4, initAgent=(0, 0), avoidCommitPoint=(3, 3), crossPoint=(5, 5), targetDisToCrossPoint=[4, 5, 6], fixedObstacles=[(2, 2), (2, 0), (2, 4), (0, 2), (4, 2), (2, 5), (5, 2)])

    map1ObsStep6 = condition(name='expCondition1', decisionSteps=6, initAgent=(0, 0), avoidCommitPoint=(4, 4), crossPoint=(5, 5), targetDisToCrossPoint=[4, 5, 6], fixedObstacles=[(3, 3), (4, 0), (3, 1), (3, 5), (5, 3), (1, 3), (0, 4)])

    map2ObsStep6 = condition(name='expCondition2', decisionSteps=6, initAgent=(0, 0), avoidCommitPoint=(4, 4), crossPoint=(6, 6), targetDisToCrossPoint=[4, 5, 6], fixedObstacles=[(3, 3), (4, 0), (3, 1), (3, 5), (5, 3), (1, 3), (0, 4), (3, 6), (6, 3), ])

    specialCondition = condition(name='specialCondition', decisionSteps=0, initAgent=(0, 0), avoidCommitPoint=[-1, -1], crossPoint=(5, 5), targetDisToCrossPoint=[5], fixedObstacles=[(3, 0), (0, 3), (1, 4), (1, 6), (4, 1), (6, 1), (6, 2), (2, 6), (5, 3), (3, 5)])

    controlCondition1 = condition(name='controlCondition', decisionSteps=0, initAgent=(0, 0), avoidCommitPoint=[-1, -1], crossPoint=(5, 5), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(3, 0), (0, 3), (1, 4), (1, 6), (4, 1), (6, 1), (6, 2), (2, 6), (5, 3), (3, 5)])

    controlCondition2 = condition(name='controlCondition', decisionSteps=0, initAgent=(0, 0), avoidCommitPoint=[-1, -1], crossPoint=(5, 5), targetDisToCrossPoint=[5, 6, 7], fixedObstacles=[(3, 0), (0, 3), (1, 4), (5, 3), (4, 1), (3, 5), (2, 2), (6, 1), (6, 2), (1, 6), (2, 6), (5, 3)])

    numOfObstacles = 18
    controlDiffList = [0, 1, 2]
    minSteps = 8
    maxsteps = 13
    minDistanceBetweenTargets = 4

    minDistanceBetweenGrids = max(controlDiffList) + 1
    maxDistanceBetweenGrids = calculateMaxDistanceOfGrid(bounds) - minDistanceBetweenGrids
    randomWorld = RandomWorld(bounds, minDistanceBetweenGrids, maxDistanceBetweenGrids, numOfObstacles)
    randomCondition = namedtuple('condition', 'name creatMap decisionSteps minSteps maxsteps minDistanceBetweenTargets controlDiffList')
    randomMaps = randomCondition(name='randomCondition', creatMap=randomWorld, decisionSteps=0, minSteps=minSteps, maxsteps=maxsteps, minDistanceBetweenTargets=minDistanceBetweenTargets, controlDiffList=controlDiffList)

    conditionList = [map1ObsStep0a, map1ObsStep0b, map1ObsStep1a, map1ObsStep1b, controlCondition1] + [map1ObsStep2, map1ObsStep4, map1ObsStep6] * 2 + [map2ObsStep0a, map2ObsStep0b, map2ObsStep1a, map2ObsStep1b, controlCondition2] + [map2ObsStep2, map2ObsStep4, map2ObsStep6] * 2 + [randomMaps] * 2
    targetDiffsList = [0, 1, 2, 'controlAvoid']

    # conditionList = [randomMaps] * 10
    # targetDiffsList = ['controlAvoid']

    numBlocks = 3
    expDesignValues = [[condition, diff] for condition in conditionList for diff in targetDiffsList] * numBlocks
    numExpTrial = len(expDesignValues)

    numNormalTrial = len(expDesignValues)

    random.shuffle(expDesignValues)
    specialDesign = [specialCondition, 0]
    expDesignValues.append(specialDesign)

    numTrialsPerNoiseBlock = 3
    noiseCondition = list(permutations([1, 2, 0], numTrialsPerNoiseBlock)) + [(1, 1, 1)]
    blockNumber = int(numNormalTrial / numTrialsPerNoiseBlock)
    noiseDesignValues = createNoiseDesignValue(noiseCondition, blockNumber)
    # noiseDesignValues = [0] * numNormalTrials

# deubg
    # expDesignValues = [specialDesign] * 2
    # noiseDesignValues = ['special'] * 2
# debugs

    print('trial:', len(expDesignValues))
    if len(expDesignValues) != len(noiseDesignValues):
        print(len(noiseDesignValues))
        raise Exception("unmatch condition design")

    writerPath = os.path.join(resultsPath, experimentValues["name"] + '_noTime' + '.csv')
    writer = WriteDataFrameToCSV(writerPath)

    rotatePoint = RotatePoint(gridSize)
    isInBoundary = IsInBoundary([0, gridSize - 1], [0, gridSize - 1])

    rotateAngles = [0, 90, 180, 270]
    creatMap = CreatMap(rotateAngles, gridSize, rotatePoint, numOfObstacles)

    pygameActionDict = {pg.K_UP: (0, -1), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0), pg.K_RIGHT: (1, 0)}

    responseTimeLimits = 8000
    humanController = HumanController(pygameActionDict, responseTimeLimits)
    controller = humanController

    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0), (1, 1), (1, -1), (-1, -1), (-1, 1)]
    # noiseActionSpace = [(0, 0)]

    normalNoise = AimActionWithNoise(noiseActionSpace, gridSize)
    specialNoise = backToCrossPointNoise

    normalTrial = NormalTrial(controller, drawNewState, drawText, normalNoise, checkBoundary)
    specialTrial = SpecialTrial(controller, drawNewState, drawText, specialNoise, checkBoundary)

    restTrialInterval = math.ceil(numExpTrial / 4)
    restTrialIndex = list(range(restTrialInterval, numExpTrial, restTrialInterval))

    experiment = ObstacleExperiment(creatMap, normalTrial, specialTrial, writer, experimentValues, drawImage, restTrialIndex, restImage)

# start exp
    drawImage(formalTrialImage)
    experiment(noiseDesignValues, expDesignValues)
    drawImage(finishImage)
Exemple #3
0
def main():
    picturePath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))
    machinePolicyPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'machinePolicy'))
    dataPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'conditionData'))

    gridSize = 15
    bounds = [0, 0, gridSize - 1, gridSize - 1]

    screenWidth = 600
    screenHeight = 600
    fullScreen = False
    renderOn = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    pg.mouse.set_visible(False)

    leaveEdgeSpace = 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    introductionImage = pg.image.load(
        os.path.join(picturePath, 'introduction.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    introductionImage = pg.transform.scale(introductionImage,
                                           (screenWidth, screenHeight))
    finishImage = pg.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

    # condition maps
    condition = namedtuple(
        'condition',
        'name decisionSteps initAgent avoidCommitPoint crossPoint targetDisToCrossPoint fixedObstacles'
    )

    map1ObsStep0a = condition(name='expCondition1',
                              decisionSteps=0,
                              initAgent=(0, 2),
                              avoidCommitPoint=(1, 2),
                              crossPoint=(3, 3),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep0a = condition(name='expCondition2',
                              decisionSteps=0,
                              initAgent=(0, 2),
                              avoidCommitPoint=(1, 2),
                              crossPoint=(4, 4),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4),
                                              (4, 1)])

    map1ObsStep0b = condition(name='expCondition1',
                              decisionSteps=0,
                              initAgent=(2, 0),
                              avoidCommitPoint=(2, 1),
                              crossPoint=(3, 3),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep0b = condition(name='expCondition2',
                              decisionSteps=0,
                              initAgent=(2, 0),
                              avoidCommitPoint=(2, 1),
                              crossPoint=(4, 4),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4),
                                              (4, 1)])

    map1ObsStep1a = condition(name='expCondition1',
                              decisionSteps=1,
                              initAgent=(1, 0),
                              avoidCommitPoint=(2, 1),
                              crossPoint=(3, 3),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep1a = condition(name='expCondition2',
                              decisionSteps=1,
                              initAgent=(1, 0),
                              avoidCommitPoint=(2, 1),
                              crossPoint=(4, 4),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4),
                                              (4, 1)])

    map1ObsStep1b = condition(name='expCondition1',
                              decisionSteps=1,
                              initAgent=(0, 1),
                              avoidCommitPoint=(1, 2),
                              crossPoint=(3, 3),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep1b = condition(name='expCondition2',
                              decisionSteps=1,
                              initAgent=(0, 1),
                              avoidCommitPoint=(1, 2),
                              crossPoint=(4, 4),
                              targetDisToCrossPoint=[5, 6, 7],
                              fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4),
                                              (4, 1)])

    map1ObsStep2 = condition(name='expCondition1',
                             decisionSteps=2,
                             initAgent=(0, 0),
                             avoidCommitPoint=(2, 2),
                             crossPoint=(3, 3),
                             targetDisToCrossPoint=[4, 5, 6],
                             fixedObstacles=[(1, 1), (1, 3), (3, 1)])

    map2ObsStep2 = condition(name='expCondition2',
                             decisionSteps=2,
                             initAgent=(0, 0),
                             avoidCommitPoint=(2, 2),
                             crossPoint=(4, 4),
                             targetDisToCrossPoint=[4, 5, 6],
                             fixedObstacles=[(1, 1), (1, 3), (3, 1), (1, 4),
                                             (4, 1)])

    map1ObsStep4 = condition(name='expCondition1',
                             decisionSteps=4,
                             initAgent=(0, 0),
                             avoidCommitPoint=(3, 3),
                             crossPoint=(4, 4),
                             targetDisToCrossPoint=[4, 5, 6],
                             fixedObstacles=[(2, 2), (2, 0), (2, 4), (0, 2),
                                             (4, 2)])

    map2ObsStep4 = condition(name='expCondition2',
                             decisionSteps=4,
                             initAgent=(0, 0),
                             avoidCommitPoint=(3, 3),
                             crossPoint=(5, 5),
                             targetDisToCrossPoint=[4, 5, 6],
                             fixedObstacles=[(2, 2), (2, 0), (2, 4), (0, 2),
                                             (4, 2), (2, 5), (5, 2)])

    map1ObsStep6 = condition(name='expCondition1',
                             decisionSteps=6,
                             initAgent=(0, 0),
                             avoidCommitPoint=(4, 4),
                             crossPoint=(5, 5),
                             targetDisToCrossPoint=[4, 5, 6],
                             fixedObstacles=[(3, 3), (4, 0), (3, 1), (3, 5),
                                             (5, 3), (1, 3), (0, 4)])

    map2ObsStep6 = condition(name='expCondition2',
                             decisionSteps=6,
                             initAgent=(0, 0),
                             avoidCommitPoint=(4, 4),
                             crossPoint=(6, 6),
                             targetDisToCrossPoint=[4, 5, 6],
                             fixedObstacles=[
                                 (3, 3),
                                 (4, 0),
                                 (3, 1),
                                 (3, 5),
                                 (5, 3),
                                 (1, 3),
                                 (0, 4),
                                 (3, 6),
                                 (6, 3),
                             ])

    specialCondition = condition(name='specialCondition',
                                 decisionSteps=0,
                                 initAgent=(0, 0),
                                 avoidCommitPoint=[-1, -1],
                                 crossPoint=(5, 5),
                                 targetDisToCrossPoint=[5],
                                 fixedObstacles=[
                                     (3, 0), (0, 3), (1, 4), (1, 6), (4, 1),
                                     (6, 1), (6, 2), (2, 6), (5, 3), (3, 5)
                                 ])

    controlCondition1 = condition(name='controlCondition',
                                  decisionSteps=0,
                                  initAgent=(0, 0),
                                  avoidCommitPoint=[-1, -1],
                                  crossPoint=(5, 5),
                                  targetDisToCrossPoint=[5, 6, 7],
                                  fixedObstacles=[
                                      (3, 0), (0, 3), (1, 4), (1, 6), (4, 1),
                                      (6, 1), (6, 2), (2, 6), (5, 3), (3, 5)
                                  ])

    controlCondition2 = condition(name='controlCondition',
                                  decisionSteps=0,
                                  initAgent=(0, 0),
                                  avoidCommitPoint=[-1, -1],
                                  crossPoint=(5, 5),
                                  targetDisToCrossPoint=[5, 6, 7],
                                  fixedObstacles=[
                                      (3, 0), (0, 3), (1, 4), (5, 3), (4, 1),
                                      (3, 5), (2, 2), (6, 1), (6, 2), (1, 6),
                                      (2, 6), (5, 3)
                                  ])

    numOfObstacles = 18
    controlDiffList = [0, 1, 2]
    minSteps = 8
    maxsteps = 13
    minDistanceBetweenTargets = 4

    minDistanceBetweenGrids = max(controlDiffList) + 1
    maxDistanceBetweenGrids = calculateMaxDistanceOfGrid(
        bounds) - minDistanceBetweenGrids
    randomWorld = RandomWorld(bounds, minDistanceBetweenGrids,
                              maxDistanceBetweenGrids, numOfObstacles)
    randomCondition = namedtuple(
        'condition',
        'name creatMap decisionSteps minSteps maxsteps minDistanceBetweenTargets controlDiffList'
    )
    randomMaps = randomCondition(
        name='randomCondition',
        creatMap=randomWorld,
        decisionSteps=0,
        minSteps=minSteps,
        maxsteps=maxsteps,
        minDistanceBetweenTargets=minDistanceBetweenTargets,
        controlDiffList=controlDiffList)

    # conditionList = [map1ObsStep0a, map1ObsStep0b, map1ObsStep1a, map1ObsStep1b, controlCondition1] + [map1ObsStep2, map1ObsStep4, map1ObsStep6] * 2 + [map2ObsStep0a, map2ObsStep0b, map2ObsStep1a, map2ObsStep1b, controlCondition2] + [map2ObsStep2, map2ObsStep4, map2ObsStep6] * 2 + [randomMaps] * 2
    # targetDiffsList = [0, 1, 2, 'controlAvoid']

    conditionList = [
        map1ObsStep0a, map1ObsStep0b, map1ObsStep1a, map1ObsStep1b,
        controlCondition1
    ] + [map1ObsStep2, map1ObsStep4, map1ObsStep6] * 2 + [
        map2ObsStep0a, map2ObsStep0b, map2ObsStep1a, map2ObsStep1b,
        controlCondition2
    ] + [map2ObsStep2, map2ObsStep4, map2ObsStep6] * 2
    targetDiffsList = [0]

    # conditionList = [map1ObsStep0a] + [randomMaps] * 2

    n = 1
    numBlocks = 3 * n
    expDesignValues = [[condition, diff] for condition in conditionList
                       for diff in targetDiffsList] * numBlocks
    numExpTrial = len(expDesignValues)

    # conditionList = [condition2]
    specialDesign = [specialCondition, 0]

    numTrialsPerNoiseBlock = 3
    noiseCondition = list(permutations([1, 2, 0],
                                       numTrialsPerNoiseBlock)) + [(1, 1, 1)]
    blockNumber = int(numExpTrial / numTrialsPerNoiseBlock)
    noiseDesignValues = createNoiseDesignValue(noiseCondition, blockNumber)

    rotatePoint = RotatePoint(gridSize)
    isInBoundary = IsInBoundary([0, gridSize - 1], [0, gridSize - 1])

    rotateAngles = [0, 90, 180, 270]
    creatMap = CreatMap(rotateAngles, gridSize, rotatePoint, numOfObstacles)

    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0), (1, 1), (1, -1),
                        (-1, -1), (-1, 1)]

    normalNoise = AimActionWithNoise(noiseActionSpace, gridSize)
    specialNoise = backToCrossPointNoise

    # inferGoalPosterior = InferGoalPosterior(goalPolicy)

    softmaxBetaList = [2.5]
    noise = 0.067
    gamma = 0.9
    goalReward = [30, 30]
    actionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0)]
    runVI = RunVI(gridSize, actionSpace, noiseActionSpace, noise, gamma,
                  goalReward)

    intentionInfoScale = [-0.5, 0.5]

    for softmaxBeta in softmaxBetaList:
        for i in range(20):
            print(i)

            expDesignValues = [[condition, diff] for condition in conditionList
                               for diff in targetDiffsList] * numBlocks
            random.shuffle(expDesignValues)
            expDesignValues.append(specialDesign)

            runModel = RunIntentionModel(runVI, softmaxBeta,
                                         intentionInfoScale)

            # modelController = ModelControllerOnline(softmaxBeta)
            # modelController = AvoidCommitModel(softmaxBeta, actionSpace, checkBoundary)
            controller = SampleSoftmaxAction(softmaxBeta)

            renderOn = 1
            normalTrial = NormalTrialOnline(renderOn, controller, drawNewState,
                                            drawText, normalNoise,
                                            checkBoundary)
            specialTrial = SpecialTrialOnline(renderOn, controller,
                                              drawNewState, drawText,
                                              specialNoise, checkBoundary)

            experimentValues = co.OrderedDict()
            experimentValues["name"] = "noise" + str(
                noise) + '_' + "intentionInfoScale" + str(
                    intentionInfoScale[1]) + '_' + str(i)
            # resultsDirPath = os.path.join(resultsPath, "Intention-" + "noise" + str(noise) + '_' + "softmaxBeta" + str(softmaxBeta))

            resultsDirPath = os.path.join(resultsPath, "showIntention3")

            if not os.path.exists(resultsDirPath):
                os.mkdir(resultsDirPath)

            writerPath = os.path.join(resultsDirPath,
                                      experimentValues["name"] + '.csv')
            writer = WriteDataFrameToCSV(writerPath)
            experiment = IntentionModelSimulation(creatMap, normalTrial,
                                                  specialTrial, writer,
                                                  experimentValues, drawImage,
                                                  resultsPath, runModel)
            experiment(noiseDesignValues, expDesignValues)
Exemple #4
0
def main():
    picturePath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))
    machinePolicyPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'machinePolicy'))
    dataPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'conditionData'))
    df = pd.read_csv(
        os.path.join(dataPath, 'DesignConditionForAvoidCommitmentZone.csv'))
    df['intentionedDisToTargetMin'] = df.apply(
        lambda x: x['minDis'] - x['avoidCommitmentZone'], axis=1)

    gridSize = 15

    screenWidth = 600
    screenHeight = 600
    fullScreen = False
    renderOn = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    pg.mouse.set_visible(False)

    leaveEdgeSpace = 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    introductionImage = pg.image.load(
        os.path.join(picturePath, 'introduction.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    introductionImage = pg.transform.scale(introductionImage,
                                           (screenWidth, screenHeight))
    finishImage = pg.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

    width = [3, 4, 5]
    height = [3, 4, 5]
    intentionDis = [2, 4, 6]
    direction = [45, 135, 225, 315]

    distanceDiffList = [0, 2, 4]
    minDisList = range(5, 15)
    intentionedDisToTargetList = [2, 4, 6]
    rectAreaSize = [6, 8, 10, 12, 14, 16, 18, 20, 25, 30, 36]
    lineAreaSize = [4, 5, 6, 7, 8, 9, 10]

    condition = namedtuple(
        'condition',
        'name areaType distanceDiff minDis areaSize intentionedDisToTarget')

    expCondition = condition(name='expCondition',
                             areaType='rect',
                             distanceDiff=[0],
                             minDis=minDisList,
                             areaSize=rectAreaSize,
                             intentionedDisToTarget=intentionedDisToTargetList)
    rectCondition = condition(
        name='controlRect',
        areaType='rect',
        distanceDiff=[2, 4],
        minDis=minDisList,
        areaSize=rectAreaSize,
        intentionedDisToTarget=intentionedDisToTargetList)
    straightLineCondition = condition(
        name='straightLine',
        areaType='straightLine',
        distanceDiff=distanceDiffList,
        minDis=minDisList,
        areaSize=lineAreaSize,
        intentionedDisToTarget=intentionedDisToTargetList)
    midLineCondition = condition(
        name='MidLine',
        areaType='midLine',
        distanceDiff=distanceDiffList,
        minDis=minDisList,
        areaSize=lineAreaSize,
        intentionedDisToTarget=intentionedDisToTargetList)
    noAreaCondition = condition(
        name='noArea',
        areaType='none',
        distanceDiff=distanceDiffList,
        minDis=minDisList,
        areaSize=[0],
        intentionedDisToTarget=intentionedDisToTargetList)

    # Q_dict = pickle.load(open(os.path.join(machinePolicyPath, "noise0.1commitAreaGird15_policy.pkl"), "rb"))

    goal_Q_dict = pickle.load(
        open(
            os.path.join(machinePolicyPath,
                         "noise0.1commitAreaGoalGird15_policy.pkl"), "rb"))

    actionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0)]
    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -2), (0, 2), (-2, 0), (2, 0), (1, 1), (1, -1),
                        (-1, -1), (-1, 1)]
    normalNoise = NormalNoise(noiseActionSpace, gridSize)
    sampleToZoneNoise = SampleToZoneNoise(noiseActionSpace)

    initPrior = [0.5, 0.5]

    # softmaxBeta = 2.5
    priorBeta = 5

    commitBetaList = np.arange(1, 10, 1)

    rewardVarianceList = [50]
    softmaxBetaList = np.round(np.arange(0.4, 0.5, 0.01), decimals=2)
    softmaxBetaList = [10]
    print(softmaxBetaList)

    for softmaxBeta in softmaxBetaList:
        # goalPolicy = SoftmaxGoalPolicy(goal_Q_dict, softmaxBeta)
        # policy = SoftmaxRLPolicy(Q_dict, softmaxBeta)
        # for commitBeta in commitBetaList:
        for i in range(30):
            print(i)
            expDesignValues = [[b, h, d] for b in width for h in height
                               for d in intentionDis]
            numExpTrial = len(expDesignValues)
            random.shuffle(expDesignValues)
            expDesignValues.append(random.choice(expDesignValues))
            createExpCondition = CreatExpCondition(direction, gridSize)
            samplePositionFromCondition = SamplePositionFromCondition(
                df, createExpCondition, expDesignValues)
            numExpTrial = len(expDesignValues) - 1
            numControlTrial = int(numExpTrial * 2 / 3)
            expTrials = [expCondition] * numExpTrial
            conditionList = list(expTrials + [rectCondition] * numExpTrial +
                                 [straightLineCondition] * numControlTrial +
                                 [midLineCondition] * numControlTrial +
                                 [noAreaCondition] * numControlTrial)
            numNormalTrials = len(conditionList)

            random.shuffle(conditionList)
            conditionList.append(expCondition)

            numTrialsPerBlock = 3
            noiseCondition = list(permutations([1, 2, 0], numTrialsPerBlock))
            noiseCondition.append((1, 1, 1))
            blockNumber = int(numNormalTrials / numTrialsPerBlock)
            noiseDesignValues = createNoiseDesignValue(noiseCondition,
                                                       blockNumber)

            # deubg
            # conditionList = [expCondition] * 27
            # noiseDesignValues = ['special'] * 27

            # model simulation
            noise = 0.1
            gamma = 0.9
            goalReward = 10
            runVI = RunVI(gridSize, actionSpace, noiseActionSpace, noise,
                          gamma, goalReward)

            intentionInfoScale = [-0.025, 0.025]
            runModel = RunIntentionModel(runVI, softmaxBeta,
                                         intentionInfoScale)

            controller = SampleSoftmaxAction(softmaxBeta)

            renderOn = 0

            # controller = AvoidCommitModel(goal_Q_dict, Q_dict, softmaxBeta, actionSpace)
            normalTrial = NormalTrialOnline(controller, drawNewState, drawText,
                                            normalNoise, checkBoundary,
                                            renderOn)
            specialTrial = SpecialTrialOnline(controller, drawNewState,
                                              drawText, sampleToZoneNoise,
                                              checkBoundary, renderOn)

            experimentValues = co.OrderedDict()
            experimentValues["name"] = "hide0.025commitModelSoftmaxBeta" + str(
                softmaxBeta) + '_' + str(i)
            resultsDirPath = os.path.join(
                resultsPath,
                "hide0.025commitModelSoftmaxBeta" + str(softmaxBeta))

            if not os.path.exists(resultsDirPath):
                os.makedirs(resultsDirPath)
            writerPath = os.path.join(resultsDirPath,
                                      experimentValues["name"] + '.csv')
            writer = WriteDataFrameToCSV(writerPath)

            experiment = CommitModelExperiment(runModel, normalTrial,
                                               specialTrial, writer,
                                               experimentValues,
                                               samplePositionFromCondition,
                                               drawImage, resultsPath)
            experiment(noiseDesignValues, conditionList)
def main():
    gridSize = 15

    picturePath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))
    machinePolicyPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'machinePolicy'))
    dataPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'conditionData'))
    df = pd.read_csv(
        os.path.join(dataPath, 'DesignConditionForAvoidCommitmentZone.csv'))
    df['intentionedDisToTargetMin'] = df.apply(
        lambda x: x['minDis'] - x['avoidCommitmentZone'], axis=1)

    screenWidth = 600
    screenHeight = 600
    fullScreen = False

    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    pg.mouse.set_visible(False)

    leaveEdgeSpace = 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    testTrialImage = pg.image.load(
        os.path.join(picturePath, 'testTrialTime.png'))
    formalTrialImage = pg.image.load(
        os.path.join(picturePath, 'formalTrialTime.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    restImage = pg.image.load(os.path.join(picturePath, 'rest.png'))

    testTrialImage = pg.transform.scale(testTrialImage,
                                        (screenWidth, screenHeight))
    formalTrialImage = pg.transform.scale(formalTrialImage,
                                          (screenWidth, screenHeight))
    restImage = pg.transform.scale(
        restImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    finishImage = pg.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

    # condition
    width = [5]
    height = [5]
    intentionDis = [3, 4, 5]
    rotateAngles = [0, 90, 180, 270]
    decisionSteps = [2, 4, 6, 10]
    targetDiffs = [0, 0, 1, 2]

    obstaclesMap1 = [[(2, 2), (2, 4), (2, 5), (2, 6), (4, 2), (5, 2), (6, 2)]]
    obstaclesMap2 = [[(3, 3), (4, 1), (1, 4), (5, 3), (3, 5), (6, 3), (3, 6)]]
    obstaclesMap3 = [[(4, 4), (4, 1), (4, 2), (6, 4), (4, 6), (1, 4), (2, 4)]]

    speicalObstacleMap = [[(4, 1), (4, 2), (6, 3), (6, 4), (1, 4), (2, 4),
                           (3, 6), (4, 6)]]
    obstaclesCondition = [
        obstaclesMap1, obstaclesMap2, obstaclesMap3, speicalObstacleMap
    ]
    obstaclesMaps = dict(zip(decisionSteps, obstaclesCondition))

    numBlocks = 3
    expDesignValues = [[b, h, d, m, diff] for b in width for h in height
                       for d in intentionDis for m in decisionSteps
                       for diff in targetDiffs] * numBlocks

    random.shuffle(expDesignValues)
    numExpTrial = len(expDesignValues)

    specialDesign = [5, 5, 4, 10, 0]
    expDesignValues.append(specialDesign)

    condition = namedtuple('condition', 'name decisionSteps')
    expCondition = condition(name='expCondition',
                             decisionSteps=decisionSteps[:-1])
    lineCondition = condition(name='lineCondition',
                              decisionSteps=decisionSteps[:-1])
    specialCondition = condition(name='specialCondition', decisionSteps=[10])

    numControlTrial = int(numExpTrial / 2)
    conditionList = [expCondition] * numControlTrial + [lineCondition
                                                        ] * numControlTrial
    # conditionList = [lineCondition] * numControlTrial

    random.shuffle(conditionList)
    conditionList.append(specialCondition)

    numNormalTrials = len(conditionList)
    numTrialsPerBlock = 3
    noiseCondition = list(permutations([1, 2, 0],
                                       numTrialsPerBlock)) + [(1, 1, 1)]
    blockNumber = int(numNormalTrials / numTrialsPerBlock)
    noiseDesignValues = createNoiseDesignValue(noiseCondition, blockNumber)

    noise = 0.067
    if noise == 0:
        noiseDesignValues = [0] * numNormalTrials

    if len(conditionList) != len(noiseDesignValues):
        raise Exception("unmatch condition design")

    print(len(conditionList))
    # deubg
    # expDesignValues = [specialDesign] * 10
    # noiseDesignValues = ['special'] * 10
    # conditionList = [specialCondition] * 10
    # debug

    experimentValues = co.OrderedDict()
    # experimentValues["name"] = input("Please enter your name:").capitalize()
    experimentValues["name"] = 'test'

    writerPath = os.path.join(resultsPath,
                              't' + experimentValues["name"] + '.csv')
    writer = WriteDataFrameToCSV(writerPath)

    baseLineWriterPath = os.path.join(
        resultsPath, 'baseLine' + experimentValues["name"] + '.csv')
    baseLineWriter = WriteDataFrameToCSV(baseLineWriterPath)

    rotatePoint = RotatePoint(gridSize)
    isInBoundary = IsInBoundary([0, gridSize - 1], [0, gridSize - 1])
    creatRectMap = CreatRectMap(rotateAngles, gridSize, obstaclesMaps,
                                rotatePoint)
    creatLineMap = CreatLineMap(rotateAngles, gridSize, rotatePoint,
                                isInBoundary)
    samplePositionFromCondition = SamplePositionFromCondition(
        creatRectMap, creatLineMap, expDesignValues)

    pygameActionDict = {
        pg.K_UP: (0, -1),
        pg.K_DOWN: (0, 1),
        pg.K_LEFT: (-1, 0),
        pg.K_RIGHT: (1, 0)
    }
    humanController = HumanController(pygameActionDict)
    controller = humanController

    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0), (1, 1), (1, -1),
                        (-1, -1), (-1, 1)]
    normalNoise = AimActionWithNoise(noiseActionSpace, gridSize)
    specialNoise = backToCrossPointNoise

    normalTrial = NormalTrial(controller, drawNewState, drawText, normalNoise,
                              checkBoundary)
    specialTrial = SpecialTrial(controller, drawNewState, drawText,
                                specialNoise, checkBoundary)

    experiment = ObstacleExperiment(normalTrial, specialTrial, writer,
                                    experimentValues,
                                    samplePositionFromCondition, drawImage,
                                    resultsPath)

    singleGoalTrial = SingleGoalTrial(controller, drawNewState, drawText,
                                      normalNoise, checkBoundary)
    creatSingleGoalMap = CreatSingleGoalMap(gridSize)
    singleGoalExperiment = SingleGoalExperiment(singleGoalTrial,
                                                baseLineWriter,
                                                experimentValues,
                                                creatSingleGoalMap)

    baseLineTrialCondition = [6, 8, 10, 12, 14]
    numBaseLineTrialBlock = 2
    numBaseLineTrial = len(baseLineTrialCondition) * numBaseLineTrialBlock
    baseLineNoiseDesignValues = np.array([
        random.choice(noiseCondition) for _ in range(numBaseLineTrial)
    ]).flatten().tolist()
    baseLineConditionList = baseLineTrialCondition * numBaseLineTrialBlock
    random.shuffle(baseLineConditionList)

    drawImage(testTrialImage)
    singleGoalExperiment(baseLineNoiseDesignValues, baseLineConditionList)
    drawImage(restImage)
    drawImage(formalTrialImage)
    experiment(noiseDesignValues, conditionList)
    drawImage(finishImage)
def main():
    picturePath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))
    machinePolicyPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'machinePolicy'))
    dataPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'conditionData'))
    df = pd.read_csv(
        os.path.join(dataPath, 'DesignConditionForAvoidCommitmentZone.csv'))
    df['intentionedDisToTargetMin'] = df.apply(
        lambda x: x['minDis'] - x['avoidCommitmentZone'], axis=1)

    gridSize = 15

    screenWidth = 600
    screenHeight = 600
    fullScreen = False
    renderOn = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    # pg.mouse.set_visible(False)

    leaveEdgeSpace = 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

    # condition
    width = [5]
    height = [5]
    intentionDis = [2, 3, 4]
    decisionSteps = [0, 1, 2, 4, 6, 8]
    # decisionSteps = [1]
    targetDiffs = [0, 0, 1, 2]

    rotateAngles = [0, 90, 180, 270]

    obstaclesMapStep0 = [[(1, 4), (1, 5), (1, 6), (3, 2), (4, 2), (5, 2),
                          (6, 2), (7, 2)]]

    obstaclesMapStep1 = [[(1, 2), (1, 4), (1, 5), (1, 6), (3, 2), (4, 2),
                          (5, 2), (6, 2)]]

    obstaclesMapStep2 = [[(2, 2), (2, 4), (3, 5), (3, 6), (4, 2), (5, 3),
                          (6, 3)],
                         [(2, 2), (2, 4), (2, 5), (3, 6), (4, 2), (5, 2),
                          (6, 3)],
                         [(2, 2), (2, 4), (3, 5), (2, 6), (4, 2), (5, 3),
                          (6, 2)]]

    obstaclesMapStep4 = [[(3, 3), (4, 1), (1, 4), (5, 3), (3, 5), (6, 3),
                          (3, 6)],
                         [(3, 3), (5, 1), (1, 5), (5, 3), (3, 5), (6, 3),
                          (3, 6)],
                         [(3, 3), (3, 1), (1, 3), (5, 3), (3, 5), (6, 3),
                          (3, 6)]]

    obstaclesMapStep6 = [[(4, 4), (4, 1), (4, 2), (6, 4), (4, 6), (1, 4),
                          (2, 4)],
                         [(4, 4), (5, 1), (4, 2), (6, 4), (4, 6), (1, 5),
                          (2, 4)],
                         [(4, 4), (3, 1), (4, 2), (6, 4), (4, 6), (1, 3),
                          (2, 4)]]

    obstaclesMapStep8 = [[(5, 5), (4, 2), (2, 4), (6, 1), (6, 2), (6, 3),
                          (1, 6), (2, 6), (3, 6), (7, 5), (5, 7)]]

    obstaclesCondition = [
        obstaclesMapStep0, obstaclesMapStep1, obstaclesMapStep2,
        obstaclesMapStep4, obstaclesMapStep6, obstaclesMapStep8
    ]
    obstaclesMaps = dict(zip(decisionSteps, obstaclesCondition))

    rotatePoint = RotatePoint(gridSize)
    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0), (1, 1), (1, -1),
                        (-1, -1), (-1, 1)]
    normalNoise = AimActionWithNoise(noiseActionSpace, gridSize)
    specialNoise = backToCrossPointNoise

    initPrior = [0.5, 0.5]
    # inferGoalPosterior = InferGoalPosterior(goalPolicy)

    softmaxBetaList = [5]
    noise = 0
    gamma = 0.9
    goalReward = [50, 50]
    actionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0)]
    runVI = RunVI(gridSize, actionSpace, noiseActionSpace, noise, gamma,
                  goalReward)

    for softmaxBeta in softmaxBetaList:
        # for noise in noiseList:
        for i in range(20):
            print(i)

            numBlocks = 3
            expDesignValues = [[b, h, d, m, diff] for b in width
                               for h in height for d in intentionDis
                               for m in decisionSteps
                               for diff in targetDiffs] * numBlocks

            random.shuffle(expDesignValues)
            numExpTrial = len(expDesignValues)

            condition = namedtuple('condition', 'name decisionSteps')
            expCondition = condition(name='expCondition',
                                     decisionSteps=decisionSteps)
            lineCondition = condition(name='lineCondition',
                                      decisionSteps=decisionSteps)
            conditionList = [expCondition
                             ] * numExpTrial  # + [lineCondition] * numExpTrial

            random.shuffle(conditionList)

            numNormalTrials = len(conditionList)
            noiseDesignValues = [0] * numNormalTrials

            if len(conditionList) != len(noiseDesignValues):
                raise Exception("unmatch condition design")

    # deubg
    #         expDesignValues = [specialDesign] * 10
    #         noiseDesignValues = ['special'] * 10
    #         conditionList = [expCondition] * 10
    # debug

            isInBoundary = IsInBoundary([0, gridSize - 1], [0, gridSize - 1])
            creatRectMap = CreatRectMap(rotateAngles, gridSize, obstaclesMaps,
                                        rotatePoint)
            creatLineMap = CreatLineMap(rotateAngles, gridSize, rotatePoint,
                                        isInBoundary)
            samplePositionFromCondition = SamplePositionFromCondition(
                creatRectMap, creatLineMap, expDesignValues)

            modelController = ModelControllerOnline(softmaxBeta)

            # modelController = AvoidCommitModel(softmaxBeta, actionSpace, checkBoundary)

            controller = modelController

            renderOn = 1
            normalTrial = NormalTrial(renderOn, controller, drawNewState,
                                      drawText, normalNoise, checkBoundary)

            experimentValues = co.OrderedDict()
            experimentValues["name"] = "noise" + str(
                noise) + '_' + "softmaxBeta" + str(softmaxBeta) + '_' + str(i)
            resultsDirPath = os.path.join(
                resultsPath,
                "noise" + str(noise) + '_' + "softmaxBeta" + str(softmaxBeta))
            if not os.path.exists(resultsDirPath):
                os.mkdir(resultsDirPath)

            writerPath = os.path.join(resultsDirPath,
                                      experimentValues["name"] + '.csv')
            writer = WriteDataFrameToCSV(writerPath)
            experiment = ObstacleModelSimulation(normalTrial, writer,
                                                 experimentValues,
                                                 samplePositionFromCondition,
                                                 drawImage, resultsPath, runVI)
            experiment(noiseDesignValues, conditionList)
def main():
    numOfAgent = 4

    manipulatedHyperVariables = co.OrderedDict()
    conditionList = [1, 2]  #0;practice 1:off=0 2:off=12
    manipulatedHyperVariables['ChaseCondition'] = conditionList
    trajetoryIndexList = range(20)
    manipulatedHyperVariables['TrajIndex'] = trajetoryIndexList

    exprimentVarableList = crateVariableProduct(manipulatedHyperVariables)

    print('loading')

    positionIndex = [0, 1]
    FPS = 60
    dataFileDir = '../PataData/withoutLine'
    rawXRange = [-10, 10]
    rawYRange = [-10, 10]
    scaledXRange = [200, 600]
    scaledYRange = [200, 600]
    scaleTrajectory = ScaleTrajectory(positionIndex, rawXRange, rawYRange,
                                      scaledXRange, scaledYRange)
    oldFPS = 5
    adjustFPS = AdjustDfFPStoTraj(oldFPS, FPS)
    getTrajectory = lambda trajectoryDf: scaleTrajectory(
        adjustFPS(trajectoryDf))
    trajectoryDf = lambda condition, index: pd.read_pickle(
        os.path.join(dataFileDir, '{}'.format(condition) + ' ({}).pickle'.
                     format(index)))
    stimulus = {
        condition: [
            getTrajectory(trajectoryDf(condition, index))
            for index in trajetoryIndexList
        ]
        for condition in conditionList
    }

    print('loding success')

    experimentValues = co.OrderedDict()
    experimentValues["name"] = input("Please enter your name:").capitalize()

    screenWidth = 800
    screenHeight = 800

    fullScreen = True
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()

    leaveEdgeSpace = 200
    circleSize = 10
    clickImageHeight = 80
    lineWidth = 3
    fontSize = 50
    xBoundary = [leaveEdgeSpace, screenWidth - leaveEdgeSpace * 2]
    yBoundary = [leaveEdgeSpace, screenHeight - leaveEdgeSpace * 2]

    screenColor = THECOLORS['black']
    lineColor = THECOLORS['white']
    textColor = THECOLORS['white']
    fixationPointColor = THECOLORS['white']

    colorSpace = [(203, 164, 4, 255), (49, 153, 0, 255), (255, 90, 16, 255),
                  (251, 7, 255, 255), (9, 204, 172, 255), (3, 28, 255, 255)]

    picturePath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'pictures')
    resultsPath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'results')

    introductionImage1 = pygame.image.load(
        os.path.join(picturePath, 'introduction1.png'))
    introductionImage2 = pygame.image.load(
        os.path.join(picturePath, 'introduction2.png'))
    finishImage = pygame.image.load(os.path.join(picturePath, 'over.jpg'))
    introductionImage1 = pygame.transform.scale(introductionImage1,
                                                (screenWidth, screenHeight))
    introductionImage2 = pygame.transform.scale(introductionImage2,
                                                (screenWidth, screenHeight))

    finishImage = pygame.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    clickWolfImage = pygame.image.load(
        os.path.join(picturePath, 'clickwolf.png'))
    clickSheepImage = pygame.image.load(
        os.path.join(picturePath, 'clicksheep.png'))
    restImage = pygame.image.load(os.path.join(picturePath, 'rest.jpg'))

    drawImage = DrawImage(screen)
    drawText = DrawText(screen, fontSize, textColor)
    drawBackGround = DrawBackGround(screen, screenColor, xBoundary, yBoundary,
                                    lineColor, lineWidth)
    drawFixationPoint = DrawFixationPoint(screen, drawBackGround,
                                          fixationPointColor)
    drawImageClick = DrawImageClick(screen, clickImageHeight, drawText)

    drawState = DrawState(screen, circleSize, numOfAgent, positionIndex,
                          drawBackGround)
    # drawStateWithRope = DrawStateWithRope(screen, circleSize, numOfAgent, positionIndex, ropeColor, drawBackground)

    writerPath = os.path.join(resultsPath, experimentValues["name"]) + '.csv'
    writer = WriteDataFrameToCSV(writerPath)

    displayFrames = 600
    keysForCheck = {'f': 0, 'j': 1}
    checkHumanResponse = CheckHumanResponse(keysForCheck)
    trial = ChaseTrial(conditionList, displayFrames, drawState, drawImage,
                       stimulus, checkHumanResponse, colorSpace, numOfAgent,
                       drawFixationPoint, drawText, drawImageClick,
                       clickWolfImage, clickSheepImage, FPS)

    experiment = Experiment(trial, writer, experimentValues, drawImage,
                            restImage, drawBackGround)
    numOfBlock = 1
    numOfTrialsPerBlock = 1
    designValues = createDesignValues(
        exprimentVarableList * numOfTrialsPerBlock, numOfBlock)
    restDuration = 20

    drawImage(introductionImage1)
    drawImage(introductionImage2)

    experiment(designValues, restDuration)
    # self.darwBackground()
    drawImage(finishImage)

    print("Result saved at {}".format(writerPath))
def main():
    screenWidth = 800
    screenHeight = 800
    FPS = 60
    fullScreen = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()

    saveImage = False
    saveImageFile = 'noRopeCondition1'

    numOfAgent = 4
    leaveEdgeSpace = 200
    circleSize = 10
    clickImageHeight = 80
    lineWidth = 3
    fontSize = 50
    xBoundary = [leaveEdgeSpace, screenWidth - leaveEdgeSpace * 2]
    yBoundary = [leaveEdgeSpace, screenHeight - leaveEdgeSpace * 2]
    stimulusXBoundary = [xBoundary[0] + circleSize, xBoundary[1] - circleSize]
    stimulusYBoundary = [yBoundary[0] + circleSize, yBoundary[1] - circleSize]

    screenColor = THECOLORS['black']
    lineColor = THECOLORS['white']
    textColor = THECOLORS['white']
    fixationPointColor = THECOLORS['white']
    ropeColor = THECOLORS['white']
    colorSpace = [
        THECOLORS['grey'], THECOLORS['red'], THECOLORS['blue'],
        THECOLORS['yellow'], THECOLORS['purple'], THECOLORS['orange']
    ]
    random.shuffle(colorSpace)
    # circleColorList = [THECOLORS['grey']] * numOfAgent
    # circleColorList = [THECOLORS['red'], THECOLORS['green'], THECOLORS['grey'], THECOLORS['yellow']]
    circleColorList = colorSpace[:numOfAgent]

    stateIndex = ['wolf', 'sheep', 'master', 'distractor']
    identityColorPairs = dict(zip(stateIndex, circleColorList))

    picturePath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'pictures')
    resultsPath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'results')

    introductionImage = pygame.image.load(
        os.path.join(picturePath, 'introduction2.png'))
    finishImage = pygame.image.load(os.path.join(picturePath, 'over.jpg'))
    introductionImage = pygame.transform.scale(introductionImage,
                                               (screenWidth, screenHeight))
    finishImage = pygame.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    clickWolfImage = pygame.image.load(
        os.path.join(picturePath, 'clickwolf.png'))
    clickSheepImage = pygame.image.load(
        os.path.join(picturePath, 'clicksheep.png'))
    restImage = pygame.image.load(os.path.join(picturePath, 'rest.jpg'))
    # restImage = pygame.transform.scale(restImage, (screenWidth, screenHeight))

    drawImage = DrawImage(screen)
    drawText = DrawText(screen, fontSize, textColor)
    drawBackGround = DrawBackGround(screen, screenColor, xBoundary, yBoundary,
                                    lineColor, lineWidth)
    drawFixationPoint = DrawFixationPoint(screen, drawBackGround,
                                          fixationPointColor)
    drawImageClick = DrawImageClick(screen, clickImageHeight, drawText)
    drawState = DrawState(drawBackGround, numOfAgent, screen, circleSize)
    drawStateWithRope = DrawStateWithRope(drawBackGround, numOfAgent, screen,
                                          circleSize, ropeColor)

    conditionList = [1]  #[1, 2, 3, 4]
    trajetoryIndexList = [1]  # [1, 2, 3, 4, 5]
    dataFileDir = '../PataData'
    dataSetBoundary = [26, 26]
    generateTrajetoryData = GenerateTrajetoryData(dataFileDir,
                                                  stimulusXBoundary,
                                                  stimulusYBoundary,
                                                  dataSetBoundary)
    stimulus = {
        condition: generateTrajetoryData(condition, trajetoryIndexList)
        for condition in conditionList
    }

    experimentValues = co.OrderedDict()

    # experimentValues["name"] = input("Please enter your name:").capitalize()
    experimentValues["name"] = 'csz'

    writerPath = os.path.join(resultsPath, experimentValues["name"]) + '.csv'
    writer = WriteDataFrameToCSV(writerPath)

    displayFrames = FPS * 3
    keysForCheck = {'f': 0, 'j': 1}
    checkHumanResponse = CheckHumanResponse(keysForCheck)
    trial = ChaseTrial(displayFrames, drawState, drawImage, stimulus,
                       checkHumanResponse, colorSpace, numOfAgent,
                       drawFixationPoint, drawText, drawImageClick,
                       clickWolfImage, clickSheepImage, FPS, saveImage,
                       saveImageFile)

    experiment = Experiment(trial, writer, experimentValues, drawImage,
                            restImage)
    numOfBlock = 2
    numOfTrialsPerBlock = 1
    designValues = createDesignValues(conditionList * numOfTrialsPerBlock,
                                      numOfBlock)
    print(designValues)
    drawImage(introductionImage)
    experiment(designValues, numOfTrialsPerBlock)
    # drawImage(finishImage)
    print("Result saved at {}".format(writerPath))
Exemple #9
0
def main():
    experimentValues = co.OrderedDict()
    experimentValues["name"] = 'test'
    # experimentValues["name"] = input("Please enter your name:").capitalize()
    fullScreen = 1
    mouseVisible = False

    screenWidth = 600
    screenHeight = 600
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    pg.mouse.set_visible(mouseVisible)

    gridSize = 15
    leaveEdgeSpace = 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    picturePath = os.path.abspath(os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))

    formalTrialImage = pg.image.load(os.path.join(picturePath, 'formalTrial.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    restImage = pg.image.load(os.path.join(picturePath, 'rest.png'))

    formalTrialImage = pg.transform.scale(formalTrialImage, (screenWidth, screenHeight))
    restImage = pg.transform.scale(restImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    finishImage = pg.transform.scale(finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))

    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace, backgroundColor, lineColor, lineWidth, textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor, playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

# condition
    width = [5]
    height = [5]
    intentionDis = [2, 3, 4]
    decisionSteps = [0, 1, 2, 4, 6]
    targetDiffs = [0, 0, 1, 2]
    rotateAngles = [0, 90, 180, 270]

    # obstaclesMapStep
    obstaclesMapStep0 = [[(3, 2), (4, 2), (5, 3), (6, 3), (7, 4), (1, 3), (2, 4), (2, 5), (3, 6)]]

    obstaclesMapStep1 = [[(1, 2), (2, 4), (2, 5), (3, 2), (4, 2), (3, 6), (4, 7), (5, 3), (6, 4)]]

    obstaclesMapStep2 = [[(2, 2), (2, 4), (2, 5), (3, 6), (4, 7), (4, 2), (5, 2), (6, 3), (7, 3)]]

    obstaclesMapStep4 = [[(3, 3), (4, 1), (1, 4), (5, 1), (1, 5), (5, 3), (6, 3), (3, 5), (3, 6)]]

    obstaclesMapStep6 = [[(4, 4), (4, 1), (4, 2), (6, 4), (7, 4), (4, 6), (4, 7), (1, 4), (2, 4)]]

    speicalObstacleMap = [[(4, 1), (4, 2), (6, 3), (6, 4), (1, 4), (2, 4), (3, 6), (4, 6)],
                          [(5, 1), (4, 2), (6, 3), (6, 4), (1, 5), (2, 4), (3, 6), (4, 6)],
                          [(3, 1), (4, 2), (6, 3), (6, 4), (1, 3), (2, 4), (3, 6), (4, 6)]]

    obstaclesCondition = [obstaclesMapStep0, obstaclesMapStep1, obstaclesMapStep2, obstaclesMapStep4, obstaclesMapStep6]

# debug
    # decisionSteps = [6]
    # obstaclesCondition = [obstaclesMapStep6]
# debug

    obstaclesMaps = dict(zip(decisionSteps, obstaclesCondition))

    numBlocks = 3
    expDesignValues = [[b, h, d, m, diff] for b in width for h in height for d in intentionDis for m in decisionSteps for diff in targetDiffs] * numBlocks

    random.shuffle(expDesignValues)
    numExpTrial = len(expDesignValues)

    specialDesign = [5, 5, 4, 10, 0]
    expDesignValues.append(specialDesign)

    condition = namedtuple('condition', 'name decisionSteps')
    expCondition = condition(name='expCondition', decisionSteps=decisionSteps)
    lineCondition = condition(name='lineCondition', decisionSteps=decisionSteps)
    # specialCondition = condition(name='specialCondition', decisionSteps=[10])

    conditionList = [expCondition] * numExpTrial  # + [lineCondition] * numExpTrial
    # conditionList = [lineCondition] * numExpTrial

    random.shuffle(conditionList)
    numNormalTrials = len(conditionList)

    # numTrialsPerBlock = 3
    # noiseCondition = list(permutations([1, 2, 0], numTrialsPerBlock)) + [(1, 1, 1)]
    # blockNumber = int(numNormalTrials / numTrialsPerBlock)
    # noiseDesignValues = createNoiseDesignValue(noiseCondition, blockNumber)

    # conditionList.append(specialCondition)

    noiseDesignValues = [0] * numNormalTrials

# deubg
    # expDesignValues = [specialDesign] * 10
    # noiseDesignValues = ['special'] * 10
    # conditionList = [specialCondition] * 10
# debug

    print('trial:', len(conditionList))
    if len(conditionList) != len(noiseDesignValues):
        raise Exception("unmatch condition design")

    writerPath = os.path.join(resultsPath, experimentValues["name"] + '.csv')
    writer = WriteDataFrameToCSV(writerPath)

    rotatePoint = RotatePoint(gridSize)
    isInBoundary = IsInBoundary([0, gridSize - 1], [0, gridSize - 1])
    creatRectMap = CreatRectMap(rotateAngles, gridSize, obstaclesMaps, rotatePoint)
    creatLineMap = CreatLineMap(rotateAngles, gridSize, rotatePoint, isInBoundary)
    samplePositionFromCondition = SamplePositionFromCondition(creatRectMap, creatLineMap, expDesignValues)

    pygameActionDict = {pg.K_UP: (0, -1), pg.K_DOWN: (0, 1), pg.K_LEFT: (-1, 0), pg.K_RIGHT: (1, 0)}
    humanController = HumanController(pygameActionDict)
    controller = humanController

    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0), (1, 1), (1, -1), (-1, -1), (-1, 1)]
    # noiseActionSpace = [(0, 0)]

    normalNoise = AimActionWithNoise(noiseActionSpace, gridSize)
    specialNoise = backToCrossPointNoise

    normalTrial = NormalTrial(controller, drawNewState, drawText, normalNoise, checkBoundary)

    restTrialInterval = math.ceil(numNormalTrials / 4)
    restTrial = list(range(restTrialInterval, numNormalTrials, restTrialInterval))

    experiment = ObstacleExperiment(normalTrial, writer, experimentValues, samplePositionFromCondition, drawImage, resultsPath, restTrial, restImage)

# start exp
    drawImage(formalTrialImage)
    experiment(noiseDesignValues, conditionList)
    drawImage(finishImage)
Exemple #10
0
def main():
    gridSize = 60
    bounds = [0, 0, gridSize - 1, gridSize - 1]
    minDistanceForReborn = 5
    condition = [-5, -3, -1, 0, 1, 3, 5]
    counter = [0] * len(condition)
    numPlayers = 2
    initialWorld = InitialWorld(bounds, numPlayers, minDistanceForReborn)
    updateWorld = UpdateWorld(bounds, condition, counter, minDistanceForReborn)

    screenWidth = 800
    screenHeight = 800
    screenCenter = [screenWidth / 2, screenHeight / 2]
    fullScreen = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()

    leaveEdgeSpace = 6
    lineWidth = 1
    backgroundColor = THECOLORS['grey']  # [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [
        THECOLORS['blue'], (0, 0, 128), (0, 168, 107), (0, 168, 107)
    ]  # [255, 50, 50]
    playerColors = [THECOLORS['orange'], THECOLORS['red']]
    targetRadius = 10
    playerRadius = 10
    totalBarLength = 100
    barHeight = 20
    stopwatchUnit = 100
    finishTime = 1000 * 60 * 2
    block = 1
    softmaxBeita = -1
    textColorTuple = THECOLORS['green']
    stopwatchEvent = pg.USEREVENT + 1

    saveImage = False
    killzone = 2
    wolfSpeedRatio = 1

    pg.time.set_timer(stopwatchEvent, stopwatchUnit)
    pg.event.set_allowed([pg.KEYDOWN, pg.QUIT, stopwatchEvent])
    pg.key.set_repeat(120, 120)
    picturePath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))
    experimentValues = co.OrderedDict()
    # experimentValues["name"] = input("Please enter your name:").capitalize()
    experimentValues["name"] = 'kill' + str(killzone)
    experimentValues["condition"] = 'all'
    writerPath = os.path.join(resultsPath, experimentValues["name"]) + '.csv'
    writer = WriteDataFrameToCSV(writerPath)

    introductionImage = pg.image.load(
        os.path.join(picturePath, 'introduction.png'))
    restImage = pg.image.load(os.path.join(picturePath, 'rest.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    introductionImage = pg.transform.scale(introductionImage,
                                           (screenWidth, screenHeight))
    finishImage = pg.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))

    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple, playerColors)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColors, targetRadius, playerRadius)
    drawImage = DrawImage(screen)
    drawAttributionTrail = DrawAttributionTrail(screen, playerColors,
                                                totalBarLength, barHeight,
                                                screenCenter)
    saveImageDir = os.path.join(
        os.path.join(os.path.abspath(os.path.join(os.getcwd(), os.pardir)),
                     'data'), experimentValues["name"])

    xBoundary = [bounds[0], bounds[2]]
    yBoundary = [bounds[1], bounds[3]]
    stayInBoundary = StayInBoundary(xBoundary, yBoundary)
    ############
    actionSpace = [(10, 0), (7, 7), (0, 10), (-7, 7), (-10, 0), (-7, -7),
                   (0, -10), (7, -7)]
    preyPowerRatio = 3
    sheepActionSpace = list(map(tuple, np.array(actionSpace) * preyPowerRatio))
    sheepActionSpace.append((0, 0))
    numActionSpace = len(sheepActionSpace)

    actionSpaceStill = [(10, 0), (7, 7), (0, 10), (-7, 7), (-10, 0), (-7, -7),
                        (0, -10), (7, -7)]
    sheepActionSpaceStill = list(
        map(tuple,
            np.array(actionSpaceStill) * preyPowerRatio))
    sheepActionSpaceStill.append((0, 0))
    numActionSpaceStill = len(sheepActionSpaceStill)

    regularizationFactor = 1e-4
    sharedWidths = [128]
    actionLayerWidths = [128]
    valueLayerWidths = [128]
    resBlockSize = 2
    dropoutRate = 0.0
    initializationMethod = 'uniform'
    depth = 5
    generateSheepModelSingle = GenerateModel(4, numActionSpace,
                                             regularizationFactor)
    generateSheepModelMulti = GenerateModel(6, numActionSpaceStill,
                                            regularizationFactor)
    initSheepNNModelSingle = generateSheepModelSingle(
        sharedWidths * depth, actionLayerWidths, valueLayerWidths,
        resBlockSize, initializationMethod, dropoutRate)
    initSheepNNModelMulti = generateSheepModelMulti(
        sharedWidths * depth, actionLayerWidths, valueLayerWidths,
        resBlockSize, initializationMethod, dropoutRate)
    sheepPreTrainModelPathSingle = os.path.join(
        '..', 'trainedModelsSingle',
        'agentId=0_dataSize=5000_depth=5_learningRate=0.0001_maxRunningSteps=150_miniBatchSize=256_numSimulations=100_sampleOneStepPerTraj=0_trainSteps=50000'
    )
    sheepPreTrainModelPathMulti = os.path.join(
        '..', 'trainedModelsMulti',
        'agentId=0_depth=5_learningRate=0.0001_maxRunningSteps=150_miniBatchSize=256_numSimulations=200_trainSteps=50000'
    )
    sheepPreTrainModelSingle = restoreVariables(initSheepNNModelSingle,
                                                sheepPreTrainModelPathSingle)
    sheepPreTrainModelMulti = restoreVariables(initSheepNNModelMulti,
                                               sheepPreTrainModelPathMulti)

    sheepPolicySingleModel = ApproximatePolicy(sheepPreTrainModelSingle,
                                               sheepActionSpace)
    sheepPolicyMulti = ApproximatePolicy(sheepPreTrainModelMulti,
                                         sheepActionSpaceStill)

    from src.sheepPolicy import SingleChasingPolicy, inferNearestWolf, ComputeLikelihoodByHeatSeeking, InferCurrentWolf, BeliefPolicy
    sheepPolicySingle = SingleChasingPolicy(sheepPolicySingleModel,
                                            inferNearestWolf)
    baseProb = 0.5
    assumePrecision = 50
    computeLikelihoodByHeatSeeking = ComputeLikelihoodByHeatSeeking(
        baseProb, assumePrecision)
    inferCurrentWolf = InferCurrentWolf(computeLikelihoodByHeatSeeking)
    beliefPolicy = BeliefPolicy(sheepPolicySingle, sheepPolicySingleModel,
                                sheepPolicyMulti, inferCurrentWolf)
    # sheepPolicySingle = sheepPolicySingleModel

    # ## mcts sheep
    # from mcts import sheepMCTS
    # sheepPolicy = sheepMCTS()
    checkTerminationOfTrial = CheckTerminationOfTrial(finishTime)
    checkEaten = CheckEaten(killzone, isAnyKilled)
    totalScore = 10
    attributionTrail = AttributionTrail(totalScore, saveImageDir, saveImage,
                                        drawAttributionTrail)

    # sheepPolicy = [sheepPolicySingle,sheepPolicyMulti]

    softMaxBeta = 30
    softmaxAction = SoftmaxAction(softMaxBeta)
    humanController = HumanController(writer, gridSize, stopwatchEvent,
                                      stopwatchUnit, wolfSpeedRatio,
                                      drawNewState, finishTime, stayInBoundary,
                                      saveImage, saveImageDir, beliefPolicy,
                                      chooseGreedyAction)
    # policy = pickle.load(open("SingleWolfTwoSheepsGrid15.pkl","rb"))
    # modelController = ModelController(policy, gridSize, stopwatchEvent, stopwatchUnit, drawNewState, finishTime, softmaxBeita)

    actionSpace = list(it.product([0, 1, -1], repeat=2))

    trial = Trial(actionSpace, killzone, stopwatchEvent, drawNewState,
                  checkTerminationOfTrial, checkEaten, attributionTrail,
                  humanController)
    experiment = Experiment(trial, writer, experimentValues, initialWorld,
                            updateWorld, drawImage, resultsPath)
    giveExperimentFeedback = GiveExperimentFeedback(screen, textColorTuple,
                                                    screenWidth, screenHeight)

    # drawImage(introductionImage)
    score = [0] * block
    for i in range(block):
        score[i] = experiment(finishTime)
        giveExperimentFeedback(i, score)
        if i == block - 1:
            drawImage(finishImage)
        # else:
        # drawImage(restImage)

    participantsScore = np.sum(np.array(score))
    print(participantsScore)
Exemple #11
0
def main():
    picturePath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'pictures'))
    resultsPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'results'))
    machinePolicyPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'machinePolicy'))
    dataPath = os.path.abspath(
        os.path.join(os.path.join(os.getcwd(), os.pardir), 'conditionData'))
    df = pd.read_csv(
        os.path.join(dataPath, 'DesignConditionForAvoidCommitmentZone.csv'))
    df['intentionedDisToTargetMin'] = df.apply(
        lambda x: x['minDis'] - x['avoidCommitmentZone'], axis=1)

    gridSize = 15

    screenWidth = 600
    screenHeight = 600
    fullScreen = False
    renderOn = False
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()
    pg.mouse.set_visible(False)

    leaveEdgeSpace = 2
    lineWidth = 1
    backgroundColor = [205, 255, 204]
    lineColor = [0, 0, 0]
    targetColor = [255, 50, 50]
    playerColor = [50, 50, 255]
    targetRadius = 10
    playerRadius = 10
    textColorTuple = (255, 50, 50)

    introductionImage = pg.image.load(
        os.path.join(picturePath, 'introduction.png'))
    finishImage = pg.image.load(os.path.join(picturePath, 'finish.png'))
    introductionImage = pg.transform.scale(introductionImage,
                                           (screenWidth, screenHeight))
    finishImage = pg.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    drawBackground = DrawBackground(screen, gridSize, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple)
    drawText = DrawText(screen, drawBackground)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColor, targetRadius, playerRadius)
    drawImage = DrawImage(screen)

    # condition
    width = [5]
    height = [5]
    intentionDis = [3, 4, 5]
    decisionSteps = [2, 4, 6, 10]
    targetDiffs = [0, 2, 4]

    rotateAngles = [0, 90, 180, 270]
    obstaclesMap1 = [(2, 2), (2, 4), (2, 5), (2, 6), (4, 2), (5, 2), (6, 2)]
    obstaclesMap2 = [(3, 3), (4, 1), (1, 4), (5, 3), (3, 5), (6, 3), (3, 6)]
    obstaclesMap3 = [(4, 4), (4, 1), (4, 2), (6, 4), (4, 6), (1, 4), (2, 4)]

    speicalObstacleMap = [(4, 1), (4, 2), (6, 3), (6, 4), (1, 4), (2, 4),
                          (3, 6), (4, 6)]
    obstaclesCondition = [
        obstaclesMap1, obstaclesMap2, obstaclesMap3, speicalObstacleMap
    ]
    obstaclesMaps = dict(zip(decisionSteps, obstaclesCondition))

    rotatePoint = RotatePoint(gridSize)

    checkBoundary = CheckBoundary([0, gridSize - 1], [0, gridSize - 1])
    noiseActionSpace = [(0, -1), (0, 1), (-1, 0), (1, 0), (1, 1), (1, -1),
                        (-1, -1), (-1, 1)]
    normalNoise = AimActionWithNoise(noiseActionSpace, gridSize)
    specialNoise = backToCrossPointNoise

    initPrior = [0.5, 0.5]
    # inferGoalPosterior = InferGoalPosterior(goalPolicy)

    softmaxBetaList = [6]
    noiseList = [0]
    # noise = 0.067
    # for softmaxBeta in softmaxBetaList:
    softmaxBeta = 6
    for noise in noiseList:
        for i in range(20):
            print(i)
            numBlocks = 5
            expDesignValues = [[b, h, d, m, diff] for b in width
                               for h in height for d in intentionDis
                               for m in decisionSteps
                               for diff in targetDiffs] * numBlocks

            random.shuffle(expDesignValues)
            numExpTrial = len(expDesignValues)

            specialDesign = [5, 5, 4, 10, 0]
            expDesignValues.append(specialDesign)

            condition = namedtuple('condition', 'name decisionSteps')
            expCondition = condition(name='expCondition',
                                     decisionSteps=decisionSteps[:-1])
            lineCondition = condition(name='lineCondition',
                                      decisionSteps=decisionSteps[:-1])
            specialCondition = condition(name='specialCondition',
                                         decisionSteps=[10])

            numControlTrial = int(numExpTrial / 2)
            conditionList = [expCondition] * numControlTrial + [
                lineCondition
            ] * numControlTrial
            conditionList = [lineCondition] * numControlTrial

            random.shuffle(conditionList)
            numNormalTrials = len(conditionList)

            numTrialsPerBlock = 3
            noiseCondition = list(permutations([1, 2, 0], numTrialsPerBlock))
            noiseCondition.append((1, 1, 1))
            blockNumber = int(numNormalTrials / numTrialsPerBlock)
            noiseDesignValues = createNoiseDesignValue(noiseCondition,
                                                       blockNumber)

            conditionList.append(specialCondition)

            if noise == 0:
                noiseDesignValues = [0] * len(conditionList)

            if len(conditionList) != len(noiseDesignValues):
                raise Exception("unmatch condition design")

    # deubg
    # expDesignValues = [specialDesign] * 10
    # noiseDesignValues = ['special'] * 10
    # conditionList = [expCondition] * 10
    # debug

            isInBoundary = IsInBoundary([0, gridSize - 1], [0, gridSize - 1])
            creatRectMap = CreatRectMap(rotateAngles, gridSize, obstaclesMaps,
                                        rotatePoint)
            creatLineMap = CreatLineMap(rotateAngles, gridSize, rotatePoint,
                                        isInBoundary)
            samplePositionFromCondition = SamplePositionFromCondition(
                creatRectMap, creatLineMap, expDesignValues)

            runVI = RunVI(gridSize, noise, noiseActionSpace)
            modelController = ModelControllerOnline(softmaxBeta, runVI)
            controller = modelController

            renderOn = 0
            normalTrial = NormalTrial(renderOn, controller, drawNewState,
                                      drawText, normalNoise, checkBoundary)
            specialTrial = SpecialTrial(renderOn, controller, drawNewState,
                                        drawText, specialNoise, checkBoundary)

            experimentValues = co.OrderedDict()
            experimentValues["name"] = "noise" + str(
                noise) + '_' + "softmaxBeta" + str(softmaxBeta) + '_' + str(i)
            writerPath = os.path.join(resultsPath,
                                      experimentValues["name"] + '.csv')
            writer = WriteDataFrameToCSV(writerPath)
            experiment = ObstacleExperiment(normalTrial, specialTrial, writer,
                                            experimentValues,
                                            samplePositionFromCondition,
                                            drawImage, resultsPath)
            experiment(noiseDesignValues, conditionList)
def main():
    numOfAgent = 4
    sheepId = 0
    wolfId = 1
    masterId = 2
    distractorId = 3

    experimentValues = co.OrderedDict()
    experimentValues["condition"] = input(
        "Please enter your condition 1 or 2:").capitalize()
    experimentValues["name"] = input("Please enter your name:").capitalize()
    #experimentValues["name"] = 'test'

    manipulatedHyperVariables = co.OrderedDict()
    conditionList = [int(experimentValues["condition"])]
    manipulatedHyperVariables['ChaseConditon'] = conditionList
    trajetoryIndexList = [0]
    manipulatedHyperVariables['TrajIndex'] = trajetoryIndexList

    exprimentVarableList = crateVariableProduct(manipulatedHyperVariables)

    screenWidth = 800
    screenHeight = 800
    FPS = 60
    fullScreen = True
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()

    leaveEdgeSpace = 200
    circleSize = 10
    clickImageHeight = 80
    lineWidth = 3
    fontSize = 50
    xBoundary = [leaveEdgeSpace, screenWidth - leaveEdgeSpace * 2]
    yBoundary = [leaveEdgeSpace, screenHeight - leaveEdgeSpace * 2]
    stimulusXBoundary = [xBoundary[0] + circleSize, xBoundary[1] - circleSize]
    stimulusYBoundary = [yBoundary[0] + circleSize, yBoundary[1] - circleSize]

    screenColor = THECOLORS['black']
    lineColor = THECOLORS['white']
    textColor = THECOLORS['white']
    fixationPointColor = THECOLORS['white']
    ropeColor = THECOLORS['white']
    # colorSpace = [THECOLORS['grey'], THECOLORS['red'], THECOLORS['blue'], THECOLORS['yellow'], THECOLORS['purple'], THECOLORS['orange']]
    # random.shuffle(colorSpace)
    colorSpace = [
        THECOLORS['purple'], THECOLORS['orange'], THECOLORS['red'],
        THECOLORS['blue']
    ]  # sheep wolf master distractor
    # circleColorList = [THECOLORS['grey']] * numOfAgent
    # circleColorList = [THECOLORS['red'], THECOLORS['green'], THECOLORS['grey'], THECOLORS['yellow']]
    circleColorList = colorSpace[:numOfAgent]

    picturePath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'pictures')
    resultsPath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'results')

    introductionImage1 = pygame.image.load(
        os.path.join(picturePath, 'report1.png'))
    finishImage = pygame.image.load(os.path.join(picturePath, 'over.jpg'))
    introductionImage1 = pygame.transform.scale(
        introductionImage1, (int(screenWidth * 3 / 4), int(screenHeight / 4)))
    finishImage = pygame.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    restImage = pygame.image.load(os.path.join(picturePath, 'rest.jpg'))
    reportInstrucImage = pygame.image.load(
        os.path.join(picturePath, 'report2.png'))
    reportInstrucImage = pygame.transform.scale(
        reportInstrucImage,
        (int(screenWidth * 1.4 * 3 / 5), int(screenHeight * 1.7 * 2 / 5)))
    drawImage = DrawImage(screen)
    drawText = DrawText(screen, fontSize, textColor)
    drawBackGround = DrawBackGround(screen, screenColor, xBoundary, yBoundary,
                                    lineColor, lineWidth)
    drawFixationPoint = DrawFixationPoint(screen, drawBackGround,
                                          fixationPointColor)
    drawImageClick = DrawImageClick(screen, clickImageHeight, drawText)

    positionIndex = [0, 1]
    drawState = DrawState(screen, circleSize, numOfAgent, positionIndex,
                          drawBackGround)
    # drawStateWithRope = DrawStateWithRope(screen, circleSize, numOfAgent, positionIndex, ropeColor, drawBackground)

    dataFileDir = '../PataData'
    rawXRange = [-10, 10]
    rawYRange = [-10, 10]
    scaledXRange = [200, 600]
    scaledYRange = [200, 600]
    scaleTrajectory = ScaleTrajectory(positionIndex, rawXRange, rawYRange,
                                      scaledXRange, scaledYRange)
    oldFPS = 5
    adjustFPS = AdjustDfFPStoTraj(oldFPS, FPS)
    getTrajectory = lambda trajectoryDf: scaleTrajectory(
        adjustFPS(trajectoryDf))
    trajectoryDf = lambda condition, index: pd.read_pickle(
        os.path.join(
            dataFileDir, 'condition={}'.format(condition) +
            'sampleIndex={}.pickle'.format(index)))
    stimulus = {
        condition: [
            getTrajectory(trajectoryDf(condition, index))
            for index in trajetoryIndexList
        ]
        for condition in conditionList
    }

    writerPath = os.path.join(
        resultsPath,
        experimentValues["name"]) + experimentValues["condition"] + '.csv'
    writer = WriteDataFrameToCSV(writerPath)

    txtPath = (os.path.join(
        resultsPath, experimentValues["condition"] + '-' +
        experimentValues["name"] + '.txt'))
    openReportTxt = OpenReportTxt(txtPath)
    displayFrames = 600

    trial = ReportTrial(conditionList, displayFrames, drawState, drawImage,
                        stimulus, colorSpace, numOfAgent, drawFixationPoint,
                        drawText, FPS, reportInstrucImage, openReportTxt)
    experiment = Experiment(trial,
                            writer,
                            experimentValues,
                            drawImage,
                            restImage,
                            drawBackGround,
                            hasRest=False)
    numOfBlock = 1
    numOfTrialsPerBlock = 1
    designValues = createDesignValues(
        exprimentVarableList * numOfTrialsPerBlock, numOfBlock)

    restDuration = 3
    drawImage(introductionImage1)

    experiment(designValues, restDuration)
    # drawBackGround()
    # drawImage(finishImage)

    print("Result saved at {}".format(txtPath))
Exemple #13
0
def main():
    numOfAgent = 4

    manipulatedVariables = co.OrderedDict()
    # manipulatedVariables['damping'] = [0.0]  # [0.0, 1.0]
    # manipulatedVariables['frictionloss'] = [0.0]  # [0.0, 0.2, 0.4]
    # manipulatedVariables['masterForce'] = [0.0]
    manipulatedVariables['damping'] = [0.0, 0.5]  # [0.0, 1.0]
    manipulatedVariables['frictionloss'] = [1.0]  # [0.0, 0.2, 0.4]
    manipulatedVariables['masterForce'] = [0.0]  # [0.0, 2.0]
    manipulatedVariables['offset'] = [0.0, 1.0]
    manipulatedVariables['hideId'] = [3, 4]
    manipulatedVariables['fps'] = [40, 50]
    manipulatedVariables['displayTime'] = [10, 15]

    chaseTrailVariables = manipulatedVariables.copy()
    # catchTrailVariables = manipulatedVariables.copy()
    # chaseTrailVariables['hideId'] = [3,4] #0 wolf 1 sheep 2 master 3 4 distractor
    # catchTrailVariables['hideId'] = [1]
    chaseTrailconditions = [
        dict(list(specificValueParameter))
        for specificValueParameter in it.product(
            *[[(key, value) for value in values]
              for key, values in chaseTrailVariables.items()])
    ]
    # catchTrailconditions = [dict(list(specificValueParameter)) for specificValueParameter in it.product(*[[(key, value) for value in values] for key, values in catchTrailVariables.items()])]

    conditionsWithId = list(
        zip(range(len(chaseTrailconditions)), chaseTrailconditions))
    print(conditionsWithId)
    conditions = chaseTrailconditions
    conditions = [
        condition.update({'conditionId': condtionId})
        for condtionId, condition in zip(range(len(conditions)), conditions)
    ]

    chaseTrailNum = 10
    chaseTrailTrajetoryIndexList = range(chaseTrailNum)
    chaseTrailManipulatedVariablesForExp = co.OrderedDict()
    chaseTrailManipulatedVariablesForExp['conditonId'] = range(
        len(chaseTrailconditions))
    chaseTrailManipulatedVariablesForExp[
        'trajetoryIndex'] = chaseTrailTrajetoryIndexList
    chaseTrailProductedValues = it.product(
        *[[(key, value) for value in values]
          for key, values in chaseTrailManipulatedVariablesForExp.items()])

    # catchTrailNum = 0
    # catchTrailTrajetoryIndexList = range (catchTrailNum)
    # catchTrailManipulatedVariablesForExp =  co.OrderedDict()
    # catchTrailManipulatedVariablesForExp['conditonId'] = range(len(chaseTrailconditions),len(chaseTrailconditions)+len(catchTrailconditions))
    # catchTrailManipulatedVariablesForExp['trajetoryIndex'] = catchTrailTrajetoryIndexList
    # catchTrailProductedValues = it.product(*[[(key, value) for value in values] for key, values in catchTrailManipulatedVariablesForExp.items()])

    # print(productedValues)
    exprimentVarableList = [
        dict(list(specificValueParameter))
        for specificValueParameter in chaseTrailProductedValues
    ]

    [
        exprimentVarable.update(
            {'condition': conditionsWithId[exprimentVarable['conditonId']][1]})
        for exprimentVarable in exprimentVarableList
    ]
    # exprimentVarableList = [exprimentVarable.update({'condition': conditionsWithId[exprimentVarable['conditonId']][1]}) for exprimentVarable in exprimentVarableList ]
    # print(exprimentVarableList)
    # print(len(exprimentVarableList))
    numOfBlock = 1
    numOfTrialsPerBlock = 1
    isShuffle = True
    designValues = createDesignValues(
        exprimentVarableList * numOfTrialsPerBlock, numOfBlock, isShuffle)

    positionIndex = [0, 1]
    standardFPS = 50
    rawXRange = [200, 600]
    rawYRange = [200, 600]
    scaledXRange = [200, 600]
    scaledYRange = [200, 600]
    scaleTrajectoryInSpace = ScaleTrajectory(positionIndex, rawXRange,
                                             rawYRange, scaledXRange,
                                             scaledYRange)
    oldFPS = 50
    numFramesToInterpolate = int(standardFPS / oldFPS - 1)
    interpolateState = InterpolateState(numFramesToInterpolate)
    scaleTrajectoryInTime = ScaleTrajectoryInTime(interpolateState)
    horizontalRotationTransformTrajectory = HorizontalRotationTransformTrajectory(
        positionIndex, rawXRange, rawYRange)
    rotationTransformTrajectory = RotationTransformTrajectory(
        positionIndex, rawXRange, rawYRange)

    def transFormTrajectory(trajList, randomId):
        randomSeed = np.mod(randomId, 8)
        rotationAngle = np.mod(randomSeed, 4) * np.pi / 2
        rotationTraj = rotationTransformTrajectory(trajList, rotationAngle)
        # if np.mod(randomSeed//4,2) ==1:
        # finalTrajs = horizontalRotationTransformTrajectory(rotationTraj)
        # else:
        finalTrajs = rotationTraj
        return finalTrajs

    trajectoriesSaveDirectory = '../PataData/exp2Traj'
    # trajectoriesSaveDirectory =os.path.join(dataFolder, 'trajectory', modelSaveName)
    trajectorySaveExtension = '.pickle'

    selctDict = {3: chaseTrailNum, 4: chaseTrailNum}
    evaluateEpisode = 120000
    evalNum = 20
    fixedParameters = {
        'distractorNoise': 3.0,
        'evaluateEpisode': evaluateEpisode
    }
    generateTrajectoryLoadPath = GetSavePath(trajectoriesSaveDirectory,
                                             trajectorySaveExtension,
                                             fixedParameters)
    trajectoryDf = lambda condition: loadFromPickle(
        generateTrajectoryLoadPath({
            'offset': condition['offset'],
            'hideId': condition['hideId'],
            'damping': condition['damping'],
            'frictionloss': condition['frictionloss'],
            'masterForce': condition['masterForce'],
            'select': selctDict[condition['hideId']]
        }))

    getTrajectory = lambda trajectoryDf: scaleTrajectoryInTime(
        scaleTrajectoryInSpace(trajectoryDf))

    print('loading')
    # stimulus = {conditionId:getTrajectory(trajectoryDf(condition))  for conditionId,condition in conditionsWithId}
    #
    transformedStimulus = {
        conditionId:
        transFormTrajectory(getTrajectory(trajectoryDf(condition)),
                            conditionId)
        for conditionId, condition in conditionsWithId
    }
    print('loding success')
    print(len(transformedStimulus[1]))
    experimentValues = co.OrderedDict()
    experimentValues["name"] = input("Please enter your name:").capitalize()

    screenWidth = 800
    screenHeight = 800

    fullScreen = True
    initializeScreen = InitializeScreen(screenWidth, screenHeight, fullScreen)
    screen = initializeScreen()

    leaveEdgeSpace = 180
    circleSize = 10
    clickImageHeight = 80
    lineWidth = 3
    fontSize = 50
    xBoundary = [leaveEdgeSpace, screenWidth - leaveEdgeSpace * 2]
    yBoundary = [leaveEdgeSpace, screenHeight - leaveEdgeSpace * 2]

    screenColor = THECOLORS['black']
    lineColor = THECOLORS['white']
    textColor = THECOLORS['white']
    fixationPointColor = THECOLORS['white']

    colorSpace = [(203, 164, 4, 255), (49, 153, 0, 255), (255, 90, 16, 255),
                  (251, 7, 255, 255), (9, 204, 172, 255), (3, 28, 255, 255)]

    picturePath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'pictures')
    resultsPath = os.path.join(
        os.path.abspath(os.path.join(os.getcwd(), os.pardir)), 'results',
        'exp2')
    if not os.path.exists(resultsPath):
        os.makedirs(resultsPath)
    introductionImage1 = pygame.image.load(
        os.path.join(picturePath, 'IdOnlyIntro1.png'))
    introductionImage2 = pygame.image.load(
        os.path.join(picturePath, 'IdOnlyIntro2.png'))
    finishImage = pygame.image.load(os.path.join(picturePath, 'over.jpg'))
    introductionImage1 = pygame.transform.scale(introductionImage1,
                                                (screenWidth, screenHeight))
    introductionImage2 = pygame.transform.scale(introductionImage2,
                                                (screenWidth, screenHeight))

    finishImage = pygame.transform.scale(
        finishImage, (int(screenWidth * 2 / 3), int(screenHeight / 4)))
    clickWolfImage = pygame.image.load(
        os.path.join(picturePath, 'clickwolf.png'))
    clickSheepImage = pygame.image.load(
        os.path.join(picturePath, 'clicksheep.png'))
    restImage = pygame.image.load(os.path.join(picturePath, 'rest.jpg'))

    drawImage = DrawImage(screen)
    drawText = DrawText(screen, fontSize, textColor)
    drawBackGround = DrawBackGround(screen, screenColor, xBoundary, yBoundary,
                                    lineColor, lineWidth)
    drawFixationPoint = DrawFixationPoint(screen, drawBackGround,
                                          fixationPointColor)
    drawImageClick = DrawImageClick(screen, clickImageHeight, drawText)

    drawState = DrawState(screen, circleSize, numOfAgent, positionIndex,
                          drawBackGround)

    writerPath = os.path.join(resultsPath, experimentValues["name"]) + '.csv'
    writer = WriteDataFrameToCSV(writerPath)

    # displayFrames = 500
    reactionWindowStart = 50
    keysForCheck = {'space': 1}
    checkHumanResponse = CheckHumanResponseWithSpace(keysForCheck)
    trial = ChaseTrialMujocoFps(conditionsWithId, reactionWindowStart,
                                drawState, drawImage, transformedStimulus,
                                checkHumanResponse, colorSpace, numOfAgent,
                                drawFixationPoint, drawText, drawImageClick,
                                clickWolfImage, clickSheepImage)

    experiment = Experiment(trial, writer, experimentValues, drawImage,
                            restImage, drawBackGround)

    restDuration = 120

    drawImage(introductionImage1)
    drawImage(introductionImage2)

    experiment(designValues, restDuration)
    # self.darwBackground()
    drawImage(finishImage)

    print("Result saved at {}".format(writerPath))