Variational approach for pair optimization in the nucleon pair approximation