Exemplo n.º 1
0
        //swaps (u, x) and (v, y) by (u, y) and (x, v)
        private void Swap2(int u, int x, int v, int y, PrinsEncoding parent, PrinsEncoding child)
        {
            int i = 0;

            while (i < child.Length)
            {
                if (parent[i] == x)
                {
                    child[i] = y;
                }
                else if (parent[i] == v)
                {
                    child[i] = x;
                }
                else if (parent[i] == y)
                {
                    child[i] = v;
                }
                else
                {
                    child[i] = parent[i];
                }

                i++;
            }
        }
Exemplo n.º 2
0
 private void M4(PrinsEncoding parent, PrinsEncoding child, int u, int v)
 {
     if (u != depot && v != depot)
     {
         Swap(u, v, parent, child);
     }
 }
    protected override void Manipulate(IRandom random, PrinsEncoding individual) {
      InnerManipulatorParameter.ActualValue.PermutationParameter.ActualName = VRPToursParameter.ActualName;

      IAtomicOperation op = this.ExecutionContext.CreateOperation(
        InnerManipulatorParameter.ActualValue, this.ExecutionContext.Scope);
      op.Operator.Execute((IExecutionContext)op, CancellationToken);
    }
        protected override PrinsEncoding Crossover(IRandom random, PrinsEncoding parent1, PrinsEncoding parent2)
        {
            if (parent1.Length != parent2.Length)
            {
                return(parent1.Clone() as PrinsEncoding);
            }

            //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

            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 PrinsEncoding(permutation, ProblemInstance));
            }
            else
            {
                return(null);
            }
        }
    protected override void Manipulate(IRandom random, PrinsEncoding individual) {
      List<Tour> tours = individual.GetTours();
      bool improvement = false;
      int iterations = 0;

      do {
        improvement = false;
        double originalQuality = GetQuality(individual);
        PrinsEncoding child = null;

        int samples = 0;
        while (!improvement &&
          samples < SampleSize.Value.Value) {
          int u = random.Next(ProblemInstance.Cities.Value);
          int v = random.Next(ProblemInstance.Cities.Value);

          child = Manipulate(individual,
                originalQuality, u, v);

          improvement = child != null;

          samples++;
        }

        if (improvement) {
          for (int i = 0; i < child.Length; i++) {
            individual[i] = child[i];
          }
        }

        iterations++;
      } while (improvement &&
        iterations < Iterations.Value.Value);
    }
Exemplo n.º 6
0
        private void M9(PrinsEncoding parent, PrinsEncoding child, int u, int v)
        {
            if (u != depot && v != depot)
            {
                Tour tu = FindTour(child, u + 1);
                Tour tv = FindTour(child, v + 1);

                if (tu.Stops[0] != tv.Stops[0])
                {
                    int iu = tu.Stops.IndexOf(u + 1);
                    if (iu < tu.Stops.Count - 1)
                    {
                        int x = tu.Stops[iu + 1] - 1;

                        int iv = tv.Stops.IndexOf(v + 1);
                        if (iv < tv.Stops.Count - 1)
                        {
                            int y = tv.Stops[iv + 1] - 1;

                            if (x != v && y != u)
                            {
                                Swap2(u, x, v, y, parent, child);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 7
0
        //swaps (u, x) and v
        private void Swap(int u, int x, int v, PrinsEncoding parent, PrinsEncoding child)
        {
            int childIndex  = 0;
            int parentIndex = 0;

            while (childIndex < child.Length)
            {
                if (parent[parentIndex] == u)
                {
                    child[childIndex] = v;
                    parentIndex++;
                }
                else if (parent[parentIndex] == v)
                {
                    child[childIndex] = u;
                    childIndex++;
                    child[childIndex] = x;
                }
                else
                {
                    child[childIndex] = parent[parentIndex];
                }

                childIndex++;
                parentIndex++;
            }
        }
Exemplo n.º 8
0
        protected override void Manipulate(IRandom random, PrinsEncoding individual)
        {
            InnerManipulatorParameter.ActualValue.PermutationParameter.ActualName = VRPToursParameter.ActualName;

            IAtomicOperation op = this.ExecutionContext.CreateOperation(
                InnerManipulatorParameter.ActualValue, this.ExecutionContext.Scope);

            op.Operator.Execute((IExecutionContext)op, CancellationToken);
        }
Exemplo n.º 9
0
 //swaps u and v
 private void Swap(int u, int v, PrinsEncoding parent, PrinsEncoding child) {
   for (int i = 0; i < child.Length; i++) {
     if (parent[i] == u)
       child[i] = v;
     else if (parent[i] == v)
       child[i] = u;
     else
       child[i] = parent[i];
   }
 }
Exemplo n.º 10
0
 private void M1(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
   if (u != depot) {
     if (v == depot) {
       Tour tour = FindTour(child, u + 1);
       v = tour.Stops[0] - 1;
       InsertBefore(u, v, parent, child);
     } else {
       InsertAfter(u, v, parent, child);
     }
   }
 }
Exemplo n.º 11
0
    private void M5(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
      if (u != depot && v != depot) {
        Tour tour = FindTour(child, u + 1);
        int iu = tour.Stops.IndexOf(u + 1);
        if (iu < tour.Stops.Count - 1) {
          int x = tour.Stops[iu + 1] - 1;

          if (x != v)
            Swap(u, x, v, parent, child);
        }
      }
    }
Exemplo n.º 12
0
    private int FindCity(PrinsEncoding individual, int city) {
      int index = -1;

      int i = 0;
      while (i < individual.Length && index == -1) {
        if (individual[i] == city)
          index = i;

        i++;
      }

      return index;
    }
Exemplo n.º 13
0
        public override IOperation InstrumentedApply()
        {
            IVRPEncoding solution = VRPToursParameter.ActualValue;

            if (!(solution is PrinsEncoding))
            {
                VRPToursParameter.ActualValue = PrinsEncoding.ConvertFrom(solution, ProblemInstance);
            }

            Manipulate(RandomParameter.ActualValue, VRPToursParameter.ActualValue as PrinsEncoding);

            return(base.InstrumentedApply());
        }
Exemplo n.º 14
0
    private Tour FindTour(PrinsEncoding individual, int city) {
      Tour found = null;

      List<Tour> tours = individual.GetTours();
      int i = 0;

      while (found == null && i < tours.Count) {
        if (tours[i].Stops.Contains(city))
          found = tours[i];

        i++;
      }

      return found;
    }
Exemplo n.º 15
0
    //inserts u after v in the child
    private void InsertAfter(int u, int v, PrinsEncoding parent, PrinsEncoding child) {
      int pi = 0;
      int ci = 0;

      while (ci != child.Length) {
        if (parent[pi] != u) {
          child[ci] = parent[pi];
          ci++;
        }
        if (parent[pi] == v) {
          child[ci] = u;
          ci++;
        }

        pi++;
      }
    }
Exemplo n.º 16
0
    private void M3(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
      if (u != depot) {
        Tour tour = FindTour(child, u + 1);
        int iu = tour.Stops.IndexOf(u + 1);
        if (iu < tour.Stops.Count - 1) {
          int x = tour.Stops[iu + 1] - 1;

          if (v == depot) {
            tour = FindTour(child, u + 1);
            v = tour.Stops[0] - 1;
            InsertBefore(x, u, v, parent, child);
          } else {
            InsertAfter(x, u, v, parent, child);
          }
        }
      }
    }
        protected override void Manipulate(IRandom random, PrinsEncoding individual)
        {
            List <Tour> tours       = individual.GetTours();
            bool        improvement = false;
            int         iterations  = 0;

            do
            {
                int u = depot;
                improvement = false;
                double        originalQuality = GetQuality(individual);
                PrinsEncoding child           = null;

                while (!improvement && u < ProblemInstance.Cities.Value)
                {
                    int v = depot;
                    while (!improvement && v < ProblemInstance.Cities.Value)
                    {
                        if (u != v)
                        {
                            child = Manipulate(individual,
                                               originalQuality, u, v);

                            improvement = child != null;
                        }
                        v++;
                    }
                    u++;
                }

                if (improvement)
                {
                    for (int i = 0; i < child.Length; i++)
                    {
                        individual[i] = child[i];
                    }
                }

                iterations++;
            } while (improvement &&
                     iterations < Iterations.Value.Value);
        }
Exemplo n.º 18
0
    //inserts (u, x) before v in the child
    private void InsertBefore(int u, int x, int v, PrinsEncoding parent, PrinsEncoding child) {
      int pi = 0;
      int ci = 0;

      while (ci != child.Length) {
        if (parent[pi] == v) {
          child[ci] = u;
          ci++;

          child[ci] = x;
          ci++;
        }
        if (parent[pi] != u && parent[pi] != x) {
          child[ci] = parent[pi];
          ci++;
        }

        pi++;
      }
    }
Exemplo n.º 19
0
    protected override PrinsEncoding Crossover(IRandom random, PrinsEncoding parent1, PrinsEncoding parent2) {
      if (parent1.Length != parent2.Length)
        return parent1.Clone() as PrinsEncoding;

      //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

      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 PrinsEncoding(permutation, ProblemInstance);
      } else
        return null;
    }
Exemplo n.º 20
0
        private void M6(PrinsEncoding parent, PrinsEncoding child, int u, int v)
        {
            if (u != depot && v != depot)
            {
                Tour tour = FindTour(child, u + 1);
                int  iu   = tour.Stops.IndexOf(u + 1);
                if (iu < tour.Stops.Count - 1)
                {
                    int x = tour.Stops[iu + 1] - 1;

                    tour = FindTour(child, v + 1);
                    int iv = tour.Stops.IndexOf(v + 1);
                    if (iv < tour.Stops.Count - 1)
                    {
                        int y = tour.Stops[iv + 1] - 1;

                        if (x != v && y != u)
                        {
                            Swap(u, x, v, y, parent, child);
                        }
                    }
                }
            }
        }
Exemplo n.º 21
0
        public override IOperation InstrumentedApply()
        {
            ItemArray <IVRPEncoding> parents = new ItemArray <IVRPEncoding>(ParentsParameter.ActualValue.Length);

            for (int i = 0; i < ParentsParameter.ActualValue.Length; i++)
            {
                IVRPEncoding solution = ParentsParameter.ActualValue[i];

                if (!(solution is PrinsEncoding))
                {
                    parents[i] = PrinsEncoding.ConvertFrom(solution, ProblemInstance);
                }
                else
                {
                    parents[i] = solution;
                }
            }
            ParentsParameter.ActualValue = parents;

            ChildParameter.ActualValue =
                Crossover(RandomParameter.ActualValue, parents[0] as PrinsEncoding, parents[1] as PrinsEncoding);

            return(base.InstrumentedApply());
        }
Exemplo n.º 22
0
    private void M6(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
      if (u != depot && v != depot) {
        Tour tour = FindTour(child, u + 1);
        int iu = tour.Stops.IndexOf(u + 1);
        if (iu < tour.Stops.Count - 1) {
          int x = tour.Stops[iu + 1] - 1;

          tour = FindTour(child, v + 1);
          int iv = tour.Stops.IndexOf(v + 1);
          if (iv < tour.Stops.Count - 1) {
            int y = tour.Stops[iv + 1] - 1;

            if (x != v && y != u)
              Swap(u, x, v, y, parent, child);
          }
        }
      }
    }
Exemplo n.º 23
0
 protected PrinsEncoding(PrinsEncoding original, Cloner cloner)
   : base(original, cloner) {
 }
Exemplo n.º 24
0
 protected abstract void Manipulate(IRandom random, PrinsEncoding individual);
Exemplo n.º 25
0
 protected abstract PrinsEncoding Crossover(IRandom random, PrinsEncoding parent1, PrinsEncoding parent2);
Exemplo n.º 26
0
 protected PrinsEncoding(PrinsEncoding original, Cloner cloner)
     : base(original, cloner)
 {
 }
Exemplo n.º 27
0
    //swaps (u, x) and v
    private void Swap(int u, int x, int v, PrinsEncoding parent, PrinsEncoding child) {
      int childIndex = 0;
      int parentIndex = 0;

      while (childIndex < child.Length) {
        if (parent[parentIndex] == u) {
          child[childIndex] = v;
          parentIndex++;
        } else if (parent[parentIndex] == v) {
          child[childIndex] = u;
          childIndex++;
          child[childIndex] = x;
        } else {
          child[childIndex] = parent[parentIndex];
        }

        childIndex++;
        parentIndex++;
      }
    }
Exemplo n.º 28
0
 protected abstract PrinsEncoding Crossover(IRandom random, PrinsEncoding parent1, PrinsEncoding parent2);
Exemplo n.º 29
0
    //swaps (u, x) and (v, y) by (u, y) and (x, v)
    private void Swap2(int u, int x, int v, int y, PrinsEncoding parent, PrinsEncoding child) {
      int i = 0;

      while (i < child.Length) {
        if (parent[i] == x) {
          child[i] = y;
        } else if (parent[i] == v) {
          child[i] = x;
        } else if (parent[i] == y) {
          child[i] = v;
        } else {
          child[i] = parent[i];
        }

        i++;
      }
    }
Exemplo n.º 30
0
 private void M4(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
   if (u != depot && v != depot) {
     Swap(u, v, parent, child);
   }
 }
Exemplo n.º 31
0
 protected double GetQuality(PrinsEncoding individual)
 {
     return(ProblemInstance.Evaluate(individual).Quality);
 }
Exemplo n.º 32
0
 protected double GetQuality(PrinsEncoding individual) {
   return ProblemInstance.Evaluate(individual).Quality;
 }
Exemplo n.º 33
0
        protected PrinsEncoding Manipulate(PrinsEncoding individual,
                                           double originalQuality, int u, int v)
        {
            PrinsEncoding child       = null;
            bool          improvement = false;

            if (u != v)
            {
                child = individual.Clone() as PrinsEncoding;
                M1(individual, child, u, v);
                improvement = GetQuality(child) < originalQuality;

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M2(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M3(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M4(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M5(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M6(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M7(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M8(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }

                if (!improvement)
                {
                    child = individual.Clone() as PrinsEncoding;
                    M9(individual, child, u, v);
                    improvement = GetQuality(child) < originalQuality;
                }
            }

            if (improvement)
            {
                return(child);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 34
0
    private void M9(PrinsEncoding parent, PrinsEncoding child, int u, int v) {
      if (u != depot && v != depot) {
        Tour tu = FindTour(child, u + 1);
        Tour tv = FindTour(child, v + 1);

        if (tu.Stops[0] != tv.Stops[0]) {
          int iu = tu.Stops.IndexOf(u + 1);
          if (iu < tu.Stops.Count - 1) {
            int x = tu.Stops[iu + 1] - 1;

            int iv = tv.Stops.IndexOf(v + 1);
            if (iv < tv.Stops.Count - 1) {
              int y = tv.Stops[iv + 1] - 1;

              if (x != v && y != u)
                Swap2(u, x, v, y, parent, child);
            }
          }
        }
      }
    }
Exemplo n.º 35
0
    protected PrinsEncoding Manipulate(PrinsEncoding individual,
      double originalQuality, int u, int v) {
      PrinsEncoding child = null;
      bool improvement = false;

      if (u != v) {
        child = individual.Clone() as PrinsEncoding;
        M1(individual, child, u, v);
        improvement = GetQuality(child) < originalQuality;

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M2(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M3(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M4(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M5(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M6(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M7(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M8(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }

        if (!improvement) {
          child = individual.Clone() as PrinsEncoding;
          M9(individual, child, u, v);
          improvement = GetQuality(child) < originalQuality;
        }
      }

      if (improvement)
        return child;
      else
        return null;
    }
Exemplo n.º 36
0
 protected abstract void Manipulate(IRandom random, PrinsEncoding individual);