public CartesianIndividual(CartesianIndividual other) { Name = IndividualTools.CreateName(); Values = new double[other.Values.Length]; for (int i = 0; i < Values.Length; ++i) { Values[i] = other.Values[i]; } }
public static double EvaluateAckley(CartesianIndividual individual) { double sumOfSquares = individual.Values.Select(i => i * i).Sum(); double sumOfCos = individual.Values.Select(i => Math.Cos(2 * Math.PI * i)).Sum(); double res = -20 * Math.Exp(-0.2 * Math.Sqrt(0.5 * sumOfSquares)) - Math.Exp(0.5 * sumOfCos) + 20; return((float)res); }
private static double _EvaluatePolynomial(CartesianIndividual ind, double[] powersOfX) { double[] coefficients = ind.Values; double polynomialEval = 0; for (int j = 0; j < coefficients.Length; ++j) { polynomialEval += powersOfX[j] * coefficients[j]; } return(polynomialEval); }
public virtual IIndividual Mutate(float probability, float sigma, Random r) { var newInd = new CartesianIndividual(this); for (int i = 0; i < Values.Length; ++i) { if (r.ProbabilityPass(probability)) { newInd.Values[i] += r.GausianNoise(sigma); } } return(newInd); }
//TODO: Fix for cartesian individuals of different sizes public static float Distance(CartesianIndividual a, CartesianIndividual b) { double res = 0; for (int i = 0; i < a.Values.Length && i < b.Values.Length; ++i) { double a_i = i < a.Values.Length ? a.Values[i] : 0; double b_i = i < b.Values.Length ? b.Values[i] : 0; double diff = b_i - a_i; res += diff * diff; } //not technically mandatory res = Math.Sqrt(res); return((float)res); }
public static IIndividual CrossOver(List <IIndividual> parents, Random r) { var parent = (CartesianIndividual)parents[0]; int newLength = parents .Select(i => ((CartesianIndividual)i).Values.Length) .ToList() .PickRandom(); var newInd = new CartesianIndividual(newLength, r, empty: true); for (var ii = 0; ii < newLength; ++ii) { //33% chance of taking single random parent gene, 66% chance of taking average of all parents var dice = r.Next(0, 3); if (dice < 1) { //Take average of parents respective alleles double tot = 0.0f; for (var kk = 0; kk < parents.Count; ++kk) { var par = (CartesianIndividual)parents[kk]; //TODO: is the absence of value zero? if (ii < par.Values.Length) { tot += par.Values[ii]; } } //TODO: is the absence of value zero? tot = tot / parents.Count; newInd.Values[ii] = tot; } else //Take single parent gene randomly { var par = (CartesianIndividual)parents[r.Next(0, parents.Count)]; //TODO: Is the absence of value zero? newInd.Values[ii] = ii < par.Values.Length ? par.Values[ii] : 0; } } return(newInd); }
/// <summary> /// Returns the mean-squared error of the individual if it's values /// represent the coefficients of a nth degree polynomial /// versus the expected values /// </summary> /// <param name="individual">The Cartesian individual made up of n+1 coefficients</param> /// <param name="powsOfXforAllX">First index represents the sample number (m). /// Second index represents the order of the coefficient x[m][n] /// e.g. : x[1][2] = x_1^2 where x_1 is the 1st sample /// </param> /// <param name="expectedVals">The expected values for the sample veing evaluated at that point y[m]</param> /// <returns></returns> public static float PolynomialEval(this CartesianIndividual individual, double[][] powsOfXforAllX, double[] expectedVals) { Debug.Assert(expectedVals.Length == powsOfXforAllX.Length); double score = EvaluatePolynomial(individual, powsOfXforAllX) .Zip(expectedVals, (av, ev) => { double diff = av - ev; return(diff * diff); }) .Sum(); double mse = score / expectedVals.Length; if (double.IsPositiveInfinity(mse)) { Debugger.Break(); } return((float)Math.Log10(mse)); }
public static IEnumerable <double> EvaluatePolynomial(CartesianIndividual ind, double[][] powersOfXForAllX) { return(powersOfXForAllX.Select(pows => _EvaluatePolynomial(ind, pows))); }