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])
def spa_derivX3_6d(i, j, k, l, m, n, V, g): # Left -> right == Outer Most -> Inner Most left_deriv = hcl.scalar(0, "left_deriv") right_deriv = hcl.scalar(0, "right_deriv") with hcl.if_(k == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k, l, m, n] + my_abs(V[i, j, k + 1, l, m, n] - V[i, j, k, l, m, n]) * my_sign( V[i, j, k, l, m, n]) left_deriv[0] = (V[i, j, k, l, m, n] - left_boundary[0]) / g.dx[2] right_deriv[0] = (V[i, j, k + 1, l, m, n] - V[i, j, k, l, m, n]) / g.dx[2] with hcl.elif_(k == V.shape[2] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k, l, m, n] + my_abs(V[i, j, k, l, m, n] - V[i, j, k - 1, l, m, n]) * my_sign( V[i, j, k, l, m, n]) left_deriv[0] = (V[i, j, k, l, m, n] - V[i, j, k - 1, l, m, n]) / g.dx[2] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m, n]) / g.dx[2] with hcl.elif_(k != 0 and k != V.shape[2] - 1): left_deriv[0] = (V[i, j, k, l, m, n] - V[i, j, k - 1, l, m, n]) / g.dx[2] right_deriv[0] = (V[i, j, k + 1, l, m, n] - V[i, j, k, l, m, n]) / g.dx[2] return left_deriv[0], right_deriv[0]
def spa_derivX4_5d(i, j, k, l, m, V, g): # Left -> right == Outer Most -> Inner Most left_deriv = hcl.scalar(0, "left_deriv") right_deriv = hcl.scalar(0, "right_deriv") if 4 not in g.pDim: with hcl.if_(l == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k, l, m] + my_abs(V[i, j, k, l + 1, m] - V[i, j, k, l, m]) * my_sign( V[i, j, k, l, m]) left_deriv[0] = (V[i, j, k, l, m] - left_boundary[0]) / g.dx[3] right_deriv[0] = (V[i, j, k, l + 1, m] - V[i, j, k, l, m]) / g.dx[3] with hcl.elif_(l == V.shape[3] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k, l, m] + my_abs(V[i, j, k, l, m] - V[i, j, k, l - 1, m]) * my_sign( V[i, j, k, l, m]) left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l - 1, m]) / g.dx[3] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m]) / g.dx[3] with hcl.elif_(l != 0 and l != V.shape[3] - 1): left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l - 1, m]) / g.dx[3] right_deriv[0] = (V[i, j, k, l + 1, m] - V[i, j, k, l, m]) / g.dx[3] return left_deriv[0], right_deriv[0] else: with hcl.if_(l == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k, V.shape[3] - 1, m] left_deriv[0] = (V[i, j, k, l, m] - left_boundary[0]) / g.dx[3] right_deriv[0] = (V[i, j, k, l + 1, m] - V[i, j, k, l, m]) / g.dx[3] with hcl.elif_(l == V.shape[3] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k, 0, m] left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l - 1, m]) / g.dx[3] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m]) / g.dx[3] with hcl.elif_(l != 0 and l != V.shape[3] - 1): left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l - 1, m]) / g.dx[3] right_deriv[0] = (V[i, j, k, l + 1, m] - V[i, j, k, l, m]) / g.dx[3] return left_deriv[0], right_deriv[0]
def spa_derivX1_5d(i, j, k, l, m, V, g): # Left -> right == Outer Most -> Inner Most left_deriv = hcl.scalar(0, "left_deriv") right_deriv = hcl.scalar(0, "right_deriv") if 1 not in g.pDim: with hcl.if_(i == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k, l, m] + my_abs(V[i + 1, j, k, l, m] - V[i, j, k, l, m]) * my_sign( V[i, j, k, l, m]) left_deriv[0] = (V[i, j, k, l, m] - left_boundary[0]) / g.dx[0] right_deriv[0] = (V[i + 1, j, k, l, m] - V[i, j, k, l, m]) / g.dx[0] with hcl.elif_(i == V.shape[0] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k, l, m] + my_abs(V[i, j, k, l, m] - V[i - 1, j, k, l, m]) * my_sign( V[i, j, k, l, m]) left_deriv[0] = (V[i, j, k, l, m] - V[i - 1, j, k, l, m]) / g.dx[0] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m]) / g.dx[0] with hcl.elif_(i != 0 and i != V.shape[0] - 1): left_deriv[0] = (V[i, j, k, l, m] - V[i - 1, j, k, l, m]) / g.dx[0] right_deriv[0] = (V[i + 1, j, k, l, m] - V[i, j, k, l, m]) / g.dx[0] return left_deriv[0], right_deriv[0] else: with hcl.if_(i == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[V.shape[0] - 1, j, k, l, m] left_deriv[0] = (V[i, j, k, l, m] - left_boundary[0]) / g.dx[0] right_deriv[0] = (V[i + 1, j, k, l, m] - V[i, j, k, l, m]) / g.dx[0] with hcl.elif_(i == V.shape[0] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[0, j, k, l, m] left_deriv[0] = (V[i, j, k, l, m] - V[i - 1, j, k, l, m]) / g.dx[0] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m]) / g.dx[0] with hcl.elif_(i != 0 and i != V.shape[0] - 1): left_deriv[0] = (V[i, j, k, l, m] - V[i - 1, j, k, l, m]) / g.dx[0] right_deriv[0] = (V[i + 1, j, k, l, m] - V[i, j, k, l, m]) / g.dx[0] return left_deriv[0], right_deriv[0]
def opt_ctrl(self, t, spat_deriv): uOpt1 = hcl.scalar(0, "uOpt1") uOpt2 = hcl.scalar(0, "uOpt2") uOpt3 = hcl.scalar(0, "uOpt3") uOpt4 = hcl.scalar(0, "uOpt4") parSum1 = hcl.scalar(0, "parSum1") parSum2 = hcl.scalar(0, "parSum2") with hcl.if_(self.uMode == "min"): with hcl.if_(spat_deriv[0] > 0): uOpt3[0] = -self.pMax[0] with hcl.elif_(spat_deriv[0] < 0): uOpt3[0] = -self.pMin[0] with hcl.if_(spat_deriv[1] > 0): uOpt4[0] = -self.pMax[1] with hcl.elif_(spat_deriv[1] < 0): uOpt4[0] = -self.pMin[1] parSum1[0] = (1/(self.m - self.X_udot)) * spat_deriv[2] * self.B[0,0] + (1/(self.m - self.Z_wdot)) * spat_deriv[3] * self.B[1,0] with hcl.if_(parSum1[0] > 0): uOpt1[0] = self.uMin[0] with hcl.elif_(parSum1[0] < 0): uOpt1[0] = self.uMax[0] parSum2[0] = (1/(self.m - self.X_udot)) * spat_deriv[2] * self.B[0,1] + (1/(self.m - self.Z_wdot)) * spat_deriv[3] * self.B[1,1] with hcl.if_(parSum2[0] > 0): uOpt2[0] = self.uMin[1] with hcl.elif_(parSum2[0] < 0): uOpt2[0] = self.uMax[1] return (uOpt1[0], uOpt2[0], uOpt3[0], uOpt4[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 opt_ctrl(self, t, state, spat_deriv): """ :param t: time t :param state: tuple of coordinates in 6 dimensions :param spat_deriv: spatial derivative in all dimensions :return: tuple of optimal control """ # Optimal control 1, 2, 3 uOpt1 = hcl.scalar(0, "uOpt1") uOpt2 = hcl.scalar(0, "uOpt2") uOpt3 = hcl.scalar(0, "uOpt3") SumUU = hcl.scalar(0, "SumUU") SumUL = hcl.scalar(0, "SumUL") SumLU = hcl.scalar(0, "SumLU") SumLL = hcl.scalar(0, "SumLL") parSum = hcl.scalar(0, "parSum") with hcl.if_(self.uMode == "min"): # everything containing (u1, u2) in Hamiltonian, for all combinations of u1 and u2 SumUU[0] = spat_deriv[1]*(self.g + self.uMax[1]) * (state[0] + self.uMax[0])/state[2] + \ spat_deriv[3] * self.uMax[1] SumUL[0] = spat_deriv[1]*(self.g + self.uMax[1]) * (state[0] + self.uMin[0])/state[2] + \ spat_deriv[3] * self.uMax[1] SumLU[0] = spat_deriv[1]*(self.g + self.uMin[1]) * (state[0] + self.uMax[0])/state[2] + \ spat_deriv[3] * self.uMin[1] SumLL[0] = spat_deriv[1]*(self.g + self.uMin[1]) * (state[0] + self.uMin[0])/state[2] + \ spat_deriv[3] * self.uMin[1] # try every combination of u1 and u2 and take minimum with hcl.if_(SumUU[0] > SumUL[0]): uOpt1[0] = self.uMin[0] uOpt2[0] = self.uMax[1] SumUU[0] = SumUL[0] with hcl.elif_(SumUU[0] < SumUL[0]): uOpt1[0] = self.uMax[0] uOpt2[0] = self.uMax[1] with hcl.if_(SumUU[0] > SumLU[0]): uOpt1[0] = self.uMax[0] uOpt2[0] = self.uMin[1] SumUU[0] = SumLU[0] with hcl.if_(SumUU[0] > SumLL[0]): uOpt1[0] = self.uMin[0] uOpt2[0] = self.uMin[1] # Find u3 # everything multiplied by u3 in Hamiltonian parSum[0] = spat_deriv[1] + spat_deriv[5] * state[2] / self.J with hcl.if_(parSum[0] > 0): uOpt3[0] = self.uMin[2] with hcl.elif_(parSum[0] < 0): uOpt3[0] = self.uMax[2] return (uOpt1[0], uOpt2[0], uOpt3[0])
def spa_derivX5_5d(i, j, k, l, m, V, g): # Left -> right == Outer Most -> Inner Most left_deriv = hcl.scalar(0, "left_deriv") right_deriv = hcl.scalar(0, "right_deriv") if 5 not in g.pDim: with hcl.if_(m == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k, l, m] + my_abs(V[i, j, k, l, m + 1] - V[i, j, k, l, m]) * my_sign( V[i, j, k, l, m]) left_deriv[0] = (V[i, j, k, l, m] - left_boundary[0]) / g.dx[4] right_deriv[0] = (V[i, j, k, l, m + 1] - V[i, j, k, l, m]) / g.dx[4] with hcl.elif_(m == V.shape[4] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k, l, m] + my_abs(V[i, j, k, l, m] - V[i, j, k, l, m - 1]) * my_sign( V[i, j, k, l, m]) left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l, m - 1]) / g.dx[4] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m]) / g.dx[4] with hcl.elif_(m != 0 and m != V.shape[4] - 1): left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l, m - 1]) / g.dx[4] right_deriv[0] = (V[i, j, k, l, m + 1] - V[i, j, k, l, m]) / g.dx[4] return left_deriv[0], right_deriv[0] else: with hcl.if_(m == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k, l, V.shape[4] - 1] left_deriv[0] = (V[i, j, k, l, m] - left_boundary[0]) / g.dx[4] right_deriv[0] = (V[i, j, k, l, m + 1] - V[i, j, k, l, m]) / g.dx[4] with hcl.elif_(m == V.shape[4] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k, l, 0] left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l, m - 1]) / g.dx[4] right_deriv[0] = (right_boundary[0] - V[i, j, k, l, m]) / g.dx[4] with hcl.elif_(m != 0 and m != V.shape[4] - 1): left_deriv[0] = (V[i, j, k, l, m] - V[i, j, k, l, m - 1]) / g.dx[4] right_deriv[0] = (V[i, j, k, l, m + 1] - V[i, j, k, l, m]) / g.dx[4] return left_deriv[0], right_deriv[0]
def opt_ctrl(self, t, state, spat_deriv): """ :param t: time t :param state: tuple of coordinates in 6 dimensions :param spat_deriv: spatial derivative in all dimensions :return: tuple of optimal control """ # Optimal control 1, 2, 3 uOpt1 = hcl.scalar(0, "uOpt1") uOpt2 = hcl.scalar(0, "uOpt2") uOpt3 = hcl.scalar(0, "uOpt3") SumUU = hcl.scalar(0, "SumUU") SumUL = hcl.scalar(0, "SumUL") SumLU = hcl.scalar(0, "SumLU") SumLL = hcl.scalar(0, "SumLL") parSum = hcl.scalar(0, "parSum") with hcl.if_(self.uMode == "min"): parSum[0] = spat_deriv[1] + spat_deriv[5] * state[2] / self.J SumUU[0] = spat_deriv[1] * (self.g + self.uMax[1]) * ( state[0] + self.uMax[0]) / state[2] + spat_deriv[3] * self.uMax[1] SumUL[0] = spat_deriv[1] * (self.g + self.uMax[1]) * ( state[0] + self.uMin[0]) / state[2] + spat_deriv[3] * self.uMax[1] SumLU[0] = spat_deriv[1] * (self.g + self.uMin[1]) * ( state[0] + self.uMax[0]) / state[2] + spat_deriv[3] * self.uMin[1] SumLL[0] = spat_deriv[1] * (self.g + self.uMin[1]) * ( state[0] + self.uMin[0]) / state[2] + spat_deriv[3] * self.uMin[1] with hcl.if_(SumUU[0] > SumUL[0]): uOpt1[0] = self.uMin[0] uOpt2[0] = self.uMax[1] SumUU[0] = SumUL[0] with hcl.elif_(SumUU[0] < SumUL[0]): uOpt1[0] = self.uMax[0] uOpt2[0] = self.uMax[1] with hcl.if_(SumUU[0] > SumLU[0]): uOpt1[0] = self.uMax[0] uOpt2[0] = self.uMin[1] SumUU[0] = SumLU[0] with hcl.if_(SumUU[0] > SumLL[0]): uOpt1[0] = self.uMin[0] uOpt2[0] = self.uMin[1] # Find third controls with hcl.if_(parSum[0] > 0): uOpt3[0] = self.uMin[2] with hcl.elif_(parSum[0] < 0): uOpt3[0] = self.uMax[2] return (uOpt1[0], uOpt2[0], uOpt3[0])
def optDstb(self, t, state, spat_deriv): """ :param spat_deriv: tuple of spatial derivative in all dimensions state: x1, x2, x3 t: time :return: a tuple of optimal disturbances """ # Graph takes in 4 possible inputs, by default, for now d1 = hcl.scalar(self.dMax, "d1") d2 = hcl.scalar(0, "d2") # Just create and pass back, even though they're not used d3 = hcl.scalar(0, "d3") # Declare a variable b_term = hcl.scalar(0, "b_term") # use the scalar by indexing 0 everytime b_term[0] = spat_deriv[2] with hcl.if_(b_term[0] >= 0): with hcl.if_(self.dMode == "min"): d1[0] = -d1[0] with hcl.elif_(b_term[0] < 0): with hcl.if_(self.dMode == "max"): d1[0] = -d1[0] return (d1[0], d2[0], d3[0])
def opt_ctrl(self, t, state, spat_deriv): """ :param spat_deriv: tuple of spatial derivative in all dimensions state: x1, x2, x3 t: time :return: a tuple of optimal disturbances """ opt_w = hcl.scalar(self.wMax, "opt_w") # Just create and pass back, even though they're not used in3 = hcl.scalar(0, "in3") in4 = hcl.scalar(0, "in4") #a_term = spat_deriv[0] * self.x[1] - spat_deriv[1]*self.x[0] - spat_deriv[2] # Declare a variable a_term = hcl.scalar(0, "a_term") # use the scalar by indexing 0 everytime a_term[0] = spat_deriv[0] * state[1] - spat_deriv[1] * state[ 0] - spat_deriv[2] with hcl.if_(a_term >= 0): with hcl.if_(self.uMode == "min"): opt_w[0] = -opt_w[0] with hcl.elif_(a_term < 0): with hcl.if_(self.uMode == "max"): opt_w[0] = -opt_w[0] return (opt_w[0], in3[0], in4[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 clamp(val, min_, max_): local = hcl.scalar(val) with hcl.if_(val < min_): local[0] = min_ with hcl.elif_(val > max_): local[0] = max_ return local[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)
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)
def opt_ctrl(self, spat_deriv): opt_w = hcl.scalar(self.wMax, "opt_w") with hcl.if_(spat_deriv[2] > 0): with hcl.if_(self.uMode == "min"): opt_w[0] = -opt_w with hcl.elif_(spat_deriv[2] < 0): with hcl.if_(self.uMode == "max"): opt_w[0] = -opt_w return opt_w[0]
def optDstb(self, spat_deriv): dOpt1 = hcl.scalar(0, "dOpt1") dOpt2 = hcl.scalar(0, "dOpt2") dOpt3 = hcl.scalar(0, "dOpt3") dOpt4 = hcl.scalar(0, "dOpt4") with hcl.if_(self.dMode == "max"): with hcl.if_(spat_deriv[0] > 0): dOpt1[0] = self.dMax[0] with hcl.elif_(spat_deriv[0] < 0): dOpt1[0] = self.dMin[0] with hcl.if_(spat_deriv[1] > 0): dOpt2[0] = self.dMax[1] with hcl.elif_(spat_deriv[1] < 0): dOpt2[0] = self.dMin[1] with hcl.if_(spat_deriv[2] > 0): dOpt3[0] = self.dMax[2] with hcl.elif_(spat_deriv[2] < 0): dOpt3[0] = self.dMin[2] with hcl.if_(spat_deriv[3] > 0): dOpt4[0] = self.dMax[3] with hcl.elif_(spat_deriv[3] < 0): dOpt4[0] = self.dMin[3] with hcl.elif_(self.dMode == "min"): with hcl.if_(spat_deriv[0] > 0): dOpt1[0] = self.dMin[0] with hcl.elif_(spat_deriv[0] < 0): dOpt1[0] = self.dMax[0] with hcl.if_(spat_deriv[1] > 0): dOpt2[0] = self.dMin[1] with hcl.elif_(spat_deriv[1] < 0): dOpt2[0] = self.dMax[1] with hcl.if_(spat_deriv[2] > 0): dOpt3[0] = self.dMin[2] with hcl.elif_(spat_deriv[2] < 0): dOpt3[0] = self.dMax[2] with hcl.if_(spat_deriv[3] > 0): dOpt4[0] = self.dMin[3] with hcl.elif_(spat_deriv[3] < 0): dOpt4[0] = self.dMax[3] return (dOpt1[0], dOpt2[0], dOpt3[0], dOpt4[0])
def spa_derivX(i, j, k, V): left_deriv = hcl.scalar(0, "left_deriv") right_deriv = hcl.scalar(0, "right_deriv") with hcl.if_(i == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V[i, j, k] + my_abs(V[i + 1, j, k] - V[i, j, k]) * my_sign(\ V[i, j, k]) left_deriv[0] = (V[i, j, k] - left_boundary[0]) / g.dx[0] right_deriv[0] = (V[i + 1, j, k] - V[i, j, k]) / g.dx[0] with hcl.elif_(i == V.shape[0] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V[i, j, k] + my_abs(V[i, j, k] - V[i - 1, j, k]) * my_sign( V[i, j, k]) left_deriv[0] = (V[i, j, k] - V[i - 1, j, k]) / g.dx[0] right_deriv[0] = (right_boundary[0] - V[i, j, k]) / g.dx[0] with hcl.elif_(i != 0 and i != V.shape[0] - 1): left_deriv[0] = (V[i, j, k] - V[i - 1, j, k]) / g.dx[0] right_deriv[0] = (V[i + 1, j, k] - V[i, j, k]) / g.dx[0] return left_deriv[0], right_deriv[0]
def spa_derivY(i, j, k): left_deriv = hcl.scalar(0, "left_deriv") right_deriv = hcl.scalar(0, "right_deriv") with hcl.if_(j == 0): left_boundary = hcl.scalar(0, "left_boundary") left_boundary[0] = V_init[i, j, k] + my_abs(V_init[ i, j + 1, k] - V_init[i, j, k]) * my_sign(V_init[i, j, k]) left_deriv[0] = (V_init[i, j, k] - left_boundary[0]) / g.dx[1] right_deriv[0] = (V_init[i, j + 1, k] - V_init[i, j, k]) / g.dx[1] with hcl.elif_(j == V_init.shape[1] - 1): right_boundary = hcl.scalar(0, "right_boundary") right_boundary[0] = V_init[i, j, k] + my_abs(V_init[ i, j, k] - V_init[i, j - 1, k]) * my_sign(V_init[i, j, k]) left_deriv[0] = (V_init[i, j, k] - V_init[i, j - 1, k]) / g.dx[1] right_deriv[0] = (right_boundary[0] - V_init[i, j, k]) / g.dx[1] with hcl.elif_(j != 0 and j != V_init.shape[1] - 1): left_deriv[0] = (V_init[i, j, k] - V_init[i, j - 1, k]) / g.dx[1] right_deriv[0] = (V_init[i, j + 1, k] - V_init[i, j, k]) / g.dx[1] return left_deriv[0], right_deriv[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 opt_ctrl(self, t, state, spat_deriv): opt_w = hcl.scalar(self.wMax, "opt_w") # Just create and pass back, even though they're not used in3 = hcl.scalar(0, "in3") in4 = hcl.scalar(0, "in4") with hcl.if_(spat_deriv[2] > 0): with hcl.if_(self.uMode == "min"): opt_w[0] = -opt_w with hcl.elif_(spat_deriv[2] < 0): with hcl.if_(self.uMode == "max"): opt_w[0] = -opt_w return (opt_w[0], in3[0], in4[0])
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 knn_vote(knn_mat, j): id0 = hcl.local(0, "id0") id1 = hcl.local(0, "id1") id2 = hcl.local(0, "id2") count = hcl.local(0, "count") with hcl.for_(0, 10) as n: with hcl.if_(knn_mat[n][0] < knn_mat[id0.v][0]): id0.v = n with hcl.for_(0, 10) as m: with hcl.if_(knn_mat[m][0] < knn_mat[id1.v][0]): id1.v = m with hcl.for_(0, 10) as k: with hcl.if_(knn_mat[k][0] < knn_mat[id2.v][0]): id2.v = k with hcl.if_(j == id0.v): count.v += 1 with hcl.elif_(j == id1.v): count.v += 1 with hcl.elif_(j == id2.v): count.v += 1 with hcl.else_(): count.v += 0 return count.v