A hierarchicial algorithm for neural training and control