Пример #1
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():
    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():
    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)
Пример #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
    screen = pg.display.set_mode((screenWidth, screenHeight))
    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)

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

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

        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)

        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)

        numControlTrial = int(numExpTrial * 2 / 3)
        expTrials = [expCondition] * numExpTrial
        conditionList = list(expTrials + [rectCondition] * numExpTrial +
                             [straightLineCondition] * numControlTrial +
                             [midLineCondition] * numControlTrial +
                             [noAreaCondition] * numControlTrial)
        random.shuffle(conditionList)
        numNormalTrials = len(conditionList)
        conditionList.append(expCondition)

        noiseCondition = list(permutations([1, 2, 0], 3))
        noiseCondition.append((1, 1, 1))
        blockNumber = int(numNormalTrials / 3)
        noiseDesignValues = np.array([
            random.choice(noiseCondition) for _ in range(blockNumber)
        ]).flatten().tolist()
        noiseDesignValues.append('special')

        policy = pickle.load(
            open(
                os.path.join(machinePolicyPath,
                             "noise0.1WolfToTwoSheepGird15_policy.pkl"), "rb"))
        softmaxBeta = 2.5
        modelController = ModelController(policy, gridSize, softmaxBeta)
        controller = modelController

        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 = NormalNoise(noiseActionSpace, gridSize)

        experimentValues = co.OrderedDict()

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

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

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

        singleGoalExperiment(baseLineNoiseDesignValues, baseLineConditionList)
Пример #5
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)
Пример #6
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)