Difference between revisions of "Help:Help"
Jump to navigation
Jump to search
| Line 1: | Line 1: | ||
| + | == Help!, in both meanings of the term == | ||
| + | |||
| + | |||
This is a page for help, please edit the page and add the link to your problem, and maybe one of the more experienced coders will have a solution. This is a potluck help page so, bring your problems, and be ready to share your expertise and knowledge with the less fortunate! | This is a page for help, please edit the page and add the link to your problem, and maybe one of the more experienced coders will have a solution. This is a potluck help page so, bring your problems, and be ready to share your expertise and knowledge with the less fortunate! | ||
| + | |||
| + | --[[User:Pkdeken|Pkdeken]] 15:38, 29 April 2009 (UTC) | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | I know it says post a link, but this is easier | ||
| + | |||
| + | <pre> | ||
| + | package tutorials; | ||
| + | |||
| + | import robocode.*; | ||
| + | import robocode.util.Utils; | ||
| + | import java.util.ArrayList; | ||
| + | import java.awt.geom.Point2D; | ||
| + | |||
| + | public class GFTTutorial extends AdvancedRobot { | ||
| + | |||
| + | public final static int BINS = 47; | ||
| + | public final static int MIDDLE_BIN = (BINS - 1) / 2; | ||
| + | public final static double MAX_ESC_ANGLE = 0.8143399421265254D; | ||
| + | public final static double BIN_WIDTH = MAX_ESC_ANGLE / MIDDLE_BIN; | ||
| + | public static ArrayList aimFactors = new ArrayList(); | ||
| + | public double prevVelocity = 0; | ||
| + | public double t = 0; | ||
| + | public Point2D.Double enemyLoc; | ||
| + | |||
| + | public void run() { | ||
| + | while (true) { | ||
| + | turnRadarRightRadians(Double.POSITIVE_INFINITY); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public void onScannedRobot(ScannedRobotEvent e) { | ||
| + | double absBearing = getHeadingRadians() + e.getBearingRadians(); | ||
| + | double firePower = 3D; | ||
| + | BulletWave w = new BulletWave(); | ||
| + | w.firedLocation = new Point2D.Double(getX(), getY()); | ||
| + | enemyLoc = new Point2D.Double(getX() + Math.sin(absBearing) * e.getDistance(), getY() | ||
| + | + Math.cos(absBearing) * e.getDistance()); | ||
| + | w.bearingDir = (e.getVelocity() * Math.sin(e.getHeadingRadians() - absBearing) < 0 ? -BIN_WIDTH | ||
| + | : BIN_WIDTH); | ||
| + | w.bearing = absBearing; | ||
| + | w.velocity = bulletVelocity(firePower); | ||
| + | //originalDistance,wall,acceleration,myVelocity,time,timeSinceAccel; | ||
| + | w.originalDistance = e.getDistance(); | ||
| + | w.wallX = getX() + Math.sin(absBearing) * e.getDistance() > 400 ? 800 - getX() + Math.sin(absBearing) * e.getDistance() | ||
| + | : getX() + Math.sin(absBearing) * e.getDistance() == 0 ? 0 : getX() + Math.sin(absBearing) * e.getDistance(); | ||
| + | w.wallY = getY()+ Math.cos(absBearing) * e.getDistance() > 300 ? 600 - getY()+ Math.cos(absBearing) * e.getDistance() | ||
| + | : getY()+ Math.cos(absBearing) * e.getDistance() == 0 ? 0 : getY()+ Math.cos(absBearing) * e.getDistance(); | ||
| + | w.acceleration = e.getVelocity() - prevVelocity; | ||
| + | w.time = getTime(); | ||
| + | t = w.acceleration == 0 ? t++ : 0; | ||
| + | w.timeSinceAccel = t; | ||
| + | prevVelocity = e.getVelocity(); | ||
| + | int XX = 100; | ||
| + | int[] TopXX = new int[XX]; | ||
| + | double[] TopDiff = new double[XX]; | ||
| + | for (int i=0;i<aimFactors.size();i++){ | ||
| + | double diff = findDifference(w,(AimFactor)aimFactors.get(i)); | ||
| + | for (int r=XX - 1;r>=0;r--) { | ||
| + | if (diff < TopDiff[r]){ | ||
| + | TopXX[r] = ((AimFactor)aimFactors.get(i)).angle; | ||
| + | TopDiff[r] = diff; | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | double[] stats = new double[BINS]; | ||
| + | for (int i = 0; i < XX; i++) { | ||
| + | stats[TopXX[i]]++; | ||
| + | } | ||
| + | int mostVisited = MIDDLE_BIN; | ||
| + | for (int i = 0; i < BINS; i++) { | ||
| + | if (stats[i] > stats[mostVisited]) | ||
| + | mostVisited = i; | ||
| + | } | ||
| + | setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians() | ||
| + | + w.bearingDir * (mostVisited - MIDDLE_BIN))); | ||
| + | if (setFireBullet(firePower) != null) | ||
| + | addCustomEvent(w); | ||
| + | setTurnRadarRightRadians(Utils.normalRelativeAngle(absBearing - getRadarHeadingRadians()) * 2); | ||
| + | } | ||
| + | |||
| + | double findDifference(BulletWave w, AimFactor a) { | ||
| + | double diff = 0; | ||
| + | diff += Math.sqrt(Math.pow((1-(w.originalDistance/a.distanceIndex)),2)); | ||
| + | diff += Math.sqrt(Math.pow((1-(w.wallX/a.wallIndexX)),2)); | ||
| + | diff += Math.sqrt(Math.pow((1-(w.wallY/a.wallIndexY)),2)); | ||
| + | diff += Math.sqrt(Math.pow((1-(w.acceleration/a.accelerationIndex)),2)); | ||
| + | diff += Math.sqrt(Math.pow((1-(w.myVelocity/a.velocityIndex)),2)); | ||
| + | diff += Math.sqrt(Math.pow((1-(w.time/a.time)),2)); | ||
| + | diff += Math.sqrt(Math.pow((1-(w.timeSinceAccel/a.timeSinceAccel)),2)); | ||
| + | return diff; | ||
| + | } | ||
| + | |||
| + | double absoluteBearing(Point2D.Double p, Point2D.Double q) { | ||
| + | return Math.atan2(q.x - p.x, q.y - p.y); | ||
| + | } | ||
| + | |||
| + | double bulletVelocity(double power) { | ||
| + | return 20 - 3 * power; | ||
| + | } | ||
| + | |||
| + | public class BulletWave extends Condition { | ||
| + | Point2D.Double firedLocation; | ||
| + | double velocity, distanceTraveled, bearing, bearingDir; | ||
| + | double originalDistance,wallX,wallY,acceleration,myVelocity,time,timeSinceAccel; | ||
| + | |||
| + | public boolean test() { | ||
| + | distanceTraveled += velocity; | ||
| + | double distance = firedLocation.distance(enemyLoc); | ||
| + | if (distanceTraveled > distance - 18) { | ||
| + | try { | ||
| + | aimFactors.add(new AimFactor(originalDistance,wallX,wallY,acceleration,myVelocity,time,timeSinceAccel,(int) ((Utils.normalRelativeAngle(absoluteBearing(firedLocation, enemyLoc)) - bearing) / bearingDir) + MIDDLE_BIN)); | ||
| + | } catch (Exception e) { | ||
| + | } | ||
| + | removeCustomEvent(this); | ||
| + | } | ||
| + | return false; | ||
| + | } | ||
| + | } | ||
| + | public class AimFactor { | ||
| + | double distanceIndex,wallIndexX,wallIndexY,accelerationIndex,velocityIndex,time,timeSinceAccel; | ||
| + | int angle; | ||
| + | public AimFactor(double ad,double awx,double awy,double aa,double av,double at,double ati,int aangle){ | ||
| + | distanceIndex = ad; | ||
| + | wallIndexX = awx; | ||
| + | wallIndexY = awy; | ||
| + | accelerationIndex = aa; | ||
| + | velocityIndex = av; | ||
| + | time = at; | ||
| + | timeSinceAccel = ati; | ||
| + | angle = aangle; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | |||
| + | If you run this you end up with it always firing at guessFactor -1, How can I fix this? | ||
| + | It is supposed to be running a patternMatchingStatitistical Targeting gun. | ||
| + | Basically, it segments the data without using nested arrays | ||
| + | --[[User:Pkdeken|Pkdeken]] 15:38, 29 April 2009 (UTC) | ||
Revision as of 16:38, 29 April 2009
Help!, in both meanings of the term
This is a page for help, please edit the page and add the link to your problem, and maybe one of the more experienced coders will have a solution. This is a potluck help page so, bring your problems, and be ready to share your expertise and knowledge with the less fortunate!
--Pkdeken 15:38, 29 April 2009 (UTC)
I know it says post a link, but this is easier
package tutorials;
import robocode.*;
import robocode.util.Utils;
import java.util.ArrayList;
import java.awt.geom.Point2D;
public class GFTTutorial extends AdvancedRobot {
public final static int BINS = 47;
public final static int MIDDLE_BIN = (BINS - 1) / 2;
public final static double MAX_ESC_ANGLE = 0.8143399421265254D;
public final static double BIN_WIDTH = MAX_ESC_ANGLE / MIDDLE_BIN;
public static ArrayList aimFactors = new ArrayList();
public double prevVelocity = 0;
public double t = 0;
public Point2D.Double enemyLoc;
public void run() {
while (true) {
turnRadarRightRadians(Double.POSITIVE_INFINITY);
}
}
public void onScannedRobot(ScannedRobotEvent e) {
double absBearing = getHeadingRadians() + e.getBearingRadians();
double firePower = 3D;
BulletWave w = new BulletWave();
w.firedLocation = new Point2D.Double(getX(), getY());
enemyLoc = new Point2D.Double(getX() + Math.sin(absBearing) * e.getDistance(), getY()
+ Math.cos(absBearing) * e.getDistance());
w.bearingDir = (e.getVelocity() * Math.sin(e.getHeadingRadians() - absBearing) < 0 ? -BIN_WIDTH
: BIN_WIDTH);
w.bearing = absBearing;
w.velocity = bulletVelocity(firePower);
//originalDistance,wall,acceleration,myVelocity,time,timeSinceAccel;
w.originalDistance = e.getDistance();
w.wallX = getX() + Math.sin(absBearing) * e.getDistance() > 400 ? 800 - getX() + Math.sin(absBearing) * e.getDistance()
: getX() + Math.sin(absBearing) * e.getDistance() == 0 ? 0 : getX() + Math.sin(absBearing) * e.getDistance();
w.wallY = getY()+ Math.cos(absBearing) * e.getDistance() > 300 ? 600 - getY()+ Math.cos(absBearing) * e.getDistance()
: getY()+ Math.cos(absBearing) * e.getDistance() == 0 ? 0 : getY()+ Math.cos(absBearing) * e.getDistance();
w.acceleration = e.getVelocity() - prevVelocity;
w.time = getTime();
t = w.acceleration == 0 ? t++ : 0;
w.timeSinceAccel = t;
prevVelocity = e.getVelocity();
int XX = 100;
int[] TopXX = new int[XX];
double[] TopDiff = new double[XX];
for (int i=0;i<aimFactors.size();i++){
double diff = findDifference(w,(AimFactor)aimFactors.get(i));
for (int r=XX - 1;r>=0;r--) {
if (diff < TopDiff[r]){
TopXX[r] = ((AimFactor)aimFactors.get(i)).angle;
TopDiff[r] = diff;
break;
}
}
}
double[] stats = new double[BINS];
for (int i = 0; i < XX; i++) {
stats[TopXX[i]]++;
}
int mostVisited = MIDDLE_BIN;
for (int i = 0; i < BINS; i++) {
if (stats[i] > stats[mostVisited])
mostVisited = i;
}
setTurnGunRightRadians(Utils.normalRelativeAngle(absBearing - getGunHeadingRadians()
+ w.bearingDir * (mostVisited - MIDDLE_BIN)));
if (setFireBullet(firePower) != null)
addCustomEvent(w);
setTurnRadarRightRadians(Utils.normalRelativeAngle(absBearing - getRadarHeadingRadians()) * 2);
}
double findDifference(BulletWave w, AimFactor a) {
double diff = 0;
diff += Math.sqrt(Math.pow((1-(w.originalDistance/a.distanceIndex)),2));
diff += Math.sqrt(Math.pow((1-(w.wallX/a.wallIndexX)),2));
diff += Math.sqrt(Math.pow((1-(w.wallY/a.wallIndexY)),2));
diff += Math.sqrt(Math.pow((1-(w.acceleration/a.accelerationIndex)),2));
diff += Math.sqrt(Math.pow((1-(w.myVelocity/a.velocityIndex)),2));
diff += Math.sqrt(Math.pow((1-(w.time/a.time)),2));
diff += Math.sqrt(Math.pow((1-(w.timeSinceAccel/a.timeSinceAccel)),2));
return diff;
}
double absoluteBearing(Point2D.Double p, Point2D.Double q) {
return Math.atan2(q.x - p.x, q.y - p.y);
}
double bulletVelocity(double power) {
return 20 - 3 * power;
}
public class BulletWave extends Condition {
Point2D.Double firedLocation;
double velocity, distanceTraveled, bearing, bearingDir;
double originalDistance,wallX,wallY,acceleration,myVelocity,time,timeSinceAccel;
public boolean test() {
distanceTraveled += velocity;
double distance = firedLocation.distance(enemyLoc);
if (distanceTraveled > distance - 18) {
try {
aimFactors.add(new AimFactor(originalDistance,wallX,wallY,acceleration,myVelocity,time,timeSinceAccel,(int) ((Utils.normalRelativeAngle(absoluteBearing(firedLocation, enemyLoc)) - bearing) / bearingDir) + MIDDLE_BIN));
} catch (Exception e) {
}
removeCustomEvent(this);
}
return false;
}
}
public class AimFactor {
double distanceIndex,wallIndexX,wallIndexY,accelerationIndex,velocityIndex,time,timeSinceAccel;
int angle;
public AimFactor(double ad,double awx,double awy,double aa,double av,double at,double ati,int aangle){
distanceIndex = ad;
wallIndexX = awx;
wallIndexY = awy;
accelerationIndex = aa;
velocityIndex = av;
time = at;
timeSinceAccel = ati;
angle = aangle;
}
}
}
If you run this you end up with it always firing at guessFactor -1, How can I fix this?
It is supposed to be running a patternMatchingStatitistical Targeting gun. Basically, it segments the data without using nested arrays
--Pkdeken 15:38, 29 April 2009 (UTC)