Example #1
0
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            parent1 = parent1.Clone() as ZhuEncoding;
            parent2 = parent2.Clone() as ZhuEncoding;

            ZhuEncoding child = parent2.Clone() as ZhuEncoding;

            if (parent1.Length != parent2.Length)
            {
                return(child);
            }

            int breakPoint = random.Next(child.Length);
            int i          = breakPoint;

            DoubleArray dueTime = null;

            if (ProblemInstance is ITimeWindowedProblemInstance)
            {
                dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime;
            }

            do
            {
                if (i == breakPoint)
                {
                    child[i] = parent1[i];
                    Swap(parent2, parent2[i], parent1[i]);
                }
                else
                {
                    if (
                        (dueTime != null) &&
                        (dueTime[parent1[i] + 1] <
                         dueTime[parent2[i] + 1]))
                    {
                        child[i] = parent1[i];
                        Swap(parent2, parent2[i], parent1[i]);
                    }
                    else
                    {
                        child[i] = parent2[i];
                        Swap(parent1, parent1[i], parent2[i]);
                    }
                }

                i = (i + 1) % child.Length;
            } while (i != breakPoint);

            return(child);
        }
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            parent1 = parent1.Clone() as ZhuEncoding;
            parent2 = parent2.Clone() as ZhuEncoding;

            ZhuEncoding child = parent2.Clone() as ZhuEncoding;

            if (parent1.Length != parent2.Length)
            {
                return(child);
            }

            int breakPoint  = random.Next(child.Length);
            int i           = breakPoint;
            int predecessor = breakPoint - 1;

            if (predecessor < 0)
            {
                predecessor = predecessor + child.Length;
            }

            while (i != predecessor)
            {
                if (i == breakPoint)
                {
                    child[i] = parent1[i];
                    Swap(parent2, parent2[i], parent1[i]);
                }

                if (ProblemInstance.GetDistance(
                        child[i] + 1, parent1[(i + 1) % child.Length] + 1, child)
                    <
                    ProblemInstance.GetDistance(
                        child[i] + 1, parent2[(i + 1) % child.Length] + 1, child))
                {
                    child[(i + 1) % child.Length] = parent1[(i + 1) % child.Length];
                    Swap(parent2, parent2[(i + 1) % child.Length], parent1[(i + 1) % child.Length]);
                }
                else
                {
                    child[(i + 1) % child.Length] = parent2[(i + 1) % child.Length];
                    Swap(parent1, parent1[(i + 1) % child.Length], parent2[(i + 1) % child.Length]);
                }

                i = (i + 1) % child.Length;
            }

            return(child);
        }
Example #3
0
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            //note - the inner crossover is called here and the result is converted to a prins 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 ZhuEncoding);
            }

            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 ZhuEncoding(permutation, ProblemInstance));
            }
            else
            {
                return(null);
            }
        }
    protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
      List<int> p1 = new List<int>(parent1);
      List<int> p2 = new List<int>(parent2);

      ZhuEncoding child = parent2.Clone() as ZhuEncoding;

      if (parent1.Length != parent2.Length)
        return child;

      int breakPoint = random.Next(child.Length);
      int i = breakPoint;
      int predecessor = breakPoint - 1;
      if (predecessor < 0)
        predecessor = predecessor + child.Length;

      int parent1Index = i;
      int parent2Index = i;

      while (i != predecessor) {
        if (i == breakPoint) {
          child[i] = p1[parent1Index];

          p1.Remove(child[i]);
          if (parent1Index >= p1.Count)
            parent1Index = 0;

          p2.Remove(child[i]);
          if (parent2Index >= p2.Count)
            parent2Index = 0;
        }

        if (ProblemInstance.GetDistance(
         child[i] + 1, p1[parent1Index] + 1, child)
         <
         ProblemInstance.GetDistance(
         child[i] + 1, p2[parent2Index] + 1, child)) {
          child[(i + 1) % child.Length] = p1[parent1Index];
        } else {
          child[(i + 1) % child.Length] = p2[parent2Index];
        }

        p1.Remove(child[(i + 1) % child.Length]);
        if (parent1Index >= p1.Count)
          parent1Index = 0;

        p2.Remove(child[(i + 1) % child.Length]);
        if (parent2Index >= p2.Count)
          parent2Index = 0;

        i = (i + 1) % child.Length;
      }

      return child;
    }
    protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
      parent1 = parent1.Clone() as ZhuEncoding;
      parent2 = parent2.Clone() as ZhuEncoding;

      ZhuEncoding child = parent2.Clone() as ZhuEncoding;

      if (parent1.Length != parent2.Length)
        return child;

      int breakPoint = random.Next(child.Length);
      int i = breakPoint;

      DoubleArray dueTime = null;
      if (ProblemInstance is ITimeWindowedProblemInstance) {
        dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime;
      }

      do {
        if (i == breakPoint) {
          child[i] = parent1[i];
          Swap(parent2, parent2[i], parent1[i]);
        } else {
          if (
            (dueTime != null) &&
            (dueTime[parent1[i] + 1] <
             dueTime[parent2[i] + 1])) {
            child[i] = parent1[i];
            Swap(parent2, parent2[i], parent1[i]);
          } else {
            child[i] = parent2[i];
            Swap(parent1, parent1[i], parent2[i]);
          }
        }

        i = (i + 1) % child.Length;
      } while (i != breakPoint);

      return child;
    }
    protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
      parent1 = parent1.Clone() as ZhuEncoding;
      parent2 = parent2.Clone() as ZhuEncoding;

      ZhuEncoding child = parent2.Clone() as ZhuEncoding;

      if (parent1.Length != parent2.Length)
        return child;

      int breakPoint = random.Next(child.Length);
      int i = breakPoint;
      int predecessor = breakPoint - 1;
      if (predecessor < 0)
        predecessor = predecessor + child.Length;

      while (i != predecessor) {
        if (i == breakPoint) {
          child[i] = parent1[i];
          Swap(parent2, parent2[i], parent1[i]);
        }

        if (ProblemInstance.GetDistance(
          child[i] + 1, parent1[(i + 1) % child.Length] + 1, child)
          <
          ProblemInstance.GetDistance(
          child[i] + 1, parent2[(i + 1) % child.Length] + 1, child)) {
          child[(i + 1) % child.Length] = parent1[(i + 1) % child.Length];
          Swap(parent2, parent2[(i + 1) % child.Length], parent1[(i + 1) % child.Length]);
        } else {
          child[(i + 1) % child.Length] = parent2[(i + 1) % child.Length];
          Swap(parent1, parent1[(i + 1) % child.Length], parent2[(i + 1) % child.Length]);
        }

        i = (i + 1) % child.Length;
      }

      return child;
    }
    protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
      List<int> p1 = new List<int>(parent1);
      List<int> p2 = new List<int>(parent2);

      ZhuEncoding child = parent2.Clone() as ZhuEncoding;

      if (parent1.Length != parent2.Length)
        return child;

      int breakPoint = random.Next(child.Length);
      int i = breakPoint;

      int parent1Index = i;
      int parent2Index = i;

      DoubleArray dueTime = null;
      if (ProblemInstance is ITimeWindowedProblemInstance) {
        dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime;
      }

      do {
        if (i == breakPoint) {
          child[i] = p1[parent1Index];
        } else {
          if (dueTime != null &&
            (dueTime[p1[parent1Index] + 1] <
            dueTime[p2[parent2Index] + 1])) {
            child[i] = p1[parent1Index];
          } else {
            child[i] = p2[parent2Index];
          }
        }

        p1.Remove(child[i]);
        if (parent1Index >= p1.Count)
          parent1Index = 0;

        p2.Remove(child[i]);
        if (parent2Index >= p2.Count)
          parent2Index = 0;

        i = (i + 1) % child.Length;
      } while (i != breakPoint);

      return child;
    }
    protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2) {
      //note - the inner crossover is called here and the result is converted to a prins 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 ZhuEncoding;

      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 ZhuEncoding(permutation, ProblemInstance);
      } else
        return null;
    }
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            List <int> p1 = new List <int>(parent1);
            List <int> p2 = new List <int>(parent2);

            ZhuEncoding child = parent2.Clone() as ZhuEncoding;

            if (parent1.Length != parent2.Length)
            {
                return(child);
            }

            int breakPoint = random.Next(child.Length);
            int i          = breakPoint;

            int parent1Index = i;
            int parent2Index = i;

            DoubleArray dueTime = null;

            if (ProblemInstance is ITimeWindowedProblemInstance)
            {
                dueTime = (ProblemInstance as ITimeWindowedProblemInstance).DueTime;
            }

            do
            {
                if (i == breakPoint)
                {
                    child[i] = p1[parent1Index];
                }
                else
                {
                    if (dueTime != null &&
                        (dueTime[p1[parent1Index] + 1] <
                         dueTime[p2[parent2Index] + 1]))
                    {
                        child[i] = p1[parent1Index];
                    }
                    else
                    {
                        child[i] = p2[parent2Index];
                    }
                }

                p1.Remove(child[i]);
                if (parent1Index >= p1.Count)
                {
                    parent1Index = 0;
                }

                p2.Remove(child[i]);
                if (parent2Index >= p2.Count)
                {
                    parent2Index = 0;
                }

                i = (i + 1) % child.Length;
            } while (i != breakPoint);

            return(child);
        }
Example #10
0
        protected override ZhuEncoding Crossover(IRandom random, ZhuEncoding parent1, ZhuEncoding parent2)
        {
            List <int> p1 = new List <int>(parent1);
            List <int> p2 = new List <int>(parent2);

            ZhuEncoding child = parent2.Clone() as ZhuEncoding;

            if (parent1.Length != parent2.Length)
            {
                return(child);
            }

            int breakPoint  = random.Next(child.Length);
            int i           = breakPoint;
            int predecessor = breakPoint - 1;

            if (predecessor < 0)
            {
                predecessor = predecessor + child.Length;
            }

            int parent1Index = i;
            int parent2Index = i;

            while (i != predecessor)
            {
                if (i == breakPoint)
                {
                    child[i] = p1[parent1Index];

                    p1.Remove(child[i]);
                    if (parent1Index >= p1.Count)
                    {
                        parent1Index = 0;
                    }

                    p2.Remove(child[i]);
                    if (parent2Index >= p2.Count)
                    {
                        parent2Index = 0;
                    }
                }

                if (ProblemInstance.GetDistance(
                        child[i] + 1, p1[parent1Index] + 1, child)
                    <
                    ProblemInstance.GetDistance(
                        child[i] + 1, p2[parent2Index] + 1, child))
                {
                    child[(i + 1) % child.Length] = p1[parent1Index];
                }
                else
                {
                    child[(i + 1) % child.Length] = p2[parent2Index];
                }

                p1.Remove(child[(i + 1) % child.Length]);
                if (parent1Index >= p1.Count)
                {
                    parent1Index = 0;
                }

                p2.Remove(child[(i + 1) % child.Length]);
                if (parent2Index >= p2.Count)
                {
                    parent2Index = 0;
                }

                i = (i + 1) % child.Length;
            }

            return(child);
        }