public static void Improve(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) { double evalSolPerMove = 4.0 / assignment.Length; for (int i = localIterations.Value; i < maxIterations; i++) { Swap2Move bestMove = null; double bestQuality = 0; // we have to make an improvement, so 0 is the baseline double evaluations = 0.0; foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) { double moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances); evaluations += evalSolPerMove; if (maximization && moveQuality > bestQuality || !maximization && moveQuality < bestQuality) { bestQuality = moveQuality; bestMove = move; } } evaluatedSolutions.Value += (int)Math.Ceiling(evaluations); if (bestMove == null) { break; } Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2); quality.Value += bestQuality; localIterations.Value++; cancellation.ThrowIfCancellationRequested(); } }
public static void ImproveFast(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) { Swap2Move bestMove = null; double evaluations = 0.0; var lastQuality = new double[assignment.Length, assignment.Length]; for (int i = localIterations.Value; i < maxIterations; i++) { double bestQuality = 0; // we have to make an improvement, so 0 is the baseline var lastMove = bestMove; bestMove = null; foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) { double moveQuality; if (lastMove == null) { moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances); evaluations += 4.0 / assignment.Length; } else { moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, lastQuality[move.Index1, move.Index2], weights, distances, lastMove); if (move.Index1 == lastMove.Index1 || move.Index2 == lastMove.Index1 || move.Index1 == lastMove.Index2 || move.Index2 == lastMove.Index2) { evaluations += 4.0 / assignment.Length; } else { evaluations += 2.0 / (assignment.Length * assignment.Length); } } lastQuality[move.Index1, move.Index2] = moveQuality; if (maximization && moveQuality > bestQuality || !maximization && moveQuality < bestQuality) { bestQuality = moveQuality; bestMove = move; } } if (bestMove == null) { break; } Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2); quality.Value += bestQuality; localIterations.Value++; if (cancellation.IsCancellationRequested) { evaluatedSolutions.Value += (int)Math.Round(evaluations); throw new OperationCanceledException(); } } evaluatedSolutions.Value += (int)Math.Round(evaluations); }
protected QAPSwap2MoveEvaluator(QAPSwap2MoveEvaluator original, Cloner cloner) : base(original, cloner) { }