Many power distribution methods of microgrid (MG) are all limited by fixed structure of MG and mathematical model, which will be out of effect once the structure changes. In order to solve the above problems, this paper proposed an adaptive hierarchical control method considering generation cost. Firstly, the distributed generators (DGs) of MG are considered as multi-agent system, and the secondary controller based on finite-time theory is constructed. Secondly, the improved gray wolf optimization (IGWO) algorithm is used as the tertiary controller to dynamically optimize the rated power of DG, and the objective function and constraints are established. Thirdly, the adaptive virtual impedance is introduced to realize accurate power sharing and the closed-loop control is formed by combining the current loop and voltage loop controllers. Finally, by dynamically optimizing the rated power, the on-line real-time optimal power distribution of MG is achieved, and the stability of the system is proved by the theories of multi-agent consistency and finite-time stability. The control method proposed in this paper accelerates the convergence speed of MG, and the calculation results have high accuracy. At the same time, the flexibility and reliability of MG are improved. The simulation model is established in Matlab/Simulink environment, and the simulation results show that the method is effective. INDEX TERMS Finite time, grey wolf optimization, hierarchical control, microgrid, power sharing