Пример #1
0
    def reward(self, sVals, action, bounds, goal, trans):
        di  = hcl.scalar(0, "di") 
        dj  = hcl.scalar(0, "dj") 
        dk  = hcl.scalar(0, "dk") 
        dl  = hcl.scalar(0, "dl") 
        dm  = hcl.scalar(0, "dm") 
        mag = hcl.scalar(0, "mag")
        rwd = hcl.scalar(0, "rwd")

        # Check if moving from a collision state, if so, assign a penalty
        with hcl.if_(hcl.or_(sVals[0] <= bounds[0,0], sVals[0] >= bounds[0,1])):
            rwd[0] = -400
        with hcl.elif_(hcl.or_(sVals[1] <= bounds[1,0], sVals[1] >= bounds[1,1])):
            rwd[0] = -400
        with hcl.elif_(hcl.or_(sVals[2] <= bounds[2,0], sVals[2] >= bounds[2,1])):
            rwd[0] = -400
        with hcl.elif_(hcl.or_(sVals[3] <= bounds[3,0], sVals[3] >= bounds[3,1])):
            rwd[0] = -400
        with hcl.elif_(hcl.or_(sVals[4] <= bounds[4,0], sVals[4] >= bounds[4,1])):
            rwd[0] = -400

        with hcl.else_():
            # Check if moving from a goal state
            di[0]  = (sVals[0] - goal[0]) * (sVals[0] - goal[0])
            dj[0]  = (sVals[1] - goal[1]) * (sVals[1] - goal[1])
            dk[0]  = (sVals[2] - goal[2]) * (sVals[2] - goal[2])
            dl[0]  = (sVals[3] - goal[3]) * (sVals[3] - goal[3])
            dm[0]  = (sVals[4] - goal[4]) * (sVals[4] - goal[4])
            mag[0] = hcl.sqrt(di[0] + dj[0] + dk[0] + dl[0] + dm[0])
            with hcl.if_(mag[0] <= 5.0):
                rwd[0] = 1000
            # Standard move
            with hcl.else_():                
                rwd[0] = 0
        return rwd[0]
Пример #2
0
def reward(sVals, action, bounds, goal, trans):
    dx = hcl.scalar(0, "dx")
    dy = hcl.scalar(0, "dy")
    mag = hcl.scalar(0, "mag")
    rwd = hcl.scalar(0, "rwd")

    # Check if moving from a collision state, if so, assign a penalty
    with hcl.if_(
            hcl.or_(sVals[0] < bounds[0, 0] + 0.2,
                    sVals[0] > bounds[0, 1] - 0.2)):
        rwd[0] = -400
    with hcl.elif_(
            hcl.or_(sVals[1] < bounds[1, 0] + 0.2,
                    sVals[1] > bounds[1, 1] - 0.2)):
        rwd[0] = -400
    with hcl.else_():
        # Check if moving from a goal state
        dx[0] = sVals[0] - goal[0, 0]
        dy[0] = sVals[1] - goal[0, 1]
        mag[0] = hcl.sqrt((dx[0] * dx[0]) + (dy[0] * dy[0]))
        with hcl.if_(
                hcl.and_(mag[0] <= 1.0, sVals[2] <= goal[1, 1],
                         sVals[2] >= goal[1, 0])):
            rwd[0] = 1000
        # Standard move
        with hcl.else_():
            rwd[0] = 0
    return rwd[0]
    def update(l, prototype, prototypeCounter, max):
        hcl.print((l + 1),
                  "%d:Use hard examples to update the prototype counters.\n")

        ###data preparation
        distance = hcl.compute((hdTrainData.shape[1], ), lambda x: 0,
                               'distance')
        hamming_dist = hcl.compute((numClasses, ), lambda x: 0, "hamming_dist")
        m = hcl.reduce_axis(0, hdTrainData.shape[1], "m")
        ###

        with hcl.for_(0, hdTrainData.shape[0]) as i:
            with hcl.for_(0, numClasses) as n:
                #Do hdc multiplication(XOR) on sample[i]'s hdv and prototype[n]'s hdv (elementwise on the high-bit data)
                hcl.update(distance,
                           lambda x: hdTrainData[i][x] ^ prototype[n][x])
                #Calculate the hamming distance of the two vectors by adding 1s
                hamming_dist[n] = hcl.sum(distance[m], axis=m)

            #Find the one having the least hamming distance and choose it's label as the predicted label
            pred = hcl.scalar(0, 'pred')
            with hcl.for_(0, hamming_dist.shape[0]) as j:
                with hcl.if_(hamming_dist[j] < hamming_dist[pred]):
                    pred.v = j

            #Adjust the proto vectors by adding the sample vector on its label proto hdv and substrct it on its predicted proto hdv
            with hcl.if_(pred.v != trainLabels[i]):
                max[trainLabels[i]] += 1
                max[pred] -= 1
                with hcl.for_(0, hdTrainData.shape[1]) as m:
                    prototypeCounter[trainLabels[i]][m] += hdTrainData[i][m]
                    prototypeCounter[pred][m] -= hdTrainData[i][m]
                    with hcl.if_(max[trainLabels[i]] % 2 == 0):
                        with hcl.if_(prototypeCounter[trainLabels[i]][m] -
                                     max[trainLabels[i]] / 2 == 0):
                            prototype[trainLabels[i]][m] &= 1
                    with hcl.else_():
                        prototype[trainLabels[i]][m] = hcl.select(
                            prototypeCounter[trainLabels[i]][m] -
                            max[trainLabels[i]] / 2 > 0, 1, 0)

                    with hcl.if_(max[pred] % 2 == 0):
                        with hcl.if_(prototypeCounter[pred][m] -
                                     max[pred] / 2 == 0):
                            prototype[pred][m] &= 1
                    with hcl.else_():
                        prototype[pred][m] = hcl.select(
                            prototypeCounter[pred][m] - max[pred] / 2 > 0, 1,
                            0)

        #print the accuracy
        hcl.mutate(
            (1, ),
            lambda x: test_hdc_accu(prototype, hdTrainData, trainLabels, 1),
            'training_update')
        hcl.mutate(
            (1, ),
            lambda x: test_hdc_accu(prototype, hdTestData, testLabels, 2),
            'testing_update')
    def update(l, prototype, prototypeCounter, max):
        hcl.print((l+1),"%d:Use hard examples to update the prototype counters.\n")

        ###data preparation
        distance = hcl.compute((in_train.shape[1],), lambda x: 0, 'distance', dtype=hcl.UInt(in_bw))
        pre_dist = hcl.compute((in_train.shape[1],), lambda x: 0, "pre_dist")
        hamming_dist = hcl.compute((numClasses,), lambda x: 0, "hamming_dist")
        m = hcl.reduce_axis(0, in_train.shape[1], "m")
        ###

        with hcl.for_(0, in_train.shape[0]) as i:
            hcl.print((i),"%d suc\n")
            # pack_proto = hcl.pack(prototype, axis=1, dtype=hcl.UInt(in_bw), name="pack_proto") 
            with hcl.for_(0, numClasses) as n:
                #Do hdc multiplication(XOR) on sample[i]'s hdv and prototype[n]'s hdv (elementwise on the high-bit data)
                hcl.update(distance, lambda x: in_train[i][x] ^ prototype[n][x])
                #Calculate the hamming distance of the two vectors by adding 1s
                hcl.update(pre_dist, lambda x: popcount(distance[x]))
                hcl.print((),"sum of 1s suc")
                hamming_dist[n] = hcl.sum(pre_dist[m], axis=m)

            #Find the one having the least hamming distance and choose it's label as the predicted label
            pred = hcl.scalar(0, 'pred')
            with hcl.for_(0, hamming_dist.shape[0]) as j:
                with hcl.if_(hamming_dist[j] < hamming_dist[pred]):
                    pred.v = j

            #Adjust the proto vectors by adding the sample vector on its label proto hdv and substrct it on its predicted proto hdv
            with hcl.if_(pred.v != trainLabels[i]):
                max[trainLabels[i]] += 1
                max[pred] -= 1
                with hcl.for_(0, in_train.shape[1]) as m:
                    with hcl.for_(0, in_bw) as bit:
                        # with hcl.if_(in_train[i][m][bit] == 1):
                        #     ###########
                        #     prototypeCounter[trainLabels[i]][m*in_bw+bit] += 1
                        #     prototypeCounter[pred][m*in_bw+bit] -= 1
                        prototypeCounter[trainLabels[i]][m*in_bw+bit] += in_train[i][m][bit]
                        prototypeCounter[pred][m*in_bw+bit] -= in_train[i][m][bit]
                        with hcl.if_(max[trainLabels[i]] % 2 == 0):
                            with hcl.if_(prototypeCounter[trainLabels[i]][m*in_bw+bit] - max[trainLabels[i]]/2 == 0):
                                prototype[trainLabels[i]][m][bit] &= 1
                        with hcl.else_():
                            prototype[trainLabels[i]][m][bit] = hcl.select(prototypeCounter[trainLabels[i]][m*in_bw+bit] - max[trainLabels[i]]/2 > 0, 1, 0)

                        with hcl.if_(max[pred] % 2 == 0):
                            with hcl.if_(prototypeCounter[pred][m*in_bw+bit] - max[pred]/2 == 0):
                                prototype[pred][m][bit] &= 1
                        with hcl.else_():
                            prototype[pred][m][bit] = hcl.select(prototypeCounter[pred][m*in_bw+bit] - max[pred]/2 > 0, 1, 0)

        #print the accuracy
        hcl.mutate((1,), lambda x: test_hdc_accu(prototype, in_train, trainLabels, 1), 'training_update')
        hcl.mutate((1,), lambda x: test_hdc_accu(prototype, in_test, testLabels, 2), 'testing_update')
Пример #5
0
        def align(curr_i, curr_j, next_i, next_j):
            outA = hcl.local(0, "a")
            outB = hcl.local(0, "b")

            with hcl.if_(next_i[0] == curr_i[0]):
                outA[0] = 0
            with hcl.else_():
                outA[0] = seqA[curr_i[0] - 1]

            with hcl.if_(next_j[0] == curr_j[0]):
                outB[0] = 0
            with hcl.else_():
                outB[0] = seqB[curr_j[0] - 1]
            return outA[0], outB[0]
def kernel(trainData, testData, itemMem, idMem, rdv1, rdv2):
    def train_encoding(m, preTrainData):
        train_temp = hcl.compute((trainData.shape[1], dim), lambda x, y: itemMem[trainData[m][x]][y] ^ idMem[x][y], name = "train_temp")
        k1 = hcl.reduce_axis(0, trainData.shape[1], 'k1')
        train_result = hcl.compute((dim,), lambda x: hcl.sum(train_temp[k1, x], axis = k1, dtype=hcl.Int()), name = "train_result")
        with hcl.for_(0, dim) as n:
            preTrainData[m][n] = train_result[n]
        with hcl.if_((m + 1) % 1000 == 0):
            hcl.print((m+1), "Finish encoding %d training data\n")

    def test_encoding(m, preTestData):
        test_temp = hcl.compute((testData.shape[1], dim), lambda x, y: itemMem[testData[m][x]][y]^idMem[x][y], name = "test_temp")
        k2 = hcl.reduce_axis(0, testData.shape[1], 'k2')
        test_result = hcl.compute((dim,), lambda x: hcl.sum(test_temp[k2, x], axis = k2, dtype=hcl.Int()), name = "test_result")
        with hcl.for_(0, dim) as n:
            preTestData[m][n] = test_result[n]
        with hcl.if_((m+1)%100 == 0):
            hcl.print((m+1), "Finish encoding %d testing data\n")

    #Encoding
    hcl.print((), "Encoding the training data into HDVs.\n")
    preTrainData = hcl.compute((trainData.shape[0], dim), lambda x, y: 0, "preTrainData")
    hcl.mutate((trainData.shape[0], ), lambda x: train_encoding(x, preTrainData))

    hdTrainData = hcl.compute((trainData.shape[0], dim), lambda x, y: 0, "hdTrainData", dtype=hcl.UInt(1))
    with hcl.Stage("S1"):
        with hcl.if_(trainData.shape[1] % 2 == 0):
            hcl.print((), "Use the random vector\n")
            hcl.update(hdTrainData, lambda x, y: hcl.select(preTrainData[x][y] + rdv1[x][y] - trainData.shape[1]/2 > 0, 1, 0))
        with hcl.else_():
            hcl.update(hdTrainData, lambda x, y: hcl.select(preTrainData[x][y] - trainData.shape[1]/2 > 0, 1, 0))

    hcl.print((),"Encoding the testing data into HDVs.\n")
    preTestData = hcl.compute((testData.shape[0], dim), lambda x, y: 0, "preTestData")
    hcl.mutate((testData.shape[0], ), lambda x: test_encoding(x, preTestData))

    hdTestData = hcl.compute((testData.shape[0], dim), lambda x, y: 0, "hdTestData", dtype=hcl.UInt(1))
    with hcl.Stage("S2"):
        with hcl.if_(testData.shape[1] % 2 == 0):
            hcl.print((), "Use the random vector\n")
            hcl.update(hdTestData, lambda x, y: hcl.select(preTestData[x][y] + rdv2[x][y] - testData.shape[1]/2 > 0, 1, 0))
        with hcl.else_():
            hcl.update(hdTestData, lambda x, y: hcl.select(preTestData[x][y] - testData.shape[1]/2 > 0, 1, 0))

    ###data_packing
    pack_train = hcl.pack(hdTrainData, axis=1, dtype=hcl.UInt(bw), name="pack_train")
    pack_test = hcl.pack(hdTestData, axis=1, dtype=hcl.UInt(bw), name="pack_test")
    return pack_train, pack_test
Пример #7
0
    def optDstb(self, spat_deriv):
        """
        :param spat_deriv: tuple of spatial derivative in all dimensions
        :return: a tuple of optimal disturbances
        """
        # Graph takes in 4 possible inputs, by default, for now
        d1 = hcl.scalar(0, "d1")
        d2 = hcl.scalar(0, "d2")
        # Just create and pass back, even though they're not used
        d3 = hcl.scalar(0, "d3")
        d4 = hcl.scalar(0, "d4")

        with hcl.if_(self.dMode == "max"):
            with hcl.if_(spat_deriv[0] > 0):
                d1[0] = self.dMax[0]
            with hcl.elif_(spat_deriv[0] < 0):
                d1[0] = self.dMin[0]
            with hcl.if_(spat_deriv[1] > 0):
                d2[0] = self.dMax[1]
            with hcl.elif_(spat_deriv[1] < 0):
                d2[0] = self.dMin[1]
        with hcl.else_():
            with hcl.if_(spat_deriv[0] > 0):
                d1[0] = self.dMin[0]
            with hcl.elif_(spat_deriv[0] < 0):
                d1[0] = self.dMax[0]
            with hcl.if_(spat_deriv[1] > 0):
                d2[0] = self.dMin[1]
            with hcl.elif_(spat_deriv[1] < 0):
                d2[0] = self.dMax[1]
        return (d1[0], d2[0], d3[0], d4[0])
Пример #8
0
 def absolute(A, B):
     with hcl.for_(0, A.shape[0], name="x") as x:
         with hcl.for_(0, A.shape[1], name="y") as y:
             with hcl.if_(A[x, y] >= 0):
                 B[x, y] = A[x, y]
             with hcl.else_():
                 B[x, y] = -A[x, y]
Пример #9
0
    def loop_body(x, y):
        q = 255
        r = 255
        c1 = hcl.and_((0 <= angle[x][y]), (angle[x][y] < 22.5))
        c2 = hcl.and_((157.5 <= angle[x][y]), (angle[x][y] <= 180))
        c3 = hcl.and_((22.5 <= angle[x][y]), (angle[x][y] < 67.5))
        c4 = hcl.and_((67.5 <= angle[x][y]), (angle[x][y] < 112.5))
        c5 = hcl.and_((112.5 <= angle[x][y]), (angle[x][y] < 157.5))

        #angle 0
        with hcl.if_(hcl.or_(c1, c2)):
            q = image[x][y + 1]
            r = image[x][y - 1]
        #angle 45
        with hcl.elif_(c3):
            q = image[x + 1][y - 1]
            r = image[x - 1][y + 1]
        #angle 90
        with hcl.elif_(c4):
            q = image[x + 1][y]
            r = image[x - 1, ][y]
        #angle 135
        with hcl.elif_(c5):
            q = image[x - 1, y - 1]
            r = image[x + 1, y + 1]

        with hcl.if_(hcl.and_((image[x, y] >= q), (image[x, y] >= r))):
            Z[x][y] = image[x][y]

        with hcl.else_():
            Z[x][y] = 0
Пример #10
0
 def freduce(x, Y):
     with hcl.if_(x < Y[0]):
         Y[1] = Y[0]
         Y[0] = x
     with hcl.else_():
         with hcl.if_(x < Y[1]):
             Y[1] = x
Пример #11
0
 def pad(x, y, z):
     out = hcl.scalar(0, "out")
     with hcl.if_(hcl.and_(x > 0, y > 0)):
         out.v = imgF[x - 1, y - 1, z]
     with hcl.else_():
         out.v = 0
     return out.v
def my_min(a, b):
    result = hcl.scalar(0, "result")
    with hcl.if_(a < b):
        result[0] = a
    with hcl.else_():
        result[0] = b
    return result[0]
Пример #13
0
 def loop_body(x):
     with hcl.if_(A[x] > M[0]):
         with hcl.if_(A[x] > M[1]):
             M[0] = M[1]
             M[1] = A[x]
         with hcl.else_():
             M[0] = A[x]
def my_max(a, b):
    result = hcl.scalar(0, "result")
    with hcl.if_(a > b):
        result.v = a
    with hcl.else_():
        result.v = b
    return result.v
Пример #15
0
def transition(sVals, action, bounds, trans, goal):
    dx = hcl.scalar(0, "dx")
    dy = hcl.scalar(0, "dy")
    mag = hcl.scalar(0, "mag")

    # Check if moving from a goal state
    dx[0] = sVals[0] - goal[0, 0]
    dy[0] = sVals[1] - goal[0, 1]
    mag[0] = hcl.sqrt((dx[0] * dx[0]) + (dy[0] * dy[0]))
    with hcl.if_(
            hcl.and_(mag[0] <= 1.0, sVals[2] <= goal[1, 1],
                     sVals[2] >= goal[1, 0])):
        trans[0, 0] = 0
    # Check if moving from an obstacle
    with hcl.elif_(
            hcl.or_(sVals[0] < bounds[0, 0] + 0.2,
                    sVals[0] > bounds[0, 1] - 0.2)):
        trans[0, 0] = 0
    with hcl.elif_(
            hcl.or_(sVals[1] < bounds[1, 0] + 0.2,
                    sVals[1] > bounds[1, 1] - 0.2)):
        trans[0, 0] = 0
    # Standard move
    with hcl.else_():
        trans[0, 0] = 1.0
        trans[0, 1] = sVals[0] + (0.6 * action[0] * hcl.cos(sVals[2]))
        trans[0, 2] = sVals[1] + (0.6 * action[0] * hcl.sin(sVals[2]))
        trans[0, 3] = sVals[2] + (0.6 * action[1])
        # Adjust for periodic dimension
        with hcl.while_(trans[0, 3] > 3.141592653589793):
            trans[0, 3] -= 6.283185307179586
        with hcl.while_(trans[0, 3] < -3.141592653589793):
            trans[0, 3] += 6.283185307179586
 def learn(k, hdTrainData, prototype, prototypeCounter):
     #Find samples that have the label k
     match = hcl.compute(
         hdTrainData.shape,
         lambda x, y: hcl.select(trainLabels[x] == k, hdTrainData[x][y], 0),
         "match")
     #Record the number of these samples
     with hcl.for_(0, hdTrainData.shape[0]) as a:
         with hcl.if_(trainLabels[a] == k):
             max[k] += 1
     #Do hdc sum on these samples' hdv
     r = hcl.reduce_axis(0, hdTrainData.shape[0], 'r')
     result = hcl.compute((hdTrainData.shape[1], ),
                          lambda y: hcl.sum(match[r][y], axis=r), "result")
     #Do the binary voting
     sum1 = hcl.compute((hdTrainData.shape[1], ), lambda x: 0, "sum1")
     with hcl.if_(max[k] % 2 == 0):
         hcl.update(
             sum1, lambda x: hcl.select(
                 result[x] + rdv3[k][x] - max[k] / 2 > 0, 1, 0))
     with hcl.else_():
         hcl.update(sum1,
                    lambda x: hcl.select(result[x] - max[k] / 2 > 0, 1, 0))
     #Push the binary sum to prototype and the original sum to prototypeCounter
     with hcl.for_(0, hdTrainData.shape[1]) as t:
         prototype[k][t] = sum1[t]
         prototypeCounter[k][t] = result[t]
def new_abs(my_x):
    my_value = hcl.scalar(0, "my_value", dtype=hcl.Float())
    with hcl.if_(my_x > 0):
        my_value.v = my_x
    with hcl.else_():
        my_value.v = -my_x
    return my_value.v
Пример #18
0
 def kernel(A):
     with hcl.if_(A[0] > 5):
         A[0] = 5
     with hcl.elif_(A[0] > 3):
         A[0] = 3
     with hcl.else_():
         A[0] = 0
def my_abs(my_x):
    abs_value = hcl.scalar(0, "abs_value", dtype=hcl.Float())
    with hcl.if_(my_x > 0):
        abs_value.v = my_x
    with hcl.else_():
        abs_value.v = -my_x
    return abs_value.v
Пример #20
0
 def loop_body(x):
     with hcl.if_(A[x]> M[0]):
         with hcl.if_(A[x]> M[1]):
             hcl.assert_(x == 2, "assert error in if--value of x: %d", x)
             M[0] = M[1]
             M[1] = A[x]
         with hcl.else_():
             M[0] = A[x]
Пример #21
0
 def Sigmoid(exponent):
     ret = hcl.scalar(0.0, "sigmoid", FTYPE)
     with hcl.if_(exponent > hcl.cast(FTYPE, 4.0)):
         ret[0] = 1.0
     with hcl.elif_(exponent < hcl.cast(FTYPE, -4.0)):
         ret[0] = 0.0
     with hcl.else_():
         with hcl.if_(exponent < hcl.cast(FTYPE, 0.0)):
             num = hcl.scalar(0, dtype=hcl.UFixed(18, 8))
             num[0][18:0] = exponent[29:11]
             num[0] = ~(num[0] << 8) + 1
             index = 2047.0 - num[0]
             ret[0] = lut[hcl.cast(hcl.Int(32), index)]
         with hcl.else_():
             index = exponent[21:11]
             ret[0] = lut[hcl.cast(hcl.Int(32), index)]
     return ret[0]
Пример #22
0
 def pe(a, b, x):
     with hcl.if_(x == 0):
         result = a * b
         hcl.return_(a)
     with hcl.elif_(x == 1):
         hcl.return_(b)
     with hcl.else_():
         hcl.return_(result)
Пример #23
0
    def use_lut(lut, in_, val):
        temp_in = hcl.scalar(in_, dtype=hcl.Int())
        with hcl.if_(in_ < 0):
            index = LUT_SIZE - (temp_in) << (LUTIN_TWIDTH - LUTIN_IWIDTH)
        with hcl.else_():
            index = (temp_in) << (LUTIN_TWIDTH - LUTIN_IWIDTH)

        val[0] = lut[index]
Пример #24
0
    def Sigmoid(lut, exponent, prob):

        with hcl.if_(exponent[0] > 4):
            prob[0] = 1.0
        with hcl.elif_(exponent[0] < -4):
            prob[0] = 0.0
        with hcl.else_():
            use_lut(lut, exponent[0], prob)
Пример #25
0
 def update_B(A, x):
     with hcl.if_(A[x] > 5):
         with hcl.if_(A[x] > 7):
             hcl.return_(-2)
         hcl.return_(-1)
     with hcl.else_():
         with hcl.if_(A[x] > 3):
             hcl.return_(-3)
     hcl.return_(A[x] + 1)
Пример #26
0
    def transition(self, sVals, action, bounds, trans, goal):
        di = hcl.scalar(0, "di")
        dj = hcl.scalar(0, "dj")
        dk = hcl.scalar(0, "dk")
        dl = hcl.scalar(0, "dl")
        dm = hcl.scalar(0, "dm")
        dn = hcl.scalar(0, "dn")
        do = hcl.scalar(0, "do")
        mag = hcl.scalar(0, "mag")

        # Check if moving from a goal state
        di[0] = (sVals[0] - goal[0]) * (sVals[0] - goal[0])
        dj[0] = (sVals[1] - goal[1]) * (sVals[1] - goal[1])
        dk[0] = (sVals[2] - goal[2]) * (sVals[2] - goal[2])
        dl[0] = (sVals[3] - goal[3]) * (sVals[3] - goal[3])
        dm[0] = (sVals[4] - goal[4]) * (sVals[4] - goal[4])
        dn[0] = (sVals[5] - goal[5]) * (sVals[5] - goal[5])
        do[0] = (sVals[6] - goal[6]) * (sVals[6] - goal[6])
        mag[0] = hcl.sqrt(di[0] + dj[0] + dk[0] + dl[0] + dm[0] + dn[0] +
                          do[0])

        # Check if moving from an obstacle
        with hcl.if_(mag[0] <= 5.0):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[0] <= bounds[0, 0], sVals[0] >= bounds[0, 1])):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[1] <= bounds[1, 0], sVals[1] >= bounds[1, 1])):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[2] <= bounds[2, 0], sVals[2] >= bounds[2, 1])):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[3] <= bounds[3, 0], sVals[3] >= bounds[3, 1])):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[4] <= bounds[4, 0], sVals[4] >= bounds[4, 1])):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[5] <= bounds[5, 0], sVals[5] >= bounds[5, 1])):
            trans[0, 0] = 0
        with hcl.elif_(
                hcl.or_(sVals[6] <= bounds[6, 0], sVals[6] >= bounds[6, 1])):
            trans[0, 0] = 0

        # Standard move
        with hcl.else_():
            trans[0, 0] = 1.0
            trans[0, 1] = sVals[0] + action[0]
            trans[0, 2] = sVals[1] + action[1]
            trans[0, 3] = sVals[2] + action[2]
            trans[0, 4] = sVals[3] + action[3]
            trans[0, 5] = sVals[4] + action[4]
            trans[0, 6] = sVals[5] + action[5]
            trans[0, 7] = sVals[6] + action[6]
Пример #27
0
 def update_B(A, x):
     with hcl.if_(A[x] > 5):
         hcl.print(0, "print if 1\n")
         hcl.assert_(A[x] <= 5, "assert in if")
         hcl.print(0, "print if 2\n")
         hcl.return_(-1)
     with hcl.else_():
         hcl.print(0, "print else 1\n")
         hcl.assert_(A[x] <= 5, "assert in else")
         hcl.print(0, "print else 2\n")
         hcl.return_(A[x] + 1)
Пример #28
0
 def step_loop(step):
     with hcl.if_(theta[0] > current[0]):
         T[0] = X[0] - (Y[0] >> step)
         Y[0] = Y[0] + (X[0] >> step)
         X[0] = T[0]
         current[0] = current[0] + C[step]
     with hcl.else_():
         T[0] = X[0] + (Y[0] >> step)
         Y[0] = Y[0] - (X[0] >> step)
         X[0] = T[0]
         current[0] = current[0] - C[step]
Пример #29
0
 def sort_knn(knn_mat, i, j):
     val = hcl.local(0, "val")
     with hcl.if_( j == 1 ):
         with hcl.if_( knn_mat[i][1] > knn_mat[i][2] ):
             val.v = knn_mat[i][1] 
             knn_mat[i][1] = knn_mat[i][2]
             knn_mat[i][2] = val.v
     with hcl.else_():
         with hcl.if_( knn_mat[i][0] > knn_mat[i][1] ):
             val.v = knn_mat[i][0] 
             knn_mat[i][0] = knn_mat[i][1]
             knn_mat[i][1] = val.v
Пример #30
0
 def two_stage(A):
     var = hcl.scalar(0, "v", dtype=hcl.UInt(32))
     var.v = 1
     with hcl.if_(var == 0):
         hcl.print((), "A\n")
     with hcl.else_():
         var.v = var - 1
         # this condition should not be optimized away
         with hcl.if_(var == 0):
             hcl.print((), "B\n")
     A[0] = var
     return A