Evolution of compact and fossil groups of galaxies from semi-analytical models of galaxy formation