Day 11: Robocode Strategies

Download Report

Transcript Day 11: Robocode Strategies

This Week
• Cover as much Robocode strategy as we can
• Monday: Robocode
– NN Homework Review
– Hand in Picbreeder hw if you haven’t
– Grades: Can’t give you an A if you haven’t at
least turned in the assignments
– Email me if you’d like to double check my records
against yours (which assignments you turned in
and how many points you received for each)
This Week Continued
• Tuesday: Quiz
– Explain how a neural network can represent a
solution to a particular problem. Explain how
you would input data and interpret the output.
• Wednesday: Robocode
• Thursday: Competition and prizes – My
last day (no late robots)
• Friday: AI Jeopardy with Dan DeBlasio
Robocode Homework
• http://www.cs.ucf.edu/~ahoover/bhcsi2010
/
Robocode and Robocode
Strategies
(taken from citations at the end)
Excellent Sources of Information
• My First Robot
– http://testwiki.roborumble.org/w/index.php?titl
e=Robocode/My_First_Robot
• FAQs
– http://testwiki.roborumble.org/w/index.php?titl
e=Robocode/FAQ
From Last Time
• Robocode tanks are java programs
– We saw this when we added a line of code,
compiled, and ran the code
• Robocode tanks are event driven
– There is a main loop
– The main loop can be interrupted by handling
different events
From Last Time
• The tank default behavior is in a loop
defined in run()
• Different events interrupt this loop
execution
Example
• public class MyRobot extends Robot
• {
•
public void run() {
•
while(true) {
•
ahead(100);
•
turnGunRight(360);
•
back(100);
•
turnGunRight(360);
•
}
•
}
• This is an example main loop. This robot doesn’t have any event
handlers so it executes these exact actions until it either wins (very
unlikely) or dies (very likely).
Example
•
•
•
•
•
•
•
•
•
public class MyRobot extends Robot
{
public void run() {
while(true) {
ahead(100);
turnGunRight(360);
back(100);
turnGunRight(360);
}
public void onHitByBullet(HitByBulletEvent e) {
turnLeft(90 - e.getBearing());
}
}
•
This example has an event handler. It will continue executing this loop until
we get hit by a bullet. When we get hit, we turn toward the enemy.
Inheritance in Java
• Ideas: Inhertiance
– common components are inherited, rather
than duplicated
– allows similar though distinct objects to exhibit
their common capabilities without requiring
the code to be in two places
Inheritance in Java
• Class GoldenRetriever extends Dog
– GoldenRetriever has any properties that Dog
has:
• Canis lupus familiaris
• Four legs
– GoldenRetriever has extra properties not
common to all dogs
• Gold hair
• Playful demeanor
• Tall
Inheritance in Java
• Class Dog extends Mammal
– Mammals have:
• Hair
• Legs
• Lungs
– Dogs have:
• Canis lupus familiaris
• Four legs
Inheritance in Java
• To give our robots all of the methods defined in
the robot API, we have been extending the
Robot class. This is so we don’t have to define
all the methods that are already defined for us.
• We could also extend the AdvancedRobot class
• Note: Beginners
– I don’t expect you to fully implement this concept
– I do expect you to understand what happens when we
extend a class (inherit from a class)
Robocode Basics
• Tank properties
– Energy
– Time
– Fire
– Radar
• Game play
– How to determine a winner (not necessarily
last tank standing)
– How the Robocode engine updates
Tank Basics: Energy
• Tanks start with a certain amount of health (energy)
– 100
• Tanks lose health by:
–
–
–
–
–
getting shot
bumping into things (the walls, other robots)
shooting bullets
fighting for too long
(Note: tanks that hit each other lose equal amounts of health)
• Tanks gain health by shooting other tanks
• Tanks that run out of health by shooting too much are
disabled (If the tanks bullet hits a target it can gain
energy back)
Game Play (point distribution)
• Robots on the field when another robot dies – 50 points
• Last robot alive given extra points – 10 points per dead
robot
• Every point of fire damage inflicted on target robot – 1
point
• Every opponent ram – 2 points per damage inflicted by
ramming
• Killing a target robot by firing at it– 20% of total damage
inflicted
• Death by ramming – 30% of total damage inflicted
Tank Basics: Time
• Rotating the gun, radar, and vehicle takes time
– Vehicle takes the longest amount of time
– Radar takes the least amount of time
• Bullets take time to hit things (need to shoot
where you expect the target to be when the
bullet should arrive)
• Gun has a short cooldown period in between
shots
• Radar must be pointed at a target to see it
– Detects velocity, bearing, heading, and energy
remaining.
Tank Basics: Fire
• 3 levels of firing
– fire(1) – costs the least health, does the least
damage
– fire(2) -costs intermediate health, does
intermediate damage
– fire(3) – costs the most health, does the most
damage
Tank Basics: Radar
• Infinite range
• One degree scan width
• Returns enemy position, orientation,
bearing, gun angle, and energy.
• Tank knows its own position, oreintation,
bearing, gun angle, and energy
Game Play (how Robocode
updates)
•
•
•
•
All robots execute their code until taking action
Time is updated
All bullets move and check for collisions
All robots move (heading, acceleration, velocity,
distance, in that order)
• All robots perform scans (and collect team
messages)
• The battlefield draws
• http://www.dinbedstemedarbejder.dk/Dat3.pdf
Robocode Strategies
1. Seeing other robots – how the robot controls
its radar
2. Choosing an enemy
3. Moving – where the robot should go
4. Targeting and Shooting - when, how powerful
and in which direction to shoot.
•
http://www.dinbedstemedarbejder.dk/Dat3.pdf
Radar Strategies – Seeing other
robots
• Remember that scanning takes time
• Simple – scan 360 degrees (while alive),
looking for target robots, remember where
these robots are
– Problem: Robots aren’t typically evenly
distributed
Example: Scan 360
• public class MyFirstRobot extends Robot {
public void run()
{
while (true) {
ahead(100);
turnGunRight(360);
}
}
public void onScannedRobot(ScannedRobotEvent
e) { fire(1); } }
Radar Strategies – Seeing other
robots
• Locked scanning strategy – radar finds a target
and sticks with it
– Implemented by moving the radar back and
forth a little to keep track of the robot, it could
scan the direction based on the target robot’s
heading
– Problem: Not good for bigger battles since you could
pass by easier to kill robots
• Radar can be used to guess when an
opponent’s bullet has fired
– Any ideas on how we could do this?
– Look at what we can gather from the radar
API
Example: Target Lock
//Follows the robot around the battlefield.
public void run() { // ...
turnRadarRightRadians(Double.POSITIVE_INFINITY);
do { scan(); } while (true);
}
public void onScannedRobot(ScannedRobotEvent e) {
double absoluteBearing = getHeadingRadians() +
e.getBearingRadians();
double radarTurn = absoluteBearing getRadarHeadingRadians();
setTurnRadarRightRadians(Utils.normalRelativeAngle(radarT
urn)); // ... }
Radar Strategy – Methods
(AdvancedRobots)
• scan() - Scans the arena using the radar
at its current setting. Robocode calls this
for each robot at each turn.
• setTurnRadarLeft and turnRadarLeft.
• setTurnRadarRight and turnRadarRight
• setAdjustRadarForRobotTurn - Enables
or disables the radar locking to the vehicle
• setAdjustRadarForGunTurn - Enables or
disables the radar locking to the gun
Choosing an Enemy
• Weakest Enemy – Pick the guy with the
lowest health
– Problem: This target could be far away.
Another robot could dip below target robot’s
health in the mean time!
• Closest Enemy – Pick the guy closest to
you
– Problem: He could be the winner. Dying
earlier means you receive less points overall
Choosing an Enemy
• No explicit methods provided by
Robocode
• Neural Network Targeting Today if we
have time
Movement Strategies (MS)
• None – easy to hit
• Straight line
– Problem: Easy for enemies to predict
• Curve
– Problem: Also easy for enemies to predict
• Oscillating
• Random
– Problem: Run a larger chance of hitting walls
Movement Strategies (MS)
• Anti-gravity – keeps the robot from
dangerous areas and attracts it toward
advantageous areas
– Many different implementations
• These and more discussed here –
– http://www.dinbedstemedarbejder.dk/Dat3.pdf
Shooting Strategies
•
•
•
•
•
Stationary
Linear
Circular
Oscillatory
Movement pattern matching
Shooting Strategies
• Stationary – Assume the robot is not going to
move and fire directly at the target robot’s
current location
– Problem: If the target moves, we miss
Target Robot
Our Robot
Example: Stationary Shooting
• onScanRobot()
{
fire(1);
}
//This method just fires where we saw the robot
without taking into account where it may be
going
Shooting Strategies
• Linear – Assume that the robot is moving in a straight line, fire at
where the robot should be in the time it takes to fire (Assumes
constant velocity)
• All robot motion can be approximated for a short period of time this
way
– Problem: Not effective over longer periods of time
Target Robot
Our Robot
X
Shooting Strategies
Circular- Assumes the target moves at a constant velocity and angular
velocity (slightly better than linear targeting)
Problem: Can be avoided by stopping/starting quickly or changing
directions sporadically
Target Robot
X
Our Robot
Shooting Strategies
• Circular
• change in x = cos(initialheading) * radius cos(initialheading + changeinheading) * radius
• change in y = sin(initialheading + changeinheading) *
radius - sin(initialheading) * radius
Shooting Strategies
Oscillatory – Assumes that the target robot is moving back and forth
(not many robots do this)
Target Robot
Our Robot
X
Shooting Strategies
• Movement pattern matching – Predicts target robots position based
on past moves
• Problems: Time expensive
Target Robot
Our Robot
X
Recall how inheritance works
– common components are inherited, rather
than duplicated
– allows similar though distinct objects to exhibit
their common capabilities without requiring
the code to be in two places
Creating Your Own Event
• Note: We’ve been extending the Robot
class
• If we extend the AdvancedRobot class, we
can create our own events
Creating Your Own Event
• Events happen when a certain condition is met
– E.g.
• onScanRobot() – checks to see if another robot is found,
returns true if a robot is found, event fires
• Event needs to check for some kind of
“behavior,” call this a condition
• Your robots onCustomEvent(CustomEvent e)
method is called when the test condition is true
Creating Your Own Event
• addCustomEvent(Condition condition)
Registers a custom event to be called when a
condition is met.
• Condition()
Creates a new, unnamed Condition with the
default priority.
• Condition(String name)
Creates a new Condition with the specified
name, and default priority.
• Condition(String name, int priority)
Creates a new Condition with the specified
name and priority.
Example: Our own event
• public class MyEvent extends AdvancedRobot {
... public void run()
{
//the condition is given a name “canFire”
addCustomEvent(new Condition(“canFire")
{
public boolean test()
{
// When the gun has cooled off, test returns true
return (getGunHeat() == 0);
};
}
);
}
Example: Handling our event
• public void onMyEvent(CustomEvent ev) {
fire(3);
}
Citations…
• http://people.csail.mit.edu/jacobe/papers/r
obocode.pdf
• http://www.ibm.com/developerworks/java/li
brary/j-tipstrats.html
• http://sclab.yonsei.ac.kr/~hjinh/PAPER/IC2
004-1.pdf
• http://www.dinbedstemedarbejder.dk/Dat3.
pdf