Auto-translated
It seems to me that the AI aims to inflict not maximum damage, but maximum reduction of damage to our stack, possibly without considering the difference between attack and defense. The minimum retaliation criterion also seems to play a role.
For example, consider a hero with 99-99 stats, 4 archers, 1000 peasants, and some other units in Utopia. Dragons attack the peasants (retaliation of 4k), rather than the single units (~0) or archers (800). However, the attack removes only about 100 retaliation damage (and the archangel doesn't die from the attack at all). There, dragons often attack minotaurs first (high relative damage/HP) IF none of them die from the retaliation. Otherwise, it's difficult to explain this choice of targets.
It was also said that the AI, sensing defeat, starts attacking the main stack. Is there a more precise definition? Perhaps it was confused with the top stack.