Exemplo n.º 1
0
        /// <summary>
        /// Breeds the parents.
        /// </summary>
        /// <param name="mother">Mother.</param>
        /// <param name="father">Father.</param>
        /// <param name="firstChild">First child.</param>
        /// <param name="secondChild">Second child.</param>
        private void BreedParents(SeatingConfiguration mother, SeatingConfiguration father, out SeatingConfiguration firstChild, out SeatingConfiguration secondChild)
        {
            firstChild  = null;
            secondChild = null;

            // Create list of tables for both children
            List <Table> tablesFirst = new List <Table>();

            Program.InitializeTables(tablesFirst);
            List <Table> tablesSecond = new List <Table>();

            Program.InitializeTables(tablesSecond);

            // Get the permutation form of parents
            var motherPermutation = mother.ToPermutationForm();
            var fatherPermutation = father.ToPermutationForm();

            // Perform  order 1 crossover
            Person[] firstChildPermutation;
            Person[] secondChildPermutation;
            this.Order1Crossover(motherPermutation, fatherPermutation, out firstChildPermutation);
            this.Order1Crossover(fatherPermutation, motherPermutation, out secondChildPermutation);

            // Perform mutation
            this.SwapMutation(firstChildPermutation);
            this.SwapMutation(secondChildPermutation);

            // Create the seating configuration
            firstChild  = new SeatingConfiguration(tablesFirst, firstChildPermutation);
            secondChild = new SeatingConfiguration(tablesSecond, secondChildPermutation);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the population.
        /// </summary>
        /// <param name="populationSize">Population size.</param>
        /// <param name="configs">Configs.</param>
        private static void InitializePopulation(int populationSize, out List <SeatingConfiguration> configs)
        {
            configs = new List <SeatingConfiguration>();

            for (int i = 0; i < populationSize; ++i)
            {
                // Create list of tables
                List <Table> tables = new List <Table>();
                InitializeTables(tables);

                // Create the guest list
                List <Person> guestList = new List <Person>();
                InitializeGuestList(guestList);

                // Create a config
                SeatingConfiguration config = new SeatingConfiguration(tables, guestList);

                // Add it to the population
                configs.Add(config);
            }
        }
Exemplo n.º 3
0
        private void CrowdingDiversity(ref List <SeatingConfiguration> population, List <SeatingConfiguration> parents)
        {
            // Iterate through the parents by picking two at random until all have been picked
            while (parents.Count != 0)
            {
                // Pick two parents at random and remove them list
                Random randomIndex = new Random();

                SeatingConfiguration parentA = parents[randomIndex.Next(parents.Count)];
                parents.Remove(parentA);
                SeatingConfiguration parentB = parents[randomIndex.Next(parents.Count)];
                parents.Remove(parentB);

                // Breed these two parents
                this.BreedParents(parentA, parentB, out SeatingConfiguration childA, out SeatingConfiguration childB);

                // Calculate the distance values using diversity metric
                // d(p1,o1) + d(p2,o2) < d(p1,o2) + d(p2,o1)
                int dPACA = this.MeasureDiversity(parentA, childA);
                int dPACB = this.MeasureDiversity(parentA, childB);
                int dPBCA = this.MeasureDiversity(parentB, childA);
                int dPBCB = this.MeasureDiversity(parentB, childB);

                // Create the competition
                if (dPACA + dPBCB < dPACB + dPBCA)
                {
                    // PA with CA
                    // PB with CB
                    // Compare the fitness
                    if (childA.Fitness < parentA.Fitness)
                    {
                        // Child is more fit, so add to pop and remove parent
                        population.Remove(parentA);
                        population.Add(childA);
                    }

                    if (childB.Fitness < parentB.Fitness)
                    {
                        population.Remove(parentB);
                        population.Add(childB);
                    }
                }
                else
                {
                    // PA with CB
                    // PB with CA
                    if (childB.Fitness < parentA.Fitness)
                    {
                        population.Remove(parentA);
                        population.Add(childB);
                    }

                    if (childA.Fitness < parentB.Fitness)
                    {
                        population.Remove(parentB);
                        population.Add(childA);
                    }
                }
            }

            // Order the population based on absolute rank
            population = population.OrderBy(x => x.Fitness).ToList();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Measures the diversity.
        /// </summary>
        /// <returns>The diversity.</returns>
        /// <param name="seatingA">Seating a.</param>
        /// <param name="seatingB">Seating b.</param>
        public int MeasureDiversity(SeatingConfiguration seatingA, SeatingConfiguration seatingB)
        {
            int diversity = 0;

            foreach (var guest in seatingA.GuestList)
            {
                bool seatingMatched  = false;
                int  positionMatched = 0; // 0 for nextToR and 1 for nextToL

                int idGuestRightA = seatingA.GetTableOfGuest(guest.Identity).IdOfNextGuest(guest.Identity) < 0 ?
                                    0 : seatingA.GetTableOfGuest(guest.Identity).IdOfNextGuest(guest.Identity);
                int idGuestLeftA = seatingA.GetTableOfGuest(guest.Identity).IdOfPreviousGuest(guest.Identity) < 0 ?
                                   0 : seatingA.GetTableOfGuest(guest.Identity).IdOfPreviousGuest(guest.Identity);
                int idGuestRightB = seatingB.GetTableOfGuest(guest.Identity).IdOfNextGuest(guest.Identity) < 0 ?
                                    0 : seatingB.GetTableOfGuest(guest.Identity).IdOfNextGuest(guest.Identity);
                int idGuestLeftB = seatingB.GetTableOfGuest(guest.Identity).IdOfPreviousGuest(guest.Identity) < 0 ?
                                   0 : seatingB.GetTableOfGuest(guest.Identity).IdOfPreviousGuest(guest.Identity);

                int temp = 0;

                // Do not bother with empty seats
                if (!(guest.Identity < 0))
                {
                    if (idGuestRightA != idGuestRightB && idGuestRightA != idGuestLeftB)
                    {
                        diversity++;
                    }
                    else
                    {
                        seatingMatched = true;
                        if (idGuestRightA == idGuestRightB)
                        {
                            positionMatched = 0;
                        }
                        else
                        {
                            positionMatched = 1;
                        }
                    }

                    if (!seatingMatched)
                    {
                        if (idGuestLeftA != idGuestRightB && idGuestLeftA != idGuestLeftB)
                        {
                            diversity++;
                        }
                    }
                    else
                    {
                        if (positionMatched == 0 && idGuestLeftA != idGuestLeftB)
                        {
                            diversity++;
                        }
                        else if (positionMatched == 1 && idGuestLeftA != idGuestRightB)
                        {
                            diversity++;
                        }
                    }

                    // Diversity if guests are same table
                    // Get table of guest
                    Table      tableGuestA = seatingA.GetTableOfGuest(guest.Identity);
                    Table      tableGuestB = seatingB.GetTableOfGuest(guest.Identity);
                    List <int> idsA        = tableGuestA.GetIdsOfSittingGuests();
                    idsA.Remove(guest.Identity);

                    foreach (int id in idsA)
                    {
                        if (!(id < 0) && tableGuestB.IsGuestSitting(id))
                        {
                            temp++;
                        }
                    }

                    int commonEmpty = Math.Min(tableGuestA.GetNumberOfEmptySeats(), tableGuestB.GetNumberOfEmptySeats());
                    temp = temp + commonEmpty;

                    diversity = diversity + (tableGuestA.NumberOfSeats - 1 - temp);
                }
            }

            return(diversity);
        }