Exemplo n.º 1
0
        /// <summary>
        /// BASIC CALCULATION NECESSARY FOR FITNESS
        /// </summary>
        /// <param name="r"></param>
        /// <param name="c"></param>
        public override void FillBasic(ref GADataSet.SolutionsRow r)
        {
            GADataSet.DataRow d = r.DataAxuliar.NewDataRow();
            r.DataAxuliar.AddDataRow(d);
            //auxiliar data row
            for (int i = 0; i < VariableNames.Length; i++)
            {
                //Field A, B or C
                double dummy = Aid.SetBasic(r.GenesAsInts, ProblemData, VariableNames[i]);
                d.SetField(VariableNames[i], dummy); //first
                                                     //save total in a dataauxiliar Row
            }

            DataRow row = d;

            object[] results = findFines(ref row);
            r.Okays = results[0] as string;
            double fine = Convert.ToDouble(results[1]);

            r.Okays   += " " + Decimal.Round(Convert.ToDecimal(fine), 3);
            r.Fitness  = d.C;
            r.Fitness /= (1 + fine); //max vol, max value * (1+fine)

            r.Genotype = Aid.SetStrings(r.GenesAsInts);
        }
Exemplo n.º 2
0
        /// <summary>
        /// BASIC CALCULATION NECESSARY FOR FITNESS
        /// </summary>
        /// <param name="r"></param>
        /// <param name="c"></param>
        public override void FillBasic(ref GADataSet.SolutionsRow r)
        {
            int badRoute = 1;

            HashSet <int> nonRepeated = new HashSet <int>();

            r.GenesAsInts.Where(o => nonRepeated.Add(o)).ToList();

            double  Fine = 0;
            DataRow cond = Conditions.FirstOrDefault();

            //if non-repeated list counts to number of variables.. OK!
            if (nonRepeated.Count == ProblemData.Count())
            {
                try
                {
                    List <int> fullList = nonRepeated.ToList();
                    fullList.Add(0); //adds origin to the end

                    //create rows for subtable Data auxiliar
                    for (int j = 0; j < fullList.Count; j++)
                    {
                        GADataSet.DataRow d = r.DataAxuliar.NewDataRow();
                        r.DataAxuliar.AddDataRow(d);
                        //auxiliar data row
                        for (int i = 0; i < VariableNames.Length; i++)
                        {
                            //Field A, B or C
                            string varField = VariableNames[i];
                            double var      = Aid.SetDifferences(cond, fullList, j, ProblemData, varField);
                            d.SetField <double>(varField, var); //set Distance Differences for A or B
                        }
                    }

                    foreach (GADataSet.DataRow item in r.DataAxuliar)
                    {
                        double a2 = Math.Pow(item.A, 2); //x^2
                        double b2 = Math.Pow(item.B, 2); //y^2
                        item.C = Math.Sqrt(a2 + b2);     //distance = sqrt (x^2,y^2)
                    }
                    //put total distance parcourred
                    double totalDistance = r.DataAxuliar.Sum(i => i.C);

                    Fine = totalDistance / (r.DataAxuliar.Count * 10);

                    fullList.Insert(0, 0);

                    foreach (GADataSet.ConditionsRow c in Conditions)
                    {
                        string prohibited = c.MinC.ToString() + c.MaxC.ToString();
                        string genotype   = String.Join("", fullList.Select(o => o));
                        if (genotype.Contains(prohibited))
                        {
                            badRoute++;
                        }
                    }

                    //   if (s.Fine > 1 && badRoute>1) s.Fine = 0.80;
                    if (Fine > 1)
                    {
                        Fine = 0.90;
                    }

                    fullList.Clear();
                    fullList = null;
                }
                catch (SystemException ex)
                {
                    // e = ex.StackTrace;
                }
            }
            else
            {
                Fine = cond.Field <double>("CFine"); //a million
            }
            nonRepeated.Clear();
            nonRepeated = null;

            r.Okays = badRoute.ToString() + " " + Decimal.Round(Convert.ToDecimal(Fine), 3);

            r.Fitness = 1 - Fine; //max vol, max value * (1+fine)

            r.Fitness /= badRoute;

            r.Genotype = Aid.SetStrings(r.GenesAsInts);
        }