Example #1
0
        public static Tuple <Kromosom, Kromosom> Krizaj(Kromosom prvi, Kromosom drugi)
        {
            string x1   = prvi.x;
            string x2   = drugi.x;
            string y1   = prvi.y;
            string y2   = drugi.y;
            Random rand = new Random();

            int    tockaKriz = rand.Next(0, x1.Length);
            string x3        = x1.Substring(0, tockaKriz) + x2.Substring(tockaKriz);
            string x4        = x2.Substring(0, tockaKriz) + x1.Substring(tockaKriz);

            tockaKriz = rand.Next(0, y1.Length);
            string y3 = y1.Substring(0, tockaKriz) + y2.Substring(tockaKriz);
            string y4 = y2.Substring(0, tockaKriz) + y1.Substring(tockaKriz);

            Kromosom k1 = new Kromosom();

            k1.x = x3;
            k1.y = y3;
            k1.Mutate();
            k1.Funkcija();

            Kromosom k2 = new Kromosom();

            k2.x = x4;
            k2.y = y4;
            k2.Mutate();
            k2.Funkcija();

            Tuple <Kromosom, Kromosom> kromosomi = new Tuple <Kromosom, Kromosom>(k1, k2);

            return(kromosomi);
        }
Example #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            sw = new System.Diagnostics.Stopwatch();
            sw.Start();
            Kromosom.duljina    = Convert.ToInt32(textBox1.Text);
            Kromosom.preciznost = Convert.ToInt32(textBox2.Text);
            Kromosom.MutateP    = Convert.ToInt32(textBox3.Text);
            velicinaGeneracije  = Convert.ToInt32(textBox4.Text);
            brojGeneracija      = Convert.ToInt32(textBox5.Text);
            int faktorMutacije = Convert.ToInt32(textBox6.Text);

            listView1.Items.Clear();
            kromosomi = new Dictionary <int, Kromosom>();
            for (int i = 0; i < velicinaGeneracije; i++)
            {
                Kromosom.MutateP += 1 * faktorMutacije;;
                Kromosom kromosom = new Kromosom();
                kromosom.Init();
                while (kromosomi.ContainsKey(kromosom.Dobrota))
                {
                    kromosom.Init();
                }
                kromosomi.Add(kromosom.Dobrota, kromosom);
            }
            evolution();
            sw.Stop();
            label10.Text = "t = " + ((double)sw.ElapsedMilliseconds / 1000).ToString() + " sec";
            Kromosom max = kromosomi[kromosomi.Keys.Max()];

            label1.Text = "x = " + max.X.ToString();
            label2.Text = "y = " + max.Y.ToString();
            label3.Text = "z = " + ((double)max.Dobrota / 10000000).ToString();
        }
Example #3
0
        private void evolution()
        {
            int    maxTrajanje = Convert.ToInt32(textBox7.Text) * 1000;
            Random rand        = new Random();

            for (int i = 0; i < brojGeneracija; i++)
            {
                Dictionary <int, Kromosom> sorted = (from krom in kromosomi orderby krom.Key ascending select krom).ToDictionary(pair => pair.Key, pair => pair.Value);
                Dictionary <int, Kromosom> tmp    = new Dictionary <int, Kromosom>();
                int lowValue   = 1;
                int totalValue = 0;
                foreach (var nes in sorted)
                {
                    totalValue += lowValue;
                    tmp.Add(totalValue, nes.Value);
                    lowValue += 1;
                }
                Kromosom najbolji = sorted.Last().Value;
                sorted = new Dictionary <int, Kromosom>();
                sorted.Add(najbolji.Dobrota, najbolji);
                while (sorted.Count != velicinaGeneracije)
                {
                    int rand1;
                    int rand2;
                    do
                    {
                        rand1 = rand.Next(0, totalValue);
                        rand2 = rand.Next(0, totalValue);
                    }while (rand1 == rand2);
                    Kromosom k1 = null;
                    Kromosom k2 = null;
                    foreach (var nes in tmp)
                    {
                        if (nes.Key > rand1 && k1 == null)
                        {
                            k1 = nes.Value;
                        }
                        if (nes.Key > rand2 && k2 == null)
                        {
                            k2 = nes.Value;
                        }
                    }
                    Tuple <Kromosom, Kromosom> tmpKromi = Kromosom.Krizaj(k1, k2);
                    try
                    {
                        sorted.Add(tmpKromi.Item1.Dobrota, tmpKromi.Item1);
                        if (sorted.Count == velicinaGeneracije)
                        {
                            break;
                        }
                    }
                    catch { }
                    try
                    {
                        sorted.Add(tmpKromi.Item2.Dobrota, tmpKromi.Item2);
                        if (sorted.Count == velicinaGeneracije)
                        {
                            break;
                        }
                    }
                    catch { }
                    if (sw.ElapsedMilliseconds > maxTrajanje && maxTrajanje != 0)
                    {
                        return;
                    }
                }
                ListViewItem red = new ListViewItem();
                red.Text = i.ToString();
                foreach (var nes in kromosomi)
                {
                    red.SubItems.Add("x=" + nes.Value.X.ToString() + ", y=" + nes.Value.Y.ToString() + ", z=" + nes.Value.Z.ToString());
                }
                listView1.Items.Add(red);
                kromosomi = sorted;
            }
        }