We report on the introduction of active optical elements into confocal and multiphoton microscopes in order to reduce the sample-induced aberration. Using a flexible membrane mirror as the active element, the beam entering the rear of the microscope objective is altered to produce the smallest point spread function once it is brought to a focus inside the sample. The conventional approach to adaptive optics, commonly used in astronomy, is to utilise a wavefront sensor to determine the required mirror shape. We have developed a technique that uses optimisation algorithms to improve the returned signal without the use of a wavefront sensor. We have investigated a number of possible optimisation methods, covering hill climbing, genetic algorithms, and more random search methods. The system has demonstrated a significant enhancement in the axial resolution of a confocal microscope when imaging at depth within a sample. We discuss the trade-offs of the various approaches adopted, comparing speed with resolution enhancement.