Codon usage bias is the preferential use of synonymous codons. First models that studied this phenomenon assumed that the population is at mutation-selection-drift equilibrium, but more advanced models were proposed later to incorporate demographic changes. One of these models proposed by Zeng and Charlesworth represents the evolutionary process by a Markov model, allowing for changes in the population size. Their model is, however, too simple to reflect many realistic demographic changes. In this paper, we extend their model by allowing complex demographies with many changes in population size. Such extension requires a more powerful optimization algorithm compared with the simple one used in the model proposed by Zeng and Charlesworth. The optimization algorithm we use is a version of the genetic algorithm that we develop particularly for this purpose. We validate our method using simulated data.