Electronic institutions are socially-inspired multi-agent systems, typically operating under a set of policies, which are required to determine system operation and to deal with violations and other non-compliant behaviour. They are often faced with a dynamic population of agents, social network, and environment and their policy should suit this context. However, there is usually a large space of possible system policies, but no tractable systematic method to find an appropriate policy given a joint state of the population, social network, and the environment. We have developed a model of an energy system which encompasses several interconnected community energy systems. We propose two methods, an offline and an online procedure, which enable this system model to approximately optimise its performance through adaptation and evolution of its operating policy. The policies evolved by our procedures clearly outperform a baseline policy we have designed by hand. Both procedures return policies which are appropriate for a system, given some performance criterion, without a human designer's intervention. This could lay the foundations for the development of a new methodological paradigm for the engineering of collective adaptive systems.