Optimization-driven emergence of deep hierarchies with applications in data mining and evolution