Esempio n. 1
0
        public static Multiset Randomize()
        {
            Multiset parent = new Multiset(Global.Xvalue(), Global.Yvalue(), Global.Zvalue());
            Random   rand   = new Random();
            var      xList  = new List <int>(parent.X);
            var      yList  = new List <int>(parent.Y);
            var      zList  = new List <int>(parent.Z);

            for (int i = 0; i < parent.X.Length; i++)
            {
                int randomNum = rand.Next(0, xList.Count);
                parent.X[i] = xList[randomNum];
                xList.RemoveAt(randomNum);
            }
            for (int j = 0; j < parent.Y.Length; j++)
            {
                int randomNum = rand.Next(0, yList.Count);
                parent.Y[j] = yList[randomNum];
                yList.RemoveAt(randomNum);
            }
            for (int k = 0; k < parent.Z.Length; k++)
            {
                int randomNum = rand.Next(0, zList.Count);
                parent.Z[k] = zList[randomNum];
                zList.RemoveAt(randomNum);
            }
            parent.totalScore = 0;
            for (int p = 0; p < parent.X.Length; p++)
            {
                parent.totalScore += Math.Abs(((parent.X[p] + parent.Y[p] + parent.Z[p]) - Global.b()));
            }

            return(parent);
        }
Esempio n. 2
0
 private static void CheckScore(Multiset ms)
 {
     if (ms.totalScore == 0)
     {
         Console.WriteLine("Perfect Solution Found: ");
         Node.print(ms.ToNodeList());
         Console.ReadKey(true);
     }
 }
Esempio n. 3
0
        public static Multiset FindBest(List <Node> initialNodes)
        {
            Multiset    parent           = Global.Initial();
            var         xList            = new List <int>(parent.X);
            var         yList            = new List <int>(parent.Y);
            var         zList            = new List <int>(parent.Z);
            List <Node> ParentToChild    = new List <Node>();
            int         childSizeCounter = 0;

            foreach (Node nodes in initialNodes)
            {
                if (xList.Contains(nodes.X) && yList.Contains(nodes.Y) && zList.Contains(nodes.Z))
                {
                    //Node n is new and needs to be added to child
                    ParentToChild.Add(nodes);
                    xList.Remove(nodes.X);
                    yList.Remove(nodes.Y);
                    zList.Remove(nodes.Z);
                    childSizeCounter++;
                }
            }
            List <Node> child = new List <Node>();

            if (childSizeCounter < parent.X.Length)
            {
                List <Node> roulette = new List <Node>();
                xList.OrderByDescending(i => i);;
                yList.OrderByDescending(i => i);
                zList.OrderByDescending(i => i);
                for (int i = 0; i < xList.Count(); i++)
                {
                    roulette.Add(new Node(xList[i], 0, 0));
                }
                roulette.Sort((x, y) => x.score.CompareTo(y.score));
                for (int i = 0; i < yList.Count(); i++)
                {
                    roulette[i].UpdateY(yList[i]);
                }
                roulette.Sort((x, y) => x.score.CompareTo(y.score));
                for (int i = 0; i < zList.Count(); i++)
                {
                    roulette[i].UpdateZ(zList[i]);
                }
                child = Node.MergeNodeLists(ParentToChild, roulette);
            }
            else
            {
                child = ParentToChild;
            }
            Multiset final = Node.ToMultiset(child);

            return(final);
        }
Esempio n. 4
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);
            }
        }
Esempio n. 5
0
        public static Multiset CreateChild(Multiset parent1, Multiset parent2, int shuffle = 0)
        {
            List <Node> childNodeList = new List <Node>();

            childNodeList = Node.MergeNodeLists(parent1.ToNodeList(), parent2.ToNodeList());
            if (shuffle == 1)
            {
                Node.Shuffle(childNodeList);
            }
            Multiset final = Node.FindBest(childNodeList);

            return(final);
        }
Esempio n. 6
0
        public static Multiset ToMultiset(List <Node> n)
        {
            int[] XList = Global.Xvalue(), YList = Global.Yvalue(), ZList = Global.Zvalue();
            Shuffle(n);
            for (int i = 1; i < n.Count; i++)
            {
                XList[i - 1] = n[i].X;
                YList[i - 1] = n[i].Y;
                ZList[i - 1] = n[i].Z;
            }
            XList[n.Count - 1] = n[0].X;
            YList[n.Count - 1] = n[0].Y;
            ZList[n.Count - 1] = n[0].Z;
            Multiset final = new Multiset(XList, YList, ZList);

            return(final);
        }
Esempio n. 7
0
        public static Multiset GenerateRandom(int count)
        {
            List <int> X    = new List <int>();
            List <int> Y    = new List <int>();
            List <int> Z    = new List <int>();
            Random     rand = new Random();

            for (int i = 0; i < count; i++)
            {
                X.Add(rand.Next(1, Global.b()));
                Y.Add(rand.Next(1, Global.b() - X[i]));
                Z.Add(Global.b() - X[i] - Y[i]);
            }
            int[]    Xa    = Multiset.Shuffle(X.ToArray());
            int[]    Ya    = Multiset.Shuffle(Y.ToArray());
            int[]    Za    = Multiset.Shuffle(Z.ToArray());
            Multiset final = new Multiset(Xa, Ya, Za);

            return(final);
        }
Esempio n. 8
0
        public Multiset MutateChild()
        {
            Multiset mutatedChild = this;

            Random rnd    = new Random();
            int    number = rnd.Next(0, 3); // creates a number between 0 and 2

            if (number == 0)
            {
                mutatedChild.X = Shuffle(mutatedChild.X);
            }
            else if (number == 1)
            {
                mutatedChild.Y = Shuffle(mutatedChild.Y);
            }
            else if (number == 2)
            {
                mutatedChild.Z = Shuffle(mutatedChild.Z);
            }
            CheckScore(mutatedChild);
            return(mutatedChild);
        }
Esempio n. 9
0
        public static Multiset Initial()
        {
            Multiset initial = new Multiset(Xvalue(), Yvalue(), Zvalue());

            return(initial);
        }
Esempio n. 10
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
        }