Ejemplo n.º 1
0
    def authorizedMove(self, move, possibleNorm=NORM_MOVES):
        '''
        Returns if the move is authorized given a optional direction for the collision constraints
        :param move: the move to check
        :param possibleNorm: check only the norm provided
        :return: a boolean true if the position is authorized
        '''
        head = self.head()

        # backward moves are forbidden
        if move.direction() == utils.mult(self.orientation(), -1):
            return False

        target = move.applyDirection(head)
        # If a collision already occurred we can't do another one
        if (self.on_tail and self.onSnakeExceptLastOrNotGrid(target, 1)):
            return False
        # If we would need two collisions in a row there is a problem
        next_target = move.applyDirection(head, mu=2)
        if (self.onSnakeExceptLastOrNotGrid(target, 1)
                and self.onSnakeExceptLastOrNotGrid(next_target, 2)):
            return False

        if move.norm() == 2 and 2 in possibleNorm:
            # We can only accelerate when the snake is big enough
            if self.size() <= 2:
                return False

            next_acc_target = move.applyDirection(head, mu=3)
            # We make sure that we can move without causing death at the next time
            if (self.onSnakeExceptLastOrNotGrid(next_acc_target, 3)
                    and self.onSnakeExceptLastOrNotGrid(next_target)):
                return False

        return True
Ejemplo n.º 2
0
 def getRate(self):
     return (
         len(self.scalas),
         len(self.getAllNotes()),
         reduce(utils.lcm, self.scalas),
         utils.mult(self.scalas),
     )
Ejemplo n.º 3
0
 def tree(primes, check, level, r = ()):
     if r == ():
         if not check(1): return
     for p in primes():
         if level < p: break  # ugly !!!
         r1 = (p,) + r
         if not check(utils.mult(r1)): return  # skip other primes
         RangeSearcher.tree(primes, check, p, r1)
Ejemplo n.º 4
0
 def getFundamentals(self):
     result = Spectrum()
     keys = sorted(self.keys(), key=float)  # might be not sorted
     variations = list(utils.variations(len(keys)))[1:]
     for v in variations:  # (0,0,1), (0,1,0), (0,1,1),.. (1,1,1)
         ks = [k for (b, k) in zip(v, keys) if b]
         d = Rational.gcd(*ks)
         value = utils.mult(self[k] for k in ks)
         value **= 1.0 / len(ks)  # geometric mean
         result[d] += value
     return result
Ejemplo n.º 5
0
    def run(self):
        """
        Run the network simulation by evaluating model equations at every time
        step.
        """
        # integration constant
        rho_a = 1./(self.stim_len)

        # simulation time
        time = np.arange(0, self.t_max)

        for t in time[1:]:
            # stop simulation if target found or max guesses
            if self._terminate_condition(t):
                break

            #  ----- Semantic Layer -----
            # winning/active units
            self.z[t-1] = step(self.w[t-1], self.theta_w)

            # unit activities
            self.a[t] = self.a[t-1] + rho_a*(mult(self.W,
                                                  self.z[t-1],
                                                  self.a[t-1]) +
                                             self.I[t-1])
            # ----- WTA Layer -----
            eta = np.random.random(self.N) - self.noise_offset

            # normalise activities to 0-1
            divisor = np.max([1., self.a[t-1].max()])

            self.w[t] = self.w[t-1] + self.rho_w * \
                (self.c1*self.z[t-1] +
                 self.c2*self.a[t-1]/divisor -
                 self.c3*self.y[t-1] -
                 self.c4*self.r[t-1] +
                 self.c5*eta)

            self.y[t] = self.z[t-1].sum()

            # ----- Inhibitory Layer -----
            # -2 added because of discreete update lags
            self.r[t] = (self.r[t-1] +
                         step(self.z[t-self.stim_len:t].sum(axis=0),
                              self.stim_len-2)).clip(max=1)

            # several winners are not allowed
            if self.y[t] > 1:
                raise Exception('Multiple winners')

            # rectify WTA activities to 0 (if needed)
            self.w[t] = self.w[t].clip(min=0)
Ejemplo n.º 6
0
    def run(self):
        """
        Run the network simulation by evaluating model equations at every time
        step.
        """
        # integration constant
        rho_a = 1. / (self.stim_len)

        # simulation time
        time = np.arange(0, self.t_max)

        for t in time[1:]:
            # stop simulation if target found or max guesses
            if self._terminate_condition(t):
                break

            #  ----- Semantic Layer -----
            # winning/active units
            self.z[t - 1] = step(self.w[t - 1], self.theta_w)

            # unit activities
            self.a[t] = self.a[t - 1] + rho_a * (
                mult(self.W, self.z[t - 1], self.a[t - 1]) + self.I[t - 1])
            # ----- WTA Layer -----
            eta = np.random.random(self.N) - self.noise_offset

            # normalise activities to 0-1
            divisor = np.max([1., self.a[t - 1].max()])

            self.w[t] = self.w[t-1] + self.rho_w * \
                (self.c1*self.z[t-1] +
                 self.c2*self.a[t-1]/divisor -
                 self.c3*self.y[t-1] -
                 self.c4*self.r[t-1] +
                 self.c5*eta)

            self.y[t] = self.z[t - 1].sum()

            # ----- Inhibitory Layer -----
            # -2 added because of discreete update lags
            self.r[t] = (self.r[t - 1] +
                         step(self.z[t - self.stim_len:t].sum(axis=0),
                              self.stim_len - 2)).clip(max=1)

            # several winners are not allowed
            if self.y[t] > 1:
                raise Exception('Multiple winners')

            # rectify WTA activities to 0 (if needed)
            self.w[t] = self.w[t].clip(min=0)
Ejemplo n.º 7
0
def getHarmonicDistance(chord, weights=()):
    "chord as a list of integers, weights - floats"
    from rationals import Rational
    import harmonicity

    #print chord, weights
    if not weights: weights = (1.0, ) * len(chord)

    variations = list(utils.variations(len(chord)))
    distance = 0.0
    for var in variations:
        ns = [n for (n, b) in zip(chord, var) if b]
        if len(ns) >= 2:
            gcd = reduce(utils.gcd, ns)
            ns = [n / gcd for n in ns]  # reduce notes
            d = sum([harmonicity.simple(5)(Rational(n)) for n in ns])
            dw = d * utils.mult([w for (w, b) in zip(weights, var) if b])
            #print "  ", ns, lcm, `r`, d #, dw
            distance += dw
    return distance
Ejemplo n.º 8
0
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""

table = [[int(j) for j in i.split(" ")] for i in table.splitlines()]
values = []
w, h = len(table[0]), len(table)
take = 4
for x in range(w):
    for y in range(h):
        if y + take <= h:
            values.append([table[y + i][x] for i in range(take)])
            if x + take <= w:
                values.append([table[y + i][x + i] for i in range(take)])
            if x - take >= -1:
                values.append([table[y + i][x - i] for i in range(take)])
        values.append(table[y][x:x + take])
print(max(mult(i) for i in values))
Ejemplo n.º 9
0
def n_totient(n):
    f = factors[n]
    if f:
        return 1 / (mult(1 - 1 / i for i in f))
    return n / (n - 1)