How do I create a super AI in Godot?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ossamaGamer10xyzt

How to create an AI that learns by itself by trying and iterating in a game top down

:bust_in_silhouette: Reply From: godot_dev_

If you want the AI to learn by exploring the world, reinforced learning (e.g., Q-learning) can accomplish this. The idea is initially the agent (the AI acting upon the game) doesn’t know anything about the environment, so it tries out random inputs and examines the effects on the environment, and when it accomplishes something useful, you reward it (increase the probability that it does that action in the current state of the game) and if it does something bad you punish it

:bust_in_silhouette: Reply From: aXu_AP

Another option besides reinforced learning, is genetic algorithms. These are easier to create, so I might suggest going this route.

No matter which route you decide to go, first you need to make an ai agent capable of perceiving the world (input) and make actions (output). The latter is rather easy to do, but having good inputs is an art form in itself. For example, your ai might get info of few different objects in front of itself. For example, distance and direction to the closest enemy, pickup and walls.

Next step is connecting the inputs to the outputs. For reinforcement learning, neural network is the way to go. Neural networks use mathematical model to calculate output values according to input values. This is a big topic so I won’t go deeper into that, there’s dedicated tutorials if you’re interested. Genetic algorithms are more freeform in how they handle this, but one way is to have a table of certain input combinations and corresponding actions (for example, if wall is left, enemy and pickup are absent: turn right).

With genetic algorithms you make a lot of these agents with randomized parameters, and after a period of time check how successful they were (for example more health remaining, more collected goods etc.). Then delete badly performed agents and create new ones by taking good agents as a template and change few parameters. Repeat a lot of times, and you should end up with somewhat competent ai.

In the end this isn’t the easiest of topics, and I suggest to start small. Simple game with not many rules will be easier to conquer.

Edit: Also I suggest building a hand crafted ai. The first step of figuring out what inputs this ai gets and how it controls the character is the same, and then you write in code how it should react. This way you can set yourself in place of ai, and figure if the inputs are good enough to decide the next move. It doesn’t need to be super intelligent, but you get experience which will be useful for making self learning bot. Also your ai can act as a way to meter the success of other ais.