wrote new gradient based function which seems more stable with the A* expansion which doesn't have enough potentials for gradient calculations