In addressing challenges associated with the constrained enhancement of range performance in pure electric tractors, the difficulty in achieving genuinely eco-friendly agricultural machinery with hybrid powertrains, and the outdated approaches to energy optimization in hydrogen fuel cell tractors, a comprehensive methodology covering the entire spectrum of system design, optimization, and validation is presented. This method constructs an integrated electromechanical and hydraulic power bond graph model for an agricultural tractor, encompassing energy systems, drive systems, and lifting systems. It achieves rapid interaction between 20-sim software and Matlab/Simulink software through a script toolbox. In order to tackle the real-time applicability challenges of dynamic programming, a hierarchical dynamic programming is proposed. The dynamic programming strategy results serve as the upper-level output. At the lower layer, preprocessing of impact value variables is carried out to filter the input variables for the general regression neural network. This enables the real-time application of the dynamic programming algorithm and significantly reduces training time. Finally, a Matlab-HIL test platform is established, utilizing 20-sim, Matlab/Simulink, HIL cabinet, and PowerECU-57A controller as the core components for the test of modelin-the-loop and hardware-in-the-loop. The results demonstrate the correctness and superiority of the designed tractor simulation model and energy management strategy model compared to the power-following strategy. Specifically, in plowing conditions, the dynamic programming and hierarchical dynamic programming strategies exhibited reductions of 10.278% and 6.728%, respectively, in equivalent hydrogen consumption compared to power-following strategy. In transportation conditions, the reductions are 16.02% for dynamic programming strategy and 4.87% for hierarchical dynamic programming strategy, both relative to powerfollowing strategy. This study lays the theoretical groundwork for modeling dynamic systems of tractors and optimizing energy management.INDEX TERMS hydrogen fuel cell tractors, bond graph, energy management strategy, model-in-the-loop, hard-ware-in-the-loop.