Ejemplo n.º 1
0
def react_loveU(msg):
    prtLog("pat: "+msg.nick)
    value = getPerson(msg).plus(MAX)
    if value:
        send_msg(msg.channel, Value.randSatisfyMsg(msg))
    else:
        send_msg(msg.channel, Value.randAnnoyedMsg(msg))
Ejemplo n.º 2
0
def do_bellman_backup(state, momdp, prune1, prune2, valueFunction):
    """
    This function performs a multi-objective Bellman backup for a
    given state, and a given (previous) value function.
    Note that this value function consists of sets of value vectors
    per state, and is an intermediate coverage set
    """
    ##############################################################
    # V' = prune1 (Union_a R(s,a) +                              #
    #    (Fold(prune2 after crosssum, s') gamma*T(s,a,s') V(s')))#
    ##############################################################
    nActions = momdp.n_actions
    gamma = momdp.discountFactor()
    Vs = Value.ValueVectorSet()
    for i in range(nActions):
        tList = momdp.getTransitionProbabilities(state, i)
        rExpect = momdp.expected_reward(state, i)
        # DEBUG: print("R(s,"+str(i)+"): " + str(rExpect))
        # DEBUG: print(str(tList))
        # DEBUG: print("\n")
        Vnew = Value.ValueVectorSet()
        for j in range(len(tList)):
            factor = gamma * tList[j]
            Vsprime = valueFunction.getValue(j)
            Vtrans = Vsprime.multiplyByScalar(factor)
            Vnew = Vnew.crossSum(Vtrans)
            Vnew = prune2(Vnew)
        Vnew = Vnew.translate(rExpect)
        Vs.addAll(Vnew.set)
    Vs = prune1(Vs)
    return Vs
Ejemplo n.º 3
0
def react_mode(msg):
    if msg.msg == "+o " + NICK:
        send_msg(msg.channel, Value.randOPMsg(msg))
    elif msg.msg == "-o " + NICK:
        send_msg(msg.channel, Value.randDEOPMsg(msg))
    elif msg.msg.find(NICK) != -1:
        send_msg(msg.channel, Value.randCuriousMsg(msg))
Ejemplo n.º 4
0
def _parse(tokenIter):
    """ _parse() recursively extract the expression from the given
        token iterator
        //param tokenIter is the token iterator that represents the
               stream of tokens
        //return a pair, where the first parameter reports the error code
                occurs while parsing (OK indicates no error happens);
                the second parameter is the extracted expression, which will
                be None if error occurs.
    """
    try:
        token = tokenIter.next()
    except StopIteration:
        return ParseError.TOKEN_NORMALLY_ENDS, None

    # Token type is ERROR
    if token.tokenType == Tokens.ERROR:
        return ParseError.TOKENIZE_ERROR, token

    # All tokens, except the special characters, are considered. special char are: ( ) '
    if not token.tokenType in Tokens.specialCharacters:
        return ParseError.OK, Value.makeFromToken(token)

    # check if this is the end of an expression
    if token.tokenType == Tokens.RPAREN:
        return ParseError.CE_ENDS, token

    """
    # If the token either indicates the expression element
    # (operator, parameters)nor the end of expression, then it
    # must be the "beginning" of a expression.
    if token.tokenType != Tokens.LPAREN:
        return ParseError.EXPECT_LPAREN, token
    """
    if token.tokenType == Tokens.QUOTE:
        error, result = _parse(tokenIter)
        return ParseError.OK, Value.makeLiteral(result)

    sList = []
    # Read parameters, operator is placed together with parameter as the first element of the list
    while True:
        error, param = _parse(tokenIter)

        if error != ParseError.OK:
            # reach the end the this expression?
            if error == ParseError.CE_ENDS:
                break
            elif error == ParseError.TOKEN_NORMALLY_ENDS:
                return ParseError.TOKEN_UNEXPECTED_ENDS, None #position: the end of the expression
            # or real error occurs
            else:
                return error, param

        sList.append(param)

    return ParseError.OK, Value.makeList(sList)
Ejemplo n.º 5
0
def react_sleep(msg):
    prtLog("sleep: "+msg.nick)
    if msg.ID == ID_NORANG:
        quit(msg.channel, Value.randQuitMsg(msg))
        return True
    else:
        if getPerson(msg).minus(MAX):
            send_msg(msg.channel, Value.randNoQuitMsg(msg))
        else:
            send_msg(msg.channel, Value.randAnnoyedMsg(msg))
        return False
Ejemplo n.º 6
0
def pointBasedPrune(vvSet, points=[]):
    """
    Returns a new ValueVectorSet from which all vectors that are not
    optimal for any weight in `points' are removed, as well as a list
    of the scalarised values at each eight in `points'
    """
    result = Value.ValueVectorSet()
    scalVals = []
    for i in range(len(points)):
        tup = vvSet.removeMaximisingLinearScalarisedValue(points[i])
        result.add(tup[0])
        scalval = Value.inner_product(tup[0], points[i])
        scalVals.append(scalval)
    return result, scalVals
Ejemplo n.º 7
0
def multiobjective_value_iteration(momdp, prune1, prune2, differenceFunction,
                                   threshold, maxiter):
    """
    This function does multi-objective variable elimination.
    For a description of the algorithm + examples see our tutorial slides:
        Multi-Objective Decision Making
        Shimon Whiteson & Diederik M. Roijers
        European Agent Systems Summer School (EASSS)
        Catatia (Italy), 25th July 2016
        http://roijers.info/pub/easss.pdf
    Part 2: from slide 62 (pdf numbering 91) onwards (CHVI)
    """
    #####################################
    # first initialise the value function#
    #####################################
    lst = []
    nObj = momdp.n_objectives
    for i in range(nObj):
        lst.append(0.0)
    vec = numpy.array(lst)
    V = Value.ValueFunction(momdp.n_states)
    V.addVectorToAllSets(vec)
    ############################################################
    # then do Bellman backups until the value function converges#
    ############################################################
    cnt = 0
    difference = True
    while (cnt < maxiter) and difference:
        V2 = do_bellman_backup_all_states(momdp, prune1, prune2, V)
        diff = differenceFunction(V, V2)
        cnt += 1
        difference = diff > threshold
        V = V2
    return V
Ejemplo n.º 8
0
    def parse_octal(self):
        result = ""
        while (self.current_char != '\0' and self.current_char.isdigit()):
            result += self.current_char
            self.advance()

        return self.make_token(TokenType.INTEGER, Value(int(result, 8)))
Ejemplo n.º 9
0
    def parse_match_spec(self, with_m=False):
        """ Parse out a regex match specification i.e. m/Hello/
            This returns a MATCH_SPEC token with a value that is 
            an object:
            { 'type':'m', 'spec':<regex pattern>, 'opts':<flags> }
        """
        
        start_pos = self.pos
        match_regex = ''
        opts = ''
        start_char = '/'
        if with_m:
            start_char = self.current_char
        char_count = 1  # have to have 2 start char's for a good pat
        self.advance()
        last_char = None
        while (self.current_char != start_char) and self.current_char != '\0':
            last_char = self.current_char
            #if self.current_char == '\\' and last_char != '\\':
            match_regex += self.current_char
            self.advance()
        if self.current_char == start_char:
            char_count += 1
        self.advance()
        while self.current_char.isalpha():
            opts += self.current_char
            self.advance()

        if char_count == 2:
            return self.make_token(TokenType.MATCH_SPEC, Value({'type': 'm', 'spec': match_regex, 'opts': opts}))
        else:
            self.pos = start_pos # go back to the divide sign because its a math op, not a pattern
            return None
Ejemplo n.º 10
0
    def parse_trans_spec(self):
        """ Parse out a transliteration specification (e.g. tr/a-z/A-Z/ or y/a-z/A-Z/)
            This returns an object:
            { 'type':'y', 'spec':<tr pattern>, 'repl':<replacement string> }
        """
        
        search_spec = ''
        repl_spec = ''
        start_char = self.current_char
        self.advance()
        last_char = None
        while (self.current_char != start_char) and self.current_char != '\0':
            last_char = self.current_char
            search_spec += self.current_char
            self.advance()

        self.advance()
        last_char = None
        while (self.current_char != start_char) and self.current_char != '\0':
            last_char = self.current_char
            repl_spec += self.current_char
            self.advance()

        self.advance()
        return self.make_token(TokenType.TRANS_SPEC, Value({'type': 'y', 'spec': search_spec, 'repl': repl_spec}))
Ejemplo n.º 11
0
 def __init__(self, size_row, size_col, input_matrix, terminal, reward):
     self.val = Value.Value(size_row, size_col, input_matrix)
     self.pol = Policy.policy(size_row, size_col, terminal)
     self.reward = reward
     self.terminal = terminal
     self.size_col = size_col
     self.size_row = size_row
Ejemplo n.º 12
0
    def parse_string(self):
        """ Double quoted string """

        result = ""
        while (self.current_char != '\0'):
            if (self.current_char == '"'):
                break  # end of string
            elif (self.current_char == '\\' and self.peek() == '"'):
                self.advance()  # deal with double quote literals
            elif (self.current_char == '\\' and self.peek() == 'n'):
                result += '\n'
                self.advance()
                self.advance()
                continue
            elif (self.current_char == '\\' and self.peek() == 'r'):
                result += '\r'
                self.advance()
                self.advance()
                continue
            elif (self.current_char == '\\' and self.peek() == '\\'):
                result += '\\'
                self.advance()
                self.advance()
                continue
            elif (self.current_char == '\\' and self.peek() == 't'):
                result += '\t'
                self.advance()
                self.advance()
                continue
            result += self.current_char
            self.advance()

        self.advance()
        return self.make_token(TokenType.INTERP_STR, Value(result))
Ejemplo n.º 13
0
    def parse_number(self):
        result = ""
        while self.current_char != '\0' and self.current_char.isdigit():
            result += self.current_char
            self.advance()

        if self.current_char == '.':
            result += self.current_char
            self.advance()
            while self.current_char != '\0' and self.current_char.isdigit():
                result += self.current_char
                self.advance()
        else:
            return self.make_token(TokenType.INTEGER, Value(int(result)))

        return self.make_token(TokenType.FLOAT, Value(float(result)))
Ejemplo n.º 14
0
    def parse_backticks(self):
        cmdstr = ''
        while (self.current_char != '\0' and self.current_char != '`'):
            cmdstr += self.current_char
            self.advance()

        self.advance()
        return self.make_token(TokenType.BACKTICKS, Value(cmdstr))
Ejemplo n.º 15
0
    def parse_hex(self):
        result = ""
        while (self.current_char != '\0' and (
                self.current_char in string.hexdigits)):
            result += self.current_char
            self.advance()

        return self.make_token(TokenType.INTEGER, Value(int(result, 16)))
Ejemplo n.º 16
0
def do_trans_op(vm, name, index_expr, spec, invert):
    v = None
    idx = None
    cxt = None
    if name != None:
        if (index_expr == True):
            idx = vm.stack.pop()
            if type(idx._val) is str:
                cxt = 'hash'
                v = vm.get_variable(name, 'hash')
                v = v[str(idx)].scalar_context()
            else:
                cxt = 'list'
                v = vm.get_variable(name, 'list')
                v = v[int(idx)].scalar_context()
        else:
            cxt = 'scalar'
            v = vm.get_variable(name, 'scalar').scalar_context()
    else:
        vm.get_variable('_', 'scalar')  # get the $_ var

    trans_spec = spec._val['spec']
    repl_spec = spec._val['repl']

    # interpolate it
    vm.perform_interpolated_push(repl_spec)
    repl_spec = vm.stack.pop().stringify()

    ret = transliteration(v.stringify(), trans_spec, repl_spec)

    if (index_expr == True):
        if cxt == 'list':
            v[idx] = Value(ret)
            vm.set_variable(name, v, 'list')
        else:
            v[idx] = Value(ret)
            vm.set_variable(name, v, 'hash')
    else:
        vm.set_variable(name, Value(ret), 'scalar')

    if (bool(ret) ^ invert):
        vm.stack.push(Value(1))
    else:
        vm.stack.push(Value(0))
Ejemplo n.º 17
0
def valueTests():
    v = Value()
    print(v)
    v2 = Value(5)
    v3 = Value(7)
    v4 = Value.ea(v2, v3, 3, 2)
    v5 = Value.atLocation(Location(atAddr=v4))
    v6 = v5.plus(v4)
    v7 = v3.plus(v6)
    v8 = v7.plus(Value.atLocation(Location(atAddr=v2)))
    assert (str(v2) == "0x5")
    assert (str(v3) == "0x7")
    assert (str(v4) == "0x17")
    assert (str(v5) == "[0x17]")
    assert (str(v6) == "0x17 + [0x17]")
    assert (str(v7) == "0x1e + [0x17]")
    assert (str(v8) == "0x1e + [0x5] + [0x17]")

    print("Success for Values")
Ejemplo n.º 18
0
 def __init__(self, id, n_sellers):
     self.id = id
     self.price = 100
     self.epsilon = 0.9
     self.epsilon_decay = 0.01
     self.Qvalue = Value.Qvalue(n_sellers + 2)
     self.is_in_game = True
     self.action = 0
     self.gamma = 0.9
     self.reward = 0
Ejemplo n.º 19
0
def do_match_op(vm, name, index_expr, spec, invert):
    v = None
    if name != None:
        if (index_expr == True):
            idx = vm.stack.pop()
            if type(idx._val) is str:
                v = vm.get_variable(name, 'hash')
                v = v[str(idx)].scalar_context()
            else:
                v = vm.get_variable(name, 'list')
                v = v[int(idx)].scalar_context()
        else:
            v = vm.get_variable(name, 'scalar').scalar_context()
    else:
        vm.get_variable('_', 'scalar')  # get the $_ var

    regex = spec._val['spec']
    options = spec._val['opts']
    ret = re.search(regex, v.stringify(), parse_re_opts(vm, options))
    for i in range(10):
        vm.set_variable(str(i + 1), Value(None), 'scalar')
    vm.set_variable('+', Value(None), 'scalar')
    if ret and ret.lastindex:
        for i in range(0, ret.lastindex):
            vm.set_variable(str(i + 1), Value(ret.group(i + 1)), 'scalar')
            vm.set_variable('+', Value(ret.group(i + 1)), 'scalar')

    if (bool(ret) ^ invert):
        vm.stack.push(Value(1))
    else:
        vm.stack.push(Value(0))
Ejemplo n.º 20
0
    def testPolicyGreedy(self):
        input_matrix = [[0., -1., -1.], [-1., -1., -1.], [-1., -1., 0.]]
        terminal = [(0, 0), (2, 2)]
        val = Value.Value(3, 3, input_matrix)
        pl = Policy.policy(3, 3, terminal)
        val.update(pl, -1, terminal)
        pl.greedy(val, terminal)

        self.assertAlmostEqual(pl.policy[0][0][UP], 0.0)
        self.assertAlmostEqual(pl.policy[0][0][DN], 0.0)
        self.assertAlmostEqual(pl.policy[0][0][LT], 0.0)
        self.assertAlmostEqual(pl.policy[0][0][RT], 0.0)

        self.assertAlmostEqual(pl.policy[0][1][UP], 0.0)
        self.assertAlmostEqual(pl.policy[0][1][DN], 0.0)
        self.assertAlmostEqual(pl.policy[0][1][LT], 1.0)
        self.assertAlmostEqual(pl.policy[0][1][RT], 0.0)

        self.assertAlmostEqual(pl.policy[0][2][UP], 0.0)
        self.assertAlmostEqual(pl.policy[0][2][DN], 0.5)
        self.assertAlmostEqual(pl.policy[0][2][LT], 0.5)
        self.assertAlmostEqual(pl.policy[0][2][RT], 0.0)

        self.assertAlmostEqual(pl.policy[1][0][UP], 1.0)
        self.assertAlmostEqual(pl.policy[1][0][DN], 0.0)
        self.assertAlmostEqual(pl.policy[1][0][LT], 0.0)
        self.assertAlmostEqual(pl.policy[1][0][RT], 0.0)

        self.assertAlmostEqual(pl.policy[1][1][UP], 0.25)
        self.assertAlmostEqual(pl.policy[1][1][DN], 0.25)
        self.assertAlmostEqual(pl.policy[1][1][LT], 0.25)
        self.assertAlmostEqual(pl.policy[1][1][RT], 0.25)

        self.assertAlmostEqual(pl.policy[1][2][UP], 0.0)
        self.assertAlmostEqual(pl.policy[1][2][DN], 1.0)
        self.assertAlmostEqual(pl.policy[1][2][LT], 0.0)
        self.assertAlmostEqual(pl.policy[1][2][RT], 0.0)

        self.assertAlmostEqual(pl.policy[2][0][UP], 0.5)
        self.assertAlmostEqual(pl.policy[2][0][DN], 0.0)
        self.assertAlmostEqual(pl.policy[2][0][LT], 0.0)
        self.assertAlmostEqual(pl.policy[2][0][RT], 0.5)

        self.assertAlmostEqual(pl.policy[2][1][UP], 0.0)
        self.assertAlmostEqual(pl.policy[2][1][DN], 0.0)
        self.assertAlmostEqual(pl.policy[2][1][LT], 0.0)
        self.assertAlmostEqual(pl.policy[2][1][RT], 1.0)

        self.assertAlmostEqual(pl.policy[2][2][UP], 0.0)
        self.assertAlmostEqual(pl.policy[2][2][DN], 0.0)
        self.assertAlmostEqual(pl.policy[2][2][LT], 0.0)
        self.assertAlmostEqual(pl.policy[2][2][RT], 0.0)
Ejemplo n.º 21
0
    def testValueConstructor(self):
        input_matrix = []
        input_matrix = [[0., -1., -1.], [-1., -1., -1.], [-1., -1., 0.]]
        val = Value.Value(3, 3, input_matrix)

        self.assertAlmostEqual(val.value[0][0], 0)
        self.assertAlmostEqual(val.value[0][1], -1)
        self.assertAlmostEqual(val.value[0][2], -1)
        self.assertAlmostEqual(val.value[1][0], -1)
        self.assertAlmostEqual(val.value[1][1], -1)
        self.assertAlmostEqual(val.value[1][2], -1)
        self.assertAlmostEqual(val.value[2][0], -1)
        self.assertAlmostEqual(val.value[2][1], -1)
        self.assertAlmostEqual(val.value[2][2], 0)
Ejemplo n.º 22
0
    def testValueUpdate_vi(self):
        input_matrix = [[0., -1., -1.], [-1., -1., -1.], [-1., -1., 0.]]
        terminal = [(0, 0), (2, 2)]
        val = Value.Value(3, 3, input_matrix)

        val.update_vi(terminal, -1)

        self.assertAlmostEqual(val.value[0][0], 0)
        self.assertAlmostEqual(val.value[0][1], -1)
        self.assertAlmostEqual(val.value[0][2], -2)
        self.assertAlmostEqual(val.value[1][0], -1)
        self.assertAlmostEqual(val.value[1][1], -2)
        self.assertAlmostEqual(val.value[1][2], -1)
        self.assertAlmostEqual(val.value[2][0], -2)
        self.assertAlmostEqual(val.value[2][1], -1)
        self.assertAlmostEqual(val.value[2][2], 0)
Ejemplo n.º 23
0
    def shuffle(self):
        self.reset_grid()

        # It creates a linear space of dim dimension spaced from 0 to len of colors list
        linear_space = np.linspace(0, len(self.colors) - 1, num=self.dim)

        for i in range(self.dim):
            random_x = random.randint(0, self.dim)
            random_y = random.randint(0, self.dim)
            random_value = random.randint(0, self.dim - 1)
            self.pixel_grid.append(Value(random_x, random_y, random_value))

            color_from_linspace = int(linear_space[random_value])
            color = self.colors[color_from_linspace]

            self.pixel_grid[i].set_color(color)
Ejemplo n.º 24
0
    def testValueUpdate(self):
        input_matrix = [[0., -1., -1.], [-1., -1., -1.], [-1., -1., 0.]]
        terminal = [(0, 0), (2, 2)]
        val = Value.Value(3, 3, input_matrix)
        pl = Policy.policy(3, 3, terminal)
        val.update(pl, -1, terminal)

        self.assertAlmostEqual(val.value[0][0], 0)
        self.assertAlmostEqual(val.value[0][1], -1.75)
        self.assertAlmostEqual(val.value[0][2], -2)
        self.assertAlmostEqual(val.value[1][0], -1.75)
        self.assertAlmostEqual(val.value[1][1], -2)
        self.assertAlmostEqual(val.value[1][2], -1.75)
        self.assertAlmostEqual(val.value[2][0], -2)
        self.assertAlmostEqual(val.value[2][1], -1.75)
        self.assertAlmostEqual(val.value[2][2], 0)
Ejemplo n.º 25
0
    def parse_literal_string(self):
        """ Single quoted string - no interpolation """

        result = ""
        while (self.current_char != '\0'):
            if (self.current_char == "'"):
                break  # end of string
            elif (self.current_char == '\\' and self.peek() == '\\'):
                result += '\\'
                self.advance()
                self.advance()
                continue
            result += self.current_char
            self.advance()

        self.advance()
        return self.make_token(TokenType.STR, Value(result))
Ejemplo n.º 26
0
    def __init__(self, chromosome, resEval, penEval, fitnessEval, pop):

        self.Chromosome = chromosome
        self.ResEval = resEval
        self.PenEval = penEval
        self.FitnessEval = fitnessEval
        self.PopulationSize = 400
        self.Elitism = 2
        self.SelectionOp = TournamentSelectionOperator(2, 2)
        self.RecombinationOp = DrunkRecombinationOperator()
        self.MutationOp = None

        self.PenEval.Evaluate(pop)
        self.FitnessEval.Evaluate(pop)
        pop.SortByFitness()

        self.Population = pop
        rdb = Surrogate.ResponseDatabaseFromPopulation(self.Population)
        sdb = Surrogate.SuccessDatabaseFromPopulation(self.Population)
        self.ResponseModel = Surrogate.RBFNetwork()
        self.ResponseModel.Train(rdb)
        self.SuccessModel = Surrogate.RBFNetwork()
        self.SuccessModel.Train(sdb)
        self.SurrogateResEval = SurrogateResponseEvaluator(
            self.ResponseModel, self.SuccessModel, self.Chromosome)

        self.SurrogateChromosome = self.Chromosome.Clone()
        self.SurrogateChromosome.Responses.append(
            Value("TorczonMerit", "FLOAT", 0.0))
        self.SurrogatePenEval = copy.deepcopy(self.PenEval)
        self.SurrogatePenEval.ObjectivePenalties.append(
            Penalty("MAXIMIZE", "TorczonMerit", exponent=1.0))
        #		self.SurrogatePenEval.ConstraintPenalties.append(Penalty("LOWER_BOUND", "__SUCCESS__", 100.0, 1.0, 0.5))

        self.TrustRegion = TrustRegion(self.SurrogateChromosome)
        self.TrustRegionSize = 1.0
        self.TrustRegionCoeffWhenImproved = 1.3  # 1.2
        self.TrustRegionCoeffWhenNotImproved = 0.9  # 0.7
        self.TrustRegionSizeMinimum = 0.05

        self.MeritWeight = 0.01

        self.HistoryOutput = open("history.dat", "w")
Ejemplo n.º 27
0
    def parse_subs_spec(self):
        """ Parse out a substitution specification (e.g. s/Hello/World/gi) """

        search_spec = ''
        repl_spec = ''
        opts = ''
        start_char = self.current_char # establish the start char
        self.advance()
        last_char = None
        while (self.current_char != start_char) and self.current_char != '\0':
            last_char = self.current_char
            if self.current_char == '\\' and self.peek() == '\\':
                search_spec += '\\'
                self.advance()
                self.advance()
            else:
                search_spec += self.current_char
                self.advance()

        self.advance()
        last_char = None
        while (self.current_char != start_char) and self.current_char != '\0':
            last_char = self.current_char
            if self.current_char == '\\' and start_char == '\'':
                repl_spec += '\\\\'
                self.advance()
            else:
                repl_spec += self.current_char
                self.advance()

        self.advance()
        while self.current_char.isalpha():
            opts += self.current_char
            self.advance()

        return self.make_token(TokenType.SUBS_SPEC, Value({'type': 's', 'spec': search_spec, 'repl': repl_spec, 'opts': opts, 'separator': start_char}))
Ejemplo n.º 28
0
def equivalenceTests():

    v1 = Value(5)
    v2 = Value(7)
    r1 = Location(reg="eax")
    r2 = Location(reg="r1")
    r3 = Location(reg="ebx")
    r4 = Location(reg="r2")
    vl1 = Value.atLocation(r1)
    vl2 = Value.atLocation(r2)
    vl3 = Value.atLocation(r3)
    vl4 = Value.atLocation(r4)
    l3 = Location(base=vl1)
    l4 = Location(base=vl2)
    l5 = Location(base=vl1, index=vl3)
    l6 = Location(base=vl2, index=vl4)
    vl5 = Value.atLocation(l5)
    vl6 = Value.atLocation(l6)
    a1 = AddAction(r1, r3)
    a2 = AddAction(r2, r2)
    m1 = MemState()
    m2 = MemState()
    m1.setValueAt(r1, vl5)
    m1.setValueAt(r3, v1)
    m2.setValueAt(r2, vl6)
    m2.setValueAt(r4, v1)

    print(v1.equivalence(v2))
    print(v1.equivalence(Value(5)))
    print(r1, r2, r1.equivalence(r2))
    print(r3, r4, r3.equivalence(r4))
    print(l5, l6, l5.equivalence(l6))
    print(a1, a2, a1.equivalence(a2))
    print(m1, m2, m1.equivalence(m2))

    a1 = XorAction(r1, r1)
    a2 = XorAction(r1, r1)
    print(a1, a2, a1.equivalence(a2))
Ejemplo n.º 29
0
def do_bellman_backup_all_states(momdp, prune1, prune2, valueFunction):
    Vnew = Value.ValueFunction(momdp.n_states)
    for i in range(momdp.n_states):
        Vi = do_bellman_backup(i, momdp, prune1, prune2, valueFunction)
        Vnew.setValue(i, Vi)
    return Vnew
Ejemplo n.º 30
0
def react_RUOK(msg):
    prtLog("RUOK: "+msg.nick)
    if getPerson(msg).plus(LITTLE):
        send_msg(msg.channel, Value.randOKMsg(msg))
    else:
        send_msg(msg.clue.randAnnoyedMsg(msg))
Ejemplo n.º 31
0
def react_tuna(msg):
    send_msg(msg.channel, Value.randTunaMsg(msg))
Ejemplo n.º 32
0
def react_goAway(msg):
    prtLog("goAway: "+msg.nick)
    part(msg.channel, Value.randPartMsg(msg))
    getPerson(msg).minus(MEDIUM)
Ejemplo n.º 33
0
    def get_id(self):
        """ Discern between an ID/varname and a language reserved keyword """

        name = ""
        first_char = True
        while (
            self.current_char != '\0' and (self.current_char.isalnum() or self.current_char == '_'
                or (self.current_char in self.allowable_var_chars
                    and self.prev_token.type == TokenType.SCALAR and first_char))):
            name += self.current_char
            first_char = False
            self.advance()

        # check to see if its a label first
        if self.current_char == ':':
            self.advance()
            return self.make_token(TokenType.LABEL, Value(name))

        if (name == "if"):
            return self.make_token(TokenType.IF, Value('if'))
        elif (name == "elsif"):
            return self.make_token(TokenType.ELSIF, Value('elsif'))
        elif (name == "else"):
            return self.make_token(TokenType.ELSE, Value('else'))
        elif (name == "while"):
            return self.make_token(TokenType.WHILE, Value('while'))
        elif (name == "unless"):
            return self.make_token(TokenType.UNLESS, Value('unless'))
        elif (name == "for"):
            return self.make_token(TokenType.FOR, Value('for'))
        elif (name == 'sub'):
            return self.make_token(TokenType.FUNCTION_DECLARE, Value('sub'))
        elif (name == "do"):
            return self.make_token(TokenType.DO, Value('do'))
        elif (name == "last"):
            return self.make_token(TokenType.LAST, Value('last'))
        elif (name == "next"):
            return self.make_token(TokenType.NEXT, Value('next'))
        elif (name == "redo"):
            return self.make_token(TokenType.REDO, Value('redo'))
        elif (name == "goto"):
            return self.make_token(TokenType.GOTO, Value('goto'))
        elif (name == "continue"):
            return self.make_token(TokenType.CONTINUE, Value('continue'))
        elif (name == "until"):
            return self.make_token(TokenType.UNTIL, Value('until'))
        else:
            return self.make_token(TokenType.ID, Value(name))
Ejemplo n.º 34
0
def react_dog(msg):
    prtLog("dog: "+msg.nick)
    if getPerson(msg).minus(LITTLE):
        send_msg(msg.channel, Value.randHateMsg(msg))
    else:
        send_msg(msg.channel, Value.randAnnoyedMsg(msg))
Ejemplo n.º 35
0
def do_subs_op(vm, name, index_expr, spec, invert):
    var = None
    v = None
    idx = None
    cxt = None
    if name != None:
        if (index_expr == True):
            idx = vm.stack.pop()
            if type(idx._val) is str:
                cxt = 'hash'
                var = vm.get_variable(name, 'hash')
                v = var[str(idx)].scalar_context()
            else:
                cxt = 'list'
                var = vm.get_variable(name, 'list')
                v = var[int(idx)].scalar_context()
        else:
            cxt = 'scalar'
            v = vm.get_variable(name, 'scalar').scalar_context()
    else:
        name = '_'
        v = vm.get_variable('_', 'scalar')  # get the $_ var

    regex = spec._val['spec']
    repl = spec._val['repl']

    # interpolate it
    vm.perform_interpolated_push(regex)
    regex = vm.stack.pop().stringify()

    options = spec._val['opts']
    re_obj = re.compile(regex, parse_re_opts(vm, options))
    ret = re_obj.search(v.stringify())

    # populate $1 thru $10
    for i in range(10):
        vm.set_variable(str(i + 1), Value(None), 'scalar')
    if ret and ret.lastindex:
        for i in range(0, ret.lastindex):
            vm.set_variable(str(i + 1), Value(ret.group(i + 1)), 'scalar')

    if ((bool(ret) ^ invert) and ret.group(0) != ''):
        # interpolate it -- but only if the subs spec isn't delimited by " ' "
        # which is a Perl 1 thing, definitely not in Perl5
        if (spec._val['separator'] != "'"):
            vm.perform_interpolated_push(repl)
            repl = vm.stack.pop().stringify()

        mod_string = None
        num_repls = None
        if ('g' in options):
            mod_string, num_repls = re_obj.subn(repl, v.stringify(), 0)
        else:
            mod_string, num_repls = re_obj.subn(repl, v.stringify(), 1)

        if (index_expr == True):
            if cxt == 'list':
                var[int(idx)] = Value(mod_string)
                vm.set_variable(name, var, 'list')
            else:
                var[str(idx)] = Value(mod_string)
                vm.set_variable(name, var, 'hash')
        else:
            vm.set_variable(name, Value(mod_string), 'scalar')

        vm.stack.push(Value(num_repls))
    else:
        vm.stack.push(Value(0))
Ejemplo n.º 36
0
import Value

# TODO: no error checking at this time
def add(params):
    return Value.makeNumber(sum(item.val for item in params))

def minus(params):
    return Value.makeNumber(params[0].val - params[1].val)

BuildIns = {
    # TODO: should pass the param in the future
    "+": Value.makeProcedure(add, None, None, False),
    "-": Value.makeProcedure(minus, None, None, False),
}

Ejemplo n.º 37
0
    def get_next_token(self):
        """ This is used by the parser to get the next token - which 
            is of "Token" type (defined in TokenType.py).  This is kind
            of a crazy function as a lot is going on, and I know it could
            be written much better
        """
        
        while (self.current_char != '\0'):
            # TODO: fix this mess, does a lookbehind to see if previous
            # chars where '$' or '$#' or '@' so that we know we are to parse
            # an ID, and not some other token, say REPEAT, or NOT, etc...

            # first check if last char was SIGIL '$' or '$#'
            if ((self.pos != 0 and self.peek(-1) == '$')
                or (self.pos != 0 and self.peek(-1) == '#' and self.peek(-2) == '$')
                or (self.pos != 0 and self.peek(-1) == '@')):
                if self.current_char and (self.peek(-1) != '@') == ' ':
                    self.advance()
                    return self.make_token(TokenType.ID, Value(' '))

                # TODO: fix this whole mess, because the Parser will do lookaheads
                # and rewinds, this destroys the whole concept of an accurate true 'previous token'
                # hack alert - manually set the prev token type to SCALAR since previous char was '$'
                if (self.current_char.isalnum()
                        or (self.current_char in self.allowable_var_chars)):
                    self.prev_token = Token(TokenType.SCALAR, Value('$'))
                    return self.get_id()

            if (self.current_char.isspace()):
                if (self.current_char == '\r' or self.current_char == '\n'):
                    if (self.current_char == '\r' and self.peek() == '\n'):
                        self.advance()
                    self.advance()
                    self.line_number += 1
                self.skip_whitespace()
                continue

            if (self.current_char == '0' and self.peek() == 'x'):
                self.advance()
                self.advance()
                return self.parse_hex()

            if (self.current_char == '0' and self.peek() == 'b'):
                self.advance()
                self.advance()
                return self.parse_binary()

            if (self.current_char == '#'):
                self.advance()
                return self.comment()

            # if (self.current_char == 'x'):
                # self.advance()
                # if (self.current_char == '='):
                    # self.advance()
                    # return self.make_token(TokenType.REPEAT_INCR, Value('x='))
                # return self.make_token(TokenType.REPEAT, Value('x'))

            if (self.current_char == 'x' and self.peek() == '='):
                self.advance()
                self.advance()
                return self.make_token(TokenType.REPEAT_INCR, Value('x='))

            if (self.current_char.isdigit()):
                if self.pos > 0:
                    # look back to see if prevous char was sigil
                    # so we don't muck up $1, $2, ... as id's
                    if self.peek(-1) not in [ '$', '@', '%' ]:
                        if (self.current_char == '0'):
                            return self.parse_octal()
                        return self.parse_number()
                else:
                    return self.parse_number()

            if (self.current_char == '"'):
                self.advance()
                return self.parse_string()

            if (self.current_char == "'"):
                self.advance()
                return self.parse_literal_string()

            if (self.current_char == 'm' and not self.peek().isalpha()):
                self.advance()
                return self.parse_match_spec(True)

            if ((self.current_char == 'y'  and not self.peek().isalpha())
                    or (self.current_char == 't' and self.peek() == 'r') and not self.peek(2).isalpha()):
                if (self.current_char == 'y'):
                    self.advance()
                    return self.parse_trans_spec()
                else:
                    self.advance()
                    self.advance()
                    return self.parse_trans_spec()

            if (self.current_char == 's' and not self.peek().isalpha()):
                self.advance()
                return self.parse_subs_spec()

            if (self.current_char.isalnum() or self.current_char == '_'):
                if (self.current_char.upper() == 'E' and
                        self.peek().upper() == 'Q' and self.peek(2).isspace()):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.STR_EQ, Value('eq'))
                elif (self.current_char == 'n' and
                      self.peek() == 'e' and self.peek(2).isspace()):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.STR_NEQ, Value('ne'))
                elif (self.current_char == 'l' and
                      self.peek() == 't' and self.peek(2).isspace()):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.STR_LT, Value('lt'))
                elif (self.current_char == 'l' and
                      self.peek() == 'e' and self.peek(2).isspace()):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.STR_LE, Value('le'))
                elif (self.current_char == 'g' and
                      self.peek() == 'e' and self.peek(2).isspace()):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.STR_GE, Value('ge'))
                elif (self.current_char == 'g' and
                      self.peek() == 't' and self.peek(2).isspace()):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.STR_GT, Value('gt'))
                else:
                    return self.get_id()

            if (self.current_char == '?'):
                self.advance()
                return self.make_token(TokenType.TERNARY, Value('TERNARY'))

            if (self.current_char == '$' and self.peek() == ' '):
                self.advance()
                self.prev_scalar = True
                return self.make_token(TokenType.SCALAR, Value('SCALAR'))

            if (self.current_char == '$' and (self.peek() == '#') and (self.peek(2).isalpha() or self.peek(2) == '_')):
                self.advance()
                self.advance()
                return self.make_token(TokenType.LIST_MAX_INDEX, Value('$#'))

            if (self.current_char == '$'):
                self.prev_scalar = True
                self.advance()
                return self.make_token(TokenType.SCALAR, Value('SCALAR'))

            if (self.current_char == '@' and (self.peek().isalnum() or self.peek() == '_')):
                self.advance()
                return self.make_token(TokenType.LIST, Value('LIST'))

            if (self.current_char == '%' and (self.peek().isalnum() or self.peek() == '_')):
                self.advance()
                return self.make_token(TokenType.HASH, Value('HASH'))

            if (self.current_char == '&'):
                if (self.peek() == '&'):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.LOGAND, Value('&&'))
                self.advance()
                return self.make_token(TokenType.AND, Value('&'))

            if (self.current_char == '!'):
                self.advance()
                if (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.NEQ, Value('!='))
                elif (self.current_char == '~'):
                    self.advance()
                    return self.make_token(TokenType.NOT_MATCH, Value('!~'))
                return self.make_token(TokenType.NOT, Value('!'))

            if (self.current_char == '|'):
                if (self.peek() == '|'):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.LOGOR, Value('||'))
                self.advance()
                return self.make_token(TokenType.OR, Value('|'))

            if (self.current_char == '.'):
                self.advance()
                if (self.current_char == '.'):
                    self.advance()
                    return self.make_token(TokenType.DOTDOT, Value('..'))
                if (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.STR_INCR, Value('.='))
                return self.make_token(TokenType.STR_CONCAT, Value('.'))

            if (self.current_char == '='):
                self.advance()
                if (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.EQ, Value('=='))
                elif (self.current_char == '~'):
                    self.advance()
                    return self.make_token(TokenType.MATCH, Value('=~'))
                else:
                    return self.make_token(TokenType.ASSIGN, Value('='))

            if (self.current_char == ','):
                self.advance()
                return self.make_token(TokenType.COMMA, Value(','))

            if (self.current_char == '%'):
                self.advance()
                return self.make_token(TokenType.MOD, Value('%'))

            if (self.current_char == ':'):
                if (self.peek() == ':'):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.COLONCOLON, Value('::'))
                self.advance()
                return self.make_token(TokenType.COLON, Value(':'))

            if (self.current_char == '/'):
                ret = self.parse_match_spec(False)
                if ret == None:
                    self.advance()
                    if (self.current_char == '='):
                        self.advance()
                        return self.make_token(TokenType.DIV_INCR, Value("/="))
                    return self.make_token(TokenType.DIV, Value('/'))
                else:
                    return ret

            if (self.current_char == ';'):
                self.advance()
                return self.make_token(TokenType.SEMICOLON, Value(';'))

            if (self.current_char == '`'):
                self.advance()
                return self.parse_backticks()

            if (self.current_char == '<'):
                if (self.peek() == '='):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.LTE, Value("<="))
                elif (self.peek() == '<'):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.LSHIFT, Value("<<"))
                elif (self.peek() in self.allowable_var_chars or self.peek().isalnum()):
                    # check for filehandle op
                    self.advance()
                    bareword = self.current_char
                    self.advance()
                    while (self.current_char in self.allowable_var_chars or self.current_char.isalnum()):
                        bareword += self.current_char
                        self.advance()
                    if self.current_char == '>':
                        self.advance()
                        return self.make_token(TokenType.FILEHANDLE, Value(bareword))
                    else:
                        raise Exception("Lexer error attempting to scan filehandle - char was %s" % self.current_char)
                elif (self.peek() == '>'):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.FILEHANDLE, Value(''))
                self.advance()
                return self.make_token(TokenType.LT, Value("<"))

            if (self.current_char == '>'):
                if (self.peek() == '='):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.GTE, Value(">="))
                elif (self.peek() == '>'):
                    self.advance()
                    self.advance()
                    return self.make_token(TokenType.RSHIFT, Value(">>"))
                self.advance()
                return self.make_token(TokenType.GT, Value(">"))

            if (self.current_char == '+'):
                self.advance()
                if (self.current_char == '+'):
                    self.advance()
                    return self.make_token(TokenType.PLUSPLUS, Value('++'))
                if (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.INCR, Value("+="))
                return self.make_token(TokenType.PLUS, Value('+'))

            if (self.current_char == '-'):
                self.advance()
                if (self.current_char == '-'):
                    self.advance()
                    return self.make_token(TokenType.MINUSMINUS, Value('--'))
                if (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.DECR, Value("-="))
                return self.make_token(TokenType.MINUS, Value("-"))

            if (self.current_char == '^'):
                self.advance()
                if (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.XOR_INCR, Value("^="))
                return self.make_token(TokenType.XOR, Value('^'))

            if (self.current_char == "*"):
                self.advance()
                if (self.current_char == '*'):
                    self.advance()
                    return self.make_token(TokenType.POW, Value('**'))
                elif (self.current_char == '='):
                    self.advance()
                    return self.make_token(TokenType.MUL_INCR, Value("*="))
                else:
                    return self.make_token(TokenType.MUL, Value('*'))

            if (self.current_char == '('):
                self.advance()
                return self.make_token(TokenType.LPAREN, Value('('))

            if (self.current_char == ')'):
                self.advance()
                return self.make_token(TokenType.RPAREN, Value(')'))

            if (self.current_char == '{'):
                self.advance()
                return self.make_token(TokenType.LCURLY, Value('{'))

            if (self.current_char == '}'):
                self.advance()
                return self.make_token(TokenType.RCURLY, Value('}'))

            if (self.current_char == '['):
                self.advance()
                return self.make_token(TokenType.LBRACKET, Value('['))

            if (self.current_char == ']'):
                self.advance()
                return self.make_token(TokenType.RBRACKET, Value(']'))
            print self.current_char
            self.error()

        return self.make_token(TokenType.EOF, Value("EOF"))
Ejemplo n.º 38
0
def react_giveOp(msg):
    irc.send(bytes('MODE ' + msg.channel + ' +o ' + msg.nick + '\n', UTF8))
    send_msg(msg.channel, Value.randGiveOpMsg(msg))
Ejemplo n.º 39
0
def react_howMuchLove(msg):
    affection = getPerson(msg).getAffection()
    prtLog(msg.nick+": "+str(affection))
    react = Value.howMuchLoveMsg(msg, affection)
    send_msg(msg.channel, react)
Ejemplo n.º 40
0
def actionTests():
    s = MemState()
    eax = Location(reg="eax")
    ebx = Location(reg="ebx")
    esi = Location(reg="esi")
    one = Value(1)
    twenty = Value(17)
    at_one = Value.atLocation(Location(atAddr=one))
    move1 = MoveAction(eax, one)
    move2 = MoveAction(ebx, eax)
    move3 = MoveAction(eax, twenty)
    xor1 = XorAction(eax, Location(atAddr=one))
    add1 = AddAction(eax, ebx)
    add2 = AddAction(eax, at_one)
    sub1 = SubAction(eax, ebx)
    sub2 = SubAction(eax, at_one)
    print(XorAction(eax, eax))
    print(XorAction(eax, eax).apply(MemState()))

    print("\n Starting from state :")
    print(s)

    print(move1)
    s = move1.apply(s)
    print(s)

    print(move2)
    s = move2.apply(s)
    print(s)

    print(move3)
    s2 = move3.apply(s)
    print(s)
    print(s2)

    print("moving 100 to one")
    moveatone = MoveAction(Location(atAddr=one), Value(100))
    print(moveatone)
    st = moveatone.apply(s2)
    print(st)

    print("xoring")

    print(xor1)
    s3 = xor1.apply(st)
    print(s3)

    print("\nstarting back from state " + str(s))
    print(add1)
    print(add1.apply(s))
    print(add2)
    print(add2.apply(s))
    print(sub1)
    print(sub1.apply(s))
    print(sub2)
    print(sub2.apply(s))

    print("\nstarting from MemState() again :")
    lea1 = LeaAction(Location(base=one),
                     Value.atLocation(Location(atAddr=at_one)))
    #lea1 = LeaAction(Location(base = one), at_one)
    s5 = lea1.apply(MemState())
    print(lea1)
    print(s5)
    add = AddAction(Location(base=one), Value(42))
    s6 = add.apply(s5)
    print(add)
    print(s6)
    add = AddAction(eax, Location(base=one))
    s7 = add.apply(s6)
    print(add)
    print(s7)
    add = AddAction(Location(base=one), Value(42))
    s8 = add.apply(s7)
    print(add)
    print(s8)
    add = AddAction(eax, Location(base=one))
    s9 = add.apply(s8)
    print(add)
    print(s9)

    assert (XorAction(eax, eax).doesOverwriteDst())
    assert (not XorAction(eax, ebx).doesOverwriteDst())
    assert (MoveAction(eax, ebx).doesOverwriteDst())
    assert (not AddAction(eax, ebx).doesOverwriteDst())
    assert (LeaAction(eax, at_one).doesOverwriteDst())

    print(IntAction(Value(2)))
Ejemplo n.º 41
0
def react_invite(msg):
    prtLog(msg)
    prtLog("invite"+msg.nick)
    irc.send(bytes("JOIN %s\r\n" %msg.channel, UTF8))
    send_msg(msg.channel, Value.randJoinMsg(msg))
    getPerson(msg).plus(LITTLE)
Ejemplo n.º 42
0
def react_eating(msg):
    getPerson(msg).plus(MAX)
    send_msg(msg.channel, Value.randEatingMsg(msg))
Ejemplo n.º 43
0
def add(params):
    return Value.makeNumber(sum(item.val for item in params))
Ejemplo n.º 44
0
 def comment(self):
     while self.current_char != '\0' and self.current_char != '\n':
         self.advance()
     return self.make_token(TokenType.COMMENT, Value("COMMENT"))
Ejemplo n.º 45
0
def minus(params):
    return Value.makeNumber(params[0].val - params[1].val)