コード例 #1
0
        public MainWindow()
        {
            InitializeComponent();
            DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory());

            di = (di.Parent).Parent;
            string   picross = "545";
            ReadFile grid    = new ReadFile
            {
                filepath = di.FullName + "\\Data\\" + picross + ".txt"
            };

            grid.MakeConstraints();

            WriteFile outfile = new WriteFile
            {
                outFilePath = di.FullName + "\\Data\\" + picross + ".csv"
            };

            outfile.WriteHeaders();

            double xSpace = myRowCanvas.Width / grid.numColumns;
            double ySpace = myRowCanvas.Height / grid.numRows;

            LabelColumns(xSpace, grid.colConstraints, grid.numColumns);
            LabelRows(ySpace, grid.rowConstraints, grid.numRows);

            Population population  = new Population(grid.colConstraints, grid.rowConstraints, false);
            Population population2 = new Population(grid.colConstraints, grid.rowConstraints, true);

            RunGA(grid, population, population2, outfile);
        }
コード例 #2
0
        public int DrawRowBoard(ReadFile grid, Member member, double xSpace, double ySpace, double avg, Member worst)
        {
            myRowCanvas.Children.Clear();
            int correct = 0;

            for (int j = 0; j < grid.numRows; j++)
            {
                for (int i = 0; i < grid.numColumns; i++)
                {
                    Rectangle rectangle = new Rectangle
                    {
                        Stroke = Brushes.Black,
                        Width  = xSpace,
                        Height = ySpace
                    };
                    if (member.DNA[i, j] == true)
                    {
                        rectangle.Fill = Brushes.Black;
                        if (grid.solution[i, j] == true)
                        {
                            rectangle.Fill = Brushes.Lime;
                            correct++;
                        }
                    }
                    Canvas.SetLeft(rectangle, xSpace * j);
                    Canvas.SetTop(rectangle, ySpace * i);
                    myRowCanvas.Children.Add(rectangle);
                }
            }
            TextBlock text = new TextBlock();

            text.FontSize = 17;
            Canvas.SetLeft(text, 400);
            text.Text = "Best Row Fitness = " + member.Fitness.ToString();
            myRowCanvas.Children.Add(text);
            TextBlock text2 = new TextBlock();

            text2.FontSize = 17;
            Canvas.SetLeft(text2, 400);
            Canvas.SetTop(text2, 20);
            text2.Text = "Worst Row Fitness = " + worst.Fitness.ToString();
            myRowCanvas.Children.Add(text2);
            TextBlock text3 = new TextBlock();

            text3.FontSize = 17;
            Canvas.SetLeft(text3, 400);
            Canvas.SetTop(text3, 40);
            text3.Text = "Avg Row Fitness = " + avg.ToString();
            myRowCanvas.Children.Add(text3);
            TextBlock text4 = new TextBlock();

            text4.FontSize = 17;
            Canvas.SetLeft(text4, 400);
            Canvas.SetTop(text4, 60);
            text4.Text = "# of Correct Squares = " + correct.ToString();
            myRowCanvas.Children.Add(text4);
            return(correct);
        }
コード例 #3
0
        public async void RunGA(ReadFile grid, Population population1, Population population2, WriteFile outfile)
        {
            int correctWoC = 0;
            int prevFitnessWoc = -1, genCount = 1;

            do
            //for(int i = 0; i < 5000; i++)
            {
                await Task.Delay(50);

                population1.ConsultExperts(population2);

                double xSpace = myRowCanvas.Width / grid.numColumns;
                double ySpace = myRowCanvas.Height / grid.numRows;

                double avg1 = 0;
                foreach (Member member in population1.members)
                {
                    avg1 += member.Fitness;
                }
                avg1 /= population1.members.Count;
                avg1  = Math.Round(avg1, 2);
                int    correctCol = DrawColBoard(grid, population1.members[0], xSpace, ySpace, avg1, genCount, population1.members[population1.members.Count - 1]);
                double avg2       = 0;
                foreach (Member member in population2.members)
                {
                    avg2 += member.Fitness;
                }
                avg2 /= population2.members.Count;
                avg2  = Math.Round(avg2, 2);
                int correctRow = DrawRowBoard(grid, population2.members[0], xSpace, ySpace, avg2, population2.members[population2.members.Count - 1]);
                //if (population1.solution.Fitness < prevFitnessWoc || prevFitnessWoc == -1)
                //{
                xSpace         = wocCanvas.Width / grid.numColumns;
                ySpace         = wocCanvas.Height / grid.numRows;
                prevFitnessWoc = population1.solution.Fitness;
                correctWoC     = DrawWoC(grid, population1.solution, xSpace, ySpace);
                //}
                outfile.WriteToFile(genCount, population1, population2, avg1, avg2, correctCol, correctRow, correctWoC);
                population1.BreedPopulaton(true);
                population2.BreedPopulaton(true);

                genCount++;
            } while (population1.solution.Fitness != 0 || (population1.solution.Fitness != 0 && genCount != 7500));
        }
コード例 #4
0
        public int DrawWoC(ReadFile grid, Member member, double xSpace, double ySpace)
        {
            wocCanvas.Children.Clear();
            int correct = 0;

            for (int j = 0; j < grid.numRows; j++)
            {
                for (int i = 0; i < grid.numColumns; i++)
                {
                    Rectangle rectangle = new Rectangle
                    {
                        Stroke = Brushes.Black,
                        Width  = xSpace,
                        Height = ySpace
                    };
                    if (member.DNA[i, j] == true)
                    {
                        rectangle.Fill = Brushes.Black;
                        if (grid.solution[i, j] == true)
                        {
                            rectangle.Fill = Brushes.Lime;
                            correct++;
                        }
                    }
                    Canvas.SetLeft(rectangle, xSpace * j);
                    Canvas.SetTop(rectangle, ySpace * i);
                    wocCanvas.Children.Add(rectangle);
                }
            }
            TextBlock text = new TextBlock();

            text.FontSize = 17;
            Canvas.SetTop(text, 600);
            text.Text = "Wisdom of Crowds Fitness = " + member.Fitness.ToString();
            wocCanvas.Children.Add(text);
            TextBlock text2 = new TextBlock();

            text2.FontSize = 17;
            Canvas.SetTop(text2, 620);
            text2.Text = "# of Correct Squares = " + correct.ToString();
            wocCanvas.Children.Add(text2);
            return(correct);
        }