예제 #1
0
	def __add__(self, q):
		#add two points on an EC
		assert type(q) == type(ECpoint())
		
		if not self.x == self.y == 0 and not q.x == q.y == 0:
			if self.x == q.x and self.y == q.y:
				#double
				slope = (3 * self.x ** 2 + self.ec.acoef) * gmpy2.invert(2 * self.y, self.ec.prime) % self.ec.prime

			elif self.x == q.x and self.y != q.y:
				#negate
				res = copy.copy(self)
				res.x = 0
				res.y = 0
				return res

			else:
				#regular add
				slope = (self.y-q.y) * gmpy2.invert(self.x-q.x, self.ec.prime) % self.ec.prime

			x = (slope ** 2 - self.x - q.x) % self.ec.prime
			y = -(slope * (x - self.x) + self.y) % self.ec.prime
			
			#resEC = copy(self)
			#resEC.x, resEC.y = x, (y)
			#resEC.x, resEC.y, resEC.ec = x, y, self.ec
			#return resEC
			return ECpoint(x,y,self.ec)


		elif self.x == 0:#if self is pai
			return q
			
		else:
			return copy(self)
예제 #2
0
def grid_heuristic_with_orientation(field, gen_moves, is_goal, cost_function,
                                    is_passable):
    """builds heuristic for pathfinding on 2D grid with orientation in 4 directions.
        
    Arguments:
        field {List} -- [a 2D matrix representing the 'world']
        gen_moves {function} -- [a function to generate possible actions]
        is_goal {function} -- [returns True if given x, y is goal]
        cost_function {function} -- [returns cost of a given action]
        is_passable {function} -- [returns True if a given cell is passable]
    
    Returns:
        [list] -- [heuristic for all 4 orientations]
    """
    change = true
    X, Y = len(field), len(field[0])
    value = [[[
        lambda (x, y): 0 if is_goal(x, y) else sys.maxint for y in range(Y)
    ] for x in range(X)] for o in range(4)]
    policy = [deepcopy.copy(field) for o in range(4)]
    while (change):
        change = False
        for x in range(X):
            for y in range(Y):
                if is_passable(policy[0][x][y]):
                    for orient in range(4):
                        moves = getMoves(orient, [x, y])
                        for ori, x2, y2, mv in moves:
                            cost = getCost(mv) + value[orient][x2][y2]
                            if cost < value[orient][x][y]:
                                change = True
                                value[orient][x][y] = cost
                                policy[orient][x][y] = mv
    return policy
예제 #3
0
 def visit_BoolOp(self, boolop):
     root = self.add_block(boolop, 'logic_operation')
     self.add_field(root=root,
                    name='OP',
                    text=(boolop.op.__class__.__name__.upper()))
     self.add_value(root, 'A', boolop.values[0])
     if len(boolop.values) == 2:
         self.add_value(root, 'B', boolop.values[1])
     else:
         rest = deepcopy.copy(boolop)
         rest.values = rest.values[1:]
         self.add_value(root, 'B', rest)