using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightApplication14 { public class Gene { public int[] genotype; // 遺伝子型 public int[] phenotype; // 表現型 public double point; public double prob; // private Random rnd; private jp.takel.PseudoRandom.MersenneTwister rnd; // size=20 のときの、知られている最適解 // private int[] bestOf20 = { // 5, 10, 13, 15, 16, 17, 17, 18, 18, 18, // 19, 19, 19, 19, 19, 19, 19, 19, 19, 19 // }; public Gene(int size, jp.takel.PseudoRandom.MersenneTwister rnd0) { genotype = new int[size]; phenotype = new int[size]; rnd = rnd0; // 乱数系列を引き渡す init(size); } // 乱数で初期化する protected void init(int size) { for (int i = 0; i < size; i++) { genotype[i] = rnd.Next(size); // 0 ~ size-1 の範囲の数が入る } // bestOf20.CopyTo(genotype, 0); // * DEBUG * 試しに最適解で初期化してみる } // 遺伝子から表現型を作成する public void invoke() { int[] histgram = new int[genotype.Length]; int i; // ヒストグラムを集計する // for (i = 0; i < acts.Length; i++) // { // 0で初期化は不要っぽい // histgram[i] = 0; // } for (i = 0; i < genotype.Length; i++) { histgram[genotype[i]]++; } // 表現型を作る int foot = 0; for (i = 0; i < genotype.Length; i++) { foot += histgram[i]; phenotype[i] = foot; } } // 新たなコピーを返す public Gene Copy() { Gene new_g = new Gene(genotype.Length, rnd); genotype.CopyTo(new_g.genotype, 0); // 配列コピー phenotype.CopyTo(new_g.phenotype, 0); new_g.point = point; new_g.prob = prob; return new_g; } // 遺伝子の文字列表現 public String print() { String str = ""; int i; // 遺伝子 // for (i = 0; i < genotype.Length; i++) // { // str += genotype[i].ToString() + ", "; // } // str += "\n"; // 表現型 for (i = 0; i < genotype.Length; i++) { str += phenotype[i].ToString() + ", "; } // str += "\n"; return str; } } }