# Rapidly training a physics-dependent AI in Unity? (unsupervised neural network/genetic algorithm mix)

Let us say that I have some problem within unity that I would like an AI to solve. Let us further say that it is the type where a learning/evolutionary approach is superior to breaking decision hierarchies (like AngryAnt's Behave) or HFSMs in general. How best can I evolve/train the AI in Unity? I am concerned about the overhead of such a process - I can run 10,000 generations to find the global optima of a 2^32 * 2^32 search space in a couple of seconds on my home computer, but doubt Unity could do the same because of how long a fitness evaluation would take.

Application scenario:

In the specific example I am thinking of, I have an artillery gun and an airship. The airship tries to dodge incoming fire from the artillery, and the artillery gun of course tries to shoot down the airship. For purposes of training the AI, the airship remains in some large torus, which is strictly inside the artillery's effective range.

The airship's up/down axis is controlled by a ballast effect (think dirigible). It can apply thrust forward and backwards, and incrementally change the direction of that thrust (within a range). It has drag and (at the moment) always remains straight up/down, only turning on its up/down (y) axis.

The artillery gun fires a shell. This shell explodes under one of two circumstances. The first of these is whenever a raycast along its direction vector within, say, 20 meters returns true. The criteria for the second circumstance the shell will explode is two part. First the shell must pass within, say, 10 meters of the airship. Second, it will explode whenever its distance of the airship is further away this frame then it was last frame.

Whenever the shell explodes it sends, say, 8 pieces of shrapnel in front of it randomly within a 30 degree cone. This shrapnel explodes on contact and moves extremely fast.

The shrapnel and the shell explosion itself both deal damage to the airship, as a linear drop off function. (Id prefer linear+exponential dropoff, but lets keep things 'simple')

I want to do unsupervised learning. I was thinking about running an ANN (artificial neural network) whose weights are genetically evolved.

Assuming that there is no delay in the artillery gun's aiming, the inputs on both of my ANNs seem to need to be position vector of airship relative to artillery, velocity vector of airship (both local space). This gives me 6 inputs. The fitness of course is how much damage is dealt (or not, from the airship's perspective). Possibly the airship could take the position & velocity vectors of the incoming shell as inputs as well. Because of the ballast effect, the delta of y axis movement scaled to the frame rate could be used as an input as well. That is as many as 13 inputs.

I was thinking about alternatively evolving one then the other... but right now the laptop Im doing my Unity development on has 512 RAM (you may laugh, or cry, whichever you feel appropriate) and fear that each fitness call would take a second or two if not implemented cleverly.

Seems more like thinking out loud than a question, but still...

You may want to look into Ken Stanley's NEAT and HyperNEAT, as there is a C# implementation that you could plug into Unity. As to your hardware limitations, this approach is hardly computationally inexpensive, so you may find it at odds with your box plus running it within Unity.