Movement Pattern Learning

From Robowiki
Revision as of 13:59, 19 July 2017 by Dsekercioglu (talk | contribs) (→‎Code)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
Patterned Movement
A movement that keeps using the same patterns.
Pattern Matching or other learning methods beats it easily but it is still effective in nano bots.

Movement Pattern Learning

Movement that learns the correct pattern to use.
In nano bots hitting walls is a big problem but still effective against learning guns.
Learning
We think that enemy has a pattern matching gun so it keeps the movement stats of enemy like current velocity, lateral velocity.
We shouldn't do the same thing again at the same situation if we've been hit before.
Building
Create a "int[n]"
Note: Increasing n slows down learning.
Make a counter that will increase it's value every tick.
We will use this counter for choosing the pattern index.
Index Formula: counterValue / m.
Note: "m" should be >= 6. If lower enemy could hit us because of reaction time. We should move more than half of the bot width.
Calculation: 1 + 2 + 3 + 4 + 5 + 6 = 21 > 18
To learn: when we are hit we take the current pattern index and increase it so the next time we use that pattern we will make a different move :and enemy won't be able to hit our bot.

Code

The code below is bigger than it's normal code size. I used this movement with a nano gun using 17 lateral velocity indices.
Total code size is 246 bytes.
    public static int patternNum = 30; 
    public static int tickPerPattern = 15; //We will use a pattern for 15 ticks
    public static int movePerPattern = 36;
    public static int[] m = new int[patternNum];
    public static int counter = 0;
Initialisation
    public void run() {
        setTurnRadarRightRadians(Double.POSITIVE_INFINITY);
    }
Turning the radar to find a robot.
    public void onScannedRobot(robocode.ScannedRobotEvent e) {
        setTurnRightRadians((e.getBearingRadians()) + Math.PI / 2); //Staying perpendicular to the enemy to have a large escape angle
        int currentPattern = m[counter / tickPerPattern]; 
        int direction = currentPattern % 3 - 1; //Finding the direction to go
        setAhead(direction * movePerPattern);//Move!
        counter++;
        counter %= (tickPerPattern * patternNum);//We don't want an ArrayIndexOutOfBoundsException
        setTurnRadarLeftRadians(getRadarTurnRemaining());//Locking the radar
    }
Movement
    public void onHitByBullet(robocode.HitByBulletEvent e) {
        m[(counter / tickPerPattern)] += 1; //Changing the direction for not being hit the next time
    }
Learning the correct pattern
    public double wallSmoothing(double x, double y, double currentAngle, int dir) {
        if (dir != 0) {
            currentAngle += Math.PI / 10 * dir;//This is optional. I use it to get away from the enemy
            Rectangle2D battleField = new Rectangle(25, 25, 775, 575);
            while (!battleField.contains(x + Math.sin(currentAngle) * 160 * dir, y + Math.cos(currentAngle) * 160 * dir)) {
                currentAngle -= Math.PI / 45 * dir;
            }
        }
        return currentAngle;
    }
Wall Smoothing is optional. If you want to use it create the battlefield as a static variable in initialisation and replace
setTurnRightRadians((e.getBearingRadians()) + Math.PI / 2)
by
setTurnRightRadians(normalRelativeAngle(wallSmoothing(getX(), getY(), getHeadingRadians() + e.getBearingRadians() + :Math.PI / 2, direction) - getHeadingRadians()))

Improvements

Lowering the code size of Wall Smoothing.
There is a problem with Wall Smoothing.
When the bot stops it stops setting it's angle to smooth the wall so when it moves it hits to the wall.
Having more decisions to make.
For example:
Current: -8, 0, 8
After: -8, -4, 0, 4, 8

Problems to Solve

Against HOT if the enemy shoots the robot it starts being hit too much.