Grobner bases of ideals invariant under endomorphisms