Exemple #1
0
        public double[] CalculateOutput(IChromosome chromosome, IParameters param, bool isTrainingData)
        {
            var ch   = ((Chromosome)chromosome);
            var node = ch.expressionTree;

            if (TData == null && isTrainingData == false)//there is no test data
            {
                return(null);
            }

            var rowCount = isTrainingData ? Data.Length : TData.Length;
            var y        = new double[rowCount];

            //
            for (int i = 0; i < rowCount; i++)
            {
                var rowInputData = isTrainingData ? GetDataInputRow(i) : GetTestDataInputRow(i);

                // evaluate the function against each rowData
                if (param.IsMultipleOutput && ch.ExtraData != null)
                {
                    var oneHotVector = node.Evaluate2(rowInputData, param);

                    //calculate Mahanalobis distance for each training sample
                    double minMD            = double.MaxValue;
                    int    predictedCluster = 0;
                    foreach (var c in ch.ExtraData.Keys)
                    {
                        var md = AdvancedStatistics.MD(oneHotVector, ch.ExtraData[c].Item1, ch.ExtraData[c].Item2);

                        //map result with class
                        if (minMD > md)
                        {
                            predictedCluster = c;
                            minMD            = md;
                        }
                    }
                    //map actual and predicted class value
                    y[i] = predictedCluster;
                }
                else// evaluate the function against each rowData
                {
                    y[i] = node.Evaluate(rowInputData, param);
                }
            }

            return(y);
        }
        public float Evaluate(IChromosome ch, IParameters param, int it)
        {
            var iData = GetData();

            var    chr        = (Chromosome)ch;
            var    expTree    = chr.expressionTree;
            double fitness    = 0;
            double rowFitness = 0.0;

            double[] y;

            var evalData    = new List <(int, double[])>();
            var dic         = new Dictionary <int, List <double[]> >();
            var preCalcData = new Dictionary <int, (double[], double[][])>();
            var result      = new List <(int, int)>();

            //for each trainign sample
            for (int i = 0; i < iData.RowCount; i++)
            {
                var rowInputData = iData.GetDataInputRow(i);
                // evaluate the function against each rowData
                y = expTree.Evaluate2(rowInputData, param);
                if (y == null)
                {
                    return(float.NaN);
                }
                //get output Value
                var yo = (int)iData.GetRowOutput(i);

                //add result in to list
                evalData.Add((yo, y));

                //map result with class
                if (dic.ContainsKey(yo))
                {
                    dic[yo].Add(y);
                }
                else
                {
                    dic.Add(yo, new List <double[]>()
                    {
                        y
                    });
                }
            }
            //calculate centroids and Covariance matrix for each cluster
            for (int i = 0; i < dic.Keys.Count; i++)
            {
                //class value
                var c    = dic.Keys.ElementAt(i);
                var vals = dic.Values.ElementAt(i).ToArray();
                //predicted values for the class
                var cData = AdvancedStatistics.ToColumnVector <double>(vals);

                //calculated centroid for the cData
                var cen = cData.Select(x => x.MeanOf()).ToArray();

                //calculate CovarianceMatrix
                var cov = BasicStatistics.Covariance(cData);
                if (!isValid(cov))
                {
                    return(float.NaN);
                }
                preCalcData.Add(c, (cen, cov));
            }

            //for each training sample
            //go through all samples
            foreach (var item in evalData)
            {
                //calculate Mahanalobis distance for each training sample
                double minMD            = double.MaxValue;
                int    predictedCluster = 0;
                foreach (var c in dic.Keys)
                {
                    var md = AdvancedStatistics.MD(item.Item2, preCalcData[c].Item1, preCalcData[c].Item2);

                    //map result with class
                    if (minMD > md)
                    {
                        predictedCluster = c;
                        minMD            = md;
                    }
                }
                //map actual and predicted class value
                result.Add((item.Item1, predictedCluster));
            }
            float corectedPredicted = result.Where(x => x.Item1 == x.Item2).Count();
            var   rawFitness        = corectedPredicted / (float)result.Count;



            if (double.IsNaN(rawFitness) || double.IsInfinity(rawFitness))
            {
                fitness = float.NaN;
            }
            else//calculate adjusted fitness
            {
                fitness = (rawFitness * 1000.0);
            }
            //store extra data into chromosome
            chr.ExtraData = preCalcData;

            return((float)System.Math.Round(fitness, 2));
        }