Exemple #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            gr.Clear(Color.White);
            tree = genTree();
            tree.Draw(gr, new Point(PB_graph.Width / 2, 10));

            PB_graph.Invalidate();
            PB_graph.Refresh();
            PB_graph.Update();
        }
Exemple #2
0
        private void button3_Click(object sender, EventArgs e)
        {
            gr.Clear(Color.White);
            richTextBox1.AppendText("Calc: " + tree.calculate(source[0]) + "\n\n");
            tree.Draw(gr, new Point(PB_graph.Width / 2, 10));

            PB_graph.Invalidate();
            PB_graph.Refresh();
            PB_graph.Update();
            label2.Text = (tree.TreeRoot.ToString() + "\n");
        }
Exemple #3
0
 private void button2_Click(object sender, EventArgs e)
 {
     gr.Clear(Color.White);
     //tree.Mutate(gr, new Point(PB_graph.Width/2, 10), (new Random()).Next(1, 4));
     //tree.Draw(gr,new Point(PB_graph.Width/2,10));
     PB_graph.Invalidate();
     PB_graph.Refresh();
     PB_graph.Update();
     richTextBox1.Clear();
     richTextBox1.AppendText("D: " + tree.Depth() + "\n" + "S: " + tree.TreeRoot.Count() + "\n\n");
     label2.Text = (tree.TreeRoot.ToString() + "\n");
 }
Exemple #4
0
        private void ratePopulation(List <Tree> population)
        {
            Parallel.ForEach(population, individum =>
                             //  foreach (var individum in population)
            {
                //try
                {
                    int Ieq           = 0;
                    individum.raiting = 0;
                    Parallel.ForEach(source, arguments =>
                                     //foreach (var arguments in source)
                    {
                        Application.DoEvents();
                        if (arguments.Count != 0)         //== break;
                        {
                            if (!pause)
                            {
                                return;
                            }
                            individum.raiting += Math.Abs(eqs[Ieq++] -
                                                          individum.calculate(arguments));
                            //individum.raiting += individum.Count() + individum.Depth();
                        }
                    }
                                     )
                    ;
                }
                //catch (Exception) { }
            });
            population.Sort(functions.Sort);
            //int fixer = individum.Count() + individum.Depth();
            int fixer = 0;

            if (Math.Abs(population[population.Count - 1].raiting - fixer) < (EPSILON))
            {
                pause    = false;
                partyEnd = DateTime.Now;
                gr.Clear(Color.White);
                //population[0].Draw(gr, new Point(PB_graph.Width/2, PB_graph.Height/2+10));

                population[population.Count - 1].Draw(gr, new Point(PB_graph.Width / 2, 0 + 10));
                //population[population.Count - 1].Draw(gr, new Point(PB_graph.Width / 2, 0 + 10));


                PB_graph.Invalidate();
                PB_graph.Refresh();
                PB_graph.Update();
                MessageBox.Show("Solution found: " + population[population.Count - 1].TreeRoot.ToString());
            }
        }
Exemple #5
0
        private void button4_Click(object sender, EventArgs e)
        {
            if (currentTree == null)
            {
                currentTree = new Tree(source[0].Count);
                currentTree.TreeRoot.mutate('O');
                currentTree.TreeRoot.n1.mutate('O');
                currentTree.TreeRoot.n2.mutate('O');
            }

            //currentTree.Draw(gr, new Point(PB_graph.Width/2, 200));



            //PB_graph.Invalidate();
            //PB_graph.Refresh();
            //PB_graph.Update();

            //MessageBox.Show("let's ROCK!");



            gr.Clear(Color.White);
            int lal = 150;

            currentTree.Draw(gr, new Point(PB_graph.Width / 2, lal + 10));
            tree.Draw(gr, new Point(PB_graph.Width / 2, 10));


            Tree[] childs = functions.crossTrees(tree, currentTree);
            //            Tree child = crossTrees(tree, currentTree);
            int t = 2;

            foreach (var child in childs)
            {
                child.Draw(gr, new Point(PB_graph.Width / 2, 10 + lal * (t++)));
            }


            PB_graph.Invalidate();
            PB_graph.Refresh();
            PB_graph.Update();

            //tree = childs[0];
            //currentTree = childs[1];
        }
Exemple #6
0
        void Iteration(List <Tree> population, int MAX_POPULATION)
        {
            Application.DoEvents();
            //prepare population to cross...
            ratePopulation(population);

            if (pause && false)
            {
                gr.Clear(Color.White);

                population[population.Count - 1].Draw(gr, new Point(PB_graph.Width / 2, 0 + 10));
                PB_graph.Invalidate();
                PB_graph.Refresh();
                PB_graph.Update();
            }


            var pop2 = new List <Tree>();

            Parallel.ForEach(population, individum =>
                             //foreach (var individum in population)
            {
                try
                {
                    Application.DoEvents();
                    Tree[] temp = functions.crossTrees(population[population.Count - 1], individum);
                    pop2.Add(temp[0]);
                    pop2.Add(temp[1]);
                    if (!pause)
                    {
                        return;
                    }
                }
                catch (Exception)
                {
                }
                ;
            }
                             )
            ;
            if (!pause)
            {
                return;
            }
            try
            {
                population.AddRange(pop2);
            }
            catch (Exception)
            {
                //throw;
            }
            //!!!
            //for (int k = 0; k < population.Count; k++)
            //    if ((population[k].Count() > 100) || (population[k].Depth() > 7) || (population[k].Count() > 50)) population[k] = genTree();

            //mutation
            for (int i = 0; i < MAX_POPULATION / 15; i++)
            {
                population[functions.r.Next(0, population.Count)].Mutate();
            }
            if (!pause)
            {
                return;
            }
            ratePopulation(population);


            //population.RemoveRange(MAX_POPULATION, population.Count - MAX_POPULATION - 1);
            population.RemoveRange(0, population.Count - MAX_POPULATION - 1);

            gr.Clear(Color.White);
            //population[0].Draw(gr, new Point(PB_graph.Width/2, PB_graph.Height/2+10));

            if (!pause)
            {
                return;
            }
            population[population.Count - 1].Draw(gr, new Point(PB_graph.Width / 2, 0 + 10));
            //population[population.Count - 1].Draw(gr, new Point(PB_graph.Width / 2, 0 + 10));

            for (int k = 0; k < population.Count; k++)
            {
                if ((population[k].Count() > 100) /*|| (population[k].Depth() > 7)*/ || (population[k].Count() > 40))
                {
                    population[k] = genTree();
                }
            }

            PB_graph.Invalidate();
            PB_graph.Refresh();
            PB_graph.Update();
            if (!pause)
            {
                return;
            }
            //label2.Text = population[0].ToString();
            //richTextBox1.Clear();
            richTextBox1.AppendText("Badest:\nD: " + population[0].Depth() + "\nS: " + population[0].Count() + "\nR:" +
                                    population[0].calculate(source[0]));
            richTextBox1.AppendText("\n" + (++iterations) + " iterations\n");
            label2.Text = iterations + " iteratons complete.";
            Application.DoEvents();
        }