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 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 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]
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
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]
def loop_body(x, y): q = 255 r = 255 with hcl.if_(D[x][y] < 0): D[x][y] = D[x][y]+180 with hcl.if_(hcl.or_(hcl.and_(D[x][y]>=0,D[x][y]<22.5),hcl.and_(D[x][y]>=157.5,D[x][y]<=180))): q = I[x][y+1] r = I[x][y-1] with hcl.elif_(hcl.and_(22.5 <= D[x][y],D[x][y] < 67.5)): q = I[x+1][y-1] r = I[x-1][y+1] with hcl.elif_(hcl.and_(67.5 <= D[x][y],D[x][y] < 112.5)): q = I[x+1][y] r = I[x-1][y] with hcl.elif_(hcl.and_(112.5 <= D[x][y],D[x][y] < 157.5)): q = I[x-1][y-1] r = I[x+1][y+1] with hcl.if_(hcl.and_(I[x][y]>=q,I[x][y]>=r)): Z[x][y] = I[x][y] with hcl.else_(): Z[x][y] = 0
def smith_waterman(seqA, seqB, consA, consB): def similarity_score(a, b): return hcl.select(a == b, 1, penalty) def find_max(A, len_): max_ = hcl.local(A[0], "max") act_ = hcl.local(0, "act") with hcl.for_(0, len_) as i: with hcl.if_(A[i] > max_[0]): max_[0] = A[i] act_[0] = i return max_[0], act_[0] matrix_max = hcl.local(0, "maxtrix_max") i_max = hcl.local(0, "i_max") j_max = hcl.local(0, "j_max") matrix = hcl.compute((lenA + 1, lenB + 1), lambda x, y: 0, "matrix") action = hcl.compute(matrix.shape, lambda x, y: 3, "action") def populate_matrix(i, j): trace_back = hcl.compute((4, ), lambda x: 0, "trace_back") with hcl.if_(hcl.and_(i != 0, j != 0)): trace_back[0] = matrix[i-1, j-1] + \ similarity_score(seqA[i-1], seqB[j-1]) trace_back[1] = matrix[i - 1, j] + penalty trace_back[2] = matrix[i, j - 1] + penalty trace_back[3] = 0 matrix[i, j], action[i, j] = find_max(trace_back, 4) with hcl.if_(matrix[i, j] > matrix_max[0]): matrix_max[0] = matrix[i, j] i_max[0] = i j_max[0] = j P = hcl.mutate((lenA + 1, lenB + 1), lambda i, j: populate_matrix(i, j)) 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 get_next(action, i, j): act_ = hcl.local(action[i][j], "act") next_i = hcl.local(0, "next_i") next_j = hcl.local(0, "next_j") with hcl.if_(act_[0] == 0): next_i[0] = i - 1 next_j[0] = j - 1 with hcl.elif_(act_[0] == 1): next_i[0] = i - 1 next_j[0] = j with hcl.elif_(act_[0] == 2): next_i[0] = i next_j[0] = j - 1 with hcl.else_(): next_i[0] = i next_j[0] = j return next_i[0], next_j[0] with hcl.Stage("T"): curr_i = hcl.local(i_max[0], "curr_i") curr_j = hcl.local(j_max[0], "curr_j") next_i = hcl.local(0, "next_i") next_j = hcl.local(0, "next_j") next_i[0], next_j[0] = get_next(action, curr_i[0], curr_j[0]) tick = hcl.local(0, "tick") with hcl.while_( hcl.or_(curr_i[0] != next_i[0], curr_j[0] != next_j[0])): consA[tick[0]], consB[tick[0]] = \ align(curr_i, curr_j, next_i, next_j) curr_i[0], curr_j[0] = next_i[0], next_j[0] next_i[0], next_j[0] = get_next(action, curr_i[0], curr_j[0]) tick[0] += 1