|
|
|
@ -6,25 +6,33 @@ import java.awt.image.BufferedImage; |
|
|
|
|
import java.io.File; |
|
|
|
|
import java.io.IOException; |
|
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
|
|
|
|
|
|
import org.encog.engine.network.activation.ActivationSigmoid; |
|
|
|
|
import org.encog.ml.data.MLData; |
|
|
|
|
import org.encog.ml.data.basic.BasicMLData; |
|
|
|
|
import org.encog.neural.data.basic.BasicNeuralDataSet; |
|
|
|
|
import org.encog.ml.data.basic.BasicMLDataSet; |
|
|
|
|
//import org.encog.neural.data.basic.BasicNeuralDataSet;
|
|
|
|
|
import org.encog.neural.networks.BasicNetwork; |
|
|
|
|
import org.encog.neural.networks.layers.BasicLayer; |
|
|
|
|
import org.encog.neural.networks.training.propagation.resilient.ResilientPropagation; |
|
|
|
|
|
|
|
|
|
import robocode.BattleResults; |
|
|
|
|
import robocode.control.*; |
|
|
|
|
import robocode.control.events.*; |
|
|
|
|
|
|
|
|
|
public class BattlefieldParameterEvaluator { |
|
|
|
|
// Minimum allowable battlefield size is 400
|
|
|
|
|
final static int MINBATTLEFIELDSIZE = 400; |
|
|
|
|
final static int MAXBATTLEFIELDSIZE = 4000; |
|
|
|
|
// Minimum allowable gun cooling rate is 0.1
|
|
|
|
|
final static double MINGUNCOOLINGRATE = 0.1; |
|
|
|
|
final static double MAXGUNCOOLINGRATE = 10; |
|
|
|
|
// Visual image dimensions
|
|
|
|
|
final static int NUMBATTLEFIELDSIZES = 601; |
|
|
|
|
final static int NUMCOOLINGRATES = 501; |
|
|
|
|
final static int NUMSAMPLES = 5; |
|
|
|
|
final static int NUMSAMPLES = 1000; |
|
|
|
|
// Number of inputs for the multilayer perceptron (size of the input vectors)
|
|
|
|
|
final static int NUM_NN_INPUTS = 2; |
|
|
|
|
// Number of hidden neurons of the neural network
|
|
|
|
@ -33,7 +41,7 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
final static int NUM_TRAINING_EPOCHS = 100000; |
|
|
|
|
// The requested error in nn training
|
|
|
|
|
final static double NN_TRAINING_ERROR = 0.01; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int NdxBattle; |
|
|
|
|
static double[] FinalScore1; |
|
|
|
|
static double[] FinalScore2; |
|
|
|
@ -64,8 +72,8 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
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()); |
|
|
|
|
BattlefieldSize[NdxBattle] = rangedRand(rng, MINBATTLEFIELDSIZE, MAXBATTLEFIELDSIZE); |
|
|
|
|
GunCoolingRate[NdxBattle] = rangedRand(rng, MINGUNCOOLINGRATE, MAXGUNCOOLINGRATE); |
|
|
|
|
// Create the battlefield
|
|
|
|
|
BattlefieldSpecification battlefield = new BattlefieldSpecification((int) BattlefieldSize[NdxBattle], |
|
|
|
|
(int) BattlefieldSize[NdxBattle]); |
|
|
|
@ -96,7 +104,7 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
RawOutputs[NdxSample][0] = FinalScore1[NdxSample] / 250; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BasicNeuralDataSet MyDataSet = new BasicNeuralDataSet(RawInputs, RawOutputs); |
|
|
|
|
BasicMLDataSet MyDataSet = new BasicMLDataSet(RawInputs, RawOutputs); |
|
|
|
|
|
|
|
|
|
// Create and train the neural network
|
|
|
|
|
BasicNetwork network = new BasicNetwork(); |
|
|
|
@ -108,17 +116,18 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
|
|
|
|
|
System.out.println("Training network..."); |
|
|
|
|
final ResilientPropagation train = new ResilientPropagation(network, MyDataSet); |
|
|
|
|
|
|
|
|
|
int epoch = 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// int epoch = 1;
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
train.iteration(); |
|
|
|
|
System.out.println("Epoch #" + epoch + " Error:" + train.getError()); |
|
|
|
|
epoch++; |
|
|
|
|
} while(train.getError() > NN_TRAINING_ERROR); |
|
|
|
|
// System.out.println("Epoch #" + epoch + " Error:" + train.getError());
|
|
|
|
|
// epoch++;
|
|
|
|
|
} |
|
|
|
|
while (train.getError() > NN_TRAINING_ERROR); |
|
|
|
|
train.finishTraining(); |
|
|
|
|
System.out.println("Training completed."); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Testing network..."); |
|
|
|
|
// Generate test samples to build an output image
|
|
|
|
|
int[] OutputRGBint = new int[NUMBATTLEFIELDSIZES * NUMCOOLINGRATES]; |
|
|
|
@ -136,7 +145,11 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
for (int NdxBattleSize = 0; NdxBattleSize < NUMBATTLEFIELDSIZES; NdxBattleSize++) { |
|
|
|
|
for (int NdxCooling = 0; NdxCooling < NUMCOOLINGRATES; NdxCooling++) { |
|
|
|
|
// TODO INSERT SOMETHING HERE
|
|
|
|
|
double MyResult = 0.0; // MyTestData[NdxCooling+NdxBattleSize*NUMCOOLINGRATES];
|
|
|
|
|
BasicMLData input = new BasicMLData(MyTestData[NdxCooling + NdxBattleSize * NUMCOOLINGRATES]); |
|
|
|
|
final MLData output = network.compute(input.clone()); |
|
|
|
|
double MyResult = output.getData()[0]; |
|
|
|
|
// double MyResult = 0.0; // MyTestData[NdxCooling+NdxBattleSize*NUMCOOLINGRATES];
|
|
|
|
|
// double MyResult = MyTestData[NdxCooling + NdxBattleSize * NUMCOOLINGRATES];
|
|
|
|
|
MyValue = ClipColor(MyResult); |
|
|
|
|
MyColor = new Color((float) MyValue, (float) MyValue, (float) MyValue); |
|
|
|
|
OutputRGBint[NdxCooling + NdxBattleSize * NUMCOOLINGRATES] = MyColor.getRGB(); |
|
|
|
@ -161,7 +174,6 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
ImageIO.write(img, "png", f); |
|
|
|
|
} |
|
|
|
|
catch (IOException e) { |
|
|
|
|
// TODO Auto‐generated catch block
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
System.out.println("Image generated."); |
|
|
|
@ -211,4 +223,31 @@ public class BattlefieldParameterEvaluator { |
|
|
|
|
System.out.println("Err> " + e.getError()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Returns a pseudo-random number between min and max, inclusive. The difference between min and max can be at most |
|
|
|
|
* |
|
|
|
|
* @param min |
|
|
|
|
* Minimum value |
|
|
|
|
* @param max |
|
|
|
|
* Maximum value. Must be greater than min. |
|
|
|
|
* @return Double between min and max, inclusive. |
|
|
|
|
* @see java.util.Random#nextDouble(int) |
|
|
|
|
*/ |
|
|
|
|
private static double rangedRand(Random rng, double min, double max) { |
|
|
|
|
|
|
|
|
|
// Make sure the randomizer was initialised
|
|
|
|
|
if (rng == null) |
|
|
|
|
throw new RuntimeException("PRNG not initialised prior to call"); |
|
|
|
|
|
|
|
|
|
// Check for the limit
|
|
|
|
|
if (Double.valueOf(max - min).isInfinite()) |
|
|
|
|
throw new RuntimeException("Value od max - min is infinite. Change the range."); |
|
|
|
|
|
|
|
|
|
// nextInt is normally exclusive of the top value, so add 1 to make it inclusive
|
|
|
|
|
double randomNum = min + (max - min) * rng.nextDouble(); |
|
|
|
|
// int randomNum = rng.nextDouble((max - min) + 1) + min;
|
|
|
|
|
|
|
|
|
|
return randomNum; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|