コード例 #1
0
        public virtual void SetControllerFor(ref GADataSet.ProblemsRow p, int size)
        {
            if (p == null)
            {
                throw new Exception("No Problem ID given");
            }
            //DETERMINE CONDITIONS from PROBLEM ID
            conditions = p.GetConditionsRows();
            if (conditions == null)
            {
                throw new Exception("No Problem Conditions given");
            }

            problemData = p.GetKnapDataRows();

            if (problemData.Length == 0)
            {
                //todo:
                throw new Exception("No Problem Variables and Values given");
            }

            SIZE = size;

            PROBLEMID = p.ProblemID;

            variableNames = problemData.FirstOrDefault()
                            .Table.Columns.OfType <DataColumn>()
                            .Where(o => !o.ColumnName.Contains("ID"))
                            .Select(o => o.ColumnName).ToArray();
        }
コード例 #2
0
ファイル: Aid.cs プロジェクト: fulviofarina/GAlgo
        /// <summary>
        /// FUNCTION TO PERFORM AVERAGE AND HISTOGRAM
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="problema"></param>
        public static void DoStatistics <T>(T problema)
        {
            GADataSet.ProblemsRow currentProblem = problema as GADataSet.ProblemsRow;

            IEnumerable <GADataSet.GARow> gasrows = currentProblem.GetGARows();

            int min = gasrows.Min(o => o.ChromosomeLength);
            int max = gasrows.Max(o => o.ChromosomeLength);

            HashSet <string> hash;
            List <GADataSet.SolutionsRow> list;

            //    List<GADataSet.StringsRow> strs;
            hash = new HashSet <string>();
            list = new List <GADataSet.SolutionsRow>();
            //    strs = new List<GADataSet.StringsRow>();

            //create lamda expression to filter
            Func <GADataSet.SolutionsRow, bool> funcion;

            funcion = FilterByGenotype(ref hash, ref list);

            //iterate min max chromosome lenght for all genetic algorithms
            for (int j = min; j <= max; j++)
            {
                gasrows = currentProblem.GetGARows();

                IEnumerable <GADataSet.GARow> subs = gasrows
                                                     .Where(o => o.ChromosomeLength == j)
                                                     .ToList();

                GADataSet.GARow subFirst = subs.FirstOrDefault();
                subFirst.AverageGA(ref subs);

                //select GARow childrens
                IEnumerable <GADataSet.SolutionsRow> knaprows = subs
                                                                .SelectMany(o => o.GetSolutionsRows()).ToList();

                hash.Clear(); //clear
                list.Clear(); //clear

                foreach (GADataSet.SolutionsRow item in knaprows)
                {
                    funcion(item);
                }

                double bestFitness = list.Max(o => o.Fitness);
                subFirst.Fitness = bestFitness;

                int count = knaprows.Count();

                for (int d = knaprows.Count() - 1; d >= 0; d--)
                {
                    GADataSet.SolutionsRow s = knaprows.ElementAt(d);
                    if (s.ShouldDelete)
                    {
                        if (s.StringsRowParent != null)
                        {
                            s.StringsRowParent.Delete();
                        }
                        s.Delete();
                    }
                    else
                    {
                        s.GAID         = subFirst.ID;
                        s.Generations /= s.Counter;
                        s.TimeSpan    /= s.Counter;
                    }
                }

                count = subs.Count();
                for (int d = subs.Count() - 1; d >= 1; d--)
                {
                    subs.ElementAt(d).Delete();
                }
            }
        }