Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace l2
- {
- public static class Util
- {
- private static Random rnd = new Random();
- public static int GetRandom02()
- {
- return rnd.Next(0,2);
- }
- public static double GetRandom02d()
- {
- return rnd.NextDouble();
- }
- public static int GetRandomCustom(int val)
- {
- return rnd.Next(0, val);
- }
- public static char rev(char har)
- {
- char h = '0';
- if (har == h)
- return '1';
- else return '0';
- }
- public static string ReplaceAt(this string input, int index, char newChar)
- {
- char[] chars = input.ToCharArray();
- chars[index] = newChar;
- return new string(chars);
- }
- }
- class Program
- {
- public static string Reverse(string s)
- {
- char[] charArray = s.ToCharArray();
- Array.Reverse(charArray);
- return new string(charArray);
- }
- public static String genbin(double dlugosc)
- {
- String liczbastr = "";
- for (int i = 0; i < dlugosc; i++)
- {
- liczbastr = liczbastr.Insert(0, Util.GetRandom02().ToString());
- }
- return liczbastr;
- }
- public static int bintodec(String bin)
- {
- int dec = 0;
- dec = Convert.ToInt32(bin, 2);
- return dec;
- }
- public static double doprzedzialu(int liczba, double a, double b, double m)
- {
- double wynik = 0;
- wynik = a + (b - a) * liczba / (Math.Pow(2, m) - 1);
- return wynik;
- }
- public static double rastrigin(double x, double A, double omega, int n)
- {
- double wynik = 0;
- wynik = A * n + Math.Pow(x, 2) - 10 * Math.Cos(omega * x);
- return wynik;
- }
- static void Main(string[] args)
- {
- bool pierwszy = true;
- double A = 10;
- double omega = 20 * 3.14;
- int n = 3;
- int d = 6;
- double a = -1.0;
- double b = 1.0;
- double dziedzm = b - a;
- double ilosckom = (b - a) * Math.Pow(10, d);
- int m = 0;
- double dlugwekbin = Math.Pow(2, m);
- while (dlugwekbin <= ilosckom)
- {
- m++;
- dlugwekbin = Math.Pow(2, m);
- }
- int il = 10;
- List<String> lancuchy = new List<String>();
- for (int i = 0; i < il; i++)
- {
- lancuchy.Add(genbin(m));
- }
- List<String> koncowe = new List<String>();
- for (int k = 0; k < 10; k++)
- {
- //CAŁKOWITE ZASTĘPOWANIE
- if(pierwszy != true)
- lancuchy = koncowe;
- //CZĘŚCIOWE ZASTĘPOWANIE
- /*if(pierwszy != true)
- {
- List<String> temp = lancuchy;
- lancuchy = new List<String>();
- for (int i = 0; i < 5; i++)
- lancuchy.Add(koncowe[i]);
- for (int i = 0; i < 5; i++)
- lancuchy.Add(temp[i]);
- }
- */
- //LOSOWE ZASTĘPOWANIE
- /*if (pierwszy != true)
- {
- List<String> temp = lancuchy.Concat(koncowe).ToList();
- lancuchy = new List<String>();
- for (int i = 0; i < 10; i++)
- lancuchy.Add(temp[Util.GetRandomCustom(temp.Count)]);
- }
- */
- List<double> odwzorowane = new List<double>();
- for (int i = 0; i < il; i++)
- {
- odwzorowane.Add(doprzedzialu(bintodec(lancuchy[i]), a, b, m));
- }
- List<double> rastriginlista = new List<double>();
- for (int i = 0; i < il; i++)
- {
- rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n));
- }
- Console.WriteLine("Dziedzina zmienności = " + dziedzm + " Il kombinacji = " + ilosckom + " m = " + m + " dł wektora bin = " + dlugwekbin);
- Console.WriteLine();
- Console.WriteLine("Wektory binarne:");
- for (int i = 0; i < il; i++)
- Console.WriteLine(lancuchy[i]);
- Console.WriteLine();
- Console.WriteLine("Odwzorowane lancuchy binarne na liczbę X z zakresu [" + a + "," + b + "]:");
- for (int i = 0; i < il; i++)
- Console.WriteLine(odwzorowane[i]);
- Console.WriteLine();
- Console.WriteLine("Wartości funkcji Rastrigina:");
- for (int i = 0; i < il; i++)
- Console.WriteLine(rastriginlista[i]);
- Console.WriteLine();
- Console.WriteLine("RULETKA:");
- Console.WriteLine();
- double F = 0;
- for (int i = 0; i < il; i++)
- F += rastriginlista[i];
- //Console.WriteLine("Całkowite dopasowanie F = " + F);
- List<double> prawdopodobienstwa_wyboru = new List<double>();
- for (int i = 0; i < il; i++)
- prawdopodobienstwa_wyboru.Add(rastriginlista[i] / F);
- /*Console.WriteLine("Prawdopodobienstwa wyboru:");
- for (int i = 0; i < il; i++)
- Console.WriteLine(prawdopodobienstwa_wyboru[i]);*/
- List<double> dystrybuanty = new List<double>();
- for (int i = 0; i < il; i++)
- {
- double dystr = 0;
- for (int j = 0; j <= i; j++)
- dystr += prawdopodobienstwa_wyboru[j];
- dystrybuanty.Add(dystr);
- }
- /*Console.WriteLine("Dystrybuanty:");
- for (int i = 0; i < il; i++)
- Console.WriteLine(dystrybuanty[i]);*/
- List<String> ruletka = new List<string>();
- for (int i = 0; i < il; i++)
- {
- double r = Util.GetRandom02d();
- for (int j = 0; j < il; j++)
- {
- if (r < dystrybuanty[j])
- {
- //Console.WriteLine("Wybrany osobnik z indeksem " + j + ": " + lancuchy[j]);
- ruletka.Add(lancuchy[j]);
- break;
- }
- }
- }
- for (int i = 0; i < il; i++)
- Console.WriteLine(ruletka[i]);
- Console.WriteLine();
- Console.WriteLine("MUTACJA:");
- Console.WriteLine();
- double pm = 0.1;
- List<String> lancuchymut = new List<String>();
- for (int i = 0; i < ruletka.Count; i++)
- {
- string st = "";
- for (int j = 0; j < m; j++)
- {
- double rn = Util.GetRandom02d();
- if (rn < pm)
- {
- if (ruletka[i][j] == '0')
- st = st.Insert(0, "1");
- else if (ruletka[i][j] == '1')
- st = st.Insert(0, "0");
- }
- else
- {
- st = st.Insert(0, ruletka[i][j].ToString());
- }
- }
- lancuchymut.Add(Reverse(st));
- }
- /* Console.WriteLine("PO MUTACJI:");
- for (int i = 0; i < il; i++)
- Console.WriteLine(lancuchymut[i]);*/
- Console.WriteLine();
- Console.WriteLine("INWERSJA:");
- Console.WriteLine();
- List<String> inw = new List<String>();
- double pi = 0.1;
- for (int i = 0; i < ruletka.Count; i++)
- {
- double rnd = Util.GetRandom02d();
- if(rnd < pi)
- {
- int p1 = Util.GetRandomCustom(m - 1);
- int p2 = Util.GetRandomCustom(m - 1);
- int pocz;
- int kon;
- if (p1 < p2)
- {
- pocz = p1;
- kon = p2;
- }
- else
- {
- pocz = p2;
- kon = p1;
- }
- string lancuch = ruletka[i];
- for(int j = pocz + 1; j < kon; j++)
- {
- if (lancuch[j] == '0')
- lancuch = lancuch.ReplaceAt(j, '1');
- else if (lancuch[j] == '1')
- lancuch = lancuch.ReplaceAt(j, '0');
- }
- inw.Add(lancuch);
- }
- }
- /* for (int i = 0; i < inw.Count; i++)
- Console.WriteLine(inw[i]);*/
- Console.WriteLine();
- Console.WriteLine("KRZYŻOWANIE JEDNOPUNKTOWE:");
- Console.WriteLine();
- List<String> rulkopia = ruletka;
- double pk = 0.6;
- List<String> ldokrzyz = new List<String>();
- for (int i = il - 1; i >= 0; i--)
- {
- double r = Util.GetRandom02d();
- if (r < pk)
- {
- ldokrzyz.Add(rulkopia[i]);
- rulkopia.RemoveAt(i);
- }
- }
- if (ldokrzyz.Count % 2 != 0)
- {
- int rnd = Util.GetRandomCustom(rulkopia.Count);
- ldokrzyz.Add(rulkopia[rnd]);
- rulkopia.RemoveAt(rnd);
- }
- /*for (int i = 0; i < ldokrzyz.Count; i++)
- Console.WriteLine(ldokrzyz[i]);*/
- List<String> lkrzyzfin = new List<String>();
- int ile = ldokrzyz.Count / 2;
- for (int i = 0; i < ile; i++)
- {
- List<String> lkrzyztemp = new List<String>();
- int los = Util.GetRandomCustom(ldokrzyz.Count);
- lkrzyztemp.Add(ldokrzyz[los]);
- ldokrzyz.RemoveAt(los);
- los = Util.GetRandomCustom(ldokrzyz.Count);
- lkrzyztemp.Add(ldokrzyz[los]);
- ldokrzyz.RemoveAt(los);
- int punkt = Util.GetRandomCustom(m - 1);
- string t1 = lkrzyztemp[0];
- string t2 = lkrzyztemp[1];
- for (int j = punkt + 1; j < m; j++)
- {
- lkrzyztemp[0] = lkrzyztemp[0].ReplaceAt(j, t2[j]);
- lkrzyztemp[1] = lkrzyztemp[1].ReplaceAt(j, t1[j]);
- }
- lkrzyzfin.Add(lkrzyztemp[0]);
- lkrzyzfin.Add(lkrzyztemp[1]);
- }
- List<String> wszystkie = new List<string>();
- wszystkie = lancuchymut.Concat(inw).Concat(rulkopia).ToList();
- odwzorowane = new List<double>();
- for (int i = 0; i < wszystkie.Count; i++)
- {
- odwzorowane.Add(doprzedzialu(bintodec(wszystkie[i]), a, b, m));
- }
- rastriginlista = new List<double>();
- for (int i = 0; i < odwzorowane.Count; i++)
- {
- rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n));
- }
- List<Tuple<double, string>> polaczone = new List<Tuple<double, string>>();
- for (int i = 0; i < wszystkie.Count; i++)
- polaczone.Add(new Tuple<double, String>(rastriginlista[i], wszystkie[i]));
- polaczone.Sort();
- polaczone.Reverse();
- Console.WriteLine("WSZYSTKIE:");
- for (int i = 0; i < polaczone.Count; i++)
- Console.WriteLine(polaczone[i]);
- koncowe = new List<String>();
- for(int i = 0; i < 10; i++)
- {
- koncowe.Add(polaczone[i].Item2);
- }
- odwzorowane = new List<double>();
- for (int i = 0; i < koncowe.Count; i++)
- {
- odwzorowane.Add(doprzedzialu(bintodec(koncowe[i]), a, b, m));
- }
- rastriginlista = new List<double>();
- for (int i = 0; i < odwzorowane.Count; i++)
- {
- rastriginlista.Add(rastrigin(odwzorowane[i], A, omega, n));
- }
- rastriginlista.Sort();
- polaczone = new List<Tuple<double, string>>();
- for (int i = 0; i < koncowe.Count; i++)
- polaczone.Add(new Tuple<double, String>(rastriginlista[i], koncowe[i]));
- polaczone.Sort();
- polaczone.Reverse();
- pierwszy = false;
- Console.WriteLine("WYNIK:");
- for (int i = 0; i < polaczone.Count; i++)
- Console.WriteLine(polaczone[i]);
- }
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement