In this paper, DAG is utilized to represent the dependencies between musical features, and a topological sorting algorithm based on layer order relationships is used as the sampling algorithm for AI music generation models. The feature de-entanglement mechanism of VAE is utilized to learn multiple feature representations, and Transformer-XL is used as the encoder and decoder of the model to design the Control-VAE model that manipulates the latent variable representations to change the music structure. Statistical autocorrelation coefficients, spectral analysis, and diversity auto assessment metrics data were used to evaluate the model performance in terms of three dimensions: melody, timbre, and diversity. The feasibility of Control-VAE model AI music generation and melody optimization is examined through the evaluation of practical application effects. The results show that the autocorrelation coefficients and frequency amplitudes of the music generated by Control-VAE model are basically consistent with the original music, and reach human-like PPL values, seIf-BLEU values and Zipf coefficients near p=0.95.The music pieces generated by Control-VAE model have a certain degree of musicality, and the melody-optimized music is clear, accurate and novel and interesting.