public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c) { c.Fitness = 0; double rowFitness = 0.0; double val1 = 0; double y; // copy constants //Translate chromosome to list expressions int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables; for (int i = 0; i < gpTerminalSet.RowCount; i++) { // evalue the function y = gpFunctionSet.Evaluate(lst, gpTerminalSet, i); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { y = 0; } val1 += System.Math.Pow(((y - gpTerminalSet.TrainingData[i][indexOutput]) / gpTerminalSet.TrainingData[i][indexOutput]), 2.0); } rowFitness = val1 / gpTerminalSet.RowCount; if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness)) { //if output is not a number return infinity fitness c.Fitness = 0; return; } //Fitness c.Fitness = (float)((1.0 / (1.0 + rowFitness)) * 1000.0); }
public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c) { c.Fitness = 0; double rowFitness = 0.0; double y, temp; //Translate chromosome to list expressions int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables; for (int i = 0; i < gpTerminalSet.RowCount; i++) { // evalue the function y = gpFunctionSet.Evaluate(lst, gpTerminalSet, i); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { y = 0; } temp = y - gpTerminalSet.TrainingData[i][indexOutput]; rowFitness += temp * temp; } //Fitness c.Fitness = (float)((1.0 / (1.0 + rowFitness / indexOutput)) * 1000.0); }
public double Evaluate(List <int> tokens, GPTerminalSet gpTerminalSet, int numRow) { int countT = tokens.Count; //Stack fr evaluation Stack <double> arguments = new Stack <double>(); for (int i = 0; i < countT; i++) { //Debug.Assert(tokens[i] != 2004 || countT <= 2); // Ako je token argument onda na osnovu naziv tog argumenta //izvlacimo mu vrijednost iz skupa terminala i konstanti if (tokens[i] >= 1000 && tokens[i] < 2000) { arguments.Push(gpTerminalSet.TrainingData[numRow][tokens[i] - 1000]); } else//Ako je token funkcija tada ubacene argumente evaluiramo preko odredjene funkcije { //Evaluacija funkcije. Svaka funkcija ima bar 1 argument int count = functions[tokens[i] - 2000].Aritry; //Ovdje moramo unazad zapisati varijable zbog Staka double[] val = new double[count]; for (int j = count; j > 0; j--) { Debug.Assert(arguments.Count > 0); val[j - 1] = arguments.Pop(); } //Ako je neka funkcija statistička distribucija ili neka druga funkcija koja // zahtjeva ulazne varijable da bi koristila neku statisticku osobinu // double[] values = null; if (functions[tokens[i] - 2000].IsDistribution) { values = new double[gpTerminalSet.NumVariables]; for (int k = 0; k < gpTerminalSet.NumVariables; k++) { values[k] = gpTerminalSet.TrainingData[numRow][k]; } } double result = Evaluate(functions[tokens[i] - 2000], values, val); if (double.IsNaN(result) || double.IsInfinity(result)) { return(double.NaN); } //Izracunavanje izraza arguments.Push(result); } } // return the only value from stack Debug.Assert(arguments.Count == 1); return(arguments.Pop()); }
public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c) { c.Fitness = 0; double rowFitness = 0.0; double CovTP = 0.0; double SigmaP = 0.0; double SigmaT = 0.0; //number of sample case int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables; double[] y = new double[gpTerminalSet.RowCount]; double ymean = 0; //Calculate output for (int i = 0; i < gpTerminalSet.RowCount; i++) { // evalue the function y[i] = gpFunctionSet.Evaluate(lst, gpTerminalSet, i); // check for correct numeric value if (double.IsNaN(y[i]) || double.IsInfinity(y[i])) { //if output is not a number return infinity fitness c.Fitness = 0; return; } ymean += y[i]; } //calculate AverageValue output ymean = ymean / gpTerminalSet.RowCount; //Calculate Corelation coeficient for (int i = 0; i < gpTerminalSet.RowCount; i++) { CovTP = CovTP + ((gpTerminalSet.TrainingData[i][indexOutput] - gpTerminalSet.AverageValue) * (y[i] - ymean)); SigmaP += System.Math.Pow((y[i] - ymean), 2); SigmaT += System.Math.Pow((gpTerminalSet.TrainingData[i][indexOutput] - gpTerminalSet.AverageValue), 2); } rowFitness = CovTP / (System.Math.Sqrt(SigmaP * SigmaT)); if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness)) { //if output is not a number return zero fitness c.Fitness = 0; return; } //Fitness c.Fitness = (float)(rowFitness * rowFitness * 1000.0); }
public string DecodeExpressionInExcellForm(List <int> tokens, GPTerminalSet gpTerminalSet) { //Prepare chromoseme for evaluation // List<int> tokens = new List<int>(); // FunctionTree.ToListExpression(tokens, c.Root); int countT = tokens.Count; //Stack fr evaluation Stack <string> expression = new Stack <string>(); for (int i = 0; i < countT; i++) { //Debug.Assert(tokens[i] != 2004 || countT <= 2); // Ako je token argument onda na osnovu naziv tog argumenta //izvlacimo mu vrijednost iz skupa terminala i konstanti if (tokens[i] >= 1000 && tokens[i] < 2000) { string varaiable = terminals[tokens[i] - 1000].Name; expression.Push(varaiable); } else//Ako je token funkcija tada ubacene argumente evaluiramo preko odredjene funkcije { //Evaluacija funkcije. Svaka funkcija ima bar 1 argument int count = functions[tokens[i] - 2000].Aritry; string function = functions[tokens[i] - 2000].ExcelDefinition; //Ovdje moramo unazad zapisati varijable zbog Staka double[] val = new double[count]; for (int j = count; j > 0; j--) { string oldStr = "x" + (j).ToString(); string newStr = expression.Pop(); function = function.Replace(oldStr, newStr); } if (functions[tokens[i] - 2000].IsDistribution) { string oldStr = "xn"; string newStr = "X" + gpTerminalSet.NumVariables.ToString(); function = function.Replace(oldStr, newStr); function = function.Replace("x0", "X1"); } //Izracunavanje rezultata expression.Push("(" + function + ")"); //Izracunavanje izraza } } // return the only value from stack Debug.Assert(expression.Count == 1); // return arguments.Pop(); return(expression.Pop()); }
public void Evaluate(List <int> lst, GPFunctionSet gpFunctionSet, GPTerminalSet gpTerminalSet, GPChromosome c) { c.Fitness = 0; double rowFitness = 0.0; double SS_err = 0.0; double SS_tot = 0.0; double y; // copy constants //Translate chromosome to list expressions int indexOutput = gpTerminalSet.NumConstants + gpTerminalSet.NumVariables; for (int i = 0; i < gpTerminalSet.RowCount; i++) { // evalue the function y = gpFunctionSet.Evaluate(lst, gpTerminalSet, i); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { y = 0; } //Calculate square error rowFitness += System.Math.Pow(y - gpTerminalSet.TrainingData[i][indexOutput], 2); //Calculate square error SS_err += System.Math.Pow(y - gpTerminalSet.TrainingData[i][indexOutput], 2); SS_tot += System.Math.Pow(gpTerminalSet.TrainingData[i][indexOutput] - gpTerminalSet.AverageValue, 2); } if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness)) { //if output is not a number return zero fitness c.Fitness = 0; return; } //Fitness c.Fitness = (float)((1.0 / (1.0 + System.Math.Sqrt(rowFitness / gpTerminalSet.RowCount))) * 1000.0); }
public string DecodeWithOptimisationExpression(List <int> tokens, GPTerminalSet gpTerminalSet) { // return ""; //Prepare chromoseme for evaluation // List<int> tokens = new List<int>(); // FunctionTree.ToListExpression(tokens, c.Root); int countT = tokens.Count; //Stack fr evaluation Stack <double> arguments = new Stack <double>(); Stack <string> expression = new Stack <string>(); for (int i = 0; i < countT; i++) { //Debug.Assert(tokens[i] != 2004 || countT <= 2); // Ako je token argument onda na osnovu naziv tog argumenta //izvlacimo mu vrijednost iz skupa terminala i konstanti if (tokens[i] >= 1000 && tokens[i] < 2000) { double var; string varaiable = terminals[tokens[i] - 1000].Name; if (terminals[tokens[i] - 1000].IsConstant) { var = gpTerminalSet.TrainingData[0][tokens[i] - 1000]; } else { var = double.NaN; } arguments.Push(var); expression.Push(varaiable); } else//Ako je token funkcija tada ubacene argumente evaluiramo preko odredjene funkcije { //Evaluacija funkcije. Svaka funkcija ima bar 1 argument int count = functions[tokens[i] - 2000].Aritry; string function = functions[tokens[i] - 2000].Definition; //Ovdje moramo unazad zapisati varijable zbog Staka double[] val = new double[count]; for (int j = count; j > 0; j--) { val[j - 1] = arguments.Pop(); string oldStr = "x" + (j).ToString(); string newStr = expression.Pop(); function = function.Replace(oldStr, newStr); } /* * //Izracunavanje rezultata * //Ako je neka funkcija statistička distribucija ili neka druga funkcija koja * // zahtjeva ulazne varijable da bi koristila neku statisticku osobinu * // * double[] values = null; * if (functions[tokens[i] - 2000].IsDistribution) * { * values = new double[gpTerminalSet.NumVariables]; * for (int k = 0; k < gpTerminalSet.NumVariables; k++) * values[k] = gpTerminalSet.TrainingData[numRow][k]; * }*/ double result = Evaluate(functions[tokens[i] - 2000], val); if (!double.IsNaN(result)) { expression.Push(result.ToString()); } else { expression.Push("(" + function + ")"); } //Izracunavanje izraza arguments.Push(result); } } // return the only value from stack Debug.Assert(expression.Count == 1); // return arguments.Pop(); return(expression.Pop()); }
/// <summary> /// Constructor for population. /// </summary> /// <param name="size">Size of population.Number must be greather than 0.</param> /// <param name="terminalSet">Terminal set.Must be nonnull.</param> /// <param name="functionSet">Function set. Must be nonnull.</param> /// <param name="parameters">Parameter of GP. If it pass a null value default parameter is initialized.</param> /// <param name="paralelComp">True for parallel multy core evaluation. False for secvencial computing.</param> public GPPopulation(int size, GPTerminalSet terminalSet, GPFunctionSet functionSet, GPParameters parameters, bool paralelComp) { ParalelComputing = paralelComp; //During population creating the size must be grether than 1, //and papameters must be non null if (size < 1) { return; } //Ako nisu definisani parametri definisi defaultne if (parameters == null) { gpParameters = new GPParameters(); } else { gpParameters = parameters; } if (functionSet == null) { return; } if (terminalSet == null) { return; } // gpFunctionSet = functionSet; gpTerminalSet = terminalSet; //Extend capasity three time cause crossover and mutation population = new List <GPChromosome>(3 * size); if (ParalelComputing) { tempCrossover1 = new List <GPChromosome>(size); tempCrossover2 = new List <GPChromosome>(size); tempMutation = new List <GPChromosome>(); tempPermutation = new List <GPChromosome>(); temCross = new ConcurrentStack <GPChromosome>(); } //Creating starting Chromosome GPChromosome ancestor = new GPChromosome(); //Initialize population InitPopulation(size); //Evaluacija pocetne populacije if (ParalelComputing) { EvaluationParallel(); } else { EvaluationSec(); } //Calculate population statistic parameters CalculatePopulation(); }