Imperfections in integrated photonics manufacturing have a detrimental effect on the maximal achievable visibility in interferometric architectures. These limits have profound implications for further technological developments in photonics and in particular for quantum photonic technologies. Active optimization approaches, together with reconfigurable photonics, have been proposed as a solution to overcome this. In this Letter, we demonstrate an ultrahigh (>60 dB) extinction ratio in a silicon photonic device consisting of cascaded Mach-Zehnder interferometers, in which additional interferometers function as variable beamsplitters. The imperfections of fabricated beamsplitters are compensated using an automated progressive optimization algorithm with no requirement for precalibration. This work shows the possibility of integrating and accurately controlling linear-optical components for large-scale quantum information processing and other applications.