Presentation is loading. Please wait.

Presentation is loading. Please wait.

Winning Strategy in Programming Game Robocode

Similar presentations


Presentation on theme: "Winning Strategy in Programming Game Robocode"— Presentation transcript:

1 Winning Strategy in Programming Game Robocode
CSC 303 Casmon Gordon Advisor- Dr Xiang Fu

2 Introduction Robode: Programming Game. * Robots Controls in Java.
        *  Public API for Programmers (Players). Many robots included with game. Walls robot was the most successful. Robocode is a programming game where the robots in the game are controlled using Java programs. There are a variety of robots that are included with the game. It was observed that the Walls robot was the most successful when the battlefield is comprised of all the included robots.

3 Walls Robot The Walls robot movement predictable.
The Walls robot will always move in predictable ways. When the battle begins it will always move to the edge of the battlefield and then travel along its edges in a clockwise direction. Due to the fact that Walls travels in a straight line, its future path can easily be calculated. Trigonometry can be used to calculate the angle at which to fire a bullet that will hit the Walls robot as it travels in its path.

4 The Problem The Walls robot moves very fast.
The default shooting algorithm will target and fire at Walls current location. Due to the Walls speed, Walls would have moved by the time the bullet reaches.

5 Objective Design WallsKiller robot. Defeats Walls robot effectively.
Goals? Win every battle! Hit ratio above 90%. Energy level above 80. The objective was to design a robot named WallsKiller, which is specifically built to defeat the Walls robot effectively. Its goals is to win every battle, achieving a hit ratio above 90% and having an average energy level of above 80.

6 Targeting Strategy Where will Walls be?.
Wallskiller needs smart shooting algorithm. Wallskiller needs to determine where the Walls robot will be and fire at that location. The robot utilizes a custom shooting algorithm that uses trigonometry and solves quadratic equations in order to accurately calculate the trajectory to fire bullets in order to hit the fast moving Walls robot.

7 Bullet Avoidance The WallsKiller robot :
Avoids getting hit by the Walls robot’s bullets. Detects that Walls has opened fire. Moves away! The WallsKiller robot was designed to avoid getting hit by the Walls robot’s bullets. The algorithm detects when there is a change in the energy level of the Walls robot which indicates that the Walls robot has fired a bullet. The WallsKiller will then move to a position that is perpendicular to the Walls robot’s heading, reducing the likelihood that the bullet fired will hit the WallsKiller robot.

8 Robot Anotomy The tank or robot consists primarily of a gun, a radar and the vehicle itself. The gun has the ability to rotate in order to shoot at targets. The radar also rotates in order to detect enemy tanks on the battlefield.

9 Cosine Rule Cosine Rule: c2 = a2 + b2 – 2ab*cos(A)
In trigonometry, the cosine rule governs the size of edges and the angles in a triangle. The square of one edge of the triangle, is the sum of the square of the other two edges and minus their product with the cosine of the angle.

10 Quadratic Equations Quadratic formula:
For an equation of the form At2 + Bt + C, the variable t can be determined by using the following formula shown.

11 Relationship Between Tanks Positions
The corners of the triangle represent the position of the WallsKiller tank, the current position of the Walls tank and the future position of the Walls tank at which the bullet will hit it. As shown, v1 = the velocity of the bullet; v2 = the velocity of the enemy tank; and d = the distance between both tanks. The future location of Walls can be determined by calculating v2*t. Also the position where the bullet will hit the Walls robot can be found by calculating v1*t.

12 Relationship Between Angles Alpha and Beta
The relationship between angles alpha and beta are illustrated. We are going to compute the angle beta, so later we can apply the rule of cosine. As shown in this slide, there are three angles: Angle 1, Angle 2, and Angle 3 closely related to beta. They can be obtained using the formulas: Angle 1 = heading of enemy tank = ScannedRobotEvent.getHeading() Angle 2 = heading of the WallsKiller = AdvancedRobot.getHeading() Angle 3 = the bearing of the enemy tank = ScannedRobotEvent.getBearing()

13 Finding Beta Sum of angles equals 180 degrees. Solve for beta:
Hence (180-Angle 1)+(Angle2 + Angle 3)+180-beta=180 Solve for beta: beta = 180-Angle 1+ Angle 2 + Angle 3 Therefore beta = ScannedRobotEvent.getHeading() + AdvancedRobot.getHeading() + ScannedRobotEvent.getBearing() The three angles that make up the triangle must add up to 180 degrees. Therefore the following applies: (180-Angle 1)+(Angle2 + Angle 3)+180-beta=180 Solving for beta we get the following: beta = 180-Angle 1+ Angle 2 + Angle 3 Therefore beta = ScannedRobotEvent.getHeading() + AdvancedRobot.getHeading() + ScannedRobotEvent.getBearing()

14 Finding the Time Substituting and solving we get:
(v1*t)2 = d2 + (v2*t)2 – 2(d*v2*t)*cos(beta) d2 + (v2*t)2 – 2(d*v2)*cos(beta) *t - (v1*t)2 = 0 (v22 - v12)t2 – 2*d*v2*cos(beta) *t + d2 = 0 This equation is quadratic form. Apply cosine rule! Using the triangles described previously and substituting the values such that a=d, b=v2*t and c=v1*t we get: (v1*t)2 = d2 + (v2*t)2 – 2(d*v2*t)*cos(beta) d2 + (v2*t)2 – 2(d*v2)*cos(beta) *t - (v1*t)2 = 0 (v22 - v12)t2 – 2*d*v2*cos(beta) *t + d2 = 0  This equation is in the quadratic form: (At2 + Bt + C=0) and can be used to solve for t  Let A = v22 – v12, B= -2*d*v2*cos(beta), and C= d2

15 Finding Alpha Determine the value for alpha. Use cosine rule.
Substitute the three edges of the triangle : (v2*t)2 = d2 + (v1*t)2 – 2(d*v1*t)cos(alpha) Now that t can be determined, we can use the cosine rule again to determine the value for alpha. Substituting the three edges of the triangle such that a= d, b=v1*t, and c=v2*t we get the following: (v2*t)2 = d2 + (v1*t)2 – 2(d*v1*t)cos(alpha)

16 Finding Alpha Simplifying and solving for alpha we get: Therefore:

17 Fire Angle Calculation
Direct fire angle: fireAngle = (AdvancedRobot.getHeading() + ScannedRobotEvent.getBearing()) - AdvancedRobot.getGunHeading(); If we were to shoot directly at the Walls tank, the fireAngle equation would be fireAngle = (AdvancedRobot.getHeading() + ScannedRobotEvent.getBearing()) - AdvancedRobot.getGunHeading();

18 Fire Angle Calculation
Direct fire will miss! Offset angle alpha added to fire angle: fireAngle = (AdvancedRobot.getHeading() + ScannedRobotEvent.getBearing() + alpha) - AdvancedRobot.getGunHeading(); However because the tank would have moved away by the time the bullet would reach it, the calculated offset angle alpha is added to the firing angle resulting in the following equation represented by: fireAngle = (AdvancedRobot.getHeading() + ScannedRobotEvent.getBearing() + alpha) - AdvancedRobot.getGunHeading();

19 Shooting Performace for Ranges
As shown, the hit rate of the WallsKiller robot varies for different ranges. Due to precision errors associated with calculation the WallsKiller robot may sometimes miss the Walls robot at longer ranges. In order to improve the performance of the WallsKiller robot, a statistical sample of the hit rates for various ranges is performed to determine the optimal range at which to shoot the Walls robot.

20 Machine Learning The WallsKiller robot: Collects battle data.
Perform runtime data analysis. Determines optimal range! The WallsKiller robot has the ability to perform runtime data analysis of past battles to determine the optimal range at which to shoot the Walls robot. The WallsKiller robot collects and stores hit rate information for given ranges. After a threshold has been reached, which is a pre-determined value for the maximum number of records, the WallsKiller robot will perform analysis of the data it has collected to determine the range at which it was most successful. When this range is determined, WallsKiller will use this range to shoot the Walls robot.

21 Machine Learning Details
Manipulates Data via text file. File size limitations. Data analysis The WallsKiller robot stores and manipulates data via a text file. The data in the text file is a set of comma delimited key value pairs. Due to file size restrictions (~200K), the WallsKiller robot is not allowed to store more than approximately 2000 lines of data. This means that a larger statistical sample could not be taken for each gun range of the WallsKiller robot. When this file’s maximum size is reached, WallsKiller robot will then analyze the data to determine the best range at which the hit rate is the highest. The samples for each range were taken for 50 turns of Robocode after which the data was collected and stored.

22 Frequency of Data for Ranges
The graph above illustrates the occurrences of data for the respective ranges. This shows that even though the best hit rate of 100% was achieved at the 100 range, it can be considered a statistical outlier and can be ignored since there was only one hit at that range. This is also the case for the 1000 range where there was only two occurrences.

23 Check If Bullet Will Miss
In order to improve accuracy, WallsKiller robot first calculates where the bullet will hit the Walls robot. If this position is outside the battlefield then the WallsKiller will not fire until the predicted hit location is within the battlefield. The fire control system has a private function calcEnemyProjectedLocation( ) that calculates the projected location of the Walls robot. It does this by using the distance (enemyVelocity * t) to determine the Walls robot’s absolute bearing. It then uses the absolute bearing along with other battle data and sine and cosine functions to determine the Walls robot’s projected X and Y coordinates. These coordinates are then checked to see if they are within the battlefield. If they are not within the battlefield then the WallsKiller will not open fire.

24 Conclusion The WallsKiller robot is very effective!
Sometimes misses at longer ranges. Learns and adapts. Achieved its goals! The WallsKiller robot is very effective at killing the Walls robot. However, due to precision errors associated with calculation the WallsKiller robot may sometimes miss the Walls robot at longer ranges. To resolve this problem, the WallsKiller robot will at runtime collect, store and analyze battle data from past battles to determine the optimal range at which to shoot the Walls robot. WallsKiller achieved all its goals of winning every battle, achieving a hit ratio above 90% and having an average energy level of above 80.


Download ppt "Winning Strategy in Programming Game Robocode"

Similar presentations


Ads by Google