The optimization of the cogeneration systems (CGS) with the gas engine generator is studied by using the Hamiltonian algorithm (HA). The HA, invented from the study on dynamical systems, is efficient to optimize to design and control the complex systems. The results show that the HA enable us to design the optimal CGS under the objective function about the investment of the plants and the equipment. The HA is also found to be effective to control the optimal CGS operations.