Example #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);
        }
Example #2
0
        public override void FillStrings <T>(ref GADataSet.SolutionsRow r, ref T stringRow)
        {
            GADataSet.StringsRow s = stringRow as GADataSet.StringsRow;
            s.AString = String.Join(" ", r.DataAxuliar.Select(d => Aid.DecimalTxt(d.A)));
            s.BString = String.Join(" ", r.DataAxuliar.Select(d => Aid.DecimalTxt(d.B)));

            s.CString = String.Join(" ", r.DataAxuliar.Select(d => Aid.DecimalTxt(d.C)));
        }
Example #3
0
 public override void FillStrings <T>(ref GADataSet.SolutionsRow r, ref T s)
 {
     for (int i = 0; i < VariableNames.Length; i++)
     {
         string  dummy = Aid.DecodeStrings(r.GenesAsInts, ProblemData, VariableNames[i]);
         string  field = VariableNames[i] + "String";
         DataRow row   = s as DataRow;
         if (row.Table.Columns.Contains(field))
         {
             row.SetField(field, dummy); //first
         }
     }
 }
        /// <summary>
        /// Main postcript function to store best results
        /// </summary>
        private void workerProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            try
            {
                IChromosome bestChromosome = GA.Population.BestChromosome;

                //BASIC
                GADataSet ds = GARow.Table.DataSet as GADataSet;
                GADataSet.SolutionsRow currentSolution;
                currentSolution = ds.Solutions.NewSolutionsRow();

                currentSolution.Initialize(bestChromosome.GetGenes());
                currentSolution.ProblemID = PROBLEMID;

                //this is the most important function to override, does the fitness calc
                FillBasic(ref currentSolution);

                GeneticAlgorithm ga = GA;
                //fill GA data to row
                GARow.Update(ref ga); //report GA stuff

                //define filter function by genotype
                Func <GADataSet.SolutionsRow, bool> funcion;
                funcion = Aid.FilterByGenotype(ref hashListOfGenotypes, ref listOfSolutions);
                //execute filter function by genotype
                funcion(currentSolution);
                //the listOfSolutions contains the list of rows that need to be added
                //these rows are flagged ShouldDelete = false

                //REPONER ESTO
                if (!currentSolution.ShouldDelete)
                {
                    ds.Solutions.AddSolutionsRow(currentSolution);
                    GARow.FillGADataToSolution(ref currentSolution);
                }

                //callBack MEthod to Form or User Control

                CallBack.Invoke();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Example #5
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);
        }
Example #6
0
        /// <summary>
        /// BASIC CALCULATION NECESSARY FOR FITNESS
        /// </summary>
        /// <param name="r"></param>
        /// <param name="c"></param>
        public override void FillBasic(ref GADataSet.SolutionsRow r)
        {
            y[] func = new y[5];

            func[0] = y1;
            func[1] = y2;
            func[2] = y3;
            func[3] = y4;
            func[4] = y5;

            // int badRoute = 1;

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

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

            //  string e = string.Empty;
            double Fine = 0;

            double[] ai = r.GenesAsDoubles.ToArray();

            double[] yi = new double[ai.Length];
            double[] di = new double[ai.Length];

            for (int i = 0; i < di.Length; i++)
            {
                di[i] = yi[i] = 0;
            }
            for (int i = 1; i < ai.Length; i++)
            {
                ai[i] *= 5;
            }

            foreach (DataRow d in this.ProblemData)
            {
                double x    = d.Field <double>("A");
                double yexp = d.Field <double>("B");

                //  for (int i = 1; i < ai.Length; i++)
                {
                    //  yi[0] = y1(ai, x);
                    //  yi[1] = y2(ai, x);
                    //  yi[2] = y3(ai, x);
                    //  yi[3] = y4(ai, x);
                    //  yi[4] = y5(ai, x);
                    int index = Convert.ToInt32(ai[0]);
                    yi[0]  = func[index](ai, x);
                    di[0] += Math.Pow(yexp - yi[0], 2);
                    // di[1] += Math.Pow(yexp - yi[1], 2);
                    //  di[2] += Math.Pow(yexp - yi[2], 2);
                    // di[3] += Math.Pow(yexp - yi[3], 2);
                    // di[4] += Math.Pow(yexp - yi[4], 2);
                }
            }

            /*
             * int goodEq = 0;
             * double last = 10000;
             * for (int i = 0; i < di.Length; i++)
             * {
             *  if (di[i] < last)
             *  {
             *      last = di[i];
             *      goodEq = i;
             *      s.Fine = Math.Sqrt(di[i]);
             *
             *      r.Okays = i + " " + Decimal.Round(Convert.ToDecimal(s.Fine), 2);
             *  }
             * }
             */
            Fine      = Math.Sqrt(di[0]);
            r.Okays   = ai[0] + " " + Decimal.Round(Convert.ToDecimal(Fine), 3);
            r.Fitness = 1 / (1 + Fine); //max vol, max value * (1+fine)

            r.Genotype = Aid.SetStrings(r.GenesAsDoubles, 4);

            //  r.Fitness /= badRoute;
        }