public static void Apply(AlbaEncoding solution, int maxIterations,
                                 int lambda, int samples, IRandom random, IVRPProblemInstance problemInstance, ref double quality, out int evaluatedSolutions)
        {
            evaluatedSolutions = 0;

            for (int i = 0; i < maxIterations; i++)
            {
                AlbaLambdaInterchangeMove bestMove = null;
                foreach (AlbaLambdaInterchangeMove move in AlbaStochasticLambdaInterchangeMultiMoveGenerator.GenerateAllMoves(solution, problemInstance, lambda, samples, random))
                {
                    AlbaEncoding newSolution = solution.Clone() as AlbaEncoding;
                    AlbaLambdaInterchangeMoveMaker.Apply(newSolution, move);
                    double moveQuality =
                        problemInstance.Evaluate(newSolution).Quality;

                    evaluatedSolutions++;
                    if (moveQuality < quality || quality == -1)
                    {
                        quality  = moveQuality;
                        bestMove = move;
                    }
                }
                if (bestMove != null)
                {
                    AlbaLambdaInterchangeMoveMaker.Apply(solution, bestMove);
                }
            }
        }
Beispiel #2
0
        protected override AlbaEncoding Crossover(IRandom random, AlbaEncoding parent1, AlbaEncoding parent2)
        {
            //note - the inner crossover is called here and the result is converted to an alba representation
            //some refactoring should be done here in the future - the crossover operation should be called directly
            if (parent1.Length != parent2.Length)
            {
                return(parent1.Clone() as AlbaEncoding);
            }

            InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName;
            IAtomicOperation op = this.ExecutionContext.CreateOperation(
                InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);

            string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName;

            if (ExecutionContext.Scope.Variables.ContainsKey(childName))
            {
                Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation;
                ExecutionContext.Scope.Variables.Remove(childName);

                return(new AlbaEncoding(permutation, ProblemInstance));
            }
            else
            {
                return(null);
            }
        }
    protected override void Manipulate(IRandom random, AlbaEncoding individual) {
      AlbaEncoding original = (AlbaEncoding)individual.Clone();
      int cutIndex, insertIndex, number;

      int customer = random.Next(ProblemInstance.Cities.Value);
      cutIndex = FindCustomerLocation(customer, individual);

      insertIndex = random.Next(original.Length);
      number = original[cutIndex];

      int i = 0;  // index in new permutation
      int j = 0;  // index in old permutation
      while (i < original.Length) {
        if (j == cutIndex) {
          j++;
        }
        if (i == insertIndex) {
          individual[i] = number;
          i++;
        }
        if ((i < original.Length) && (j < original.Length)) {
          individual[i] = original[j];
          i++;
          j++;
        }
      }
    }
Beispiel #4
0
        protected override void Manipulate(IRandom random, AlbaEncoding individual)
        {
            AlbaEncoding original = (AlbaEncoding)individual.Clone();
            int          cutIndex, insertIndex, number;

            int customer = random.Next(ProblemInstance.Cities.Value);

            cutIndex = FindCustomerLocation(customer, individual);

            insertIndex = random.Next(original.Length);
            number      = original[cutIndex];

            int i = 0; // index in new permutation
            int j = 0; // index in old permutation

            while (i < original.Length)
            {
                if (j == cutIndex)
                {
                    j++;
                }
                if (i == insertIndex)
                {
                    individual[i] = number;
                    i++;
                }
                if ((i < original.Length) && (j < original.Length))
                {
                    individual[i] = original[j];
                    i++;
                    j++;
                }
            }
        }
    public AlbaLambdaInterchangeMove(int tour1, int position1, int length1,
      int tour2, int position2, int length2, AlbaEncoding permutation) {
      Tour1 = tour1;
      Position1 = position1;
      Length1 = length1;

      Tour2 = tour2;
      Position2 = position2;
      Length2 = length2;

      this.Individual = permutation.Clone() as AlbaEncoding;
    }
    public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
      int tour2Index, int position2, int length2) {
      List<Tour> tours = individual.GetTours();

      Tour tour1 = tours[tour1Index];
      int tour1Start = -1;
      for (int i = 0; i < individual.Length; i++) {
        if (individual[i] == tour1.Stops[0] - 1) {
          tour1Start = i;
          break;
        }
      }

      Tour tour2 = tours[tour2Index];
      int tour2Start = -1;
      for (int i = 0; i < individual.Length; i++) {
        if (individual[i] == tour2.Stops[0] - 1) {
          tour2Start = i;
          break;
        }
      }

      AlbaEncoding original = individual.Clone() as AlbaEncoding;
      int index = 0;

      int start1 = tour1Start + position1;
      int end1 = start1 + length1;

      int start2 = tour2Start + position2;
      int end2 = start2 + length2;

      for (int i = 0; i < original.Length; i++) {
        if (index == start1) {
          if (end2 - start2 == 0)
            index = end1;
          else
            index = start2;
        } else if (index == start2) {
          if (end1 - start1 == 0)
            index = end2;
          else
            index = start1;
        } else if (index == end1) {
          index = end2;
        } else if (index == end2) {
          index = end1;
        }

        individual[i] = original[index];

        index++;
      }
    }
Beispiel #7
0
        public AlbaLambdaInterchangeMove(int tour1, int position1, int length1,
                                         int tour2, int position2, int length2, AlbaEncoding permutation)
        {
            Tour1     = tour1;
            Position1 = position1;
            Length1   = length1;

            Tour2     = tour2;
            Position2 = position2;
            Length2   = length2;

            this.Individual = permutation.Clone() as AlbaEncoding;
        }
    protected override AlbaEncoding Crossover(IRandom random, AlbaEncoding parent1, AlbaEncoding parent2) {
      //note - the inner crossover is called here and the result is converted to an alba representation
      //some refactoring should be done here in the future - the crossover operation should be called directly
      if (parent1.Length != parent2.Length)
        return parent1.Clone() as AlbaEncoding;

      InnerCrossoverParameter.ActualValue.ParentsParameter.ActualName = ParentsParameter.ActualName;
      IAtomicOperation op = this.ExecutionContext.CreateOperation(
        InnerCrossoverParameter.ActualValue, this.ExecutionContext.Scope);
      op.Operator.Execute((IExecutionContext)op, CancellationToken);

      string childName = InnerCrossoverParameter.ActualValue.ChildParameter.ActualName;
      if (ExecutionContext.Scope.Variables.ContainsKey(childName)) {
        Permutation permutation = ExecutionContext.Scope.Variables[childName].Value as Permutation;
        ExecutionContext.Scope.Variables.Remove(childName);

        return new AlbaEncoding(permutation, ProblemInstance);
      } else
        return null;
    }
Beispiel #9
0
        public static void Apply(AlbaEncoding individual, int tour1Index, int position1, int length1,
                                 int tour2Index, int position2, int length2)
        {
            List <Tour> tours = individual.GetTours();

            Tour tour1      = tours[tour1Index];
            int  tour1Start = -1;

            for (int i = 0; i < individual.Length; i++)
            {
                if (individual[i] == tour1.Stops[0] - 1)
                {
                    tour1Start = i;
                    break;
                }
            }

            Tour tour2      = tours[tour2Index];
            int  tour2Start = -1;

            for (int i = 0; i < individual.Length; i++)
            {
                if (individual[i] == tour2.Stops[0] - 1)
                {
                    tour2Start = i;
                    break;
                }
            }

            AlbaEncoding original = individual.Clone() as AlbaEncoding;
            int          index    = 0;

            int start1 = tour1Start + position1;
            int end1   = start1 + length1;

            int start2 = tour2Start + position2;
            int end2   = start2 + length2;

            for (int i = 0; i < original.Length; i++)
            {
                if (index == start1)
                {
                    if (end2 - start2 == 0)
                    {
                        index = end1;
                    }
                    else
                    {
                        index = start2;
                    }
                }
                else if (index == start2)
                {
                    if (end1 - start1 == 0)
                    {
                        index = end2;
                    }
                    else
                    {
                        index = start1;
                    }
                }
                else if (index == end1)
                {
                    index = end2;
                }
                else if (index == end2)
                {
                    index = end1;
                }

                individual[i] = original[index];

                index++;
            }
        }
 public AlbaIntraRouteInversionMove(int index1, int index2, AlbaEncoding permutation)
     : base(index1, index2, null)
 {
     this.Permutation = permutation.Clone() as AlbaEncoding;
 }
 public AlbaTranslocationMove(int index1, int index2, int index3, AlbaEncoding individual) :
     base(index1, index2, index3, individual.Clone() as AlbaEncoding)
 {
 }
 public AlbaTranslocationMove(int index1, int index2, int index3, AlbaEncoding individual) :
   base(index1, index2, index3, individual.Clone() as AlbaEncoding) {
 }
    public static void Apply(AlbaEncoding solution, int maxIterations,
      int lambda, int samples, IRandom random, IVRPProblemInstance problemInstance, ref double quality, out int evaluatedSolutions) {
      evaluatedSolutions = 0;

      for (int i = 0; i < maxIterations; i++) {
        AlbaLambdaInterchangeMove bestMove = null;
        foreach (AlbaLambdaInterchangeMove move in AlbaStochasticLambdaInterchangeMultiMoveGenerator.GenerateAllMoves(solution, problemInstance, lambda, samples, random)) {
          AlbaEncoding newSolution = solution.Clone() as AlbaEncoding;
          AlbaLambdaInterchangeMoveMaker.Apply(newSolution, move);
          double moveQuality =
            problemInstance.Evaluate(newSolution).Quality;

          evaluatedSolutions++;
          if (moveQuality < quality || quality == -1) {
            quality = moveQuality;
            bestMove = move;
          }
        }
        if (bestMove != null)
          AlbaLambdaInterchangeMoveMaker.Apply(solution, bestMove);
      }
    }
 public AlbaIntraRouteInversionMove(int index1, int index2, AlbaEncoding permutation)
   : base(index1, index2, null) {
   this.Permutation = permutation.Clone() as AlbaEncoding;
 }