def chooseAction(continueDict, state):
    '''To choose whether to continue the previous direction,
    reverse the direction, or go to another direction.
    
    The decision is based on previous action and the 
    current state. For example, if previous action is 'ZOOM IN',
    then we only examine the 'z' value. The continueDict will give
    a probability to continue and a probability to reverse for each
    possible 'z' value.'''
    
    action = state["prevAction"]
    axis = related_axis(action)
    v =    related_value(action, state)
    
    #by default reverse
    p_continue, p_reverse = 0.5, 0.5
    try:
        count, p_continue, p_reverse, p_reset = continueDict[axis][v][action]
        #print "continue or not?:", axis, v, count, p_continue, p_reverse, p_reset
    except KeyError:
        print "Out of the range"
        pass #use the default value
    
    rand = random.nextDouble()
    #print "rand: ", rand, p_continue, p_continue+p_reverse, p_continue+p_reverse+p_reset
    if rand <= p_continue:
        return action
    elif rand <= p_continue + p_reverse:
        return reverseTable[action]
    elif rand <= p_continue + p_reverse + p_reset:
        return 'RESET'
    else:
        return 'CHANGE'
def selectDirection(popPlus, popMinus):
    propPlus = popPlus / (popPlus + popMinus)
    rand = random.nextDouble()
    if rand < propPlus:
        return "plus"
    else:
        return "minus"
def selectDirection(ratePlus, rateMinus):
    propPlus = ratePlus / (ratePlus + rateMinus)
    rand = random.nextDouble()
    if rand < propPlus:
        return "plus"
    else:
        return "minus"
def chooseAction(continueDict, state):
    '''To choose whether to continue the previous direction,
    reverse the direction, or go to another direction.
    
    The decision is based on previous action and the 
    current state. For example, if previous action is 'ZOOM IN',
    then we only examine the 'z' value. The continueDict will give
    a probability to continue and a probability to reverse for each
    possible 'z' value.'''
    
    action = state.prevAction
    axis = related_axis(action)
    v = eval(related_value(action))
    
    #by default reverse
    p_continue, p_reverse = 0.5, 0.5
    try:
        p_continue, p_reverse = continueDict[axis][v][action]
        print 'cont', p_continue, 'rev', p_reverse
    except KeyError:
        pass #use the default value
    
    rand = random.nextDouble()
    if rand <= p_continue:
        return action
        print 'to contiue'
    #elif rand <= p_continue + p_reverse:
    #    return reverseTable[action]
    else:
        return None
Beispiel #5
0
def divide_files(path, path_o1, path_o2):
    '''randomly divide files in a directory to two directories.'''
    for name in os.listdir(path):
        #ignore none "behavior" files
        if "behavior" not in name:
            continue
        fullname = os.path.join(path, name)
        rand = extend_random.nextDouble()
        if rand < 0.5:
            output_fullname = os.path.join(path_o1, name)
        else:
            output_fullname = os.path.join(path_o2, name)
        os.system("mkdir -p "+path_o1)
        os.system("mkdir -p "+path_o2)
        os.system("cp -f %s %s" % (fullname, output_fullname))
def chooseBeginAction(beginDict):
    '''In the beginning or after 'RESET', choose the first action
    at the default view point.'''
    accu_dict = {}
    sum = 0
    for action in sorted(beginDict.keys()):
        prob = beginDict[action]
        #print action, prob
        sum += prob
        accu_dict[action] = sum
        #print action, sum

    prob = random.nextDouble()
    for action in sorted(accu_dict.keys()):
        accu = accu_dict[action]
        #print action, accu, prob
        if prob <= accu:
            #print prob, "action ", action
            return action
def selectAxis(popularity):
    '''Choose the next axis to go along
    
    We add 1/popularity together, and choose the axis
    propotional to its 1/popularity. Exception: if the
    popularity of an axis is 0, we directly choose it.
    '''
    acc_1_pop = 0
    acc_res = []
    
    for ax, pop in popularity.iteritems():
        if (pop == 0):
            return ax
        acc_1_pop += (1 / pop)
        acc_res.append((ax, acc_1_pop))

    rand = random.nextDouble() * acc_1_pop
    for ax, acc in acc_res:
        #print "rand: ", rand, "axis", ax, "acc ", acc
        if rand <= acc:
            return ax