/**
   * After every {@link #interval} iteration, pick {@link #numberOfSentries a number of} random
   * entities from the given {@link Algorithm algorithm's} topology and compare their previous
   * fitness values with their current fitness values. An environment change is detected when the
   * difference between the previous and current fitness values are >= the specified {@link
   * #epsilon} value.
   *
   * @param algorithm used to get hold of topology of entities and number of iterations
   * @return true if a change has been detected, false otherwise
   */
  @Override
  public <A extends HasTopology & Algorithm & HasNeighbourhood> boolean detect(A algorithm) {
    if ((AbstractAlgorithm.get().getIterations() % interval == 0)
        && (AbstractAlgorithm.get().getIterations() != 0)) {
      List all = Java.List_ArrayList().f(algorithm.getTopology());

      for (int i = 0; i < numberOfSentries.getParameter(); i++) {
        // select random sentry entity
        int random = Rand.nextInt(all.size());
        StandardParticle sentry = (StandardParticle) all.get(random);

        // check for change
        // double previousFitness = sentry.getFitness().getValue();

        boolean detectedChange = false;

        if (sentry.getFitness().getClass().getName().matches("MinimisationFitness")) {
          Fitness previousFitness = sentry.getFitness();
          sentry.calculateFitness();
          Fitness currentFitness = sentry.getFitness();

          if (Math.abs(previousFitness.getValue() - currentFitness.getValue()) >= epsilon) {
            detectedChange = true;
          }
        } else if (sentry.getFitness().getClass().getName().matches("StandardMOFitness")) {
          MOFitness previousFitness = (MOFitness) sentry.getFitness();
          sentry.calculateFitness();
          MOFitness currentFitness = (MOFitness) sentry.getFitness();

          for (int k = 0; k < previousFitness.getDimension(); k++)
            if (Math.abs(
                    previousFitness.getFitness(k).getValue()
                        - currentFitness.getFitness(k).getValue())
                >= epsilon) {
              detectedChange = true;
              break;
            }
        }
        if (detectedChange) {
          System.out.println("Detected a change");
          return true;
        }

        // remove the selected element from the all list preventing it from being selected again
        all.remove(random);
      }
    }
    return false;
  }
  /** {@inheritDoc} */
  @Override
  public int compare(E o1, E o2) {
    SinglePopulationBasedAlgorithm populationBasedAlgorithm =
        (SinglePopulationBasedAlgorithm) AbstractAlgorithm.getAlgorithmList().index(0);
    MOOptimisationProblem problem =
        ((MOOptimisationProblem) populationBasedAlgorithm.getOptimisationProblem());

    Particle p1 = (Particle) o1;
    Particle p2 = (Particle) o2;
    MOFitness fitness1 = ((MOFitness) problem.getFitness(p1.getBestPosition()));
    MOFitness fitness2 = ((MOFitness) problem.getFitness(p2.getBestPosition()));

    int value = fitness1.compareTo(fitness2);
    if (fitness1.compareTo(fitness2) == 0) {
      int random = Rand.nextInt(20);
      if (random > 10) value *= -1;
    }
    return value;
  }
  /**
   * This is an Synchronous strategy:
   *
   * <ol>
   *   <li>For all particles:
   *       <ol>
   *         <li>Update the particle velocity
   *         <li>Update the particle position
   *       </ol>
   *   <li>For all particles:
   *       <ol>
   *         <li>Calculate the particle fitness
   *         <li>For all particles in the current particle's neighbourhood:
   *             <ol>
   *               <li>Update the neighbourhood best
   *             </ol>
   *       </ol>
   * </ol>
   *
   * @see
   *     net.sourceforge.cilib.PSO.IterationStrategy#performIteration(net.sourceforge.cilib.PSO.PSO)
   * @param pso The {@link PSO} to have an iteration applied.
   */
  @Override
  public void performIteration(PSO pso) {
    Topology<Particle> topology = pso.getTopology();

    for (Particle current : topology) {
      current.updateVelocity();
      current.updatePosition(); // TODO: replace with visitor (will simplify particle interface)

      boundaryConstraint.enforce(current);
    }

    Problem problem = AbstractAlgorithm.getAlgorithmList().get(0).getOptimisationProblem();

    for (Particle current : topology) {
      current.calculateFitness();
      for (Particle other : topology.neighbourhood(current)) {
        Particle p1 = current.getNeighbourhoodBest().getClone();
        Particle p2 = other.getNeighbourhoodBest().getClone();
        OptimisationSolution s1 =
            new OptimisationSolution(
                p1.getCandidateSolution().getClone(),
                problem.getFitness(p1.getCandidateSolution().getClone()));
        OptimisationSolution s2 =
            new OptimisationSolution(
                p2.getCandidateSolution().getClone(),
                problem.getFitness(p2.getCandidateSolution().getClone()));
        MOFitness fitness1 = (MOFitness) s1.getFitness();
        MOFitness fitness2 = (MOFitness) s2.getFitness();
        //                System.out.println("fitness1 = ");
        //                for (int i=0; i < fitness1.getDimension(); i++)
        //                    System.out.println(fitness1.getFitness(i).getValue());
        //
        //                System.out.println("fitness2 = ");
        //                for (int i=0; i < fitness2.getDimension(); i++)
        //                    System.out.println(fitness2.getFitness(i).getValue());
        if (fitness1.compareTo(fitness2) > 0) {
          other.setNeighbourhoodBest(current);
        }
      }
    }
  }