public static void Mate(Brain p, Brain q, out Brain c1, out Brain c2) { c1 = p.EmptyChild(); c2 = p.EmptyChild(); c1.Weight1 = new Matrix(c1.Hidden1.Rows, c1.Input.Rows); c1.Weight2 = new Matrix(c1.Hidden2.Rows, c1.Hidden1.Rows); c1.Weight3 = new Matrix(c1.Hidden3.Rows, c1.Hidden2.Rows); c1.Weight4 = new Matrix(c1.Output.Rows, c1.Hidden3.Rows); c2.Weight1 = new Matrix(c2.Hidden1.Rows, c2.Input.Rows); c2.Weight2 = new Matrix(c2.Hidden2.Rows, c2.Hidden1.Rows); c2.Weight3 = new Matrix(c2.Hidden3.Rows, c2.Hidden2.Rows); c2.Weight4 = new Matrix(c2.Output.Rows, c2.Hidden3.Rows); #region swap genes if (r.NextDouble() > 0.5) { c1.Bias1 = p.Bias1; c2.Bias1 = q.Bias1; } else { c1.Bias1 = q.Bias1; c2.Bias1 = p.Bias1; } if (r.NextDouble() > 0.5) { c1.Bias2 = p.Bias2; c2.Bias2 = q.Bias2; } else { c1.Bias2 = q.Bias2; c2.Bias2 = p.Bias2; } if (r.NextDouble() > 0.5) { c1.Bias3 = p.Bias3; c2.Bias3 = q.Bias3; } else { c1.Bias3 = q.Bias3; c2.Bias3 = p.Bias3; } if (r.NextDouble() > 0.5) { c1.Bias4 = p.Bias4; c2.Bias4 = q.Bias4; } else { c1.Bias4 = q.Bias4; c2.Bias4 = p.Bias4; } for (int i = 0; i < p.Weight1.Rows; i++) { for (int j = 0; j < p.Weight1.Columns; j++) { if (r.NextDouble() > 0.5) { c1.Weight1.Data[i][j] = p.Weight1.Data[i][j]; c2.Weight1.Data[i][j] = q.Weight1.Data[i][j]; } else { c2.Weight1.Data[i][j] = q.Weight1.Data[i][j]; c1.Weight1.Data[i][j] = p.Weight1.Data[i][j]; } } } for (int i = 0; i < p.Weight2.Rows; i++) { for (int j = 0; j < p.Weight2.Columns; j++) { if (r.NextDouble() > 0.5) { c1.Weight2.Data[i][j] = p.Weight2.Data[i][j]; c2.Weight2.Data[i][j] = q.Weight2.Data[i][j]; } else { c2.Weight2.Data[i][j] = q.Weight2.Data[i][j]; c1.Weight2.Data[i][j] = p.Weight2.Data[i][j]; } } } for (int i = 0; i < p.Weight3.Rows; i++) { for (int j = 0; j < p.Weight3.Columns; j++) { if (r.NextDouble() > 0.5) { c1.Weight3.Data[i][j] = p.Weight3.Data[i][j]; c2.Weight3.Data[i][j] = q.Weight3.Data[i][j]; } else { c2.Weight3.Data[i][j] = q.Weight3.Data[i][j]; c1.Weight3.Data[i][j] = p.Weight3.Data[i][j]; } } } for (int i = 0; i < p.Weight4.Rows; i++) { for (int j = 0; j < p.Weight4.Columns; j++) { if (r.NextDouble() > 0.5) { c1.Weight4.Data[i][j] = p.Weight4.Data[i][j]; c2.Weight4.Data[i][j] = q.Weight4.Data[i][j]; } else { c2.Weight4.Data[i][j] = q.Weight4.Data[i][j]; c1.Weight4.Data[i][j] = p.Weight4.Data[i][j]; } } } #endregion c1 = c1.Evolve(0.02, 0.0001); c2 = c2.Evolve(0.02, 0.0001); }
public Brain EmptyChild() { Brain child = new Brain(Input.Rows, Hidden1.Rows, Hidden2.Rows, Hidden3.Rows, Output.Rows); return(child); }