Advertisement

AI is harder than I thought

Started by April 12, 2014 03:18 PM
38 comments, last by LAURENT* 10 years, 9 months ago

If fuzzy logic wasn't already an established term, and used in many fields of engineering and programming systems, then I might sound more like a fangirl, but it is established, and used in more applications than I can yet expound upon. Again, the term is new, but the idea is old.

No one told you that it wasn't used for a lof of things. But like every AI system it's a tool. And as with all tools they have specific uses.I wouldn't use Fuzzy logic to solve tic tac toe and i wouldn't use a screwdriver to fell a tree.

second post addressed to me[...]

No they do not assume that. With the rules we explained regarding tic tac toe, what the opponent play doesn't matter. If he play more or less correctly he'll get a draw, if not he'll loose period.

The chess analogy isn't relevant and it's off topic, and you may want to read a bit more about the logic systemS used in chess before jumping to conclusion.

Seriously...


. A guard would have heard you, but due to distance and other factors he isn't sure sure if it's an error or your location so it continues his patrol but with a wider cone of vision / improved hearing / etc depending on how sure he is to have heard an intruder

That is the type of reasoning I am talking about. I come to this type of reasoning in pretty much all games. Because there is always a little uncertainty in real reasoning. And fuzzy logic considers this. It is not always a matter of true or false, because the condition is uncertain. When I am playing against a human opponent, I am not sure what move they will make, because they might not play the "best move." Bobby Fischer once played a move that was not the "best move" by the book, but because he understood the condition by another reasoning, he actually was playing the game against his opponent (using his pride) and not against the book.

That is all I will say on this subject though. The response was to how AI could be done, using chess as an example (more complex game). It is relevant.

They call me the Tutorial Doctor.

Advertisement

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?


Depends how you plan to use it really :)

Normal use of rand is with modulus like

int i = rand() % 10;

That will give you a random number between 0 and 9 inclusive. If you are trying to pick a random square, that would be the way to use it. Replace 10 with 9 to get an index into the (zero based) array of squares.

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?


Depends how you plan to use it really smile.png

Normal use of rand is with modulus like

int i = rand() % 10;

That will give you a random number between 0 and 9 inclusive. If you are trying to pick a random square, that would be the way to use it. Replace 10 with 9 to get an index into the (zero based) array of squares.

[insert rant about this method not actually producing uniformly distributed random integers]

You need to generate the random integer in a loop, such that if the random integer is at the very end of the range (specifically, larger than MAX_RANGE - MAX_RANGE%10) you throw it away and generate a new one, until you get one that is not a part of that "remainder" at the end of the range (thus the loop)


int range=10;
int rand=randInt();
//while (rand > MAX_INT - MAX_INT%range) <-dis wrong. dont do dis.
while (rand >= MAX_INT - MAX_INT%range) //if result part of remainder at end of possible random values, regen
{
    rand=randInt();
}
return rand % range;

Though, better read an article written by someone who loves PRNGs to be sure this is correct. ;)

o3o

Don't worry waterlimon the rand operator is in my while loop, Also I got success with the computer randomly picking spots. Unfortunately I now have a logic problem where it only circles's turn once and x turn for the rest of the game.

Advertisement

Um.......................I had this idea to use the rand operator. Not working like I hoped. Anyone thinks this will work?


Depends how you plan to use it really smile.png

Normal use of rand is with modulus like

int i = rand() % 10;

That will give you a random number between 0 and 9 inclusive. If you are trying to pick a random square, that would be the way to use it. Replace 10 with 9 to get an index into the (zero based) array of squares.

[insert rant about this method not actually producing uniformly distributed random integers]

You need to generate the random integer in a loop, such that if the random integer is at the very end of the range (specifically, larger than MAX_RANGE - MAX_RANGE%10) you throw it away and generate a new one, until you get one that is not a part of that "remainder" at the end of the range (thus the loop)


int range=10;
int rand=randInt();
while (rand > MAX_INT - MAX_INT%range) //if result part of remainder at end of possible random values, regen
{
    rand=randInt();
}
return rand % range;

Though, better read an article written by someone who loves PRNGs to be sure this is correct. ;)

Indeed, because it is not correct (see how easy it is to get it wrong?). It has a bias towards zero. Test of your code on 8-bit variables, with range 3, over 20 billion trials (there is a bias for every power of two variable size, but it's easier to detect with smaller variables, obviously):


0	6718852175
1	6640484379
2	6640663446

Oops wink.png the fix is to change the comparison to >=, so that the extra element causing the bias is excluded:


/* ... */
while (rand >= MAX_INT - MAX_INT%range)
/* ... */

Which gives (note there are cleaner ways to implement the above that are easier to prove correct):


0	6666558968
1	6666714886
2	6666726146

But this is seriously off-topic for the For Beginners section. I didn't want to reply but I had to, just so someone coming across this post in the future doesn't copy-paste your code and end up with a severe and hard-to-track bug in one of his core functions. Not to mention that trying to robustly generate pseudorandom numbers using a standard library function with an implementation-defined distribution operating on signed integers is just asking for trouble either way happy.png

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Not sure if anyone already suggested it but I find this Tic-Tac-Toe AI very helpful.

1) Don't know the name of it - 'game board' is lines of dots. You take away dots on your turn. You win if you opponent picks up the last dot.

It's called Nim, and it has been solved for over a hundred years.

That would be the one.

I know it's been solved (as all simple games of open knowledge have been), but it gives a bit more 'wiggle room' to work with some AI.

You can slice it, dice it , analyze it six ways from Sunday but there are only nine squares on a Tic-Tac-Toe board. Rotate the matrix and you have exactly three potential opening moves. You have a few more possibilities for moves 2 and 3 (though many will be 'blunders' leading to an instant win by the opponent), but the number is still small. I was substitute teaching a class for a week and we did a brute force solve of tic-tac-toe on the blackboard one day when I was bored (it was a photography class, and I couldn't give them the free rein with equipment they'd normally have - so there was a lot of 'down time'). It's possible we missed a few cases, and we stopped as soon as it was apparent a move was a blunder (no need to keep checking down that path), but I think we got them all.

Something like Nim isn't quite so obviously finite (I know, in reality it is) so I think it would be more fertile ground for developing your own AI algorithm. Especially since not everyone knows the solution - and if you want practice developing an AI, do it for something you don't know the solution for. See how well your AI works, where it falls short, why it loses.

It's the 'pong' thing. Pong games have been written for ever OS there is (probably even for some 'smart toasters'). If you want to start to program games, it's still a good place to start. That's how we learn. When there's a known solution we can check our work against, that's even better (usually), or maybe you make your pong game, then look at others and find three different approaches to solving the same problem - again, a good learning experience.

Tic-Tac-Toe doesn't have that kind of 'room' though: a move is either right or wrong, and clearly so. I'm hesitant to even call it AI: If you can win, play the winning move. If you need to block, block. If there's only one or two squares left, play it (or pick one) - game's a tie at that point. After that you load the matrix, compare it to the matrices in the computers list, iterating over the four rotations, and when you find a match, make the move. Done. The computer will never lose a game.

"The multitudes see death as tragic. If this were true, so then would be birth"

- Pisha, Vampire the Maquerade: Bloodlines

Having just completed a Unity3D book that creates the AI in Tic-Tac-Toe, this is the way the state order should work:

1. Win - Can I get 3 in a row this turn?

2. Block - So I can't win, but can my opponent this turn? If so, block.

3. Create a trap

4. Prevent a trap:

5. Take centre point

6. Take a corner

7. Take a side

8. Random Square

Now this will make the most perfect AI. To resolve this the author suggests making the AI roll the dice, if it fails it goes down the list to the next option. This way it causes the AI to make mistakes. (ie: blocking instead of taking a winning square)

Maybe not the most advanced AI, but I felt great as a beginner with AI.

This topic is closed to new replies.

Advertisement