Example #1
0
        public Hive(int totalNumberBees, int numberInactive, int numberActive, int numberScout, int maxNumberVisits,
                    int maxNumberCycles, CitiesData citiesData)
        {
            random = new Random(0);

            this.totalNumberBees = totalNumberBees;
            this.numberInactive  = numberInactive;
            this.numberActive    = numberActive;
            this.numberScout     = numberScout;
            this.maxNumberVisits = maxNumberVisits;
            this.maxNumberCycles = maxNumberCycles;

            //this.citiesData = new CitiesData(citiesData.cities.Length); // hive's copy of problem-specific data
            this.citiesData = citiesData; // reference to CityData

            // this.probPersuasion & this.probMistake are hard-coded in class definition

            this.bees = new Bee[totalNumberBees];

            // Provides the baseline best random memory matrix
            this.bestMemoryMatrix     = GenerateRandomMemoryMatrix(); // alternative initializations are possible
            this.bestMeasureOfQuality = MeasureOfQuality(this.bestMemoryMatrix);

            this.indexesOfInactiveBees = new int[numberInactive]; // indexes of bees which are currently inactive

            for (int i = 0; i < totalNumberBees; ++i)             // initialize each bee, and best solution
            {
                int currStatus;                                   // depends on i. need status before we can initialize Bee
                if (i < numberInactive)
                {
                    currStatus = 0;               // inactive
                    indexesOfInactiveBees[i] = i; // curr bee is inactive
                }
                else if (i < numberInactive + numberScout)
                {
                    currStatus = 2; // scout
                }
                else
                {
                    currStatus = 1; // active
                }

                char[] randomMemoryMatrix = GenerateRandomMemoryMatrix();
                double mq             = MeasureOfQuality(randomMemoryMatrix);
                int    numberOfVisits = 0;

                bees[i] = new Bee(currStatus, randomMemoryMatrix, mq, numberOfVisits); // instantiate current bee

                // does this bee have best solution?
                if (bees[i].measureOfQuality < bestMeasureOfQuality) // curr bee is better (< because smaller is better)
                {
                    Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix, bees[i].memoryMatrix.Length);
                    this.bestMeasureOfQuality = bees[i].measureOfQuality;
                }
            } // each bee
        }     // TravelingSalesmanHive ctor
Example #2
0
        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("\nBegin Simulated Bee Colony algorithm demo\n");
                Console.WriteLine("Loading cities data for SBC Traveling Salesman Problem analysis");
                CitiesData citiesData = new CitiesData(20);
                Console.WriteLine(citiesData.ToString());
                Console.WriteLine("Number of cities = " + citiesData.cities.Length);
                Console.WriteLine("Number of possible paths = " + citiesData.NumberOfPossiblePaths().ToString("#,###"));
                Console.WriteLine("Best possible solution (shortest path) length = " + citiesData.ShortestPathLength().ToString("F4"));

                int totalNumberBees = 500;

                // Ideal numbers are: Active 75% / Inactive 10% / Scout 15%
                int numberActive   = Convert.ToInt32(totalNumberBees * .75);
                int numberInactive = Convert.ToInt32(totalNumberBees * .10);;
                int numberScout    = Convert.ToInt32(totalNumberBees * .15);;

                //int maxNumberVisits = 300;
                //int maxNumberCycles = 32450;
                int maxNumberVisits = 95;
                int maxNumberCycles = 10570;
                //int maxNumberVisits = 300; // proportional to # of possible neighbors to given solution
                //int maxNumberCycles = 32450;

                Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, citiesData);
                Console.WriteLine("\nInitial random hive");
                Console.WriteLine(hive);

                bool doProgressBar = true;
                hive.Solve(doProgressBar);

                Console.WriteLine("\nFinal hive");
                Console.WriteLine(hive);

                Console.WriteLine("End Simulated Bee Colony demo");
                Console.ReadLine();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Fatal: " + ex.Message);
                Console.ReadLine();
            }
        } // Main()
Example #3
0
        static Random random = null; // multipurpose

        #endregion Fields

        #region Constructors

        public Hive(int totalNumberBees, int numberInactive, int numberActive, int numberScout, int maxNumberVisits,
      int maxNumberCycles, CitiesData citiesData)
        {
            random = new Random(0);

              this.totalNumberBees = totalNumberBees;
              this.numberInactive = numberInactive;
              this.numberActive = numberActive;
              this.numberScout = numberScout;
              this.maxNumberVisits = maxNumberVisits;
              this.maxNumberCycles = maxNumberCycles;
              //this.maxCyclesWithNoImprovement = maxCyclesWithNoImprovement;

              //this.citiesData = new CitiesData(citiesData.cities.Length); // hive's copy of problem-specific data
              this.citiesData = citiesData; // reference to CityData

              // this.probPersuasion & this.probMistake are hard-coded in class definition

              this.bees = new Bee[totalNumberBees];
              this.bestMemoryMatrix = GenerateRandomMemoryMatrix(); // alternative initializations are possible
              this.bestMeasureOfQuality = MeasureOfQuality(this.bestMemoryMatrix);

              this.indexesOfInactiveBees = new int[numberInactive]; // indexes of bees which are currently inactive

              for (int i = 0; i < totalNumberBees; ++i) // initialize each bee, and best solution
              {
            int currStatus; // depends on i. need status before we can initialize Bee
            if (i < numberInactive)
            {
              currStatus = 0; // inactive
              indexesOfInactiveBees[i] = i; // curr bee is inactive
            }
            else if (i < numberInactive + numberScout)
            {
              currStatus = 2; // scout
            }
            else
            {
              currStatus = 1; // active
            }

            char[] randomMemoryMatrix = GenerateRandomMemoryMatrix();
            double mq = MeasureOfQuality(randomMemoryMatrix);
            int numberOfVisits = 0;

            bees[i] = new Bee(currStatus, randomMemoryMatrix, mq, numberOfVisits); // instantiate current bee

            // does this bee have best solution?
            if (bees[i].measureOfQuality < bestMeasureOfQuality) // curr bee is better (< because smaller is better)
            {
              Array.Copy(bees[i].memoryMatrix, this.bestMemoryMatrix, bees[i].memoryMatrix.Length);
              this.bestMeasureOfQuality = bees[i].measureOfQuality;
            }
              } // each bee
        }
Example #4
0
        static void Main(string[] args)
        {
            try
              {
            Console.WriteLine("\nBegin Simulated Bee Colony algorithm demo\n");
            Console.WriteLine("Loading cities data for SBC Traveling Salesman Problem analysis");
            CitiesData citiesData = new CitiesData(20);
            Console.WriteLine(citiesData.ToString());
            Console.WriteLine("Number of cities = " + citiesData.cities.Length);
            Console.WriteLine("Number of possible paths = " + citiesData.NumberOfPossiblePaths().ToString("#,###"));
            Console.WriteLine("Best possible solution (shortest path) length = " + citiesData.ShortestPathLength().ToString("F4"));

            int totalNumberBees = 100;
            int numberInactive = 20;
            int numberActive = 50;
            int numberScout = 30;

            int maxNumberVisits = 100;
            int maxNumberCycles = 3460;
            //int maxNumberVisits = 95;
            //int maxNumberCycles = 10570;
            //int maxNumberVisits = 300; // proportional to # of possible neighbors to given solution
            //int maxNumberCycles = 32450;

            Hive hive = new Hive(totalNumberBees, numberInactive, numberActive, numberScout, maxNumberVisits, maxNumberCycles, citiesData);
            Console.WriteLine("\nInitial random hive");
            Console.WriteLine(hive);

            bool doProgressBar = true;
            hive.Solve(doProgressBar);

            Console.WriteLine("\nFinal hive");
            Console.WriteLine(hive);

            Console.WriteLine("End Simulated Bee Colony demo");
            Console.ReadLine();
              }
              catch (Exception ex)
              {
            Console.WriteLine("Fatal: " + ex.Message);
            Console.ReadLine();
              }
        }