<commit_before>package org.genericsystem.cv.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Ransac<DATA> {

	private final List<DATA> datas;
	private final int n;
	private final int k;
	private Model<DATA> bestModel;
	private Map<Integer, DATA> bestDataMap;
	private double bestError = Double.MAX_VALUE;
	private double t;
	private int d;
	private final Function<Collection<DATA>, Model<DATA>> modelProvider;

	public Ransac(List<DATA> datas, Function<Collection<DATA>, Model<DATA>> modelProvider, int n, int k, double t, int d) {
		if (n > datas.size())
			throw new IllegalStateException("n parameter must be inferior or equal to data size");
		if (d > datas.size())
			throw new IllegalStateException("d parameter must be inferior or equal to data size");
		if (n > d)
			throw new IllegalStateException("d parameter must be superior or equal to n parameter");
		assert n < datas.size();
		assert n <= d;
		this.datas = datas;
		this.t = t;
		this.n = n;
		this.k = k;
		this.d = d;
		this.modelProvider = modelProvider;
		compute();
	}

	private void compute() {
		for (int i = 0; i < k; i++) {
			Map<Integer, DATA> randomDataMap = new HashMap<>();
			for (int j = 0; j < n;) {
				int random = Double.valueOf(Math.floor(Math.random() * datas.size())).intValue();
				if (randomDataMap.put(random, datas.get(random)) == null)
					j++;
			}
			Model<DATA> possibleModel = modelProvider.apply(randomDataMap.values());
			for (int pt = 0; pt < datas.size(); pt++)
				if (!randomDataMap.containsKey(pt))
					if (possibleModel.computeError(datas.get(pt)) < t)
						randomDataMap.put(pt, datas.get(pt));

			if (randomDataMap.size() >= d) {
				possibleModel = modelProvider.apply(randomDataMap.values());
				double erreur = possibleModel.computeGlobalError(datas, randomDataMap.values());
				if (erreur < bestError) {
					bestModel = possibleModel;
					bestDataMap = randomDataMap;
					bestError = erreur;
					// System.out.println("Improved error : " + bestError);
					if (bestError == 0)
						break;
				}
			}
		}
		if (bestModel == null)
			throw new IllegalStateException("Unable to find a good model. Please, check your parameters n = " + n + ", t = " + t + ", d = " + d);
	}

	public Model<DATA> getBestModel() {
		return bestModel;
	}

	public double getBestError() {
		return bestError;
	}

	public Map<Integer, DATA> getBestDataSet() {
		return bestDataMap;
	}

	public static interface Model<DATA> {<commit_msg>gs-cv: Ransac: cosmetic<commit_after>package org.genericsystem.cv.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

public class Ransac<DATA> {

	private final List<DATA> datas;
	private final int n;
	private final int k;
	private Model<DATA> bestModel;
	private Map<Integer, DATA> bestDataMap;
	private double bestError = Double.MAX_VALUE;
	private double t;
	private int d;
	private final Function<Collection<DATA>, Model<DATA>> modelProvider;

	public Ransac(List<DATA> datas, Function<Collection<DATA>, Model<DATA>> modelProvider, int n, int k, double t, int d) {
		if (n > datas.size())
			throw new IllegalStateException("n parameter must be inferior or equal to data size");
		if (d > datas.size())
			throw new IllegalStateException("d parameter must be inferior or equal to data size");
		if (n > d)
			throw new IllegalStateException("d parameter must be superior or equal to n parameter");
		assert n < datas.size();
		assert n <= d;
		this.datas = datas;
		this.t = t;
		this.n = n;
		this.k = k;
		this.d = d;
		this.modelProvider = modelProvider;
		compute();
	}

	private void compute() {
		for (int i = 0; i < k; i++) {
			Map<Integer, DATA> randomDataMap = new HashMap<>();
			for (int j = 0; j < n;) {
				int random = Double.valueOf(Math.floor(Math.random() * datas.size())).intValue();
				if (randomDataMap.put(random, datas.get(random)) == null)
					j++;
			}
			Model<DATA> possibleModel = modelProvider.apply(randomDataMap.values());
			for (int pt = 0; pt < datas.size(); pt++)
				if (!randomDataMap.containsKey(pt))
					if (possibleModel.computeError(datas.get(pt)) < t)
						randomDataMap.put(pt, datas.get(pt));

			if (randomDataMap.size() >= d) {
				possibleModel = modelProvider.apply(randomDataMap.values());
				double error = possibleModel.computeGlobalError(datas, randomDataMap.values());
				if (error < bestError) {
					bestModel = possibleModel;
					bestDataMap = randomDataMap;
					bestError = error;
					// System.out.println("Improved error : " + bestError);
					if (bestError == 0)
						break;
				}
			}
		}
		if (bestModel == null)
			throw new IllegalStateException("Unable to find a good model. Please, check your parameters n = " + n + ", t = " + t + ", d = " + d);
	}

	public Model<DATA> getBestModel() {
		return bestModel;
	}

	public double getBestError() {
		return bestError;
	}

	public Map<Integer, DATA> getBestDataSet() {
		return bestDataMap;
	}

	public static interface Model<DATA> {
{'fraction_non_alphanumeric': 0.13236151603498542, 'fraction_numerical': 0.001554907677356657, 'mean_word_length': 3.782527881040892, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 45, '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': '2493693', 'n_tokens_mistral': 1881, 'n_tokens_neox': 1637, 'n_words': 486}