Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class Convert
- {
- private static readonly Dictionary<char, string> hexCharacterToBinary = new Dictionary<char, string> {
- { '0', "0000" },
- { '1', "0001" },
- { '2', "0010" },
- { '3', "0011" },
- { '4', "0100" },
- { '5', "0101" },
- { '6', "0110" },
- { '7', "0111" },
- { '8', "1000" },
- { '9', "1001" },
- { 'a', "1010" },
- { 'b', "1011" },
- { 'c', "1100" },
- { 'd', "1101" },
- { 'e', "1110" },
- { 'f', "1111" }
- };
- public static string HexStringToBinary(string hex)
- {
- StringBuilder result = new StringBuilder();
- foreach (char c in hex)
- {
- result.Append(hexCharacterToBinary[char.ToLower(c)]);
- }
- return result.ToString();
- }
- public static string Convert_to_str(bool[] arr)
- {
- string str = "";
- for (int i = 0; i < arr.Length; i++)
- {
- if (arr[i] == true) {
- str += "1";
- }
- else
- {
- str += "0";
- }
- }
- return str;
- }
- }
- public class lymphocyte
- {
- public bool[] detector_string;
- public double current_affinity;
- bool active;
- double affinity;
- public static bool[] get_random_string(int size)
- {
- bool[] random_string = new bool[size];
- for (int i = 0; i < size; i++)
- {
- Random r = new Random();
- if (r.NextDouble() > 0.5)
- {
- random_string[i] = true;
- }
- else random_string[i] = false;
- }
- return random_string;
- }
- public lymphocyte(int length_of_string, int size_of_lympho)
- {
- for (int i = 0; i < size_of_lympho; i++)
- {
- lymphocyte lym = new lymphocyte(length_of_string);
- lym.detector_string = get_random_string(length_of_string);
- immune_system.lympho.Add(lym);
- }
- }
- public lymphocyte(int length_of_string)
- {
- immune_system.lympho.Add(this);
- }
- public void get_affinity_related_bits(lymphocyte l1, lymphocyte l2)
- {
- string str1 = Convert.Convert_to_str(l1.detector_string);
- string str2 = Convert.Convert_to_str(l1.detector_string);
- List<int[]> num = new List<int[]>();
- int maxlen = 0;
- for (int i = 0; i < str1.Length; i++)
- {
- num.Add(new int[str2.Length]);
- for (int j = 0; j < str2.Length; j++)
- {
- if (str1[i] != str2[j])
- num[i][j] = 0;
- else
- {
- if ((i == 0) || (j == 0))
- num[i][j] = 1;
- else
- num[i][j] = 1 + num[i - 1][j - 1];
- if (num[i][j] > maxlen)
- maxlen = num[i][j];
- }
- if (i >= 2)
- num[i - 2] = null;
- }
- }
- this.current_affinity = maxlen;
- }
- public void get_affinity_Hamming(lymphocyte l1, lymphocyte l2)
- {
- string string1 = Convert.Convert_to_str(l1.detector_string);
- string string2 = Convert.Convert_to_str(l1.detector_string);
- if (string1 == null) throw new ArgumentNullException("string1");
- if (string2 == null) throw new ArgumentNullException("string2");
- int diff;
- int[,] m = new int[string1.Length + 1, string2.Length + 1];
- for (int i = 0; i <= string1.Length; i++) { m[i, 0] = i; }
- for (int j = 0; j <= string2.Length; j++) { m[0, j] = j; }
- for (int i = 1; i <= string1.Length; i++)
- {
- for (int j = 1; j <= string2.Length; j++)
- {
- diff = (string1[i - 1] == string2[j - 1]) ? 0 : 1;
- m[i, j] = Math.Min(Math.Min(m[i - 1, j] + 1,
- m[i, j - 1] + 1),
- m[i - 1, j - 1] + diff);
- }
- }
- this.affinity = m[string1.Length, string2.Length];
- }
- public void get_active()
- {
- if (this.current_affinity > affinity)
- {
- this.active = true;
- }
- }
- }
- public class immune_system
- { public static double stop;
- int size_of_lympho;
- public static double[] affinity;
- public static List<lymphocyte> lympho;
- public static List<antigens> anti;
- public lymphocyte Crossingover(lymphocyte l1, lymphocyte l2, int len)
- {
- bool[] random_str = lymphocyte.get_random_string(len);
- lymphocyte l = new lymphocyte(len);
- immune_system.lympho.Add(l);
- for (int i = 0; i < len; i++)
- {
- if (random_str[i] == true)
- {
- l.detector_string[i] = l1.detector_string[i];
- }
- else
- {
- l.detector_string[i] = l2.detector_string[i];
- }
- }
- return l;
- }
- public lymphocyte Mutation(lymphocyte l1)
- {
- for (int i = 0; i < l1.detector_string.Length; i++)
- {
- l1.detector_string[i] = !l1.detector_string[i];
- }
- return l1;
- }
- public void program_death()
- {
- foreach (lymphocyte l in immune_system.lympho)
- {
- if (l.current_affinity < immune_system.stop)
- {
- lympho.Remove(l);
- }
- }
- }
- private void insertData(lymphocyte l1)
- {
- string str = Convert.Convert_to_str(l1.detector_string);
- string conStr = "server=127.0.0.1;user=root;" +
- "database=test;password=123;";
- using (MySqlConnection con = new MySqlConnection(conStr))
- {
- try
- {
- string sql = "INSERT INTO DETECTORS (value)" +
- "VALUES (" + str + ")";
- MySqlCommand cmd = new MySqlCommand(sql, con);
- con.Open();
- cmd.ExecuteNonQuery();
- MessageBox.Show("saved");
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- }
- public List<lymphocyte> Selection(int aff)
- {
- List<lymphocyte> l = new List<lymphocyte>();
- foreach (lymphocyte lym in immune_system.lympho)
- {
- int i = 0;
- foreach (antigens ant in immune_system.anti) {
- immune_system.affinity[i] += lym.get_affinity_Hamming(lym, (lymphocyte)anti);
- }
- i++;
- }
- foreach (lymphocyte lym in immune_system.lympho)
- {
- int i = 0;
- if (immune_system.affinity[i] > aff)
- {
- l.Add(lym);
- }
- i++;
- }
- return l;
- } }
- public class antigens
- {
- bool[] antigen_string;
- public antigens(int length_of_string)
- {
- immune_system.anti.Add(this);
- }
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement