Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public static class Convert
  2.     {
  3.  
  4.         private static readonly Dictionary<char, string> hexCharacterToBinary = new Dictionary<char, string> {
  5.     { '0', "0000" },
  6.     { '1', "0001" },
  7.     { '2', "0010" },
  8.     { '3', "0011" },
  9.     { '4', "0100" },
  10.     { '5', "0101" },
  11.     { '6', "0110" },
  12.     { '7', "0111" },
  13.     { '8', "1000" },
  14.     { '9', "1001" },
  15.     { 'a', "1010" },
  16.     { 'b', "1011" },
  17.     { 'c', "1100" },
  18.     { 'd', "1101" },
  19.     { 'e', "1110" },
  20.     { 'f', "1111" }
  21.     };
  22.         public static string HexStringToBinary(string hex)
  23.         {
  24.             StringBuilder result = new StringBuilder();
  25.             foreach (char c in hex)
  26.             {
  27.                 result.Append(hexCharacterToBinary[char.ToLower(c)]);
  28.             }
  29.             return result.ToString();
  30.         }
  31.  
  32.         public static string Convert_to_str(bool[] arr)
  33.         {
  34.             string str = "";
  35.             for (int i = 0; i < arr.Length; i++)
  36.            {
  37.                if (arr[i] == true) {
  38.                    str += "1";
  39.  
  40.                }
  41.                else
  42.                {
  43.                    str += "0";
  44.                }
  45.  
  46.  
  47.            }
  48.            return str;
  49.        }
  50.    }
  51.  
  52.    public class lymphocyte
  53.    {
  54.        public bool[] detector_string;
  55.        public double current_affinity;
  56.        bool active;
  57.        
  58.        double affinity;
  59.        public static bool[] get_random_string(int size)
  60.        {
  61.            bool[] random_string = new bool[size];
  62.            for (int i = 0; i < size; i++)
  63.            {
  64.                Random r = new Random();
  65.                if (r.NextDouble() > 0.5)
  66.                 {
  67.                     random_string[i] = true;
  68.                 }
  69.                 else random_string[i] = false;
  70.             }
  71.             return random_string;
  72.  
  73.         }
  74.  
  75.         public lymphocyte(int length_of_string, int size_of_lympho)
  76.         {
  77.             for (int i = 0; i < size_of_lympho; i++)
  78.            {
  79.                lymphocyte lym = new lymphocyte(length_of_string);
  80.                lym.detector_string = get_random_string(length_of_string);
  81.                immune_system.lympho.Add(lym);
  82.            }
  83.        }
  84.  
  85.        public lymphocyte(int length_of_string)
  86.        {
  87.  
  88.            immune_system.lympho.Add(this);
  89.        }
  90.        public void get_affinity_related_bits(lymphocyte l1, lymphocyte l2)
  91.        {
  92.            string str1 = Convert.Convert_to_str(l1.detector_string);
  93.            string str2 = Convert.Convert_to_str(l1.detector_string);
  94.  
  95.  
  96.  
  97.            List<int[]> num = new List<int[]>();
  98.             int maxlen = 0;
  99.             for (int i = 0; i < str1.Length; i++)
  100.            {
  101.                num.Add(new int[str2.Length]);
  102.                for (int j = 0; j < str2.Length; j++)
  103.                {
  104.                    if (str1[i] != str2[j])
  105.                        num[i][j] = 0;
  106.                    else
  107.                    {
  108.                        if ((i == 0) || (j == 0))
  109.                            num[i][j] = 1;
  110.                        else
  111.                            num[i][j] = 1 + num[i - 1][j - 1];
  112.                        if (num[i][j] > maxlen)
  113.                             maxlen = num[i][j];
  114.                     }
  115.                     if (i >= 2)
  116.                         num[i - 2] = null;
  117.                 }
  118.             }
  119.             this.current_affinity = maxlen;
  120.         }
  121.         public void get_affinity_Hamming(lymphocyte l1, lymphocyte l2)
  122.         {
  123.             string string1 = Convert.Convert_to_str(l1.detector_string);
  124.             string string2 = Convert.Convert_to_str(l1.detector_string);
  125.             if (string1 == null) throw new ArgumentNullException("string1");
  126.             if (string2 == null) throw new ArgumentNullException("string2");
  127.             int diff;
  128.             int[,] m = new int[string1.Length + 1, string2.Length + 1];
  129.  
  130.             for (int i = 0; i <= string1.Length; i++) { m[i, 0] = i; }
  131.            for (int j = 0; j <= string2.Length; j++) { m[0, j] = j; }
  132.  
  133.            for (int i = 1; i <= string1.Length; i++)
  134.            {
  135.                for (int j = 1; j <= string2.Length; j++)
  136.                {
  137.                    diff = (string1[i - 1] == string2[j - 1]) ? 0 : 1;
  138.  
  139.                    m[i, j] = Math.Min(Math.Min(m[i - 1, j] + 1,
  140.                                             m[i, j - 1] + 1),
  141.                                             m[i - 1, j - 1] + diff);
  142.                }
  143.            }
  144.            this.affinity = m[string1.Length, string2.Length];
  145.        }
  146.  
  147.        public void get_active()
  148.        {
  149.            if (this.current_affinity > affinity)
  150.             {
  151.                 this.active = true;
  152.             }
  153.  
  154.         }
  155.  
  156.  
  157.  
  158.     }
  159.  
  160.     public class immune_system
  161.     { public static double stop;
  162.         int size_of_lympho;
  163.         public static double[] affinity;
  164.         public static List<lymphocyte> lympho;
  165.         public static List<antigens> anti;
  166.  
  167.         public lymphocyte Crossingover(lymphocyte l1, lymphocyte l2, int len)
  168.         {
  169.             bool[] random_str = lymphocyte.get_random_string(len);
  170.             lymphocyte l = new lymphocyte(len);
  171.             immune_system.lympho.Add(l);
  172.             for (int i = 0; i < len; i++)
  173.            {
  174.                if (random_str[i] == true)
  175.                {
  176.                    l.detector_string[i] = l1.detector_string[i];
  177.                }
  178.                else
  179.                {
  180.                    l.detector_string[i] = l2.detector_string[i];
  181.                }
  182.            }
  183.  
  184.            return l;
  185.        }
  186.        public lymphocyte Mutation(lymphocyte l1)
  187.        {
  188.  
  189.            for (int i = 0; i < l1.detector_string.Length; i++)
  190.            {
  191.                l1.detector_string[i] = !l1.detector_string[i];
  192.            }
  193.  
  194.            return l1;
  195.        }
  196.        public void program_death()
  197.        {
  198.            foreach (lymphocyte l in immune_system.lympho)
  199.            {
  200.                if (l.current_affinity < immune_system.stop)
  201.                {
  202.                    lympho.Remove(l);
  203.                }
  204.            }
  205.        }
  206.        private void insertData(lymphocyte l1)
  207.        {
  208.            string str = Convert.Convert_to_str(l1.detector_string);
  209.            string conStr = "server=127.0.0.1;user=root;" +
  210.                             "database=test;password=123;";
  211.  
  212.            using (MySqlConnection con = new MySqlConnection(conStr))
  213.            {
  214.                try
  215.                {
  216.  
  217.  
  218.                    string sql = "INSERT INTO DETECTORS (value)" +
  219.                                 "VALUES (" + str + ")";
  220.  
  221.                    MySqlCommand cmd = new MySqlCommand(sql, con);
  222.  
  223.                    con.Open();
  224.  
  225.                    cmd.ExecuteNonQuery();
  226.  
  227.                    MessageBox.Show("saved");
  228.  
  229.                }
  230.  
  231.                catch (Exception ex)
  232.                {
  233.                    MessageBox.Show(ex.Message);
  234.                }
  235.            }
  236.        }
  237.        
  238.  
  239.        public List<lymphocyte> Selection(int aff)
  240.         {
  241.  
  242.  
  243.             List<lymphocyte> l = new List<lymphocyte>();
  244.             foreach (lymphocyte lym in immune_system.lympho)
  245.             {
  246.                 int i = 0;
  247.                 foreach (antigens ant in immune_system.anti) {
  248.  
  249.                     immune_system.affinity[i] += lym.get_affinity_Hamming(lym, (lymphocyte)anti);
  250.  
  251.                 }
  252.  
  253.                 i++;
  254.             }
  255.  
  256.             foreach (lymphocyte lym in immune_system.lympho)
  257.             {
  258.                 int i = 0;
  259.                 if (immune_system.affinity[i] > aff)
  260.                 {
  261.                     l.Add(lym);
  262.                 }
  263.                 i++;
  264.             }
  265.  
  266.             return l;
  267.  
  268.         } }
  269.     public class antigens
  270.     {
  271.         bool[] antigen_string;
  272.         public antigens(int length_of_string)
  273.         {
  274.             immune_system.anti.Add(this);
  275.         }
  276.     } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement