How best to craft AI for a simple game

Asked

Viewed 552 times

2

Based on this little game, I started the first generation completely Andom, storing in an array all sides that the bar moved (right - 1, left - 0) and finally (at the last position of the array) how many points you scored in each match.

My idea of how to do

Based on this the second generation would like to discard the games with fewer points, and keep only one (or more if tie) game. And from the moves of this game with higher score start to randomize again to the second generation.

The array is saved this way:

inserir a descrição da imagem aqui

I wish the AI didn’t know the object of the game, I just want it to be based on whether it’s right or wrong by the amount of points you get.

Can someone shed some light on how I can make it learn the best moves or how to best execute my idea ??

inserir a descrição da imagem aqui

  • You’re talking about genetic algorithms?

  • @Victorstafusa Yes, I want the algorithm to learn for itself which way it wants to play. I understand more or less how genetic algorithms work, I just don’t quite know how to fit it into the code.

  • @Lucascarezia unsupervised learning does not need to be with genetic algorithm. It can be a utility-oriented agent. Has neural networks that learn without being supervised

  • If you want to study more on the subject, they managed to make an AI that plays Mario. I believe they share how they did it. If I’m not mistaken, they modeled a utility agent

  • Exactly, I don’t want you to be supervised. However my doubt is how to do this with the game and information I have at the moment, or maybe what more information I need to be able to do.

2 answers

5

If this is a game similar to pong, block-Breaker, Arkanoid or something, artificial intelligence is much simpler than that and genetic algorithms won’t solve it for you.

Genetic algorithms serve to find a combination of data that presents the best solution for a given fixed problem, and it is something computationally expensive to obtain. In your case, you have an agility problem, where decisions have to be made quickly in fractions of seconds. Also, each time you play is different from the previous time, because the ball goes to a different side, hits in a different place and the solution of the previous attempt will serve no purpose.

In short, this is a XY problem.

The artificial intelligence algorithm is simply trying to follow the ball in the best way possible. It would be something like this:

function ia() {
    var raquete = ...;
    var bolinha = ...;
    var a = raquete.x + raquete.width / 2;
    var b = bolinha.x + bolinha.width / 2;
    if (a < b) {
        raquete.moverParaDireita();
    } else if (a > b) {
        raquete.moverParaEsquerda();
    }
    setTimeout(ia, 100);
}
  • To be considered intelligent, you need to perform. It doesn’t involve learning, sometimes learning helps you optimize that performance. Well remembered

  • I don’t know if your answer fits me, I edited the question why I ended up forgetting to put that I want the AI to be unaware of its goal, I don’t want him to know that he has to follow the ball, he has to figure it out by himself based on the points he managed to make.

  • @Victor Stafusa Could I classify your script as artificial intelligence? An intelligence that understood the concept of the game and evolved? I’m putting together a script using the concepts of augmented imagination, to try to predict roughly where the ball will fall and position the racket. That’s not the way we try to play?

  • 1

    @Guilhermeia Yes, this script is an artificial intelligence, although it is very simple and unable to perform any kind of learning or imagination. It is an artificial intelligence for having the purpose of controlling one of the agents of the game and make it interact with what is its environment, even if the procedure performed to do this is an extremely mechanical and simplistic thing.

5


All the examples cited by @Victorstafusa in their response are complex games. Let’s look at the block Breaker, since he was the example of the question?

To define a smart agent, you must determine PEAS:

  • P: performance
  • And: ambience (Environment)
  • To: actuators
  • S: sensors

An equivalent model is PAGE, but he has ontological limitations that are best met by PEAS:

  • P: perception
  • To: action
  • G: goal (Goal)
  • And: ambience (Environment)

Have you noticed that in both you need to determine the environment? What is the environment of block Breaker?

I will leave the focus of multiple environments aside since there is only action in one environment

To begin describing the environment for the agent, we need to describe at least the following properties:

  • dynamism
    the environment is dynamic or static? That is, it undergoes changes in its agent-independent internal state?
  • determinism
    the environment is deterministic or stochastic? Is there any possibility of random event that changes its internal state? Note: chaos (as described in chaos theory, the butterfly effect) is not random, just difficult to predict
  • number of agents
    we’re dealing with how many smart agents in this environment?
  • observability
    the environment is completely observable? Or only partial knowledge?
  • continuity of time
    is an agent with episodic actions? Or an action has consequences in the internal state of the system that needs to be taken into account in a future action?

Responding:

  • is an environment dynamic, it evolves continuously in time; this is due to the mechanics of the game, since it is an action game, which requires real-time intervention
  • as far as I’m concerned, it’s an environment deterministic: given a configuration, the next step is already set; but it can be stochastic if the physics of the deployed collision allows two equal collisions to result in distinct results (I’ve seen this in Voodoo’s Idle Balls game for iPhone); it could also be stochastic if blocks appeared on the map without agent intervention
  • is a typically system mono-agent, where there is only one agent in the environment
  • he is fully observable by the agent, there is no hidden information; maybe you have information that is presented but unknown by the agent (for example, gray blocks require three collisions to break, golden blocks do not break; blue block is worth more points than red)but that doesn’t mean the information isn’t there
  • is not episodic, an action interferes with the internal state of the environment

Well, we start with these characteristics of the environment. Phew? Well, not yet...

The environment continues to be governed by a set of "laws". These "laws", when we are talking about games, we call them "mechanics". These mechanics imply how the universe evolves by itself (it’s a dynamic environment, it’s not static) and also how you can interact with the game.

We have four main mechanics in this game:

  • uniform rectilinear motion
    given that the ball follows its course
  • collisions
    when the ball comes in contact with something (wall, block, racket), there is a collision that will change the state of the ball and the collide
  • ball leak in case the ball goes through the bottom, it leaked
  • racket movement
    the player can define how to act with the racket

On top of the mechanics we have the rules. For example, ball leak implies that lost a life (or lost points). Gray blocks can withstand two collisions and only leave on the third. The paddle moves continuously limited to a maximum speed, or else it makes magical jumps between positions. The ball when colliding with the moving racket gains/loses speed. Breaking blocks generates points. Anyway, there are other possible rules that don’t come to the point.

Hmmm, did you notice that while we were describing the rules of the game, we touched on another aspect of the environment? They are the "elements" of the environment, the "objects" contained in it. These objects are divided into the following classes::

  • ball
  • racket
  • block
  • wall
  • bottomless pit

These classes are important because the agent will perceive that. The agent’s perception will be relative to the object and its intrinsic properties, which depend on each class.

  • ball
    that object contains center position ((x,y)), speed (direction/direction/magnitude) and radius
  • racket
    that object contains center position ((x,y)) and also dimensions to calculate where its four vertices are; it may also contain maximum speed if it is specified that it does not jump positions
  • block
    that object contains center position ((x,y)) and, also, dimensions to calculate where its four vertices are, also contains color, with possible implication in the rules
  • wall
    this object is located on the upper and lateral edges of the environment, having no other relevant information
  • bottomless pit
    this object is located at the bottom edge of the game, having no other relevant information

And what are the possible performances agent? Well, just move the racket. But this can happen in two ways:

  • goes to the specified side at speed X (maybe X is informable, maybe is the maximum speed)
  • go to this specific position (if it has full speed, it will move at full speed, otherwise it will be teleportation)

And how do I calculate the performance agent? Well, we can start with the score of the game.

In that specific case the block Breaker, the actions do not influence the performance directly, because no matter how much you move the racket, it will only mean something if you find the ball. So the agent needs to calculate his actions knowing that the reward is not immediate. This is particularly challenging if the agent knows the minimum of the environment ontology, as you are requesting. A learning function focused on a half-Pavlovian model of action/reward or action/punishment does not work for this type of environment (especially if the ball speed is low).

  • I really liked the explanation, but at the end you said it wouldn’t work, but what would work in this case ??

  • 1

    @Lucascarezia I will try to elaborate more, but for these types of games the best is not to have something that learns, but that simply has high performance, as Victorstafusa put in his answer. And I have nothing that, without a priori hint, can better elaborate a learning

Browser other questions tagged

You are not signed in. Login or sign up in order to post.