<commit_before>package es.optsicom.lib.approx.improvement.movement;

import es.optsicom.lib.Instance;
import es.optsicom.lib.Solution;
import es.optsicom.lib.util.ArraysUtil;
import es.optsicom.lib.util.BestMode;
import es.optsicom.lib.util.Id;
import es.optsicom.lib.util.MathUtil;

public class TabuVariableTenureImprovementMethod<S extends Solution<I>, I extends Instance>
		extends MovementImprovementMethod<S, I> implements MovementManager {

	private Mode mode;

	private double bestIncrement;
	private Object movementAttributes;

	private BestMode bestMode;

	private float tabuTenure;
	private float maxIterWoImpr;
	private Object memory;

	private TabuProblemAdapter<S, I> tabuAdapter;

	private int numIteration;
	private int itersWoImpr;
	private S bestSolution;

	private TenureProblemAdapter<S, I> tenureAdapter;

	private double solutionWeight;

	public TabuVariableTenureImprovementMethod(MovementGenerator<S, I> movementGenerator,
			Mode mode, float maxIterWoImpr, float tabuTenure,
			TabuProblemAdapter<S, I> tabuAdapter,
			TenureProblemAdapter<S, I> tenureAdapter) {
		super(movementGenerator);
		this.mode = mode;
		this.tabuAdapter = tabuAdapter;
		this.tabuTenure = tabuTenure;
		this.tenureAdapter = tenureAdapter;
		this.maxIterWoImpr = maxIterWoImpr;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void moreInternalImproveSolution() {

		this.memory = tabuAdapter.createMemory(solution);
		int maxItersWoImprInt = tabuAdapter.getMaxItersWoImprInt(solution,
				maxIterWoImpr);

		bestMode = instance.getProblem().getMode();
		numIteration = 1;
		itersWoImpr = 0;

		bestSolution = (S) solution.createCopy();

		do {

			// System.out.println("NumIteration: "+numIteration);
			bestIncrement = 0;
			movementAttributes = null;
			solutionWeight = solution.getWeight();

			try {

				movementGenerator.generateMovements(this);
				applyMovement();

			} catch (FinishGeneratingMovementsException e) {
				applyMovement();
			}

			numIteration++;
			
			tenureAdapter.finishIteration(solution, numIteration, itersWoImpr);

			// if(solution.getWeight() != bestSolution.getWeight()){
			// System.out.println("NumIterations Wo Impr: "+itersWoImpr+" limit="+maxItersWoImprInt);
			// }

			if (itersWoImpr >= maxItersWoImprInt) {
				break;
			}

			checkFinishByTime();

		} while (true);

		System.out.println("Iterations: " + numIteration + "; ItersWoImp: " + itersWoImpr);
		
		solution.asSolution(bestSolution);
	}

	private void applyMovement() {

		//System.out.println("---- ApplyMovement: " + (solutionWeight + bestIncrement) + "("+ bestIncrement+") "+ArraysUtil.toStringObj(movementAttributes));
		
		boolean newBestSolutionFound = false;
		if (bestMode.isBetterThan(solutionWeight + bestIncrement,
				bestSolution.getWeight())) {

			improvement = true;
			itersWoImpr = 0;
			newBestSolutionFound = true;
			
			//System.out.println("BestSolFound");

		} else {
			itersWoImpr++;
		}		

		tabuAdapter.markAsTabu(memory, movementAttributes, numIteration,
				tenureAdapter.getTenure(solution, movementAttributes, numIteration, itersWoImpr));

		// if(!bestMode.isImprovement(bestIncrement)){
		// System.out.println(".");
		// }

		//double originalWeight = solutionWeight;
		movementGenerator.applyMovement(movementAttributes);
		
		//System.out.println("---- New Solution: " + solution.getWeight());
		
		// System.out.print(".");

		// System.out.print(solution.getWeight() + ", ");

		// TODO This can be optimized making the copy when the first
		// non-improving
		// movement is applied
		if (newBestSolutionFound) {
			//System.out.println("New solution found: "+solution.getWeight());
			bestSolution = (S) solution.createCopy();
		}

		// TODO Correct implementation test. We need to found a good way
		// to enable or disable this kind of thing in all code.
		// if (!MathUtil.efectiveEquals(originalWeight + bestIncrement,
		// solution.getWeight())) {
		// throw new RuntimeException(
		// "Applying the movement doesn't increment the solution value as expected. It should be "
		// + (originalWeight + bestIncrement)
		// + " and is "
		// + solution.getWeight());<commit_msg>Changed order of apply and markAsTabu to allow AdaptiveTenure<commit_after>package es.optsicom.lib.approx.improvement.movement;

import es.optsicom.lib.Instance;
import es.optsicom.lib.Solution;
import es.optsicom.lib.util.ArraysUtil;
import es.optsicom.lib.util.BestMode;
import es.optsicom.lib.util.Id;
import es.optsicom.lib.util.MathUtil;

public class TabuVariableTenureImprovementMethod<S extends Solution<I>, I extends Instance>
		extends MovementImprovementMethod<S, I> implements MovementManager {

	private Mode mode;

	private double bestIncrement;
	private Object movementAttributes;

	private BestMode bestMode;

	private float tabuTenure;
	private float maxIterWoImpr;
	private Object memory;

	private TabuProblemAdapter<S, I> tabuAdapter;

	private int numIteration;
	private int itersWoImpr;
	private S bestSolution;

	private TenureProblemAdapter<S, I> tenureAdapter;

	private double solutionWeight;

	public TabuVariableTenureImprovementMethod(MovementGenerator<S, I> movementGenerator,
			Mode mode, float maxIterWoImpr, float tabuTenure,
			TabuProblemAdapter<S, I> tabuAdapter,
			TenureProblemAdapter<S, I> tenureAdapter) {
		super(movementGenerator);
		this.mode = mode;
		this.tabuAdapter = tabuAdapter;
		this.tabuTenure = tabuTenure;
		this.tenureAdapter = tenureAdapter;
		this.maxIterWoImpr = maxIterWoImpr;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void moreInternalImproveSolution() {

		this.memory = tabuAdapter.createMemory(solution);
		int maxItersWoImprInt = tabuAdapter.getMaxItersWoImprInt(solution,
				maxIterWoImpr);

		bestMode = instance.getProblem().getMode();
		numIteration = 1;
		itersWoImpr = 0;

		bestSolution = (S) solution.createCopy();

		do {

			// System.out.println("NumIteration: "+numIteration);
			bestIncrement = 0;
			movementAttributes = null;
			solutionWeight = solution.getWeight();

			try {

				movementGenerator.generateMovements(this);
				applyMovement();

			} catch (FinishGeneratingMovementsException e) {
				applyMovement();
			}

			numIteration++;
			
			tenureAdapter.finishIteration(solution, numIteration, itersWoImpr);

			// if(solution.getWeight() != bestSolution.getWeight()){
			// System.out.println("NumIterations Wo Impr: "+itersWoImpr+" limit="+maxItersWoImprInt);
			// }

			if (itersWoImpr >= maxItersWoImprInt) {
				break;
			}

			checkFinishByTime();

		} while (true);

		System.out.println("Iterations: " + numIteration + "; ItersWoImp: " + itersWoImpr);
		
		solution.asSolution(bestSolution);
	}

	private void applyMovement() {

		//System.out.println("---- ApplyMovement: " + (solutionWeight + bestIncrement) + "("+ bestIncrement+") "+ArraysUtil.toStringObj(movementAttributes));
		
		boolean newBestSolutionFound = false;
		if (bestMode.isBetterThan(solutionWeight + bestIncrement,
				bestSolution.getWeight())) {

			improvement = true;
			itersWoImpr = 0;
			newBestSolutionFound = true;
			
			//System.out.println("BestSolFound");

		} else {
			itersWoImpr++;
		}		

		// if(!bestMode.isImprovement(bestIncrement)){
		// System.out.println(".");
		// }

		//double originalWeight = solutionWeight;
		movementGenerator.applyMovement(movementAttributes);
		
		tabuAdapter.markAsTabu(memory, movementAttributes, numIteration,
				tenureAdapter.getTenure(solution, movementAttributes, numIteration, itersWoImpr));

		//System.out.println("---- New Solution: " + solution.getWeight());
		
		// System.out.print(".");

		// System.out.print(solution.getWeight() + ", ");

		// TODO This can be optimized making the copy when the first
		// non-improving
		// movement is applied
		if (newBestSolutionFound) {
			//System.out.println("New solution found: "+solution.getWeight());
			bestSolution = (S) solution.createCopy();
		}

		// TODO Correct implementation test. We need to found a good way
		// to enable or disable this kind of thing in all code.
		// if (!MathUtil.efectiveEquals(originalWeight + bestIncrement,
		// solution.getWeight())) {
		// throw new RuntimeException(
		// "Applying the movement doesn't increment the solution value as expected. It should be "
		// + (originalWeight + bestIncrement)
		// + " and is "
		// + solution.getWeight());
{'fraction_non_alphanumeric': 0.11727950012016342, 'fraction_numerical': 0.0009613073780341264, 'mean_word_length': 4.34553628773282, 'pattern_counts': {'":': 0, '<': 19, '<?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': '5331639', 'n_tokens_mistral': 3030, 'n_tokens_neox': 2610, 'n_words': 545}