Guqin music has been viewed as the symbol of Chinese music. Using artificial intelligence approaches to study Guqin music's composition will have an important theoretical and practical value. For the characteristics of Guqin tablature, a new model of lattice-gas automata with aggregation (LGAA) was constructed to generate melody, based on the theory of lattice-gas cellular automata (LGCA) and diffusion limited aggregation (DLA). Firstly, music segments were composed by the model of LGAA based on an emotional database. Then, based on the same pitch database, they were made smoother by a balance principle, which was followed by almost all Chinese traditional music. After that, composition music could be regarded as a knapsack problem, and the smooth music segments were seemed as the items. Therefore, the generation of the Guqin piece equaled the optimal solution to the knapsack problem. In the end, five musicians were invited to judge the results by two criteria and they all agreed that the automatic generated pieces of Guqin were success ful .