# How to train a physics dependent AI in Unity?

I want to create a pair of artificial neural networks (ANN) to control two objects in my scene, a dirigible-styled airship and an artillery cannon. The cannon shoots at the airship, which evades. This is a physics based problem, where I must wait >1 second per shot to be certain of the outcome, causing training to be a prohibitively lengthy process.

I am looking for a way to train physics based ANNs in realistic timescales.

— All that follows is the specifics of my scenario.

Unsupervised learning. Genetic algorithm used to evolve weights. Prefer to be able to train both AIs against each other.

Artillery:

7 inputs
airship relative position vector, airship velocity vector, airship y-axis acceleration (calculated by dv/dt)

2 outputs
x and y axis rotation.

Artillery Shell:

Airbursts when raycast along velocity vector hits airship within 20 meters.
Also airbursts if within 15 meters of airship and is further away than last frame.

Upon airburst it releases a set amount of fast-moving shrapnel in a 30 degree forward cone. For training purposes the scattering of shrapnel will be regular and static, though it will be fully random in implementation.

Shrapnel deals damage if it hits the airship, and the initial airburst of the shell deals damage as a linear function of proximity.

Airship:

13 inputs (7 without keeping track of shell): relative position vector, velocity vector, y axis acceleration (dv/dt again), relative position vector of nearest shell, velocity vector of nearest shell.

3 outputs: ballast control, thrust control, thrust direction control.

Moves along the Y axis with a ballast like effect. Moves horizontally by applying a force at aft of the ship. This force can be applied at any angle. X and Z rotation kept at 0 through scripts. Drag caps the velocity.

The airship must obviously stay within the effective range of the cannon (elsewise the airship has a clear winning strategy). How to achieve this without unrealistically restricting the airship Im not sure.

PS: I asked something similar in UnityAnswers, but it seemed a bit outside the scope of the other questions, and more likely to be addressed here.

You might be able to increase the speed of the physics simulation simply by increasing the value of Time.timeScale, although ultimately you will be limited by available processor speed, of course.

As for evolving the ANN weights, a genetic algorithm is not necessarily the best metaheuristic to use. Although GAs do tend to converge on a good solution eventually, they also make a lot of detrimental random changes that waste time. This is a particular problem when the evaluation takes a long time, as it does in this case. Furthermore, it is often difficult to map a particular problem space onto the GA’s “chromosome”. A tabu search or iterated local search might be more efficient for a project like this (and you can even combine the two techniques). I imagine the local search phase would involve modifying each weight in turn to see which has the most beneficial effect on the overall result. Then, when no further progress is possible, apply the tabu criterion (or for ILS you would “perturb” the result) and start again.