initial commit

master
Peter Babič 9 years ago
parent e1e9a55826
commit bf709f809f
  1. 13
      .gitignore
  2. 245
      src/neural/BattlefieldParameterEvaluator.java

13
.gitignore vendored

@ -1,12 +1 @@
*.class
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# Package Files #
*.jar
*.war
*.ear
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin/

@ -36,141 +36,116 @@ public class BattlefieldParameterEvaluator {
static double[] FinalScore2;
public static void main(String[] args) {
double []BattlefieldSize=new double[NUMSAMPLES];
double []GunCoolingRate=new double[NUMSAMPLES];
FinalScore1=new double[NUMSAMPLES];
FinalScore2=new double[NUMSAMPLES];
Random rng=new Random(15L);
// Disable log messages from Robocode
RobocodeEngine.setLogMessagesEnabled(false);
// Create the RobocodeEngine
// Run from C:/Robocode
RobocodeEngine engine = new RobocodeEngine(new java.io.File("C:/Robocode"));
// Add our own battle listener to the RobocodeEngine
engine.addBattleListener(new BattleObserver());
// Show the Robocode battle view
engine.setVisible(false);
// Setup the battle specification
// Setup battle parameters
int numberOfRounds = 1;
long inactivityTime = 100;
int sentryBorderSize = 50;
boolean hideEnemyNames = false;
// Get the robots and set up their initial states
RobotSpecification[] competingRobots =
engine.getLocalRepository("sample.RamFire,sample.TrackFire");
RobotSetup[] robotSetups = new RobotSetup[2];
for(NdxBattle=0;NdxBattle<NUMSAMPLES;NdxBattle++)
{
// Choose the battlefield size and gun cooling rate  
BattlefieldSize[NdxBattle]=
MAXBATTLEFIELDSIZE*(0.1+0.9*rng.nextDouble());
GunCoolingRate[NdxBattle]=MAXGUNCOOLINGRATE*(0.1+0.9*rng.nextDouble());
// Create the battlefield
BattlefieldSpecification battlefield =
new BattlefieldSpecification((int)BattlefieldSize[NdxBattle],
(int)BattlefieldSize[NdxBattle]);
// Set the robot positions
robotSetups[0]=new RobotSetup(BattlefieldSize[NdxBattle]/2.0,
BattlefieldSize[NdxBattle]/3.0,0.0);
robotSetups[1]=new RobotSetup(BattlefieldSize[NdxBattle]/2.0,
2.0*BattlefieldSize[NdxBattle]/3.0,0.0);
// Prepare the battle specification
BattleSpecification battleSpec =
new BattleSpecification(battlefield,
numberOfRounds,
inactivityTime,
GunCoolingRate[NdxBattle],
sentryBorderSize,
hideEnemyNames,
competingRobots,
robotSetups);
// Run our specified battle and let it run till it is over
engine.runBattle(battleSpec, true);
}
// Cleanup our RobocodeEngine
engine.close();
System.out.println(Arrays.toString(BattlefieldSize));
System.out.println(Arrays.toString(GunCoolingRate));
System.out.println(Arrays.toString(FinalScore1));
System.out.println(Arrays.toString(FinalScore2));
// Create the training dataset for the neural network
double [][]RawInputs=new double[NUMSAMPLES][NUM_NN_INPUTS];
double [][]RawOutputs=new double[NUMSAMPLES][1];
for(int NdxSample=0;NdxSample<NUMSAMPLES;NdxSample++)
{
// IMPORTANT: normalize the inputs and the outputs to
// the interval [0,1]  
RawInputs[NdxSample][0]=BattlefieldSize[NdxSample]/MAXBATTLEFIELDSIZE;
RawInputs[NdxSample][1]=GunCoolingRate[NdxSample]/MAXGUNCOOLINGRATE;
RawOutputs[NdxSample][0]=FinalScore1[NdxSample]/250;
}
BasicNeuralDataSet MyDataSet=new BasicNeuralDataSet(RawInputs,RawOutputs);
// Create and train the neural network   
//... TO DO ...
System.out.println("Training network...");
//... TO DO ...
System.out.println("Training completed.");
System.out.println("Testing network...");
// Generate test samples to build an output image
int []OutputRGBint=new int[NUMBATTLEFIELDSIZES*NUMCOOLINGRATES];
Color MyColor;
double MyValue=0;
double [][]MyTestData=new double
[NUMBATTLEFIELDSIZES*NUMCOOLINGRATES][NUM_NN_INPUTS];
for(int NdxBattleSize=0;NdxBattleSize<NUMBATTLEFIELDSIZES;NdxBattleSize++)
{
for(int NdxCooling=0;NdxCooling<NUMCOOLINGRATES;NdxCooling++)
{
MyTestData[NdxCooling+NdxBattleSize*NUMCOOLINGRATES][0]=
0.1+0.9*((double)NdxBattleSize)/NUMBATTLEFIELDSIZES;
MyTestData[NdxCooling+NdxBattleSize*NUMCOOLINGRATES][1]=
0.1+0.9*((double)NdxCooling)/NUMCOOLINGRATES;
}
}
// Simulate the neural network with the test samples and fill a matrix
for(int NdxBattleSize=0;NdxBattleSize<NUMBATTLEFIELDSIZES;NdxBattleSize++)
{
for(int NdxCooling=0;NdxCooling<NUMCOOLINGRATES;NdxCooling++)
{
// INSERT SOMETHING HERE
double MyTestData[NdxCooling+NdxBattleSize*NUMCOOLINGRATES];
MyValue=ClipColor(MyResult);
MyColor=new Color((float)MyValue,
(float)MyValue,
(float)MyValue);
OutputRGBint[NdxCooling+NdxBattleSize*NUMCOOLINGRATES]=
MyColor.getRGB();
}
}
System.out.println("Testing completed.");
// Plot the training samples
for(int NdxSample=0;;)
{
MyValue=ClipColor(FinalScore1[NdxSample]/250);
MyColor=new Color((float)MyValue,(float)MyValue,(float)MyValue);
int MyPixelIndex=(int)(Math.round(NUMCOOLINGRATES*((GunCoolingRate[NdxSample]/MAXGUNCOOLINGRATE)0.1)/0.9)+Math.round(NUMBATTLEFIELDSIZES*((BattlefieldSize[NdxSample]/MAXBATTLEFIELDSIZE)-0.1)/0.9)*NUMCOOLINGRATES);
if ((MyPixelIndex>=0) &&
(MyPixelIndex<NUMCOOLINGRATES*NUMBATTLEFIELDSIZES))
{
OutputRGBint[MyPixelIndex]=MyColor.getRGB();
}
}
BufferedImage img=new BufferedImage
(NUMCOOLINGRATES,NUMBATTLEFIELDSIZES,BufferedImage.TYPE_INT_RGB);
img.setRGB(0, 0, NUMCOOLINGRATES, NUMBATTLEFIELDSIZES,
OutputRGBint, 0, NUMCOOLINGRATES);
File f=new File("hello.png");
try {
ImageIO.write(img,"png",f);
} catch (IOException e) {
// TODO Auto‐generated catch block
e.printStackTrace();
}
System.out.println("Image generated.");
// Make sure that the Java VM is shut down properly
System.exit(0);
}
double[] BattlefieldSize = new double[NUMSAMPLES];
double[] GunCoolingRate = new double[NUMSAMPLES];
FinalScore1 = new double[NUMSAMPLES];
FinalScore2 = new double[NUMSAMPLES];
Random rng = new Random(15L);
// Disable log messages from Robocode
RobocodeEngine.setLogMessagesEnabled(false);
// Create the RobocodeEngine
// Run from C:/Robocode
RobocodeEngine engine = new RobocodeEngine(new java.io.File("/opt/robocode"));
// Add our own battle listener to the RobocodeEngine
engine.addBattleListener(new BattleObserver());
// Show the Robocode battle view
engine.setVisible(false);
// Setup the battle specification
// Setup battle parameters
int numberOfRounds = 1;
long inactivityTime = 100;
int sentryBorderSize = 50;
boolean hideEnemyNames = false;
// Get the robots and set up their initial states
RobotSpecification[] competingRobots = engine.getLocalRepository("sample.RamFire,sample.TrackFire");
RobotSetup[] robotSetups = new RobotSetup[2];
for (NdxBattle = 0; NdxBattle < NUMSAMPLES; NdxBattle++) {
// Choose the battlefield size and gun cooling rate  
BattlefieldSize[NdxBattle] = MAXBATTLEFIELDSIZE * (0.1 + 0.9 * rng.nextDouble());
GunCoolingRate[NdxBattle] = MAXGUNCOOLINGRATE * (0.1 + 0.9 * rng.nextDouble());
// Create the battlefield
BattlefieldSpecification battlefield = new BattlefieldSpecification((int) BattlefieldSize[NdxBattle],
(int) BattlefieldSize[NdxBattle]);
// Set the robot positions
robotSetups[0] = new RobotSetup(BattlefieldSize[NdxBattle] / 2.0, BattlefieldSize[NdxBattle] / 3.0, 0.0);
robotSetups[1] = new RobotSetup(BattlefieldSize[NdxBattle] / 2.0, 2.0 * BattlefieldSize[NdxBattle] / 3.0, 0.0);
// Prepare the battle specification
BattleSpecification battleSpec = new BattleSpecification(battlefield, numberOfRounds, inactivityTime,
GunCoolingRate[NdxBattle], sentryBorderSize, hideEnemyNames, competingRobots, robotSetups);
// Run our specified battle and let it run till it is over
engine.runBattle(battleSpec, true);
}
// Cleanup our RobocodeEngine
engine.close();
System.out.println(Arrays.toString(BattlefieldSize));
System.out.println(Arrays.toString(GunCoolingRate));
System.out.println(Arrays.toString(FinalScore1));
System.out.println(Arrays.toString(FinalScore2));
// Create the training dataset for the neural network
double[][] RawInputs = new double[NUMSAMPLES][NUM_NN_INPUTS];
double[][] RawOutputs = new double[NUMSAMPLES][1];
for (int NdxSample = 0; NdxSample < NUMSAMPLES; NdxSample++) {
// IMPORTANT: normalize the inputs and the outputs to
// the interval [0,1]  
RawInputs[NdxSample][0] = BattlefieldSize[NdxSample] / MAXBATTLEFIELDSIZE;
RawInputs[NdxSample][1] = GunCoolingRate[NdxSample] / MAXGUNCOOLINGRATE;
RawOutputs[NdxSample][0] = FinalScore1[NdxSample] / 250;
}
BasicNeuralDataSet MyDataSet = new BasicNeuralDataSet(RawInputs, RawOutputs);
// Create and train the neural network   
// ... TO DO ...
System.out.println("Training network...");
// ... TO DO ...
System.out.println("Training completed.");
System.out.println("Testing network...");
// Generate test samples to build an output image
int[] OutputRGBint = new int[NUMBATTLEFIELDSIZES * NUMCOOLINGRATES];
Color MyColor;
double MyValue = 0;
double[][] MyTestData = new double[NUMBATTLEFIELDSIZES * NUMCOOLINGRATES][NUM_NN_INPUTS];
for (int NdxBattleSize = 0; NdxBattleSize < NUMBATTLEFIELDSIZES; NdxBattleSize++) {
for (int NdxCooling = 0; NdxCooling < NUMCOOLINGRATES; NdxCooling++) {
MyTestData[NdxCooling + NdxBattleSize * NUMCOOLINGRATES][0] = 0.1 + 0.9 * ((double) NdxBattleSize)
/ NUMBATTLEFIELDSIZES;
MyTestData[NdxCooling + NdxBattleSize * NUMCOOLINGRATES][1] = 0.1 + 0.9 * ((double) NdxCooling) / NUMCOOLINGRATES;
}
}
// Simulate the neural network with the test samples and fill a matrix
for (int NdxBattleSize = 0; NdxBattleSize < NUMBATTLEFIELDSIZES; NdxBattleSize++) {
for (int NdxCooling = 0; NdxCooling < NUMCOOLINGRATES; NdxCooling++) {
// INSERT SOMETHING HERE
double MyResult = 0.0; // MyTestData[NdxCooling+NdxBattleSize*NUMCOOLINGRATES];
MyValue = ClipColor(MyResult);
MyColor = new Color((float) MyValue, (float) MyValue, (float) MyValue);
OutputRGBint[NdxCooling + NdxBattleSize * NUMCOOLINGRATES] = MyColor.getRGB();
}
}
System.out.println("Testing completed.");
// Plot the training samples
for (int NdxSample = 0; NdxSample < NUMSAMPLES; NdxSample++) {
MyValue = ClipColor(FinalScore1[NdxSample] / 250);
MyColor = new Color((float) MyValue, (float) MyValue, (float) MyValue);
int MyPixelIndex = (int) (Math.round(NUMCOOLINGRATES * ((GunCoolingRate[NdxSample] / MAXGUNCOOLINGRATE) - 0.1) / 0.9) + Math
.round(NUMBATTLEFIELDSIZES * ((BattlefieldSize[NdxSample] / MAXBATTLEFIELDSIZE) - 0.1) / 0.9)
* NUMCOOLINGRATES);
// int MyPixelIndex = 0;
if ((MyPixelIndex >= 0) && (MyPixelIndex < NUMCOOLINGRATES * NUMBATTLEFIELDSIZES)) {
OutputRGBint[MyPixelIndex] = MyColor.getRGB();
}
}
BufferedImage img = new BufferedImage(NUMCOOLINGRATES, NUMBATTLEFIELDSIZES, BufferedImage.TYPE_INT_RGB);
img.setRGB(0, 0, NUMCOOLINGRATES, NUMBATTLEFIELDSIZES, OutputRGBint, 0, NUMCOOLINGRATES);
File f = new File("hello.png");
try {
ImageIO.write(img, "png", f);
} catch (IOException e) {
// TODO Auto‐generated catch block
e.printStackTrace();
}
System.out.println("Image generated.");
// Make sure that the Java VM is shut down properly
System.exit(0);
}
/*
* Clip a color value (double precision) to lie in the valid range [0,1]

Loading…
Cancel
Save