예제 #1
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)
예제 #2
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)
예제 #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'))
    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)
예제 #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
    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)
예제 #7
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)
예제 #8
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)
예제 #9
0
    screen = pygame.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)

    drawBackground = DrawBackground(screen, dimension, leaveEdgeSpace,
                                    backgroundColor, lineColor, lineWidth,
                                    textColorTuple)
    drawNewState = DrawNewState(screen, drawBackground, targetColor,
                                playerColor, targetRadius, playerRadius)

    creatStraightLineCondition = CreatStraightLineCondition(
        direction, dimension)
    width, height, distance, distanceDiff = [2, 2, 2, 0]

    target1, target2, playerGrid = creatStraightLineCondition(
        width, height, distance, distanceDiff)

    import pandas as pd
    df = pd.DataFrame(columns=('playerGrid', 'target1', 'target2'))
    coordinations = tuple(
        it.product(range(1, dimension - 1), range(1, dimension - 1)))
    stateAll = list(it.combinations(coordinations, 3))
    # print(len(stateAll))
    # condition = []
예제 #10
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)