public void todo(IDataSet dataSet) { var randomGenerator = new UniformRandomGenerator(); var possibleFunctions = new List <IGenoTypeNode> { new Multiplication(), new Plus(), new Minus() }; var possibleTerminals = new List <IGenoTypeNode>(); foreach (var mappedColumn in dataSet.MappedColumns) { possibleTerminals.Add(new FeatureTerminal(mappedColumn.Key)); } var eaGeneExpressionParameters = new EaGeneExpressionParameters(10, possibleFunctions, possibleTerminals); var parameterTerminalFactory = new ParameterTerminalFactory(eaGeneExpressionParameters, randomGenerator); var genoTypeFactory = new GenoTypeFactory(eaGeneExpressionParameters, randomGenerator, parameterTerminalFactory); var genoType = genoTypeFactory.GetGenoType(); var phenoTypeTree = new PhenoTypeTree(genoType.GenoTypeNodes); var stringExpresssion = phenoTypeTree.ToString(); // TODO make mapped columns more sophisticated var mappedColumnsUsedInExpression = new Dictionary <string, int>(); foreach (var mappedColumn in dataSet.MappedColumns) { if (stringExpresssion.Contains(mappedColumn.Key)) { mappedColumnsUsedInExpression.Add(mappedColumn.Key, mappedColumn.Value); } } var expression = new Expression(stringExpresssion); var numberOfRows = dataSet.MappedData.GetLength(0); var sum = 0.0; for (var row = 0; row < numberOfRows; row++) { foreach (var usedMappedColumn in mappedColumnsUsedInExpression) { expression.Parameters[usedMappedColumn.Key.Replace("]", "").Replace("[", "")] = dataSet.MappedData[row, usedMappedColumn.Value]; } if (!expression.HasErrors()) { var test = (double)expression.Evaluate(); sum = sum + test; } } }
public static IObjectiveValues GetObjectiveValues(Target <double> target, IDataSet dataSet, Individual individual) { var phenoTypeTree = new PhenoTypeTree(individual.GenoType.GenoTypeNodes); var stringExpresssion = phenoTypeTree.ToString(); // TODO make mapped columns more sophisticated var mappedColumnsUsedInExpression = new Dictionary <string, int>(); foreach (var mappedColumn in dataSet.MappedColumns) { if (stringExpresssion.Contains(mappedColumn.Key)) { mappedColumnsUsedInExpression.Add(mappedColumn.Key.Replace("[", "").Replace("]", "").Trim(), mappedColumn.Value); } } var expression = new Expression(stringExpresssion); var numberOfRows = (double)dataSet.MappedData.GetLength(0); double mae = 0; //var rmse = 0m; var maxError = 0m; for (var row = 0; row < numberOfRows; row++) { foreach (var usedMappedColumn in mappedColumnsUsedInExpression) { expression.Parameters[usedMappedColumn.Key] = dataSet.MappedData[row, usedMappedColumn.Value]; } if (!expression.HasErrors()) { var prediction = (double)expression.Evaluate(); if (double.IsNaN(prediction) || double.IsInfinity(prediction)) { mae = double.MaxValue; //rmse = decimal.MaxValue; //maxError = decimal.MaxValue; break; } var error = prediction - target.Values[row]; var absError = Math.Abs(error); //if (absError > (double) maxError) //{ // if (absError <= (double) decimal.MaxValue) // { // maxError = (decimal)absError; // } // else // { // maxError = decimal.MaxValue; // } //} mae = mae + Math.Abs(error); } else { mae = double.MaxValue; //rmse = decimal.MaxValue; //maxError = decimal.MaxValue; break; } } if (mae != double.MaxValue) { mae = Math.Log(mae + 1.0); //mae = mae / numberOfRows; } var fix = 13; if (mae >= (double)decimal.MaxValue) { mae = fix; } //if (rmse != decimal.MaxValue) //{ //rmse = Sqrt( rmse / numberOfRows); //} var numberOfNodes = PhenoTypeTree.GetNumberOfNodes(individual.GenoType.GenoTypeNodes); //var numberOfCharacters = stringExpresssion.Length; //var distinctNumberOfFeatures = mappedColumnsUsedInExpression.Count; //Console.WriteLine(mse + " " + numberOfOpenBrackets); // string[] names = { "sMape", "OpenBrackets", "Mse" }; // double[] values = { sMape, numberOfOpenBrackets, mse }; string[] names = { "mae", "numberOfNodes" }; decimal[] values = { (decimal)mae, numberOfNodes }; var objectiveValues = new ObjectiveValues(values, names, individual.Guid) { ExpressionForDebugging = stringExpresssion }; if (mae == fix) { objectiveValues.Invalid = true; } return(objectiveValues); }