<commit_before>package keel.Algorithms.Discretizers.ecpsd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * <p>Title: ECPSD </p>
 *
 * <p>Description: It contains the implementation of the CHC multivariate discretizer with a historical of cut points.
 * It makes a faster convergence of the algorithm CHC_MV. </p>
 * 
 * <p>Company: KEEL </p>
 *
 * @author Written by <NAME> (University of Granada) (10/01/2014)
 * @version 1.5
 * @since JDK1.5
 */

public class EMD {
	
	private long seed;
	private float[][] cut_points;
	private float[][] original_cut_points;
	private float[][] dataset;
	private Chromosome initial_chr;
	private Chromosome best;
	
	private ArrayList <Chromosome> population;
	
	private int max_cut_points;
	private int n_cut_points;
	
	private int max_eval;
	private int n_eval;
	
	private int pop_length;
	private int nClasses;
	
	private float threshold;
	private float r;
	private float alpha, beta;
	private float best_fitness;

	private float prob1to0Rec;
	private float prob1to0Div;  
	
	private float pEvaluationsForReduction; 
	private float pReduction;

	private int n_restart_not_improving;
	private static int PROPER_SIZE_CHROMOSOME = 1000;
    
    /**
     * Creates a CHC object with its parameters
     * 
     * @param pop	Population of rules we want to select
     */
    public EMD (long seed, float[][] dataset, float [][] cut_points, int eval, int popLength, 
    		float restart_per, float alpha_fitness, float beta_fitness, float pr0to1Rec, 
    		float pr0to1Div, float pEvaluationsForReduction, float pReduction, int nClasses, boolean[] initial_chr) {
    	
    	this.seed = seed;
    	this.dataset = dataset;
    	this.cut_points = cut_points;
    	this.original_cut_points = cut_points.clone();
    	max_eval = eval;
    	pop_length = popLength;
    	r = restart_per;
    	alpha = alpha_fitness;
    	beta = beta_fitness;
    	prob1to0Rec = pr0to1Rec;
    	prob1to0Div = pr0to1Div; 
    	this.pEvaluationsForReduction = pEvaluationsForReduction;
    	this.pReduction = pReduction;
    	this.nClasses = nClasses;
    	
    	max_cut_points = 0;
    	for (int i=0; i< cut_points.length; i++) {
    		if (cut_points[i] != null) {
    			if(!isAscendingSorted(cut_points[i]))
    					throw new ExceptionInInitializerError("Cut points must be sorted");
    			max_cut_points += cut_points[i].length;
    		}
    			
    	}
    	n_cut_points = max_cut_points;
    	
    	population = new ArrayList <Chromosome> (pop_length);
    	best_fitness = 100f;
    	if(initial_chr == null) {
    		this.initial_chr = new Chromosome (initial_chr);
    	} else {
    		if(initial_chr.length == max_cut_points)
        		this.initial_chr = new Chromosome (initial_chr);
        	else 
        		this.initial_chr = new Chromosome (n_cut_points, true);
    	}
    	
    }
    
    public EMD (float[][] current_dataset, float [][] cut_points, int nClasses) {    	
    	this(964534618L, current_dataset, cut_points, 
    			1000, 50, .8f, .7f, .3f, .25f, .05f, .1f, .5f, nClasses, null);
    }
    
    public EMD (float[][] current_dataset, float [][] cut_points, boolean[] initial_chr, int nClasses) {
    	this(964534618L, current_dataset, cut_points, 
    			1000, 50, .8f, .7f, .3f, .25f, .05f, .1f, .5f, nClasses, initial_chr);
    }
    
    /**
     * Run the CHC algorithm for the data in this population
     * <commit_msg>Fixed bug in the initial chromosome (sequential discretizer)<commit_after>package keel.Algorithms.Discretizers.ecpsd;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;

/**
 * <p>Title: ECPSD </p>
 *
 * <p>Description: It contains the implementation of the CHC multivariate discretizer with a historical of cut points.
 * It makes a faster convergence of the algorithm CHC_MV. </p>
 * 
 * <p>Company: KEEL </p>
 *
 * @author Written by <NAME> (University of Granada) (10/01/2014)
 * @version 1.5
 * @since JDK1.5
 */

public class EMD {
	
	private long seed;
	private float[][] cut_points;
	private float[][] original_cut_points;
	private float[][] dataset;
	private Chromosome initial_chr;
	private Chromosome best;
	
	private ArrayList <Chromosome> population;
	
	private int max_cut_points;
	private int n_cut_points;
	
	private int max_eval;
	private int n_eval;
	
	private int pop_length;
	private int nClasses;
	
	private float threshold;
	private float r;
	private float alpha, beta;
	private float best_fitness;

	private float prob1to0Rec;
	private float prob1to0Div;  
	
	private float pEvaluationsForReduction; 
	private float pReduction;

	private int n_restart_not_improving;
	private static int PROPER_SIZE_CHROMOSOME = 1000;
    
    /**
     * Creates a CHC object with its parameters
     * 
     * @param pop	Population of rules we want to select
     */
    public EMD (long seed, float[][] dataset, float [][] cut_points, int eval, int popLength, 
    		float restart_per, float alpha_fitness, float beta_fitness, float pr0to1Rec, 
    		float pr0to1Div, float pEvaluationsForReduction, float pReduction, int nClasses, boolean[] initial_chr) {
    	
    	this.seed = seed;
    	this.dataset = dataset;
    	this.cut_points = cut_points;
    	this.original_cut_points = cut_points.clone();
    	max_eval = eval;
    	pop_length = popLength;
    	r = restart_per;
    	alpha = alpha_fitness;
    	beta = beta_fitness;
    	prob1to0Rec = pr0to1Rec;
    	prob1to0Div = pr0to1Div; 
    	this.pEvaluationsForReduction = pEvaluationsForReduction;
    	this.pReduction = pReduction;
    	this.nClasses = nClasses;
    	
    	max_cut_points = 0;
    	for (int i=0; i< cut_points.length; i++) {
    		if (cut_points[i] != null) {
    			if(!isAscendingSorted(cut_points[i]))
    					throw new ExceptionInInitializerError("Cut points must be sorted");
    			max_cut_points += cut_points[i].length;
    		}
    			
    	}
    	n_cut_points = max_cut_points;
    	
    	population = new ArrayList <Chromosome> (pop_length);
    	best_fitness = 100f;
    	if(initial_chr == null) {
    		this.initial_chr = new Chromosome (n_cut_points, true);
    	} else {
    		if(initial_chr.length == max_cut_points)
        		this.initial_chr = new Chromosome (initial_chr);
        	else 
        		this.initial_chr = new Chromosome (n_cut_points, true);
    	}
    	
    }
    
    public EMD (float[][] current_dataset, float [][] cut_points, int nClasses) {    	
    	this(964534618L, current_dataset, cut_points, 
    			1000, 50, .8f, .7f, .3f, .25f, .05f, .1f, .5f, nClasses, null);
    }
    
    public EMD (float[][] current_dataset, float [][] cut_points, boolean[] initial_chr, int nClasses) {
    	this(964534618L, current_dataset, cut_points, 
    			1000, 50, .8f, .7f, .3f, .25f, .05f, .1f, .5f, nClasses, initial_chr);
    }
    
    /**
     * Run the CHC algorithm for the data in this population
     * 
{'fraction_non_alphanumeric': 0.0993101465938488, 'fraction_numerical': 0.024432308134521415, 'mean_word_length': 3.1201894612196566, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1887362', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2385, 'n_words': 630}