An optimization unit controls electrical currents of a set of electromagnets to generate a wanted maneuvering magnetic field pattern (MMP) for moving an in-vivo device in the GI system. The optimization unit may calculate a magnetic force and a magnetic field to maneuver the in-vivo device from a current location and/or orientation to a new location and/or orientation. The optimization unit may solve a magnetic force optimization problem with respect to the magnetic force in order to determine electrical currents suitable for generating the wanted MMP. The optimization unit may additionally or alternatively solve a minimum electrical power optimization problem with respect to the electrical power to be consumed by the electromagnets in order to recalculate or adjust the electrical currents. The optimization unit may solve one or more of the optimization problems while complying with a set of constraints associated with or derived from each type of optimization objective.