<commit_before>/**
 * Computational Intelligence Library (CIlib)
 * Copyright (C) 2003 - 2010
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
package net.sourceforge.cilib.pso.crossover;

import fj.P;
import fj.P3;
import java.util.List;
import net.sourceforge.cilib.controlparameter.ConstantControlParameter;
import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.EntityType;
import net.sourceforge.cilib.entity.Particle;
import net.sourceforge.cilib.entity.Topology;
import net.sourceforge.cilib.entity.operators.crossover.CrossoverStrategy;
import net.sourceforge.cilib.entity.operators.crossover.ParentCentricCrossoverStrategy;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.pso.PSO;
import net.sourceforge.cilib.util.Cloneable;
import net.sourceforge.cilib.util.selection.Samples;
import net.sourceforge.cilib.util.selection.recipes.RandomSelector;
import net.sourceforge.cilib.util.selection.recipes.Selector;

/**
 * An operation used in the PSOCrossoverIterationStrategy which is responsible for performing the crossover and
 * performing other actions depending on the outcome of the crossover.
 */
public abstract class CrossoverSelection implements Cloneable {

    private CrossoverStrategy crossoverStrategy;
    private Selector selector;
    private ControlParameter numberOfParents;
    private ParticleProvider particleProvider;

    private enum TempEnums {
        TEMP
    };

    public CrossoverSelection() {
        this.crossoverStrategy = new ParentCentricCrossoverStrategy();
        this.selector = new RandomSelector();
        this.numberOfParents = ConstantControlParameter.of(3);
        this.particleProvider = new WorstParentParticleProvider();
    }

    public CrossoverSelection(CrossoverSelection copy) {
        this.crossoverStrategy = copy.crossoverStrategy.getClone();
        this.selector = copy.selector;
        this.numberOfParents = copy.numberOfParents.getClone();
        this.particleProvider = copy.particleProvider;
    }

    public P3<Boolean, Particle, Particle> select(PSO algorithm, Enum solutionType, Enum fitnessType) {
        boolean isBetter = false;
        Topology<Particle> topology = algorithm.getTopology();

        // get random particles
        List<Entity> parents = selector.on(topology).select(Samples.first((int) numberOfParents.getParameter()).unique());

        //put pbest as candidate solution for the crossover
        for (Entity e : parents) {
            Particle p = (Particle) e;
            e.getProperties().put(TempEnums.TEMP, p.getCandidateSolution());
            e.getProperties().put(EntityType.CANDIDATE_SOLUTION, e.getProperties().get(solutionType));
        }

        //perform crossover and compute offspring's fitness
        Particle offspring = (Particle) crossoverStrategy.crossover(parents).get(0);
        offspring.calculateFitness();

        Particle selectedParticle = particleProvider.get(parents);

        //replace selectedEntity if offspring is better
        if (((Fitness) offspring.getProperties().get(fitnessType)).compareTo((Fitness) selectedParticle.getProperties().get(fitnessType)) > 0) {
            isBetter = true;
        }

        // revert solutions
        for (Entity e : parents) {
            e.getProperties().put(EntityType.CANDIDATE_SOLUTION, e.getProperties().get(TempEnums.TEMP));
        }

        return P.p(isBetter, selectedParticle, offspring);
    }

    public void setNumberOfParents(ControlParameter numberOfParents) {
        this.numberOfParents = numberOfParents;
    }

    public ControlParameter getNumberOfParents() {
        return numberOfParents;
    }

    public void setCrossoverStrategy(CrossoverStrategy crossoverStrategy) {
        this.crossoverStrategy = crossoverStrategy;
    }

    public CrossoverStrategy getCrossoverStrategy() {<commit_msg>Removed temp enum and placed entites in a map with their stored candidate solutions.<commit_after>/**
 * Computational Intelligence Library (CIlib)
 * Copyright (C) 2003 - 2010
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
package net.sourceforge.cilib.pso.crossover;

import com.google.common.collect.Maps;
import fj.P;
import fj.P3;
import java.util.List;
import java.util.Map;
import net.sourceforge.cilib.controlparameter.ConstantControlParameter;
import net.sourceforge.cilib.controlparameter.ControlParameter;
import net.sourceforge.cilib.entity.Entity;
import net.sourceforge.cilib.entity.EntityType;
import net.sourceforge.cilib.entity.Particle;
import net.sourceforge.cilib.entity.Topology;
import net.sourceforge.cilib.entity.operators.crossover.CrossoverStrategy;
import net.sourceforge.cilib.entity.operators.crossover.ParentCentricCrossoverStrategy;
import net.sourceforge.cilib.problem.Fitness;
import net.sourceforge.cilib.pso.PSO;
import net.sourceforge.cilib.type.types.container.StructuredType;
import net.sourceforge.cilib.util.Cloneable;
import net.sourceforge.cilib.util.selection.Samples;
import net.sourceforge.cilib.util.selection.recipes.RandomSelector;
import net.sourceforge.cilib.util.selection.recipes.Selector;

/**
 * An operation used in the PSOCrossoverIterationStrategy which is responsible for performing the crossover and
 * performing other actions depending on the outcome of the crossover.
 */
public abstract class CrossoverSelection implements Cloneable {

    private CrossoverStrategy crossoverStrategy;
    private Selector selector;
    private ControlParameter numberOfParents;
    private ParticleProvider particleProvider;

    public CrossoverSelection() {
        this.crossoverStrategy = new ParentCentricCrossoverStrategy();
        this.selector = new RandomSelector();
        this.numberOfParents = ConstantControlParameter.of(3);
        this.particleProvider = new WorstParentParticleProvider();
    }

    public CrossoverSelection(CrossoverSelection copy) {
        this.crossoverStrategy = copy.crossoverStrategy.getClone();
        this.selector = copy.selector;
        this.numberOfParents = copy.numberOfParents.getClone();
        this.particleProvider = copy.particleProvider;
    }

    public P3<Boolean, Particle, Particle> select(PSO algorithm, Enum solutionType, Enum fitnessType) {
        boolean isBetter = false;
        Topology<Particle> topology = algorithm.getTopology();
	Map<Entity, StructuredType> tmp = Maps.newHashMap();

        // get random particles
        List<Entity> parents = selector.on(topology).select(Samples.first((int) numberOfParents.getParameter()).unique());

        //put pbest as candidate solution for the crossover
        for (Entity e : parents) {
            tmp.put(e, e.getCandidateSolution());
            e.getProperties().put(EntityType.CANDIDATE_SOLUTION, e.getProperties().get(solutionType));
        }

        //perform crossover and compute offspring's fitness
        Particle offspring = (Particle) crossoverStrategy.crossover(parents).get(0);
        offspring.calculateFitness();

        Particle selectedParticle = particleProvider.get(parents);

        //replace selectedEntity if offspring is better
        if (((Fitness) offspring.getProperties().get(fitnessType)).compareTo((Fitness) selectedParticle.getProperties().get(fitnessType)) > 0) {
            isBetter = true;
        }

        // revert solutions
        for (Entity e : parents) {
	    e.setCandidateSolution(tmp.get(e));
        }

        return P.p(isBetter, selectedParticle, offspring);
    }

    public void setNumberOfParents(ControlParameter numberOfParents) {
        this.numberOfParents = numberOfParents;
    }

    public ControlParameter getNumberOfParents() {
        return numberOfParents;
    }

    public void setCrossoverStrategy(CrossoverStrategy crossoverStrategy) {
        this.crossoverStrategy = crossoverStrategy;
    }

    public CrossoverStrategy getCrossoverStrategy() {
{'fraction_non_alphanumeric': 0.08493963143401821, 'fraction_numerical': 0.002965473416649015, 'mean_word_length': 3.918229166666667, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5529513', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2474, 'n_words': 680}