/// <summary> /// Evaluates function agains terminals /// </summary> /// <param name="chromosome"></param> /// <param name="functionSet"></param> /// <returns></returns> public float Evaluate(IChromosome chromosome, IFunctionSet functionSet) { var ch = chromosome as GAVChromosome; if (ch == null) { return(0); } else { double y = 0, fitness; int numVar = ch.Length; int numRow = ch.Length;//Alocation problem is squared for (int i = 0; i < numVar; i++) { var v = Globals.GetTerminalValue(i, ch.Value[i]); if (IsMinimize) { if (double.MinValue == v) { return(float.PositiveInfinity); } } else { if (double.MaxValue == v) { return(float.NegativeInfinity); } } // calculate distance between two points and make the sum y += v * 1; // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { return(float.NaN); } } //with this value we always search for maximum value of fitness if (IsMinimize) { fitness = ((1.0 / (1.0 + y)) * 1000.0); } else { fitness = y; } return((float)fitness); } }
public float Evaluate(IChromosome ch, IFunctionSet functionSet) { var expTree = ((GPChromosome)ch).expressionTree; double fitness = 0; double rowFitness = 0.0; double y; //index of output parameter int indexOutput = Globals.gpterminals.NumConstants + Globals.gpterminals.NumVariables; for (int i = 0; i < Globals.gpterminals.RowCount; i++) { // evalue the function agains eachh rowData y = functionSet.Evaluate(expTree, i); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { return(float.NaN); } //this fitness algoritm is specialized only for Binary and Classification problems if (m_ProblemType == ColumnDataType.Categorical || m_ProblemType == ColumnDataType.Binary) { var valClass = Experiment.getCategoryFromNumeric(y, Globals.classCount); if (double.IsNaN(valClass)) { return(float.NaN); } //check is the calculated value correct var val = Globals.gpterminals.TrainingData[i][indexOutput] == valClass ? 1 : 0; //add the result to the fitness rowFitness += val; } else//thros exception if the problems is not as expected { throw new Exception("Problem type is unknown!"); } } if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness)) { fitness = float.NaN; } else { fitness = (float)(rowFitness / Globals.gpterminals.RowCount) * 1000.0; } return((float)Math.Round(fitness, 2)); }
private static void validateFunctionSet() { if (functions != null && functions.GetFunctions() != null) { return; } var fs = new GPFunctionSet(); var fnsc = GenerateGPFunctionsFromXML(); fs.SetFunction(fnsc); functions = fs; }
public float Evaluate(IChromosome ch, IFunctionSet functionSet) { var expTree = ((GPChromosome)ch).expressionTree; double fitness = 0; double rowFitness = 0.0; double y, SS_tot = 0; //index of output parameter int indexOutput = Globals.gpterminals.NumConstants + Globals.gpterminals.NumVariables; for (int i = 0; i < Globals.gpterminals.RowCount; i++) { // evalue the function agains eachh rowData y = functionSet.Evaluate(expTree, i); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { return(float.NaN); } //Calculate square error rowFitness += Math.Pow(y - Globals.gpterminals.TrainingData[i][indexOutput], 2); SS_tot += Math.Pow(Globals.gpterminals.TrainingData[i][indexOutput] - Globals.gpterminals.AverageValue, 2); } if (SS_tot != 0) { rowFitness = rowFitness / SS_tot; } if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness)) { fitness = float.NaN; } else { fitness = (float)((1.0 / (1.0 + rowFitness / Globals.gpterminals.RowCount)) * 1000.0); } return((float)Math.Round(fitness, 2)); }
/// <summary> /// Evaluates function agains terminals /// </summary> /// <param name="chromosome"></param> /// <param name="functionSet"></param> /// <returns></returns> public float Evaluate(IChromosome chromosome, IFunctionSet functionSet) { var ch = chromosome as GAVChromosome; if (ch == null) { return(0); } else { double y = 0, fitness; double[] p1 = null; double[] p2 = null; int rCount = ch.Length; for (int i = 0; i < rCount; i++) { p1 = Globals.GetTerminalRow(ch.Value[i]); if (i + 1 == rCount) { p2 = Globals.GetTerminalRow(ch.Value[0]); } else { p2 = Globals.GetTerminalRow(ch.Value[i + 1]); } // calculate distance betwee two points and make the sum y += Math.Sqrt((p2[0] - p1[0]) * (p2[0] - p1[0]) + (p2[1] - p1[1]) * (p2[1] - p1[1])); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { return(float.NaN); } } //with this value we always search for maximum value of fitness fitness = ((1.0 / (1.0 + y)) * 1000.0); return((float)fitness); } }
public float Evaluate(IChromosome ch, IFunctionSet functionSet) { var expTree = ((GPChromosome)ch).expressionTree; double fitness = 0; double rowFitness = 0.0; double y; //index of output parameter int indexOutput = Globals.gpterminals.NumConstants + Globals.gpterminals.NumVariables; for (int i = 0; i < Globals.gpterminals.RowCount; i++) { // evalue the function agains eachh rowData y = functionSet.Evaluate(expTree, i); // check for correct numeric value if (double.IsNaN(y) || double.IsInfinity(y)) { return(float.NaN); } var temp = y - Globals.gpterminals.TrainingData[i][indexOutput]; rowFitness += temp * temp; } if (double.IsNaN(rowFitness) || double.IsInfinity(rowFitness)) { fitness = float.NaN; } else { fitness = (float)((1.0 / (1.0 + rowFitness)) * 1000.0); } return((float)Math.Round(fitness, 2)); }
/// <summary> /// Evaluates function agains terminals /// </summary> /// <param name="chromosome"></param> /// <param name="functionSet"></param> /// <returns></returns> public float Evaluate(IChromosome chromosome, IFunctionSet functionSet) { GANumChromosome ch = chromosome as GANumChromosome; if (ch == null) { return(0); } else { //prepare terminals var term = Globals.gpterminals.SingleTrainingData; for (int i = 0; i < ch.val.Length; i++) { term[i] = ch.val[i]; } var y = functionSet.Evaluate(_funToOptimize, -1); if (double.IsNaN(y) || double.IsInfinity(y)) { y = float.NaN; } //Save output in to output variable term[term.Length - 1] = y; if (IsMinimize) { y *= -1; } return((float)y); } }
/// <summary> /// /// </summary> /// <param name="fSet"></param> public void SetFunctionSet(IFunctionSet fSet) { functionSet = fSet; Globals.functions = fSet; }
/// <summary> /// Initialisation of chromosomes in Genetic programming /// </summary> /// <param name="size">Size of the chromosomes, default value is 1000</param> /// <param name="initMethod">Initializastion method, default HalfandHalf</param> public void InitPopulation( GPTerminalSet termSet, IFunctionSet funSet, GPParameters gpParams = null ) { if (gpParams == null) { Globals.gpparameters = new GPParameters(); } else { Globals.gpparameters = gpParams; } if (Globals.gpparameters.algorithmType == Algorithm.GA) { if (Globals.gpparameters.chromosomeKind == GAChromosome.Continue) { GANumChromosome.functionSet = funSet; } else if (Globals.gpparameters.chromosomeKind == GAChromosome.TSP) { GAVChromosome.functionSet = funSet; } else if (Globals.gpparameters.chromosomeKind == GAChromosome.ALOC) { GAVChromosome.functionSet = funSet; } else if (Globals.gpparameters.chromosomeKind == GAChromosome.TP) { GAMChromosome.terminalSet = termSet; GAMChromosome.functionSet = funSet; } else { GABinChromosome.functionSet = funSet; } } else { GPChromosome.MaxOperationLevel = Globals.gpparameters.maxOperationLevel; } //init default fitness type if (Globals.gpparameters.GPFitness == null) { Globals.gpparameters.GPFitness = new RMSEFitness(); } fitnessFunction = Globals.gpparameters.GPFitness; if (funSet == null) { throw new Exception("FunctionSet is not defined, and cannot be null!"); } else { functionSet = funSet; Globals.functions = functionSet; } if (termSet == null) { throw new Exception("TerminalSet is not defined, and cannot be null!"); } else { Globals.gpterminals = termSet; } var siz = Globals.gpparameters.popSize - chromosomes.Count; if (siz > 0) { GeneratePopulation(siz); } }