package bot1;
import robocode.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Random;
public class CooperBot1 extends AdvancedRobot {
static double mutationProbability = 0.05;
static int StartingPop = 10;
String currentGenome;
String NextGenPop[]= new String[StartingPop];
int currentBotRound;
int A, B, C, D, E, F, G;
int counterHits, counterFired;
int AMax=1000, BMax=360,CMax=1000,DMax=360,EMax=4,FMax=1000,GMax=360;
static int ScoreArray[][] = new int[StartingPop][2];
public void run() {
counterHits = 0;
counterFired = 0;
currentBotRound = getRoundNum();
try {
BufferedReader r = new BufferedReader(new FileReader(
getDataFile("Populations.txt")));
} catch (IOException e) {
createStartPopulations();
}
importCurrentGene();
out.print(currentGenome);
String[] temp;
String delimiter = "-";
temp = currentGenome.split(delimiter);
A = Integer.parseInt(temp[0]);
B = Integer.parseInt(temp[1]);
C = Integer.parseInt(temp[2]);
D = Integer.parseInt(temp[3]);
E = Integer.parseInt(temp[4]);
F = Integer.parseInt(temp[5]);
G = Integer.parseInt(temp[6]);
while (true) {
// Replace the next 4 lines with any behavior you would like
ahead(A);
turnGunLeft(B);
back(C);
turnGunRight(D);
}
}
/**
* onScannedRobot: What to do when you see another robot
*/
public void onScannedRobot(ScannedRobotEvent e) {
// Replace the next line with any behavior you would like
stop();
setFire(E);
stop();
}
/**
* onHitByBullet: What to do when you're hit by a bullet
*/
/**
* onHitWall: What to do when you hit a wall
*/
public void onHitWall(HitWallEvent e) {
// Replace the next line with any behavior you would like
back(F);
turnRight(G);
}
public void onBattleEnded(BattleEndedEvent e) {
int temp[] = new int[2];
for (int i = 0; i < ScoreArray.length - 1; i++) {
for (int j = 0; j < ScoreArray.length - 1; j++)
if (ScoreArray[j][0] > ScoreArray[j + 1][0]) {
temp[0] = ScoreArray[j][0];
temp[1] = ScoreArray[j][1];
ScoreArray[j][0] = ScoreArray[j + 1][0];
ScoreArray[j][1] = ScoreArray[j + 1][1];
ScoreArray[j + 1][0] = temp[0];
ScoreArray[j + 1][1] = temp[1];
}
}
for (int i = 0; i < ScoreArray.length; i++)
out.print("Score:" + ScoreArray[i][0] + "Round:" + ScoreArray[i][1]
+ "\n ");
PrintStream x = null;
try {
x = new PrintStream(new RobocodeFileOutputStream(
getDataFile("Scores.txt")));
for (int i = 0; i < ScoreArray.length; i++)
x.println(ScoreArray[i][0] + "-" + ScoreArray[i][1]);
x.close();
} catch (IOException f) {
out.print("Exception at printing the array of scores");
}
for(int i=0;i<StartingPop;i++)
{
NextGenPop[i]=Breed();
}
PrintStream ww = null;
try {
ww = new PrintStream(new RobocodeFileOutputStream(getDataFile("Populations.txt")));
for (int i = 0; i < StartingPop; i++)
{
ww.println(NextGenPop[i]);
}
ww.close();
} catch (IOException eee) {
}
}
public void onBulletHit(BulletHitEvent e) {
counterHits++;
counterFired++;
}
public void onBulletMiss(BulletMissedEvent e) {
counterFired++;
}
public int getFitnessScore() {
double accuracy;
if (counterFired == 0) {
accuracy = 0;
} else {
accuracy = counterHits / counterFired;
}
return (int) ((accuracy / (int) getTime()) * 1000000);
}
public void onDeath(DeathEvent e) {
ScoreArray[getRoundNum()][0] = getFitnessScore();
ScoreArray[getRoundNum()][1] = (getRoundNum());
}
public void onRoundEnded(RoundEndedEvent e) {
ScoreArray[getRoundNum()][0] = getFitnessScore();
ScoreArray[getRoundNum()][1] = (getRoundNum());
}
public String Breed(){//When called, it will return a single child.
/*
* Take top fitness bot, represent 10 times, take second ranked bot, represent 9 times... so on so on, this allows all bots a representation, even if score is 0.
*/
int selectedPairRound[] = new int[2];
String selectedPairs[] =new String[2];
int selectionarraysize=0;
for (int i=0;i<=StartingPop;i++)//Sums up how large the array needs to be
{
selectionarraysize+=i;
}
int selectionArray[]= new int[selectionarraysize];
int limiter=10;
int counter=limiter-1;
for(int i=selectionarraysize-1;i>=0;i--)// chooses two random (ranked) bot genomes and stores them for use in crossover.
{
selectionArray[i]=ScoreArray[limiter-1][1];
if(counter==0)
{
limiter--;
counter=limiter;
}
counter--;
}
Random rand = new Random();
for (int i=0;i<2;i++)
{
int selector = rand.nextInt(selectionarraysize);
selectedPairRound[i]=selectionArray[selector];//Populates the array with 2 coresponding bot round numbers.
}
for(int i=0;i<2;i++)
{
try {
BufferedReader r = new BufferedReader(new FileReader(getDataFile("Populations.txt")));
for(int j=0;j<selectedPairRound[i];j++){r.readLine();}
selectedPairs[i] = r.readLine();
r.close();
}
catch(IOException e){}
}
for(int i=0;i<2;i++)
{
out.print("Parent "+(i+1)+": "+selectedPairs[i]+"\n");
}
//Crossover And Mutate
return(Crossover(selectedPairs[0], selectedPairs[1]));
}
public String Crossover(String parent1, String parent2)
{
String[] temp1;
String[] temp2;
String delimiter = "-";
temp1 = parent1.split(delimiter);
int A1 = Integer.parseInt(temp1[0]);
int B1 = Integer.parseInt(temp1[1]);
int C1 = Integer.parseInt(temp1[2]);
int D1 = Integer.parseInt(temp1[3]);
int E1 = Integer.parseInt(temp1[4]);
int F1 = Integer.parseInt(temp1[5]);
int G1 = Integer.parseInt(temp1[6]);
temp2 = parent2.split(delimiter);
int A2 = Integer.parseInt(temp2[0]);
int B2 = Integer.parseInt(temp2[1]);
int C2 = Integer.parseInt(temp2[2]);
int D2 = Integer.parseInt(temp2[3]);
int E2 = Integer.parseInt(temp2[4]);
int F2 = Integer.parseInt(temp2[5]);
int G2 = Integer.parseInt(temp2[6]);
// 50 50 chance of choosing phenotype from parent 1 or 2
int A3=0;
int B3=0;
int C3=0;
int D3=0;
int E3=0;
int F3=0;
int G3=0;
Random rand1 = new Random();
if(rand1.nextInt(100)>50)
{
A3=A1;
}
else
{
A3=A2;
}
if(rand1.nextInt(100)>50)
{
B3=B1;
}
else
{
B3=B2;
}
if(rand1.nextInt(100)>50)
{
C3=C1;
}
else
{
C3=C2;
}
if(rand1.nextInt(100)>50)
{
D3=D1;
}
else
{
D3=D2;
}
if(rand1.nextInt(100)>50)
{
E3=E1;
}
else
{
E3=E2;
}
if(rand1.nextInt(100)>50)
{
F3=F1;
}
else
{
F3=F2;
}
if(rand1.nextInt(100)>50)
{
G3=G1;
}
else
{
G3=G2;
}
mutate(A3,AMax);
mutate(B3,BMax);
mutate(C3,CMax);
mutate(D3,DMax);
mutate(E3,EMax);
mutate(F3,FMax);
mutate(G3,GMax);
String child=null;
child=(A3+"-"+B3+"-"+C3+"-"+D3+"-"+E3+"-"+F3+"-"+G3);
return child;
}
public int mutate(int phenotype,int eeff1)
{
int mutatedphenotype;
Random randmutate=new Random();
if(randmutate.nextInt(100)<mutationProbability)
{
return mutatedphenotype=randmutate.nextInt(eeff1);
}
else
{
return phenotype;
}
}
public void createStartPopulations() {
PrintStream w = null;
try {
w = new PrintStream(new RobocodeFileOutputStream(
getDataFile("Populations.txt")));
for (int i = 0; i < StartingPop; i++) {
Random randomgenerator = new Random();
int g1 = randomgenerator.nextInt(AMax);
int g2 = randomgenerator.nextInt(BMax);
int g3 = randomgenerator.nextInt(CMax);
int g4 = randomgenerator.nextInt(DMax);
int g5 = randomgenerator.nextInt(EMax);
int g6 = randomgenerator.nextInt(FMax);
int g7 = randomgenerator.nextInt(GMax);
w.println(+g1 + "-" + g2 + "-" + g3 + "-" + g4 + "-" + g5 + "-"
+ g6 + "-" + g7);
}
w.close();
} catch (IOException e) {
}
}
public void importCurrentGene() {
try {
BufferedReader r = new BufferedReader(new FileReader(
getDataFile("Populations.txt")));
for (int i = 0; i < currentBotRound; i++) {
r.readLine();
}
currentGenome = r.readLine();
} catch (IOException e) {
}
}
}