Zulko / easyAI

Python artificial intelligence framework for games
http://zulko.github.io/easyAI/
Other
639 stars 126 forks source link

AI giving up when it foresees loss #48

Closed rob2121 closed 2 years ago

rob2121 commented 2 years ago

I would like the AI to fight a little harder even if it foresees a loss assuming correct play from the opponent. Is there a way to access the current depth of search in negamax? Maybe then I could adjust the scoring so that, for example, a loss in 7 moves is less bad than a loss in 3 moves.

JohnAD commented 2 years ago

For the sake of future folks asking a similar question: you can do this by adjusting the score. In the end, Negamax is a scoring algorithm using relative values. A typical method might be choose 1000 to mean "win" and -1000 to mean "loss" (and values in-between to mean relative strategic strength.) A possible "tweak" is to adjust the value of win/lose. So that winning/losing now is worth 1020/-1020 but winning/losing in 10 turns is worth 1010/-1010. This would both encourage a quicker win but also a longer lose.

But fair warning, the scores adjustment must be balanced. Negamax assumes a "zero-sum-game". So, both the losing and winning scores must balance each other. (It need not be positive negative like my example however. For example, if 1000 means win and 0 means lose and 500 is neutral. Then a board position of worth 600 should be worth 400 to the opponent.)

rob2121 commented 2 years ago

A possible "tweak" is to adjust the value of win/lose. So that winning/losing now is worth 1020/-1020 but winning/losing in 10 turns is worth 1010/-1010.

To do this, do I need access to something like self.current_depth of the search? That is, the scoring is based on how many moves ahead we are currently looking?

In any case I found a solution which was to score based on win and take advantage of the depth weighting in negamax: score = 100 if self.win() else self.player.hero.health - self.opponent.hero.health So now even if the AI is bound to lose to perfect play, it will keep making moves that immediately increase the difference between the player and opponent "hit points" -- it goes down fighting!