}//获得当前种群的最优适应度 public void Initializer(ref GAChromosome GAChro) { int flag1 = 0; int flag2 = 1; double flag3; while (flag2 == 1) { for (int i = 0; i < p; i++) { flag3 = m_Random.NextDouble(); if (flag3 > 0.5 && flag1 != r) { GAChro.Add(1); flag1++; } else { GAChro.Add(0); } } if (flag1 == r) { flag2 = 0; } else { GAChro.Clear(); flag1 = 0; } } }//染色体初始化
}//染色体初始化 public void Initializer1(ref GAChromosome GAChro) { List <int> ones = new List <int>(); ones.Clear(); while (ones.Count < p) { int temp = m_Random.Next(0, n); bool flag = true; for (int i = 0; i < ones.Count; i++) { if (temp == ones[i]) { flag = false; continue; } } if (flag) { ones.Add(temp); } } GAChro.Clear(); for (int i = 0; i < n; i++) { bool flag = true; for (int j = 0; j < ones.Count; j++) { if (ones[j] == i) { GAChro.Add(1); ones.Remove(i); flag = false; } } if (flag) { GAChro.Add(0); } } }