Exemplo n.º 1
0
        public void TestLocalSearch()
        {
            var grasp = new GRASP(20);

              var cities1 = new List<City> {new City(0, 0), new City(3, 3), new City(1, 1), new City(2, 2), new City(4, 4)};

              var best = new Candidate();
              best.Vector = new List<int>() {0, 1, 2, 3, 4};
              best.Cost = grasp.Cost(best.Vector, cities1);
              var rs = grasp.LocalSearch(best, cities1);

              Assert.IsNotNull(rs);
              Assert.IsNotNull(rs.Vector);
              Assert.IsNotNull(rs.Cost);
              Assert.AreNotSame(best, rs);
              Assert.AreNotEqual(best.Vector, rs.Vector);
              Assert.AreNotEqual(best.Cost, rs.Cost);

              // No improvement
              grasp = new GRASP(10);
              best = new Candidate();
              best.Vector = new List<int>() {0, 2, 3, 1, 4};
              best.Cost = grasp.Cost(best.Vector, cities1);

              rs = grasp.LocalSearch(best, cities1);
              Assert.IsNotNull(rs);
              Assert.AreEqual(best.Cost, rs.Cost);
        }
Exemplo n.º 2
0
        public Candidate ConstructRandomizedGreedySolution(List<City> cities)
        {
            var candidate = new Candidate();
              candidate.Vector = new List<int>();
              candidate.Vector.Add(_random.Next(cities.Count));

              var allCities = new List<int>();
              for (int i = 0; i < cities.Count; i++)
              {
            allCities.Add(i);
              }

              // Build a path until we have all cities in the path
              while (candidate.Vector.Count < cities.Count)
              {
            var candidates = allCities.Where(x=>!candidate.Vector.Contains(x)).ToArray();
            var costs = new List<double>();
            for (int i = 0; i < candidates.Length; i++)
            {
              costs.Add(Euc2d(cities[candidate.Vector.Last()], cities[i]));
            }
            var rcl = new List<int>();
            var max = costs.Max();
            var min = costs.Min();

            for (int i = 0; i < costs.Count; i++)
            {
              if (costs[i] <= (min + _greedinessFactor*(max - min)))
              {
            rcl.Add(candidates[i]);
              }
            }

            candidate.Vector.Add(rcl[_random.Next(rcl.Count)]);
              }
              candidate.Cost = Cost(candidate.Vector, cities);
              return candidate;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Main call for this 
        /// </summary>
        public Candidate Search(List<City> cities)
        {
            // Create a cruddy dummy instance to immediately disregard
              var best = new Candidate {Cost = int.MaxValue};

              for (int i = 0; i < _maxIterations; i++)
              {
            var candidate = ConstructRandomizedGreedySolution(cities);
            candidate = LocalSearch(candidate, cities);
            best = (candidate.Cost < best.Cost) ? candidate : best;

            Console.WriteLine(" > iteration #{0}, best=#{1}", i + 1, best.Cost);
              }

              return best;
        }
Exemplo n.º 4
0
        public Candidate LocalSearch(Candidate best, List<City> cities)
        {
            int count = 0;
              do
              {
            var candidate = new Candidate();
            candidate.Vector = StochasticTwoOpt(best.Vector);
            candidate.Cost = Cost(candidate.Vector, cities);
            count = (candidate.Cost < best.Cost) ? 0 : count + 1;

            if (candidate.Cost < best.Cost)
            {
              best = candidate;
            }

              } while (count <= _maxNoImprovements);

              return best;
        }