Пример #1
0
        public void PerformCrossover(ref IGenoType genoTypeLeft, ref IGenoType genoTypeRight)
        {
            if ((new PhenoTypeTree(genoTypeLeft.GenoTypeNodes)).ToString().Equals((new PhenoTypeTree(genoTypeRight.GenoTypeNodes)).ToString()))
            {
                return;
            }

//            var left1 = new PhenoTypeTree(genoTypeLeft.GenoTypeNodes).ToString();
//            var right1 = new PhenoTypeTree(genoTypeRight.GenoTypeNodes).ToString();

            if (!(UniformRandomGenerator.GetContinousRandomNumber(0, 1.0) <= EaGeneExpressionParameters.CrossoverProbability))
            {
                return;
            }

            if (UniformRandomGenerator.GetContinousRandomNumber(0, 1.0) < 0.5)
            {
                var numberOfNodes      = PhenoTypeTree.GetNumberOfNodes(genoTypeLeft.GenoTypeNodes);
                var crossOverPosition1 = UniformRandomGenerator.GetIntegerRandomNumber(0, numberOfNodes - 1);

                if (UniformRandomGenerator.GetContinousRandomNumber(0, 1.0) < 0.1)
                {
                    for (var index = crossOverPosition1; index < genoTypeLeft.GenoTypeNodes.Count; index++)
                    {
                        Swap(genoTypeLeft.GenoTypeNodes, genoTypeRight.GenoTypeNodes, index);
                    }
                }
                else
                {
                    for (var index = 0; index <= crossOverPosition1; index++)
                    {
                        Swap(genoTypeLeft.GenoTypeNodes, genoTypeRight.GenoTypeNodes, index);
                    }
                }
            }
            else
            {
                var crossOverPosition1 = UniformRandomGenerator.GetIntegerRandomNumber(1, genoTypeLeft.GenoTypeNodes.Count - 2);
                var crossOverPosition2 = UniformRandomGenerator.GetIntegerRandomNumber(crossOverPosition1, genoTypeLeft.GenoTypeNodes.Count - 2);

                for (var index = crossOverPosition1; index < crossOverPosition2; index++)
                {
                    Swap(genoTypeLeft.GenoTypeNodes, genoTypeRight.GenoTypeNodes, index);
                }
            }

//            var left2 = new PhenoTypeTree(genoTypeLeft.GenoTypeNodes).ToString();
//            var right2 = new PhenoTypeTree(genoTypeRight.GenoTypeNodes).ToString();
        }
Пример #2
0
        public void PerformMutation(ref IGenoType genoType)
        {
            if (!(UniformRandomGenerator.GetContinousRandomNumber(0, 1.0) <= EaGeneExpressionParameters.MutationProbability))
            {
                return;
            }

            var randomNumber = UniformRandomGenerator.GetContinousRandomNumber(0, 1.0);

            if (randomNumber <= 0.3)
            {
                genoType = GenoTypeFactory.GetGenoType();
            }
            else
            {
                //var index = UniformRandomGenerator.GetIntegerRandomNumber(0, genoType.GenoTypeNodes.Count - 1);
                var numberOfNodes = PhenoTypeTree.GetNumberOfNodes(genoType.GenoTypeNodes);
                var index         = UniformRandomGenerator.GetIntegerRandomNumber(0, numberOfNodes - 1);

                if (randomNumber <= 0.6)
                {
                    var node = genoType.GenoTypeNodes[index];
                    genoType.GenoTypeNodes.RemoveAt(index);
                    index = UniformRandomGenerator.GetIntegerRandomNumber(0, genoType.GenoTypeNodes.Count - 1);
                    genoType.GenoTypeNodes.Insert(index, node);
                }
                else
                {
                    if (genoType.GenoTypeNodes[index].GetType() == typeof(ParameterTerminal))
                    {
                        ((ParameterTerminal)genoType.GenoTypeNodes[index]).Value =
                            ((ParameterTerminal)genoType.GenoTypeNodes[index]).Value +
                            UniformRandomGenerator.GetContinousRandomNumber(-3.0, 3.0);
                    }
                    else
                    {
                        if (index < EaGeneExpressionParameters.MaximumNumberOfHeadNodes)
                        {
                            genoType.GenoTypeNodes[index] = GenoTypeFactory.GetFunctionOrTerminalNode();
                        }
                        else
                        {
                            genoType.GenoTypeNodes[index] = GenoTypeFactory.GetTerminalNode();
                        }
                    }
                }
            }
        }
Пример #3
0
        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);
        }