This Week Cover as much Robocode strategy as we can Monday: Robocode –NN Homework Review –Hand...

45
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)

Transcript of This Week Cover as much Robocode strategy as we can Monday: Robocode –NN Homework Review –Hand...

Page 1: 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.

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)

Page 2: 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.

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

Page 3: 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.

Robocode Homework

• http://www.cs.ucf.edu/~ahoover/bhcsi2010/

Page 4: 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.

Robocode and Robocode Strategies

(taken from citations at the end)

Page 5: 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.

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?title=Robocode/FAQ

Page 6: 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.

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

Page 7: 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.

From Last Time

• The tank default behavior is in a loop defined in run()

• Different events interrupt this loop execution

Page 8: 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.

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).

Page 9: 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.

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.

Page 10: 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.

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

Page 11: 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.

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

Page 12: 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.

Inheritance in Java

• Class Dog extends Mammal– Mammals have:

• Hair• Legs• Lungs

– Dogs have:• Canis lupus familiaris • Four legs

Page 13: 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.

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)

Page 14: 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.

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

Page 15: 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.

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)

Page 16: 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.

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

Page 17: 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.

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.

Page 18: 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.

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

Page 19: 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.

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

Page 20: 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.

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

Page 21: 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.

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

Page 22: 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.

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

Page 23: 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.

Example: Scan 360

• public class MyFirstRobot extends Robot {

public void run()

{

while (true) {

ahead(100);

turnGunRight(360);

}

}  

public void onScannedRobot(ScannedRobotEvent e) { fire(1); } }

Page 24: 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.

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

Page 25: 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.

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(radarTurn)); // ... }

Page 26: 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.

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

Page 27: 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.

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

Page 28: 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.

Choosing an Enemy

• No explicit methods provided by Robocode

• Neural Network Targeting Today if we have time

Page 29: 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.

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

Page 30: 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.

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

Page 31: 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.

Shooting Strategies

• Stationary• Linear• Circular• Oscillatory• Movement pattern matching

Page 32: 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.

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 RobotOur Robot

Page 33: 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.

Example: Stationary Shooting

• onScanRobot()

{fire(1);

}

//This method just fires where we saw the robot without taking into account where it may be going

Page 34: 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.

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

X

Our Robot

Page 35: 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.

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

XOur Robot

Page 36: 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.

Shooting Strategies

• Circular

• change in x = cos(initialheading) * radius - cos(initialheading + changeinheading) * radius

• change in y = sin(initialheading + changeinheading) * radius - sin(initialheading) * radius

Page 37: 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.

Shooting Strategies

Oscillatory – Assumes that the target robot is moving back and forth(not many robots do this)

Target Robot

XOur Robot

Page 38: 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.

Shooting Strategies

• Movement pattern matching – Predicts target robots position based on past moves

• Problems: Time expensive

Target Robot

XOur Robot

Page 39: 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.

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

Page 40: 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.

Creating Your Own Event

• Note: We’ve been extending the Robot class

• If we extend the AdvancedRobot class, we can create our own events

Page 41: 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.

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

Page 42: 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.

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.

Page 43: 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.

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);};

});

}

Page 44: 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.

Example: Handling our event

• public void onMyEvent(CustomEvent ev) {

fire(3);

}