This paper presents an optimization analysis for energy and power management in microgrids with renewable energy resources and diesel generators. Dynamic Programming (DP) algorithm and the simplex method have been used to minimize the total daily cost of the system and also maximize the total efficiency of the energy storage system during charge and discharge. The algorithm is validated on a hybrid system for military forward operating base camps that contain a PV array, wind generator, hydrogen-based fuel cell, batteries and diesel generators.