예제 #1
0
        static void Main(string[] args)
        {
            //init
            int population = Global.population();
            int b          = Global.b();

            // Set in Global

            int[]    X          = Global.Xvalue();
            int[]    Y          = Global.Yvalue();
            int[]    Z          = Global.Zvalue();
            Multiset initialSet = Global.Initial();

            //Random
            Multiset possibleRandom = Global.GenerateRandom(100);

            Console.Write("\n Here is a possible random solution that is solveable");

            possibleRandom.print();


            Boolean isSolveable = initialSet.CheckSolvability(b);

            if (isSolveable)
            {
                //Randomly create population parents
                List <Multiset> parentList = new List <Multiset>();
                for (int i = 0; i < population; i++)
                {
                    Multiset holder = new Multiset(Global.Xvalue(), Global.Yvalue(), Global.Zvalue());
                    holder = Multiset.Randomize();
                    parentList.Add(holder);
                }
                List <Multiset> NextGen = parentList;

                for (int i = 0; i < Global.Generations(); i++)
                {
                    Console.Write("Finding Generation {0}\n", i + 1);
                    NextGen = FindNextGen(NextGen);
                    Console.Write("Generation {0}'s best score is {1}\n", i + 1, NextGen[0].totalScore);
                    Console.Write("Gen {0}'s Solution: \n", i + 1);
                    NextGen[0].print();
                    Console.Write("\n");
                }
                List <Node> guess      = WoC(NextGen);
                Multiset    finalguess = Node.FindBest(guess);
                Console.Write("Best guess's score is {0}\n", finalguess.totalScore);
                Console.Write("Final Guess Solution: \n");
                Node.print(finalguess.ToNodeList());
                Console.WriteLine("Hello World!");
                Console.ReadKey(true);
            }
        }
예제 #2
0
        public static List <Multiset> FindNextGen(List <Multiset> parentList)
        {
            //Creates (parentList.Count/2) Children
            int             totalCount = 0; //keeps track of the amount of children in the NextGen List.
            List <Multiset> NextGen    = new List <Multiset>();

            for (int k = 0; k < parentList.Count; k += 2)
            {
                Multiset holder = new Multiset(Global.Xvalue(), Global.Yvalue(), Global.Zvalue());
                holder = Multiset.CreateChild(parentList[k], parentList[k + 1]);
                NextGen.Add(holder);
                totalCount++;
            }

            //Sort parentList by ascending totalScore, then add the top 10% to NextGen.
            parentList.Sort((x, y) => x.totalScore.CompareTo(y.totalScore));


            //Add the top 10% of parents into the NextGen population
            for (int r = 0; r < parentList.Count / 10; r++)
            {
                //parentList[r].print();
                NextGen.Add(parentList[r]);
                totalCount++;
            }

            //Creates the last 10% of the NextGen children by crossover of the top 80% parents
            //Check top 20% parent size. -> Console.WriteLine("Top 20% parents size should be 240= {0}",parentList.Count*(.8));
            for (int s = 0; s < parentList.Count * (0.2); s += 2)
            {
                Multiset holder = new Multiset(Global.Xvalue(), Global.Yvalue(), Global.Zvalue());
                holder = Multiset.CreateChild(parentList[s + 1], parentList[s]);
                NextGen.Add(holder);
                totalCount++;
            }

            //Mutator Function
            Random randMutate = new Random();

            parentList.Sort((x, y) => x.totalScore.CompareTo(y.totalScore));
            for (int v = 0; v < parentList.Count * .3; v++)
            {
                Multiset holder = new Multiset(Global.Xvalue(), Global.Yvalue(), Global.Zvalue());
                holder = Multiset.CreateChild(Multiset.Randomize(), parentList[v], 1);
                NextGen.Add(holder);
            }
            //int increment = 1;
            //int counter = 0;
            int startPercentage = 1;

            NextGen.Sort((x, y) => x.totalScore.CompareTo(y.totalScore));
            for (int v = 0; v < NextGen.Count; v++)
            {
                if (v > ((NextGen.Count * (0.01)) * startPercentage))
                {
                    startPercentage++;
                }
                int r = randMutate.Next(startPercentage, 101);
                if (r == startPercentage)
                {
                    //Mutate child
                    NextGen[v] = NextGen[v].MutateChild();
                }
                //increment++;
            }

            return(NextGen);
            //end
        }