Optimisation of doubly periodic arrays of three-dimensional metallic structures in free space and layered media