Constrained trace-optimization of polynomials in freely noncommuting variables