Difference between revisions of "Random Targeting"

From Robowiki
Jump to navigation Jump to search
m (Very minor edit.)
 
(12 intermediate revisions by 6 users not shown)
Line 2: Line 2:
  
 
== Example ==
 
== Example ==
 
+
<syntaxhighlight>
<pre>
 
// Add import robocode.util.* for Utils
 
// This code goes in your onScannedRobot() event handler.
 
 
 
 
public void onScannedRobot(ScannedRobotEvent e) {
 
public void onScannedRobot(ScannedRobotEvent e) {
    double randomGuessFactor = (Math.random() - .5) * 2;
+
// ...
    double bulletPower = 3;
+
double targetAngle = getHeadingRadians() + e.getBearingRadians();
    double maxEscapeAngle = Math.asin(8.0/(20 - (3 * bulletPower)));
 
    double firingAngle = randomGuessFactor * maxEscapeAngle;
 
    double absBearingToEnemy = e.getBearingRadians() + getHeadingRadians();
 
 
 
    setTurnGunRightRadians(Utils.normalRelativeAngle(
+
double bulletPower = Math.max(0.1,Math.random() * 3.0);
            absBearingToEnemy + firingAngle - getGunHeadingRadians()));
+
double escapeAngle = Math.asin(8 / Rules.getBulletSpeed(bulletPower));
    fire(bulletPower);
+
double randomAimOffset = -escapeAngle + Math.random() * 2 * escapeAngle;
 +
 
 +
double headOnTargeting = targetAngle - getGunHeadingRadians();
 +
setTurnGunRightRadians(Utils.normalRelativeAngle(headOnTargeting + randomAimOffset));
 +
setFire(bulletPower);
 +
// ...
 
}
 
}
</pre>
+
</syntaxhighlight>
  
 
== A simpler solution ==
 
== A simpler solution ==
  
A simpler method is to assume that the enemy is traveling in a circle around you, which is often true among NanoBots and [[:Category:1-vs-1 Bots|1-vs-1 bots]]. If the enemy is traveling in a circle around you, the maximum distance it can cover before a bullet reaches it is <code>enemy velocity / bullet velocity</code> (in radians). For example, a power 3.0 bullet fired at an enemy going at full speed should be fired at a [[Bearing Offset|bearing offset]] between -8/11 and +8/11.
+
A simpler method is to assume that the enemy is traveling in a circle around you (see [[Musashi Trick]]), which is often true among NanoBots and [[:Category:1-vs-1 Bots|1-vs-1 bots]]. If the enemy is traveling in a circle around you, the maximum distance it can cover before a bullet reaches it is <code>enemy velocity / bullet velocity</code> (in radians). For example, a power 3.0 bullet fired at an enemy going at full speed should be fired at a [[Bearing Offset|bearing offset]] between -8/11 and +8/11.
  
 
== Selecting firepower ==
 
== Selecting firepower ==
  
The advantage of a random gun is that it should have a roughly equal hit rate on any type of movement. This makes ideal firepower selection pretty easy to pre-calculate. In the following equations x is the choice of firepower. It is assumed that damage output per tick is the quantity you're interested in maximizing, it may not be.
+
The advantage of a random gun is that it should have a roughly equal hit rate against all types of [[Movement]]. This makes ideal firepower selection pretty easy to pre-calculate. In the following equations x is the choice of firepower. It is assumed that damage output per tick is the quantity you're interested in maximizing, it may not be.
  
 
Bullet damage, assuming firepower > 1:
 
Bullet damage, assuming firepower > 1:
<pre>D=4*x+2*(x-1)</pre>
+
 
 +
<math>D=4x+2(x-1)</math>
 +
 
  
 
The smallest size of a robot, in radians:
 
The smallest size of a robot, in radians:
<pre>S=36/distance</pre>
+
 
 +
<math>s=\frac{36}{d}</math>, where <math>d</math> is the distance to the other bot.
 +
 
  
 
The total escape area, in radians:
 
The total escape area, in radians:
<pre>A=2*asin(8/(20-3*x))</pre>
 
  
Probability of a hit, assuming uniform spread of bullets over A:
+
<math>\alpha=2 \cdot asin(\frac{8}{20-3x})</math>
<pre>P=min(1,S/A)</pre>
+
 
 +
 
 +
Probability of a hit, assuming uniform spread of bullets over <math>\alpha</math>:
 +
 
 +
<math>P=min(1,\frac{s}{\alpha})</math>
 +
 
  
 
The expected damage from a shot:
 
The expected damage from a shot:
<pre>E=D*P</pre>
+
 
 +
<math>E=DP</math>
 +
 
  
 
The heat created by a shot:
 
The heat created by a shot:
<pre>H=1+x/5</pre>
+
 
 +
<math>H=1+\frac{x}{5}</math>
 +
 
  
 
The firing frequency:
 
The firing frequency:
<pre>F=0.1/H</pre>
 
  
Expected damage per tick of combat:
+
<math>F=\frac{1}{ceil(10H)}</math>
<pre>DPS=E*F</pre>
+
 
 +
 
 +
Expected damage <math>E_{x,d}</math> per tick of combat:
 +
 
 +
<math>E_{x,d}=E \cdot F = D \cdot P \cdot \frac{1}{ceil(10H)} =\frac{(6x-2) \cdot min(1,\frac{16}{d \cdot asin(\frac{8}{20-3x})})}{10+ceil(2x))}</math>
  
Back substitution to get the expected damage per tick in terms of distance and x and optimization of DPS are left as an exercise for the coder.
+
Optimization of <math>E_{x,d}</math> is left as an exercise for the coder.
  
 
== See also ==
 
== See also ==
Line 58: Line 71:
 
* [[Maximum Escape Angle]]
 
* [[Maximum Escape Angle]]
  
 +
{{Targeting Navbox}}
 
[[Category:Simple Targeting Strategies]]
 
[[Category:Simple Targeting Strategies]]

Latest revision as of 16:55, 6 February 2013

A method of targeting that simply chooses a random angle among the angles that could possibly hit the opponent. Some successful NanoBots use this firing method. Its implementation is very small and for unpredictable movements, it will give a consistent hit percentage.

Example

public void onScannedRobot(ScannedRobotEvent e) {
	// ...
	double targetAngle = getHeadingRadians() + e.getBearingRadians();
	
	double bulletPower = Math.max(0.1,Math.random() * 3.0);
	double escapeAngle = Math.asin(8 / Rules.getBulletSpeed(bulletPower));
	double randomAimOffset = -escapeAngle + Math.random() * 2 * escapeAngle;

	double headOnTargeting = targetAngle - getGunHeadingRadians();
	setTurnGunRightRadians(Utils.normalRelativeAngle(headOnTargeting + randomAimOffset));
	setFire(bulletPower);
	// ...
}

A simpler solution

A simpler method is to assume that the enemy is traveling in a circle around you (see Musashi Trick), which is often true among NanoBots and 1-vs-1 bots. If the enemy is traveling in a circle around you, the maximum distance it can cover before a bullet reaches it is enemy velocity / bullet velocity (in radians). For example, a power 3.0 bullet fired at an enemy going at full speed should be fired at a bearing offset between -8/11 and +8/11.

Selecting firepower

The advantage of a random gun is that it should have a roughly equal hit rate against all types of Movement. This makes ideal firepower selection pretty easy to pre-calculate. In the following equations x is the choice of firepower. It is assumed that damage output per tick is the quantity you're interested in maximizing, it may not be.

Bullet damage, assuming firepower > 1:

<math>D=4x+2(x-1)</math>


The smallest size of a robot, in radians:

<math>s=\frac{36}{d}</math>, where <math>d</math> is the distance to the other bot.


The total escape area, in radians:

<math>\alpha=2 \cdot asin(\frac{8}{20-3x})</math>


Probability of a hit, assuming uniform spread of bullets over <math>\alpha</math>:

<math>P=min(1,\frac{s}{\alpha})</math>


The expected damage from a shot:

<math>E=DP</math>


The heat created by a shot:

<math>H=1+\frac{x}{5}</math>


The firing frequency:

<math>F=\frac{1}{ceil(10H)}</math>


Expected damage <math>E_{x,d}</math> per tick of combat:

<math>E_{x,d}=E \cdot F = D \cdot P \cdot \frac{1}{ceil(10H)} =\frac{(6x-2) \cdot min(1,\frac{16}{d \cdot asin(\frac{8}{20-3x})})}{10+ceil(2x))}</math>

Optimization of <math>E_{x,d}</math> is left as an exercise for the coder.

See also