Real-time optimization of active loops for the magnetic field minimization