Ejemplo n.º 1
1
 private TwoPointFiveMove(TwoPointFiveMove original, Cloner cloner)
   : base(original, cloner) {
   this.Index1 = original.Index1;
   this.Index2 = original.Index2;
   this.IsInvert = original.IsInvert;
   this.Permutation = cloner.Clone(original.Permutation);
 }
        public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, DoubleArray probabilities, CancellationToken cancellation)
        {
            var distanceM = (DistanceMatrix)distances;
            Func <int, int, double> distance = (a, b) => distanceM[a, b];

            for (var i = localIterations.Value; i < maxIterations; i++)
            {
                TwoPointFiveMove bestMove = null;
                var bestQuality           = quality.Value; // we have to make an improvement, so current quality is the baseline
                var evaluations           = 0.0;
                foreach (var move in ExhaustiveTwoPointFiveMoveGenerator.Generate(assignment))
                {
                    var moveQuality = PTSPAnalyticalTwoPointFiveMoveEvaluator.EvaluateMove(assignment, move, distance, probabilities);
                    evaluations++;
                    if (maximization && moveQuality > bestQuality ||
                        !maximization && moveQuality < bestQuality)
                    {
                        bestQuality = moveQuality;
                        bestMove    = move;
                    }
                }
                evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
                if (bestMove == null)
                {
                    break;
                }
                TwoPointFiveMoveMaker.Apply(assignment, bestMove);
                quality.Value = bestQuality;
                localIterations.Value++;
                cancellation.ThrowIfCancellationRequested();
            }
        }
 public static TwoPointFiveMove[] Apply(Permutation permutation, IRandom random, int sampleSize) {
   var moves = new TwoPointFiveMove[sampleSize];
   for (var i = 0; i < sampleSize; i++) {
     moves[i] = StochasticTwoPointFiveSingleMoveGenerator.Apply(permutation, random);
   }
   return moves;
 }
Ejemplo n.º 4
0
 public static void Apply(Permutation permutation, TwoPointFiveMove move) {
   if (move.IsInvert) {
     InversionManipulator.Apply(permutation, move.Index1, move.Index2);
   } else {
     TranslocationManipulator.Apply(permutation, move.Index1, move.Index1, move.Index2);
   }
 }
 public static TwoPointFiveMove[] Apply(Permutation permutation, IRandom random, int sampleSize) {
   var moves = new TwoPointFiveMove[sampleSize];
   for (var i = 0; i < sampleSize; i++) {
     moves[i] = StochasticTwoPointFiveSingleMoveGenerator.Apply(permutation, random);
   }
   return moves;
 }
Ejemplo n.º 6
0
 private TwoPointFiveMove(TwoPointFiveMove original, Cloner cloner)
     : base(original, cloner)
 {
     this.Index1      = original.Index1;
     this.Index2      = original.Index2;
     this.IsInvert    = original.IsInvert;
     this.Permutation = cloner.Clone(original.Permutation);
 }
 public static double EvaluateMove(Permutation permutation, TwoPointFiveMove move, Func<int, int, double> distance, ItemList<BoolArray> realizations) {
   if (move.IsInvert) {
     return PTSPEstimatedInversionMoveEvaluator.EvaluateMove(permutation,
       new InversionMove(move.Index1, move.Index2, move.Permutation), distance, realizations);
   } else {
     return PTSPEstimatedInsertionMoveEvaluator.EvaluateMove(permutation,
       new TranslocationMove(move.Index1, move.Index1, move.Index2), distance, realizations);
   }
 }
 public static double EvaluateMove(Permutation permutation, TwoPointFiveMove move, Func<int, int, double> distance, DoubleArray probabilities) {
   if (move.IsInvert) {
     return PTSPAnalyticalInversionMoveEvaluator.EvaluateMove(permutation,
       new InversionMove(move.Index1, move.Index2, move.Permutation), distance, probabilities);
   } else {
     return PTSPAnalyticalInsertionMoveEvaluator.EvaluateMove(permutation,
       new TranslocationMove(move.Index1, move.Index1, move.Index2), distance, probabilities);
   }
 }
Ejemplo n.º 9
0
 public static void Apply(Permutation permutation, TwoPointFiveMove move)
 {
     if (move.IsInvert)
     {
         InversionManipulator.Apply(permutation, move.Index1, move.Index2);
     }
     else
     {
         TranslocationManipulator.Apply(permutation, move.Index1, move.Index1, move.Index2);
     }
 }
Ejemplo n.º 10
0
 public static double EvaluateMove(Permutation permutation, TwoPointFiveMove move, Func <int, int, double> distance, DoubleArray probabilities)
 {
     if (move.IsInvert)
     {
         return(PTSPAnalyticalInversionMoveEvaluator.EvaluateMove(permutation,
                                                                  new InversionMove(move.Index1, move.Index2, move.Permutation), distance, probabilities));
     }
     else
     {
         return(PTSPAnalyticalInsertionMoveEvaluator.EvaluateMove(permutation,
                                                                  new TranslocationMove(move.Index1, move.Index1, move.Index2), distance, probabilities));
     }
 }
Ejemplo n.º 11
0
 public static double EvaluateMove(Permutation permutation, TwoPointFiveMove move, Func <int, int, double> distance, ItemList <BoolArray> realizations)
 {
     if (move.IsInvert)
     {
         return(PTSPEstimatedInversionMoveEvaluator.EvaluateMove(permutation,
                                                                 new InversionMove(move.Index1, move.Index2, move.Permutation), distance, realizations));
     }
     else
     {
         return(PTSPEstimatedInsertionMoveEvaluator.EvaluateMove(permutation,
                                                                 new TranslocationMove(move.Index1, move.Index1, move.Index2), distance, realizations));
     }
 }
Ejemplo n.º 12
0
        public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, ItemList <BoolArray> realizations, CancellationToken cancellation)
        {
            var distanceM = (DistanceMatrix)distances;
            Func <int, int, double> distance = (a, b) => distanceM[a, b];

            for (var i = localIterations.Value; i < maxIterations; i++)
            {
                TwoPointFiveMove bestMove = null;
                var bestQuality           = 0.0; // we have to make an improvement, so 0 is the baseline
                var evaluations           = 0.0;
                foreach (var move in ExhaustiveTwoPointFiveMoveGenerator.Generate(assignment))
                {
                    var moveQuality = PTSPEstimatedTwoPointFiveMoveEvaluator.EvaluateMove(assignment, move, distance, realizations);
                    if (move.IsInvert)
                    {
                        evaluations += realizations.Count * 4.0 / (assignment.Length * assignment.Length);
                    }
                    else
                    {
                        evaluations += realizations.Count * 6.0 / (assignment.Length * assignment.Length);
                    }
                    if (maximization && moveQuality > bestQuality ||
                        !maximization && moveQuality < bestQuality)
                    {
                        bestQuality = moveQuality;
                        bestMove    = move;
                    }
                }
                evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
                if (bestMove == null)
                {
                    break;
                }
                TwoPointFiveMoveMaker.Apply(assignment, bestMove);
                quality.Value += bestQuality;
                localIterations.Value++;
                cancellation.ThrowIfCancellationRequested();
            }
        }