/**
   * Reinitialises all the output weights of a cascade network within a PSO.
   *
   * <p>{@inheritDoc}
   */
  @Override
  public void performReaction(E algorithm) {
    NNDataTrainingProblem problem = (NNDataTrainingProblem) algorithm.getOptimisationProblem();
    NeuralNetwork network = problem.getNeuralNetwork();

    int precedingLayersSize =
        network.getArchitecture().getArchitectureBuilder().getLayerConfigurations().get(0).getSize()
            + network
                .getArchitecture()
                .getArchitectureBuilder()
                .getLayerConfigurations()
                .get(1)
                .getSize();
    if (network.getArchitecture().getArchitectureBuilder().getLayerConfigurations().get(0).isBias())
      precedingLayersSize++;

    int outputLayerSize =
        network
            .getArchitecture()
            .getArchitectureBuilder()
            .getLayerConfigurations()
            .get(2)
            .getSize();
    int nrOfweightsToDo = precedingLayersSize * outputLayerSize;

    Topology<? extends Entity> entities = algorithm.getTopology();

    for (Entity entity : entities) {
      DynamicParticle particle = (DynamicParticle) entity;

      Vector position = (Vector) particle.getPosition();
      Vector velocity = (Vector) particle.getVelocity();
      for (int curElement = position.size() - nrOfweightsToDo;
          curElement < position.size();
          ++curElement) {
        ((Real) position.get(curElement)).randomize(randomGenerator);
        velocity.setReal(curElement, 0.0);
      }
    }
  }
  @Test
  public void testBuildArchitecture() {
    NeuralNetwork network = new NeuralNetwork();
    network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(5));
    network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(3));
    network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(3, false));
    network.getArchitecture().getArchitectureBuilder().addLayer(new LayerConfiguration(2));
    network.getArchitecture().getArchitectureBuilder().getLayerBuilder().setDomain("R(-3,3)");
    network.initialize();

    // assert num layers
    Assert.assertEquals(4, network.getArchitecture().getNumLayers());

    // assert fully connected
    Assert.assertEquals(36, network.getWeights().size());

    // assert biasses
    Assert.assertEquals(true, network.getArchitecture().getLayers().get(0).isBias());
    int layerSize = network.getArchitecture().getLayers().get(0).size();
    Assert.assertEquals(6, layerSize);
    Assert.assertEquals(
        -1,
        network.getArchitecture().getLayers().get(0).getNeuralInput(layerSize - 1),
        Maths.EPSILON);

    Assert.assertEquals(true, network.getArchitecture().getLayers().get(1).isBias());
    layerSize = network.getArchitecture().getLayers().get(1).size();
    Assert.assertEquals(4, layerSize);
    Assert.assertEquals(
        -1,
        network.getArchitecture().getLayers().get(1).getNeuralInput(layerSize - 1),
        Maths.EPSILON);

    Assert.assertEquals(false, network.getArchitecture().getLayers().get(2).isBias());
    layerSize = network.getArchitecture().getLayers().get(2).size();
    Assert.assertEquals(3, layerSize);

    Assert.assertEquals(false, network.getArchitecture().getLayers().get(3).isBias());
    layerSize = network.getArchitecture().getLayers().get(3).size();
    Assert.assertEquals(2, layerSize);
  }