@Test public void value() { PSO pso = new PSO(); MeasuredStoppingCondition maximumIterations = new MeasuredStoppingCondition(new Iterations(), new Maximum(), 100); pso.addStoppingCondition(maximumIterations); for (int i = 0; i < 10; i++) pso.performIteration(); PercentageComplete percentageComplete = new PercentageComplete(); Assert.assertEquals(0.1, percentageComplete.getValue(pso).doubleValue(), 0.001); }
@Test public void algorithmExecution() { NNDataTrainingProblem problem = new NNDataTrainingProblem(); problem.getDataTableBuilder().setDataReader(new ARFFFileReader()); problem.getDataTableBuilder().setSourceURL("library/src/test/resources/datasets/iris.arff"); problem.setTrainingSetPercentage(0.7); problem.setGeneralizationSetPercentage(0.3); problem .getNeuralNetwork() .getArchitecture() .setArchitectureBuilder(new CascadeArchitectureBuilder()); problem.getNeuralNetwork().setOperationVisitor(new CascadeVisitor()); problem .getNeuralNetwork() .getArchitecture() .getArchitectureBuilder() .addLayer(new LayerConfiguration(4)); problem .getNeuralNetwork() .getArchitecture() .getArchitectureBuilder() .addLayer(new LayerConfiguration(0)); problem .getNeuralNetwork() .getArchitecture() .getArchitectureBuilder() .addLayer(new LayerConfiguration(1)); problem .getNeuralNetwork() .getArchitecture() .getArchitectureBuilder() .getLayerBuilder() .setDomain("R(-3:3)"); problem.initialise(); PSO pso = new PSO(); pso.getInitialisationStrategy().setEntityType(new DynamicParticle()); pso.addStoppingCondition(new MeasuredStoppingCondition()); pso.setOptimisationProblem(problem); pso.performInitialisation(); CascadeNetworkExpansionReactionStrategy reaction = new CascadeNetworkExpansionReactionStrategy(); Assert.assertEquals(5, ((Vector) pso.getBestSolution().getPosition()).size()); Assert.assertEquals(5, problem.getNeuralNetwork().getWeights().size()); for (int i = 0; i < Topologies.getBestEntity(pso.getTopology()).getDimension(); ++i) { ((Vector) Topologies.getBestEntity(pso.getTopology()).getPosition()) .set(i, Real.valueOf(0.0)); ((Vector) Topologies.getBestEntity(pso.getTopology()).getVelocity()) .set(i, Real.valueOf(0.0)); ((Vector) Topologies.getBestEntity(pso.getTopology()).getBestPosition()) .set(i, Real.valueOf(0.0)); } reaction.performReaction(pso); Assert.assertEquals(11, ((Vector) pso.getBestSolution().getPosition()).size()); Assert.assertEquals(11, problem.getNeuralNetwork().getWeights().size()); Assert.assertEquals( Vector.of( Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getPosition()); Assert.assertEquals( Vector.of( Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getVelocity()); Assert.assertEquals( Vector.of( Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getBestPosition()); for (int i = 0; i < Topologies.getBestEntity(pso.getTopology()).getDimension(); ++i) { ((Vector) Topologies.getBestEntity(pso.getTopology()).getPosition()) .set(i, Real.valueOf(0.0)); ((Vector) Topologies.getBestEntity(pso.getTopology()).getVelocity()) .set(i, Real.valueOf(0.0)); ((Vector) Topologies.getBestEntity(pso.getTopology()).getBestPosition()) .set(i, Real.valueOf(0.0)); } reaction.performReaction(pso); Assert.assertEquals(18, ((Vector) pso.getBestSolution().getPosition()).size()); Assert.assertEquals(18, problem.getNeuralNetwork().getWeights().size()); Assert.assertEquals( Vector.of( 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getPosition()); Assert.assertEquals( Vector.of( 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getVelocity()); Assert.assertEquals( Vector.of( 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getBestPosition()); for (int i = 0; i < Topologies.getBestEntity(pso.getTopology()).getDimension(); ++i) { ((Vector) Topologies.getBestEntity(pso.getTopology()).getPosition()) .set(i, Real.valueOf(0.0)); ((Vector) Topologies.getBestEntity(pso.getTopology()).getVelocity()) .set(i, Real.valueOf(0.0)); ((Vector) Topologies.getBestEntity(pso.getTopology()).getBestPosition()) .set(i, Real.valueOf(0.0)); } reaction.performReaction(pso); Assert.assertEquals(26, ((Vector) pso.getBestSolution().getPosition()).size()); Assert.assertEquals(26, problem.getNeuralNetwork().getWeights().size()); Assert.assertEquals( Vector.of( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getPosition()); Assert.assertEquals( Vector.of( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getVelocity()); Assert.assertEquals( Vector.of( 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, Double.NaN, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.NaN), (Vector) Topologies.getBestEntity(pso.getTopology()).getBestPosition()); }