Exemple #1
0
 public void Initialize(ProblemDefinition problem, Randomization randomObj)
 {
     for (int i = 0; i < IndList.Count; i++)
     {
         IndList[i].Initialize(problem, randomObj);
     }
 }
Exemple #2
0
 public void Evaluate(ProblemDefinition problemObj)
 {
     for (int i = 0; i < this.IndList.Count; i++)
     {
         IndList[i].Evaluate(problemObj);
     }
 }
Exemple #3
0
 public void Initialize(ProblemDefinition problem, Randomization randomObj)
 {
     for (int i = 0; i < IndList.Count; i++)
     {
         IndList[i].Initialize(problem, randomObj);
     }
 }
Exemple #4
0
 public void Evaluate(ProblemDefinition problemObj)
 {
     for (int i = 0; i < this.IndList.Count; i++)
     {
         IndList[i].Evaluate(problemObj);
     }
 }
Exemple #5
0
        /* Function to print the information of a population in a file */
        public void ReportPopulation(ProblemDefinition problemObj, string name, string info, int generation = 0)
        {
            Directory.CreateDirectory("report");
            var          file   = File.OpenWrite($"report\\{problemObj.Title}_{name}_pop.out");
            StreamWriter writer = new StreamWriter(file);

            writer.WriteLine($"# {info}");
            writer.WriteLine($"# of objectives = {problemObj.ObjectiveCount}, # of constraints = {problemObj.ConstraintCount}, # of real_var = {problemObj.RealVariableCount}, # of bits of bin_var = {problemObj.TotalBinaryBitLength}, constr_violation, rank, crowding_distance");

            if (generation > 0)
            {
                writer.WriteLine($"# Generation no: {generation}/{problemObj.MaxGeneration}");
            }

            for (int i = 0; i < problemObj.PopulationSize; i++)
            {
                for (int j = 0; j < problemObj.ObjectiveCount; j++)
                {
                    writer.Write($"{IndList[i].Obj[j].ToString(CultureInfo.InvariantCulture)}\t");
                }

                if (problemObj.ConstraintCount != 0)
                {
                    for (int j = 0; j < problemObj.ConstraintCount; j++)
                    {
                        writer.Write($"{IndList[i].Constr[j].ToString("E")}\t");
                    }
                }

                if (problemObj.RealVariableCount != 0)
                {
                    for (int j = 0; j < problemObj.RealVariableCount; j++)
                    {
                        writer.Write($"{IndList[i].Xreal[j].ToString("E")}\t");
                    }
                }

                if (problemObj.BinaryVariableCount != 0)
                {
                    for (int j = 0; j < problemObj.BinaryVariableCount; j++)
                    {
                        for (int k = 0; k < problemObj.Nbits[j]; k++)
                        {
                            writer.Write($"{IndList[i].Gene[j][ k]}\t");
                        }
                    }
                }

                writer.Write($"{IndList[i].ConstrViolation.ToString("E")}\t");

                writer.Write($"{IndList[i].Rank}\t");

                writer.Write($"{IndList[i].CrowdDist.ToString("E")}\n");
            }

            writer.Flush();
            writer.Close();
        }
Exemple #6
0
 public double HillClimb(ProblemDefinition problemObj)
 {
     double retVal = 0;
     for (int i = 0; i < IndList.Count; i++)
     {
         retVal += IndList[i].HillClimb(problemObj);
     }
     return retVal;
 }
Exemple #7
0
        public void Decode(ProblemDefinition problem)
        {
            if (problem.BinaryVariableCount == 0)
                return;

            for (int i = 0; i < IndList.Count; i++)
            {
                IndList[i].Decode(problem);
            }
        }
Exemple #8
0
        public double HillClimb(ProblemDefinition problemObj)
        {
            double retVal = 0;

            for (int i = 0; i < IndList.Count; i++)
            {
                retVal += IndList[i].HillClimb(problemObj);
            }
            return(retVal);
        }
Exemple #9
0
        public void Decode(ProblemDefinition problem)
        {
            if (problem.BinaryVariableCount == 0)
            {
                return;
            }

            for (int i = 0; i < IndList.Count; i++)
            {
                IndList[i].Decode(problem);
            }
        }
Exemple #10
0
        public void Merge( Population pop1, Population pop2, ProblemDefinition problem)
        {
            IndList.Clear();

            for (int i = 0; i < pop1.IndList.Count; i++)
            {
                IndList.Add( new Individual(pop1.IndList[i], problem));
            }
            for (int i = 0; i < pop2.IndList.Count; i++)
            {
                IndList.Add(new Individual(pop2.IndList[i], problem));
            }
        }
Exemple #11
0
        public void Merge(Population pop1, Population pop2, ProblemDefinition problem)
        {
            IndList.Clear();

            for (int i = 0; i < pop1.IndList.Count; i++)
            {
                IndList.Add(new Individual(pop1.IndList[i], problem));
            }
            for (int i = 0; i < pop2.IndList.Count; i++)
            {
                IndList.Add(new Individual(pop2.IndList[i], problem));
            }
        }
Exemple #12
0
        public double HillClimb(ProblemDefinition problemObj)
        {
            double tResultImprovement = 0;

            if (CollisionList.Count > 0)
            {
                bool continueClimb;
                Individual original = new Individual(this, problemObj);
                original.Decode(problemObj);
                original.Evaluate(problemObj);

                int seeder = 7;
                Random rnd = new Random(seeder);

                int climbRetryCount = 0;
                do
                {
                    climbRetryCount++;
                    bool climb = HillClimber(problemObj, rnd);
                    if (climb)
                        return 0;
                    //HillClimberF22(problemObj, rnd,22);
                    Decode(problemObj);
                    Evaluate(problemObj);

                    var obj0Imp = original.Obj[0] - Obj[0];
                    var obj1Imp = original.Obj[1] - Obj[1];
                    var obj2Imp = original.Obj[2] - Obj[2];

                    if (obj0Imp + obj1Imp + obj2Imp == 0)
                    {
                        if (obj0Imp > 0)
                        {
                            original = new Individual(this, problemObj);
                            original.Decode(problemObj);
                            original.Evaluate(problemObj);
                        }
                        continueClimb = false;
                    }
                    else if (obj0Imp + obj1Imp + obj2Imp > 0)
                    {
                        continueClimb = false;
                        original = new Individual(this, problemObj);
                        original.Decode(problemObj);
                        original.Evaluate(problemObj);
                    }
                    else
                    {
                        continueClimb = false;
                    }

                    tResultImprovement += obj0Imp + obj1Imp + obj2Imp;

                } while (continueClimb && climbRetryCount < 10);

                if (tResultImprovement < 0) //todo check sanki geri almıyor.
                {
                    Copy(original, problemObj);
                    Decode(problemObj);
                    Evaluate(problemObj);
                    tResultImprovement = 0;
                }
            }
            return tResultImprovement;
        }
Exemple #13
0
        public void Evaluate(ProblemDefinition problemObj)
        {
            EvaluateProblem(problemObj);

            #region constraint kısmı yok
            if (problemObj.ConstraintCount == 0)
            {
                ConstrViolation = 0.0;
            }
            else
            {
                ConstrViolation = 0.0;
                for (int j = 0; j < problemObj.ConstraintCount; j++)
                {
                    if (Constr[j] < 0.0)
                    {
                        ConstrViolation += Constr[j];
                    }
                }
            }
            #endregion
        }
Exemple #14
0
 /* Function to perform mutation of an individual */
 static void MutateIndividual(Individual ind, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     if (problemObj.RealVariableCount != 0)
     {
         RealMutate(ind, problemObj, randomizationObj);
     }
     if (problemObj.BinaryVariableCount != 0)
     {
         BinaryMutate(ind, problemObj, randomizationObj);
     }
 }
Exemple #15
0
        /* Routine to compute crowding distance based on objective function values when the population in in the form of an array */
        static void assign_crowding_distance_indices(Population pop, int c1, int c2, ProblemDefinition problemObj, Randomization randomizationObj)
        {
            int[][] objArray;
            int[] dist;
            int i, j;
            int frontSize;
            frontSize = c2 - c1 + 1;
            if (frontSize == 1)
            {
                pop.IndList[c1].CrowdDist = problemObj.Inf;
                return;
            }
            if (frontSize == 2)
            {
                pop.IndList[c1].CrowdDist = problemObj.Inf;
                pop.IndList[c2].CrowdDist = problemObj.Inf;
                return;
            }
            dist = new int[frontSize];
            objArray = new int[problemObj.ObjectiveCount][];
            //obj_array = (int**)malloc(ProblemObj.ObjectiveCount * sizeof(int*));
            for (i = 0; i < problemObj.ObjectiveCount; i++)
            {
                objArray[i] = new int[frontSize];
            }

            for (j = 0; j < frontSize; j++)
            {
                dist[j] = c1++;
            }
            assign_crowding_distance(pop, dist, objArray, frontSize, problemObj, randomizationObj);
        }
Exemple #16
0
 /* Routine to compute crowding distance based on ojbective function values when the population in in the form of a list */
 static void assign_crowding_distance_list(Population pop, Lists lst, int frontSize, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     int[][] objArray;
     int[] dist;
     int i, j;
     Lists temp;
     temp = lst;
     if (frontSize == 1)
     {
         pop.IndList[lst.index].CrowdDist = problemObj.Inf;
         return;
     }
     if (frontSize == 2)
     {
         pop.IndList[lst.index].CrowdDist = problemObj.Inf;
         pop.IndList[lst.child.index].CrowdDist = problemObj.Inf;
         return;
     }
     dist = new int[frontSize];
     objArray = new int[problemObj.ObjectiveCount][];
     //obj_array = (int**)malloc(ProblemObj.ObjectiveCount * sizeof(int*));
     for (i = 0; i < problemObj.ObjectiveCount; i++)
     {
         objArray[i] = new int[frontSize];
     }
     for (j = 0; j < frontSize; j++)
     {
         dist[j] = temp.index;
         temp = temp.child;
     }
     assign_crowding_distance(pop, dist, objArray, frontSize, problemObj, randomizationObj);
 }
Exemple #17
0
        ///* Function to display the current population for the subsequent generation */
        public void PlotPopulation(Population pop, ProblemDefinition problem, int genNo = 0, List <Individual> best = null)
        {
            if (!Use3D) //2D
            {
                var xr = new double[problem.PopulationSize];
                var yr = new double[problem.PopulationSize];

                for (int x = 0; x < problem.PopulationSize; x++)
                {
                    xr[x] = pop.IndList[x].Obj[GnuplotObjective1];
                    yr[x] = pop.IndList[x].Obj[GnuplotObjective2];
                }

                if (best != null)
                {
                    GnuPlot.HoldOn();
                }

                GnuPlot.Plot(xr, yr, $"title 'Generation #{genNo} of {problem.MaxGeneration}' with points pointtype 8 lc rgb 'blue'");

                if (best != null)
                {
                    var x = new double[best.Count];
                    var y = new double[best.Count];

                    for (int i = 0; i < best.Count; i++)
                    {
                        x[i] = best[i].Obj[GnuplotObjective1];
                        y[i] = best[i].Obj[GnuplotObjective2];
                    }
                    GnuPlot.Plot(x, y, $"title 'best ({best.First().TotalResult})' with points pointtype 6 lc rgb 'red'");
                }

                GnuPlot.Set($"xlabel \"obj[{GnuplotObjective1 }]\"");
                GnuPlot.Set($"ylabel \"obj[{GnuplotObjective2 }]\"");
            }
            else //3D
            {
                var rank1Count     = pop.IndList.Count(x => x.Rank == 1);
                var rankOtherCount = pop.IndList.Count(x => x.Rank != 1);

                if (rank1Count > 0)
                {
                    var xr = new double[rank1Count];
                    var yr = new double[rank1Count];
                    var zr = new double[rank1Count];

                    int index = 0;
                    foreach (var ind in pop.IndList.Where(x => x.Rank == 1))
                    {
                        xr[index] = ind.Obj[GnuplotObjective1];
                        yr[index] = ind.Obj[GnuplotObjective2];
                        zr[index] = ind.Obj[GnuplotObjective3];
                        index++;
                    }

                    if (best != null || rankOtherCount > 0)
                    {
                        GnuPlot.HoldOn();
                    }

                    GnuPlot.SPlot(xr, yr, zr, $"title 'Rank 1 individuals' with points pointtype 8 lc rgb 'red'");
                }

                if (rankOtherCount > 0)
                {
                    var xr = new double[rankOtherCount];
                    var yr = new double[rankOtherCount];
                    var zr = new double[rankOtherCount];

                    int index = 0;
                    foreach (var ind in pop.IndList.Where(x => x.Rank != 1))
                    {
                        xr[index] = ind.Obj[GnuplotObjective1];
                        yr[index] = ind.Obj[GnuplotObjective2];
                        zr[index] = ind.Obj[GnuplotObjective3];
                        index++;
                    }
                    GnuPlot.SPlot(xr, yr, zr, $"title 'Rank 2-{pop.IndList.Max(x=>x.Rank)} individuals' with points pointtype 8 lc rgb 'blue'");
                }

                if (best != null)
                {
                    var x = new double[best.Count];
                    var y = new double[best.Count];
                    var z = new double[best.Count];

                    for (int i = 0; i < best.Count; i++)
                    {
                        x[i] = best[i].Obj[GnuplotObjective1];
                        y[i] = best[i].Obj[GnuplotObjective2];
                        z[i] = best[i].Obj[GnuplotObjective3];
                    }

                    GnuPlot.SPlot(x, y, z, $"title 'best ({best.First().TotalResult})' with points pointtype 6 lc rgb 'green'");
                }

                GnuPlot.Set($"title 'Generation #{genNo} of {problem.MaxGeneration}'");

                GnuPlot.Set($"xlabel \"obj[{GnuplotObjective1}]\"");
                GnuPlot.Set($"ylabel \"obj[{GnuplotObjective2}]\"");
                GnuPlot.Set($"zlabel \"obj[{GnuplotObjective3}]\"");
            }
        }
Exemple #18
0
        private void ChangeGene(int geneId, int value, ProblemDefinition problem)
        {
            double minbin = problem.MinBinvar[geneId];
            double maxbin = problem.MaxBinvar[geneId];
            double nbit = problem.Nbits[geneId];

            //int valueToAdd = (int)((value - minbin) * ((Math.Pow(2, nbit) - 1) / (maxbin - minbin)));
            double sum = (double)((value - minbin) / ((maxbin - minbin) / (Math.Pow(2, nbit) - 1)));
            int valueToAdd = (int)sum;

            if (sum > (int)sum)
                valueToAdd++;

            int bits = problem.Nbits[geneId];
            for (int k = bits - 1; k >= 0; k--)
            {
                int divident = (int)Math.Pow(2, bits - 1 - k);
                int modulus = (int)Math.Pow(2, bits - k);
                if (valueToAdd % modulus == divident)
                {
                    valueToAdd = valueToAdd - divident;
                    Gene[geneId][k] = 1;
                }
                else
                {
                    Gene[geneId][k] = 0;
                }
            }
        }
Exemple #19
0
 /* Routine for two point binary Crossover */
 static void BinaryCrossover(Individual parent1, Individual parent2, Individual child1, Individual child2, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     int i, j;
     double rand;
     int temp, site1, site2;
     for (i = 0; i < problemObj.BinaryVariableCount; i++)
     {
         rand = randomizationObj.RandomPercent();
         if (rand <= problemObj.BinaryCrossoverProbability)
         {
             problemObj.BinaryCrossoverCount++;
             site1 = randomizationObj.RandomInteger(0, problemObj.Nbits[i] - 1);
             site2 = randomizationObj.RandomInteger(0, problemObj.Nbits[i] - 1);
             if (site1 > site2)
             {
                 temp = site1;
                 site1 = site2;
                 site2 = temp;
             }
             for (j = 0; j < site1; j++)
             {
                 child1.Gene[i][j] = parent1.Gene[i][j];
                 child2.Gene[i][j] = parent2.Gene[i][j];
             }
             for (j = site1; j < site2; j++)
             {
                 child1.Gene[i][j] = parent2.Gene[i][j];
                 child2.Gene[i][j] = parent1.Gene[i][j];
             }
             for (j = site2; j < problemObj.Nbits[i]; j++)
             {
                 child1.Gene[i][j] = parent1.Gene[i][j];
                 child2.Gene[i][j] = parent2.Gene[i][j];
             }
         }
         else
         {
             for (j = 0; j < problemObj.Nbits[i]; j++)
             {
                 child1.Gene[i][j] = parent1.Gene[i][j];
                 child2.Gene[i][j] = parent2.Gene[i][j];
             }
         }
     }
 }
Exemple #20
0
 /* Routine for binary mutation of an individual */
 static void BinaryMutate(Individual ind, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     int j, k;
     double prob;
     for (j = 0; j < problemObj.BinaryVariableCount; j++)
     {
         for (k = 0; k < problemObj.Nbits[j]; k++)
         {
             prob = randomizationObj.RandomPercent();
             if (prob <= problemObj.BinaryMutationProbability)
             {
                 if (ind.Gene[j][k] == 0)
                 {
                     ind.Gene[j][k] = 1;
                 }
                 else
                 {
                     ind.Gene[j][k] = 0;
                 }
                 problemObj.BinaryMutationCount += 1;
             }
         }
     }
 }
Exemple #21
0
        /* Routine for usual non-domination checking
           It will return the following values
           1 if a dominates b
           -1 if b dominates a
           0 if both a and b are non-dominated */
        static int CheckDominance(Individual a, Individual b, ProblemDefinition problemObj)
        {
            int i;
            int flag1;
            int flag2;
            flag1 = 0;
            flag2 = 0;
            if (a.ConstrViolation < 0 && b.ConstrViolation < 0)
            {
                if (a.ConstrViolation > b.ConstrViolation)
                {
                    return 1;
                }
                if (a.ConstrViolation < b.ConstrViolation)
                {
                    return -1;
                }
                return 0;
            }
            if (a.ConstrViolation < 0 && b.ConstrViolation == 0)
            {
                return -1;
            }

            if (a.ConstrViolation == 0 && b.ConstrViolation < 0)
            {
                return 1;
            }

            for (i = 0; i < problemObj.ObjectiveCount; i++)
            {
                if (a.Obj[i] < b.Obj[i])
                {
                    flag1 = 1;

                }
                else
                {
                    if (a.Obj[i] > b.Obj[i])
                    {
                        flag2 = 1;
                    }
                }
            }
            if (flag1 == 1 && flag2 == 0)
            {
                return 1;
            }
            if (flag1 == 0 && flag2 == 1)
            {
                return -1;
            }
            return 0;
        }
Exemple #22
0
 /* Function to cross two individuals */
 static void Crossover(Individual parent1, Individual parent2, Individual child1, Individual child2, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     if (problemObj.RealVariableCount != 0)
     {
         RealCrossover(parent1, parent2, child1, child2, problemObj, randomizationObj);
     }
     if (problemObj.BinaryVariableCount != 0)
     {
         BinaryCrossover(parent1, parent2, child1, child2, problemObj, randomizationObj);
     }
 }
Exemple #23
0
        /* Routine to fill a population with individuals in the decreasing order of crowding distance */
        static void crowding_fill(Population mixedPop, Population newPop, int count, int frontSize, Lists elite, ProblemDefinition ProblemObj, Randomization RandomizationObj)
        {
            int[] dist;
            Lists temp;
            int i, j;
            assign_crowding_distance_list(mixedPop, elite.child, frontSize, ProblemObj, RandomizationObj);
            dist = new int[frontSize];
            temp = elite.child;
            for (j = 0; j < frontSize; j++)
            {
                dist[j] = temp.index;
                temp = temp.child;
            }
            quicksort_dist(mixedPop, dist, frontSize, RandomizationObj);
            for (i = count, j = frontSize - 1; i < ProblemObj.PopulationSize; i++, j--)
            {
                newPop.IndList[i].Copy(mixedPop.IndList[dist[j]], ProblemObj);
                //CopyIndividual(mixedPop.IndList[dist[j]], newPop.IndList[i]);
                //newPop.IndList[i] = new Individual(mixedPop.IndList[dist[j]], ProblemObj);

            }
        }
Exemple #24
0
 /* Function to perform mutation in a population */
 static void MutatePopulation(Population pop, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     int i;
     for (i = 0; i < problemObj.PopulationSize; i++)
     {
         MutateIndividual(pop.IndList[i], problemObj, randomizationObj);
     }
 }
Exemple #25
0
 /* Function to initialize an individual randomly */
 public void Initialize(ProblemDefinition problem, Randomization randomObj)
 {
     int j;
     if (problem.RealVariableCount != 0)
     {
         for (j = 0; j < problem.RealVariableCount; j++)
         {
             Xreal[j] = randomObj.RandomDouble(problem.MinRealvar[j], problem.MaxRealvar[j]);
         }
     }
     if (problem.BinaryVariableCount != 0)
     {
         for (j = 0; j < problem.BinaryVariableCount; j++)
         {
             for (int k = 0; k < problem.Nbits[j]; k++)
             {
                 if (randomObj.RandomPercent() <= 0.5)
                 {
                     Gene[j][k] = 0;
                 }
                 else
                 {
                     Gene[j][k] = 1;
                 }
             }
         }
     }
 }
Exemple #26
0
        public Individual(Individual ind, ProblemDefinition problem)
        {
            CollisionList = new List<Collision>();
            FacultySections = new List<FacultySection>();
            DiffSemesterFacultySections = new List<FacultySection>();
            ElectiveFacultySections = new List<FacultySection>();

            TotalResult = 0;

            NRealVar = ind.NRealVar;
            NBinVar = ind.NBinVar;
            NMaxBit = ind.NMaxBit;
            NObj = ind.NObj;
            NCons = ind.NCons;

            if (ind.NRealVar != 0)
                Xreal = new double[NRealVar];

            if (ind.NBinVar != 0)
            {
                SlotId = new int[ind.NBinVar];
                Gene = new List<List<int>>();
                for (int i = 0; i < ind.NBinVar; i++)
                {
                    Gene.Add(new List<int>(ind.NBinVar));
                    for (int j = 0; j < NMaxBit; j++)
                    {
                        Gene[i].Add(0);
                    }
                }
                //Gene = new int[ind._nBinVar, ind._nMaxBit];
            }

            Obj = new double[ind.NObj];

            if (ind.NCons != 0)
                Constr = new double[ind.NCons];

            Rank = ind.Rank;
            ConstrViolation = ind.ConstrViolation;
            CrowdDist = ind.CrowdDist;
            if (ind.NRealVar > 0)
            {
                for (int i = 0; i < ind.NRealVar; i++)
                {
                    Xreal[i] = ind.Xreal[i];
                }
            }
            if (ind.NBinVar > 0)
            {
                for (int i = 0; i < ind.NBinVar; i++)
                {
                    SlotId[i] = ind.SlotId[i];
                    for (int j = 0; j < problem.Nbits[i]; j++)
                    {
                        Gene[i][j] = ind.Gene[i][j];
                    }
                }
            }
            for (int i = 0; i < ind.NObj; i++)
            {
                Obj[i] = ind.Obj[i];
            }
            if (ind.NCons > 0)
            {
                for (int i = 0; i < ind.NCons; i++)
                {
                    Constr[i] = ind.Constr[i];
                }
            }

            Timetable = new List<List<Slot>>();
        }
Exemple #27
0
 /* Routine to compute crowding distances */
 static void assign_crowding_distance(Population pop, int[] dist, int[][] objArray, int frontSize, ProblemDefinition problemObj, Randomization randomizationObj)
 {
     int i, j;
     for (i = 0; i < problemObj.ObjectiveCount; i++)
     {
         for (j = 0; j < frontSize; j++)
         {
             objArray[i][j] = dist[j];
         }
         quicksort_front_obj(pop, i, objArray[i], frontSize, randomizationObj);
     }
     for (j = 0; j < frontSize; j++)
     {
         pop.IndList[dist[j]].CrowdDist = 0.0;
     }
     for (i = 0; i < problemObj.ObjectiveCount; i++)
     {
         pop.IndList[objArray[i][0]].CrowdDist = problemObj.Inf;
     }
     for (i = 0; i < problemObj.ObjectiveCount; i++)
     {
         for (j = 1; j < frontSize - 1; j++)
         {
             if (pop.IndList[objArray[i][j]].CrowdDist != problemObj.Inf)
             {
                 if (pop.IndList[objArray[i][frontSize - 1]].Obj[i] == pop.IndList[objArray[i][0]].Obj[i])
                 {
                     pop.IndList[objArray[i][j]].CrowdDist += 0.0;
                 }
                 else
                 {
                     pop.IndList[objArray[i][j]].CrowdDist += (pop.IndList[objArray[i][j + 1]].Obj[i] - pop.IndList[objArray[i][j - 1]].Obj[i]) / (pop.IndList[objArray[i][frontSize - 1]].Obj[i] - pop.IndList[objArray[i][0]].Obj[i]);
                 }
             }
         }
     }
     for (j = 0; j < frontSize; j++)
     {
         if (pop.IndList[dist[j]].CrowdDist != problemObj.Inf)
         {
             pop.IndList[dist[j]].CrowdDist = pop.IndList[dist[j]].CrowdDist / problemObj.ObjectiveCount;
         }
     }
 }
Exemple #28
0
        private int DecodeGene(ProblemDefinition problem, int j)
        {
            var sum = 0;
            for (int k = 0; k < problem.Nbits[j]; k++)
            {
                if (Gene[j][k] == 1)
                {
                    sum += (int)Math.Pow(2, problem.Nbits[j] - 1 - k);
                }
            }

            double minbin = problem.MinBinvar[j];
            double maxbin = problem.MaxBinvar[j];
            double nbit = problem.Nbits[j];

            return (int)(minbin + sum * (maxbin - minbin) / (Math.Pow(2, nbit) - 1));
        }
Exemple #29
0
        private void EvaluateProblem(ProblemDefinition problemObj)
        {
            TotalResult = 0;
            CollisionList.Clear();

            #region faculty section lists
            FacultySections.Clear();
            foreach (var course in problemObj.FacultyCourseList.OrderBy(x => x.Code))
            {
                if (!FacultySections.Any(x => x.Code == course.Code && x.Section == course.Section))
                {
                    var temp = new FacultySection
                    {
                        Code = course.Code,
                        Section = course.Section
                    };
                    FacultySections.Add(temp);
                }
            }

            DiffSemesterFacultySections.Clear();
            foreach (var course in problemObj.FacultyCourseList.OrderBy(x => x.Code))
            {
                if (!DiffSemesterFacultySections.Any(x => x.Code == course.Code && x.Section == course.Section))
                {
                    var temp = new FacultySection
                    {
                        Code = course.Code,
                        Section = course.Section
                    };
                    DiffSemesterFacultySections.Add(temp);
                }
            }

            ElectiveFacultySections.Clear();
            foreach (var course in problemObj.FacultyCourseList.OrderBy(x => x.Code))
            {
                if (!ElectiveFacultySections.Any(x => x.Code == course.Code && x.Section == course.Section))
                {
                    var temp = new FacultySection
                    {
                        Code = course.Code,
                        Section = course.Section
                    };
                    ElectiveFacultySections.Add(temp);
                }
            }
            #endregion

            #region fill variables

            Slot[] easy = new Slot[50];
            Timetable.Clear();
            int idcounter = 1;
            for (int x = 0; x < 5; x++)
            {
                Timetable.Add(new List<Slot>());
                for (int y = 0; y < 10; y++)
                {

                    Timetable[x].Add(new Slot(problemObj.TeacherList.Count, idcounter));
                    easy[idcounter - 1] = Timetable[x][y];
                    idcounter++;
                }
            }

            Obj[0] = 0;
            Obj[1] = 0;
            Obj[2] = 0;

            for (int j = 0; j < problemObj.CourseList.Count; j++) //ders sayisi kadar,
            {
                int slotId = SlotId[j];
                AddToTimetable(slotId, problemObj.CourseList[j]); //dersleri timetable'a yerleştirdim.
            }

            foreach (Course facultyCourse in problemObj.FacultyCourseList)
            {
                for (int i = 0; i < facultyCourse.Duration; i++)
                {
                    easy[facultyCourse.SlotId + i - 1].facultyCourses.Add(facultyCourse);
                }
            }

            for (int x = 0; x < 5; x++)
            {
                for (int y = 0; y < 9; y++)
                {
                    if (problemObj.Meeting[x][y] > 0)
                        Timetable[x][y].meetingHour = true; // meeting hours

                    if (problemObj.LabScheduling[x][y] > 0)
                        Timetable[x][y].facultyLab = problemObj.LabScheduling[x][y];

                }
            }

            #endregion

            #region calc. collisions

            #region Base vs Faculty same semester

            //dönem ici dekanlik/bolum dersi cakismasi todo: scheduling'in normal slot halinde gelmesi lazım
            {
                List<Collision> col = CollisionBaseVsFaculty(0);

                foreach (var collision in col)
                {
                    double changeResult = 0;
                    var crashingfacultycourses = collision.CrashingCourses.Where(x => x.FacultyCourse);

                    foreach (var course in crashingfacultycourses)
                    {
                        if (FacultySections.Any(x => x.Code == course.Code && x.Crashing == false))
                        {
                            collision.Reason += $"; {course.Code} has noncrashing section";
                            changeResult += 0.01;
                        }
                        else
                        {
                            changeResult++;
                        }
                    }

                    collision.Result = changeResult;
                }

                var result = col.Sum(item => item.Result);
                Obj[0] += result;
                CollisionList.AddRange(col);
            }

            #endregion

            #region Base vs Base same semester
            //donem ici bolum dersi cakismasi
            for (int j = 1; j < 9; j++)
            {
                List<Collision> col = CollisionBaseVsBase(Timetable, 1, j);
                var result = col.Sum(item => item.Result);
                Obj[0] += result;
                CollisionList.AddRange(col);
            }
            #endregion

            #region Base vs Faculty -1 +1 semester.
            //dönemler arasi dekanlik/bolum dersi cakismasi todo: scheduling'in normal slot halinde gelmesi lazım
            {
                List<Collision> col = new List<Collision>();
                for (int j = 1; j < 8; j++)
                {
                    // 1-2  2-3  3-4  4-5  5-6  6-7  7-8
                    // 2-1  3-2  4-3  5-4  6-5  7-6  8-7     consecutive CSE&faculty courses
                    col.AddRange(CollisionBaseVsFacultyDiffSemester(0, j, j + 1, 1));
                    col.AddRange(CollisionBaseVsFacultyDiffSemester(0, j + 1, j, 1));

                }

                foreach (var collision in col)
                {
                    double changeResult = 0;
                    var crashingfacultycourses = collision.CrashingCourses.Where(x => x.FacultyCourse);

                    foreach (var course in crashingfacultycourses)
                    {
                        if (DiffSemesterFacultySections.Any(x => x.Code == course.Code && x.Crashing == false))
                        {
                            collision.Reason += $"; {course.Code} has noncrashing section";
                            changeResult += 0.01;
                        }
                        else
                        {
                            changeResult++;
                        }
                    }

                    collision.Result = changeResult;
                }

                var result = col.Sum(item => item.Result);
                Obj[1] += result;
                CollisionList.AddRange(col);
            }
            #endregion

            #region Base vs Base -1 +1 semester
            //dönemler arası CSE çakışmaları
            for (int j = 1; j < 8; j++)
            {
                List<Collision> col = CollisionBaseVsBaseDiffSemester(Timetable, 1, new List<int> { j, j + 1 }, 1);
                var y = col.Sum(item => item.Result);

                Obj[1] += y;
                CollisionList.AddRange(col);
            }
            #endregion

            #region Lab Count
            //aynı saatte 4'ten fazla lab olmaması lazim
            {
                List<Collision> labcol = CollisionInLabs(Timetable, 4);
                var y = labcol.Sum(item => item.Result);
                Obj[0] += y;
                CollisionList.AddRange(labcol);
            }
            //# of lab at most 4
            #endregion

            #region Teacher Collisions
            for (int j = 0; j < problemObj.TeacherList.Count; j++) //Bütün hocalar için
            {
                if (problemObj.TeacherList[j].Equals("ASSISTANT"))
                {
                    continue;
                }

                {
                    //og. gor. aynı saatte baska dersinin olmaması
                    List<Collision> col = CollisionTeacher(Timetable, j, 1);
                    var yy1 = col.Sum(item => item.Result);
                    Obj[0] += yy1;
                    CollisionList.AddRange(col);
                }

                {
                    //og. gor. gunluk 4 saatten fazla pespese dersinin olmamasi
                    List<Collision> col = CollisionTeacherConsicutive(Timetable, j, 4);
                    var y = col.Sum(item => item.Result);
                    Obj[2] += y;
                    CollisionList.AddRange(col);
                    //teacher have at most 4 consective lesson per day
                }
                {
                    //og. gor. boş gununun olması
                    List<Collision> col = CollisionTeacherFreeDay(Timetable, j);
                    var y = col.Sum(item => item.Result);
                    Obj[2] += y;
                    CollisionList.AddRange(col);
                }
                /* teacher have free day*/
            }
            #endregion

            #region Lab vs Lecture different day collision
            //lab ve lecture farklı günlerde olsun
            {
                List<Collision> col = CollisionLabLectureSameDay(Timetable);
                var y = col.Sum(item => item.Result);
                Obj[2] += y;
                CollisionList.AddRange(col);
            }
            #endregion

            #region Elecive vs Elective
            //elective vs elective collision
            {
                List<Collision> col = CollisionElectiveVsElective(Timetable, 1);
                var y = col.Sum(item => item.Result);
                Obj[0] += y;
                CollisionList.AddRange(col);
            }
            #endregion

            #region Elective vs Faculty in semester 6 7 8
            //elective vs faculty courses in semester
            {
                List<Collision> col = new List<Collision>();

                col.AddRange(CollisionElectiveVsFacultyDiffSemester(6, 0));
                col.AddRange(CollisionElectiveVsFacultyDiffSemester(7, 0));
                col.AddRange(CollisionElectiveVsFacultyDiffSemester(8, 0));

                foreach (var collision in col)
                {
                    double changeResult = 0;
                    var crashingfacultycourses = collision.CrashingCourses.Where(x => x.FacultyCourse);

                    foreach (var course in crashingfacultycourses)
                    {
                        if (ElectiveFacultySections.Any(x => x.Code == course.Code && x.Crashing == false))
                        {
                            collision.Reason += $"; {course.Code} has noncrashing section";
                            changeResult += 0.01;
                        }
                        else
                        {
                            changeResult++;
                        }
                    }

                    collision.Result = changeResult;
                }

                var y = col.Sum(item => item.Result);
                Obj[2] += y;
                CollisionList.AddRange(col);
            }
            #endregion

            #region Elective vs Base in semester 6 7 8
            //elective vs base courses in semester
            if (!problemObj.DisabledCollisions.HasFlag(UCTProblem.DisabledCollisions.ElectiveVsBase))
            {
                List<Collision> col = CollisionElectiveVsBaseDiffSemester(0, 6, 1);
                var y = col.Sum(item => item.Result);
                Obj[1] += y;
                CollisionList.AddRange(col);
                col.Clear();

                col = CollisionElectiveVsBaseDiffSemester(0, 7);
                y = col.Sum(item => item.Result);
                Obj[0] += y;
                CollisionList.AddRange(col);
                col.Clear();

                col = CollisionElectiveVsBaseDiffSemester(0, 8);
                y = col.Sum(item => item.Result);
                Obj[0] += y;
                CollisionList.AddRange(col);
            }
            #endregion

            #endregion

            TotalResult = Obj[0] + Obj[1] + Obj[2];
        }
Exemple #30
0
        /* Function to assign rank and crowding distance to a population of size pop_size*/
        static void assign_rank_and_crowding_distance(Population newPopulation, ProblemDefinition problemObj, Randomization randomizationObj)
        {
            int flag;
            int i;
            int end;
            int frontSize;
            int rank = 1;
            Lists orig;
            Lists cur;
            Lists temp1, temp2;
            orig = new Lists();
            cur = new Lists();

            orig.index = -1;
            orig.parent = null;
            orig.child = null;
            cur.index = -1;
            cur.parent = null;
            cur.child = null;
            temp1 = orig;
            for (i = 0; i < problemObj.PopulationSize; i++)
            {
                Insert(temp1, i);
                temp1 = temp1.child;
            }
            do
            {
                if (orig.child != null && orig.child.child == null)
                {
                    newPopulation.IndList[orig.child.index].Rank = rank;
                    newPopulation.IndList[orig.child.index].CrowdDist = problemObj.Inf;
                    break;
                }

                temp1 = orig.child;
                Insert(cur, temp1.index);
                frontSize = 1;
                temp2 = cur.child;
                temp1 = Delete(temp1);
                temp1 = temp1.child;
                do
                {
                    temp2 = cur.child;
                    do
                    {
                        end = 0;
                        flag = CheckDominance(newPopulation.IndList[temp1.index], newPopulation.IndList[temp2.index], problemObj);
                        if (flag == 1)
                        {
                            Insert(orig, temp2.index);
                            temp2 = Delete(temp2);
                            frontSize--;
                            temp2 = temp2.child;
                        }
                        if (flag == 0)
                        {
                            temp2 = temp2.child;
                        }
                        if (flag == -1)
                        {
                            end = 1;
                        }
                    }
                    while (end != 1 && temp2 != null);
                    if (flag == 0 || flag == 1)
                    {
                        Insert(cur, temp1.index);
                        frontSize++;
                        temp1 = Delete(temp1);
                    }
                    temp1 = temp1.child;
                }
                while (temp1 != null);
                temp2 = cur.child;
                do
                {
                    newPopulation.IndList[temp2.index].Rank = rank;
                    temp2 = temp2.child;
                }
                while (temp2 != null);
                assign_crowding_distance_list(newPopulation, cur.child, frontSize, problemObj, randomizationObj);
                temp2 = cur.child;
                do
                {
                    temp2 = Delete(temp2);
                    temp2 = temp2.child;
                }
                while (cur.child != null);
                rank += 1;
            }
            while (orig.child != null);
        }
Exemple #31
0
        private bool HillClimber(ProblemDefinition problemObj, Random rnd)
        {
            int maxClimb = 1;
            int climbCount = 0;

            var tempColl = CollisionList.ToList();

            if (tempColl.Count > 0) //collision varsa
            {

                while (tempColl.Count > 0)
                {
                    var randomColl = rnd.Next(tempColl.Count);
                    var collision = tempColl[randomColl];
                    tempColl.RemoveAt(randomColl);
                    //var collision = tempColl.First();
                    //tempColl.RemoveAt(0);

                    if (collision.CrashingCourses.Count == 0) //ögrentmen ise geçiver şimdilik.
                        continue;

                    List<int> fittingSlots = new List<int>();
                    List<int> semiFittingSlots = new List<int>();

                    #region Course type collisions
                    foreach (var courseToReposition in collision.CrashingCourses.Where(x => !x.FacultyCourse).OrderBy(x => x.Duration)) //önce küçügü koy bir yerlere
                    {
                        int maxSlot = courseToReposition.Duration == 3 ? 20 : 25;
                        List<int> testSlots = new List<int>(maxSlot);
                        for (int i = 0; i < maxSlot; i++)
                        {
                            if (i != SlotId[courseToReposition.Id]) //eski yerini deneneceklerden çıkaralım.
                                testSlots.Add(i);
                        }

                        bool fittingSlot = false;

                        int semester = courseToReposition.Semester;
                        int duration = courseToReposition.Duration;

                        while (testSlots.Count > 0)
                        {
                            var randomSlotToTest = rnd.Next(testSlots.Count);
                            int slotToTest = testSlots[randomSlotToTest];
                            testSlots.RemoveAt(randomSlotToTest);

                            var temp = GetSlot(slotToTest, duration);
                            int day = GetX(slotToTest, duration);
                            int hour = GetY(slotToTest, duration);
                            var daySlots = GetDay(day);

                            #region Check Fitting
                            for (int j = 0; j < duration; j++)
                            {
                                fittingSlot = true;

                                temp[j].Courses.RemoveAll(x => x.Id == courseToReposition.Id); //kendini çıkarıyorum ki kendinle çakışmasın.

                                #region base vs faculty same semester
                                if (!courseToReposition.Elective) //elektif degilse, o saatte fakülte dersi olmamalı.
                                {
                                    if (temp[j].facultyCourses.Any(x => x.Semester == semester)) //base vs faculty collision, same semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion

                                #region base vs base same semester
                                if (!courseToReposition.Elective) //elektif degilse, o saatte başka bölüm dersi olmamalı
                                {
                                    if (temp[j].Courses.Count(x => x.Semester == semester && !x.Elective) > 0) //base vs base collision, same semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion

                                #region base vs faculty +1 -1
                                if (!courseToReposition.Elective) //elektif degilse,
                                {
                                    if (semester - 1 > 0) // o saate bir geri dönem fakülte dersi olmasın
                                    {
                                        if (temp[j].facultyCourses.Any(x => x.Semester == semester - 1)) //base vs faculty collision, -1 semester.
                                        {
                                            fittingSlot = false;
                                            break;
                                        }
                                    }
                                    if (semester <= 8) // o saate bir ileri dönem fakülte dersi olmasın
                                    {
                                        if (temp[j].facultyCourses.Any(x => x.Semester == semester + 1)) //base vs faculty collision, +1 semester.
                                        {
                                            fittingSlot = false;
                                            break;
                                        }
                                    }
                                }
                                #endregion

                                #region base vs base +1 -1
                                if (!courseToReposition.Elective)
                                {
                                    if (semester - 1 > 0)
                                    {
                                        if (temp[j].Courses.Count(x => x.Semester == semester - 1 && !x.Elective) > 0) //base vs base collision, -1 semester.
                                        {

                                            fittingSlot = false;
                                            break;

                                        }
                                    }
                                    if (semester + 1 < 9)
                                    {
                                        if (temp[j].Courses.Count(x => x.Semester == semester + 1 && !x.Elective) > 0) //base vs base faculty collision, +1 semester.
                                        {

                                            fittingSlot = false;
                                            break;

                                        }
                                    }
                                }
                                #endregion

                                #region LAB base vs faculty
                                if (courseToReposition.Type == 1) //lab ise
                                {
                                    if (temp[j].labCount + temp[j].facultyLab >= 4) //base vs faculty collision, same semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion

                                #region Teacher coll
                                if (courseToReposition.Teacher != "ASSISTANT")
                                {
                                    if (temp[j].Courses.Count(x => x.TeacherId == courseToReposition.TeacherId || temp[j].meetingHour) > 0) //check teacher collision
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion

                                #region Teacher consecutive coll
                                //todo: obj2 og. gor. gunluk 4 saatten fazla pespese dersinin olmamasi
                                if (courseToReposition.Teacher != "ASSISTANT")
                                {
                                    int maxConsecutiveHour = 4;
                                    int counter = 0;
                                    for (int k = 0; k < 9; k++)
                                    {
                                        Slot tempSlot = daySlots[k];

                                        if (k == hour)
                                            counter++;

                                        if (tempSlot.Courses.Any(x => x.TeacherId == courseToReposition.TeacherId) || tempSlot.meetingHour) //dersi veya meetingi varsa ++
                                        {
                                            counter++;
                                        }
                                        else
                                        {
                                            counter = 0; // ara varsa 0'la
                                        }
                                        if (counter > maxConsecutiveHour)
                                        {
                                            fittingSlot = false;
                                            break;
                                        }
                                    }
                                }
                                #endregion

                                //todo: obj2 og. gor. boş gununun olması

                                //bunu duration loop dışına alabilirim.
                                #region lab lecture collision
                                if (courseToReposition.LabHour > 0) //lab'ı var. //todo: obj2 lab ve lecture farklı günlerde olsun
                                {
                                    if (courseToReposition.Type == 1)//labı yerleştiriyoruz.
                                    {
                                        if (daySlots.Any(slot => slot.Courses.Count(x => x.Code == courseToReposition.Code && x.Type == 0) > 0))
                                        {
                                            fittingSlot = false;
                                            break;
                                        }
                                    }
                                    else //dersi yerleştiriyoruz
                                    {
                                        if (daySlots.Any(slot => slot.Courses.Count(x => x.Code == courseToReposition.Code && x.Type == 1) > 0))
                                        {
                                            fittingSlot = false;
                                            break;
                                        }
                                    }
                                }
                                #endregion

                                #region Elective vs Elective
                                if (courseToReposition.Elective)
                                {
                                    if (temp[j].Courses.Count(x => x.Elective) > 0) //elective vs elective collision, all semesters.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion

                                #region Elective vs Faculty in semester 6 7 8
                                //todo: obj2
                                if (courseToReposition.Elective)
                                {
                                    if (temp[j].facultyCourses.Any(x => x.Semester == 6)) //base vs faculty collision, same semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                    if (temp[j].facultyCourses.Any(x => x.Semester == 7)) //base vs faculty collision, same semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                    if (temp[j].facultyCourses.Any(x => x.Semester == 8)) //base vs faculty collision, same semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion

                                #region Elective vs Base
                                //elective vs base courses in semester
                                if (courseToReposition.Elective)
                                {
                                    if (temp[j].Courses.Count(x => x.Semester == 8 & !x.Elective) > 0) //elective vs base collision, #8 semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }

                                    if (temp[j].Courses.Count(x => x.Semester == 7 & !x.Elective) > 0) //elective vs base collision, #7 semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }

                                    if (temp[j].Courses.Count(x => x.Semester == 6 & !x.Elective) > 0) //todo: obj1 elective vs base collision, #6 semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                else if (courseToReposition.Semester == 8 || courseToReposition.Semester == 7 || courseToReposition.Semester == 6)
                                {
                                    if (temp[j].Courses.Count(x => x.Elective) > 0) //elective vs base collision, #8 semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }

                                    if (temp[j].Courses.Count(x => x.Elective) > 0) //elective vs base collision, #7 semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }

                                    if (temp[j].Courses.Count(x => x.Elective) > 0) //todo: obj1 elective vs base collision, #6 semester.
                                    {
                                        fittingSlot = false;
                                        break;
                                    }
                                }
                                #endregion
                            } // DURATION LOOP, ders saati kadar ileriye bakıyor.
                            #endregion

                            if (fittingSlot)
                            {
                                fittingSlots.Add(slotToTest);
                                break;
                            }
                        }

                        if (fittingSlots.Count > 0)
                        {
                            int selectedSlot = fittingSlots[rnd.Next(fittingSlots.Count)];
                            int checkDay = GetX(selectedSlot, duration) + 1;
                            int checkHour = GetY(selectedSlot, duration) + 1;

                            int fixedObj = collision.Obj;
                            ChangeGene(courseToReposition.Id, selectedSlot, problemObj);
                            SlotId[courseToReposition.Id] = selectedSlot;
                            climbCount++;
                            break;
                        }
                        else
                        {
                            //no fit
                            //Console.WriteLine("0 fitting slot found :(");
                        }
                    } //CRASHING COURSE
                    #endregion

                    if (climbCount >= maxClimb)
                    {
                        return true;
                    }

                }

                if (climbCount > 0)
                    return true;

                //hiçbirini çözemedi.
                return false;

            }

            return false;
        }
Exemple #32
0
        public void Copy(Individual ind, ProblemDefinition problem)
        {
            NRealVar = ind.NRealVar;
            NBinVar = ind.NBinVar;
            NMaxBit = ind.NMaxBit;
            NObj = ind.NObj;
            NCons = ind.NCons;

            CollisionList.Clear();
            FacultySections.Clear();
            DiffSemesterFacultySections.Clear();
            ElectiveFacultySections.Clear();

            TotalResult = 0;

            Rank = ind.Rank;
            ConstrViolation = ind.ConstrViolation;
            CrowdDist = ind.CrowdDist;
            if (ind.NRealVar > 0)
            {
                for (int i = 0; i < ind.NRealVar; i++)
                {
                    Xreal[i] = ind.Xreal[i];
                }
            }
            if (ind.NBinVar > 0)
            {
                for (int i = 0; i < ind.NBinVar; i++)
                {
                    SlotId[i] = ind.SlotId[i];
                    for (int j = 0; j < problem.Nbits[i]; j++)
                    {
                        Gene[i][j] = ind.Gene[i][j];
                    }
                }
            }
            for (int i = 0; i < ind.NObj; i++)
            {
                Obj[i] = ind.Obj[i];
            }
            if (ind.NCons > 0)
            {
                for (int i = 0; i < ind.NCons; i++)
                {
                    Constr[i] = ind.Constr[i];
                }
            }
        }
Exemple #33
0
        public void Decode(ProblemDefinition problem)
        {
            TotalResult = 0;
            CollisionList.Clear();
            if(FacultySections == null)
                FacultySections = new List<FacultySection>();
            FacultySections.Clear();
            if (DiffSemesterFacultySections == null)
                DiffSemesterFacultySections = new List<FacultySection>();
            DiffSemesterFacultySections.Clear();
            if (ElectiveFacultySections == null)
                ElectiveFacultySections = new List<FacultySection>();
            ElectiveFacultySections.Clear();

            if(Obj.Length == 0)
                Obj = new double[problem.ObjectiveCount];

            if (problem.BinaryVariableCount == 0)
                return;

            for (int j = 0; j < problem.BinaryVariableCount; j++)
            {
                SlotId[j] = DecodeGene(problem, j);
            }
        }
Exemple #34
0
        /* Function to print the information of feasible and non-dominated population in a file */
        public void ReportFeasiblePopulation(ProblemDefinition problemObj, string name, string info)
        {
            Directory.CreateDirectory("report");
            var file = File.OpenWrite($"report\\{problemObj.Title}_{name}_pop.out");
            StreamWriter writer = new StreamWriter(file);
            writer.WriteLine($"# {info}");
            writer.WriteLine($"# of objectives = {problemObj.ObjectiveCount}, # of constraints = {problemObj.ConstraintCount}, # of real_var = {problemObj.RealVariableCount}, # of bits of bin_var = {problemObj.TotalBinaryBitLength}, constr_violation, rank, crowding_distance");

            for (int i = 0; i < problemObj.PopulationSize; i++)
            {
                if (IndList[i].ConstrViolation == 0.0 && IndList[i].Rank == 1)
                {
                    for (int j = 0; j < problemObj.ObjectiveCount; j++)
                    {
                        writer.Write($"{IndList[i].Obj[j].ToString(CultureInfo.InvariantCulture)}\t");
                    }
                    if (problemObj.ConstraintCount != 0)
                    {
                        for (int j = 0; j < problemObj.ConstraintCount; j++)
                        {
                            writer.Write($"{IndList[i].Constr[j].ToString("E")}\t");
                        }
                    }
                    if (problemObj.RealVariableCount != 0)
                    {
                        for (int j = 0; j < problemObj.RealVariableCount; j++)
                        {
                            writer.Write($"{IndList[i].Xreal[j].ToString("E")}\t");
                        }
                    }
                    if (problemObj.BinaryVariableCount != 0)
                    {
                        for (int j = 0; j < problemObj.BinaryVariableCount; j++)
                        {
                            for (int k = 0; k < problemObj.Nbits[j]; k++)
                            {
                                writer.Write($"{IndList[i].Gene[j][k]}\t");
                            }
                        }
                    }
                    writer.Write($"{IndList[i].ConstrViolation.ToString("E")}\t");
                    writer.Write($"{IndList[i].Rank}\t");
                    writer.Write($"{ IndList[i].CrowdDist.ToString("E")}\n");
                }
            }

            writer.Flush();
            writer.Close();
        }
Exemple #35
0
        /* Routine to perform non-dominated sorting */
        static void fill_nondominated_sort(Population mixedPop, Population newPop, ProblemDefinition problemObj, Randomization randomizationObj)
        {
            int flag;
            int i, j;
            int end;
            int frontSize;
            int archieveSize;
            int rank = 1;
            Lists pool;
            Lists elite;
            Lists temp1, temp2;
            pool = new Lists();
            elite = new Lists();
            frontSize = 0;
            archieveSize = 0;
            pool.index = -1;
            pool.parent = null;
            pool.child = null;
            elite.index = -1;
            elite.parent = null;
            elite.child = null;
            temp1 = pool;
            for (i = 0; i < 2 * problemObj.PopulationSize; i++)
            {
                Insert(temp1, i);
                temp1 = temp1.child;
            }
            i = 0;
            do
            {
                temp1 = pool.child;
                Insert(elite, temp1.index);
                frontSize = 1;
                temp2 = elite.child;
                temp1 = Delete(temp1);
                temp1 = temp1.child;
                do
                {
                    temp2 = elite.child;
                    if (temp1 == null)
                    {
                        break;
                    }
                    do
                    {
                        end = 0;
                        flag = CheckDominance(mixedPop.IndList[temp1.index], mixedPop.IndList[temp2.index], problemObj);
                        if (flag == 1)
                        {
                            Insert(pool, temp2.index);
                            temp2 = Delete(temp2);
                            frontSize--;
                            temp2 = temp2.child;
                        }
                        if (flag == 0)
                        {
                            temp2 = temp2.child;
                        }
                        if (flag == -1)
                        {
                            end = 1;
                        }
                    }
                    while (end != 1 && temp2 != null);
                    if (flag == 0 || flag == 1)
                    {
                        Insert(elite, temp1.index);
                        frontSize++;
                        temp1 = Delete(temp1);
                    }
                    temp1 = temp1.child;
                }
                while (temp1 != null);
                temp2 = elite.child;
                j = i;
                if (archieveSize + frontSize <= problemObj.PopulationSize)
                {
                    do
                    {
                        newPop.IndList[i].Copy(mixedPop.IndList[temp2.index], problemObj);
                        //CopyIndividual(mixedPop.IndList[temp2.index], newPop.IndList[i]);

                        newPop.IndList[i].Rank = rank;
                        archieveSize += 1;
                        temp2 = temp2.child;
                        i += 1;
                    }
                    while (temp2 != null);
                    assign_crowding_distance_indices(newPop, j, i - 1, problemObj, randomizationObj);
                    rank += 1;
                }
                else
                {
                    crowding_fill(mixedPop, newPop, i, frontSize, elite, problemObj, randomizationObj);
                    archieveSize = problemObj.PopulationSize;
                    for (j = i; j < problemObj.PopulationSize; j++)
                    {
                        newPop.IndList[j].Rank = rank;
                    }
                }
                temp2 = elite.child;
                do
                {
                    temp2 = Delete(temp2);
                    temp2 = temp2.child;
                }
                while (elite.child != null);
            }
            while (archieveSize < problemObj.PopulationSize);
        }
Exemple #36
0
        ///* Function to display the current population for the subsequent generation */
        public void PlotPopulation(Population pop, ProblemDefinition problem, int genNo = 0, List<Individual> best = null)
        {
            if (!Use3D) //2D
            {

                var xr = new double[problem.PopulationSize];
                var yr = new double[problem.PopulationSize];

                for (int x = 0; x < problem.PopulationSize; x++)
                {
                    xr[x] = pop.IndList[x].Obj[GnuplotObjective1];
                    yr[x] = pop.IndList[x].Obj[GnuplotObjective2];
                }

                if (best != null)
                {
                    GnuPlot.HoldOn();
                }

                GnuPlot.Plot(xr, yr, $"title 'Generation #{genNo} of {problem.MaxGeneration}' with points pointtype 8 lc rgb 'blue'");

                if (best != null)
                {
                    var x = new double[best.Count];
                    var y = new double[best.Count];

                    for (int i = 0; i < best.Count; i++)
                    {
                        x[i] = best[i].Obj[GnuplotObjective1];
                        y[i] = best[i].Obj[GnuplotObjective2];
                    }
                    GnuPlot.Plot(x, y, $"title 'best ({best.First().TotalResult})' with points pointtype 6 lc rgb 'red'");
                }

                GnuPlot.Set($"xlabel \"obj[{GnuplotObjective1 }]\"");
                GnuPlot.Set($"ylabel \"obj[{GnuplotObjective2 }]\"");

            }
            else //3D
            {
                var rank1Count = pop.IndList.Count(x => x.Rank == 1);
                var rankOtherCount = pop.IndList.Count(x => x.Rank != 1);

                if (rank1Count > 0)
                {
                    var xr = new double[rank1Count];
                    var yr = new double[rank1Count];
                    var zr = new double[rank1Count];

                    int index = 0;
                    foreach (var ind in pop.IndList.Where(x => x.Rank == 1))
                    {
                        xr[index] = ind.Obj[GnuplotObjective1];
                        yr[index] = ind.Obj[GnuplotObjective2];
                        zr[index] = ind.Obj[GnuplotObjective3];
                        index++;
                    }

                    if (best != null || rankOtherCount > 0)
                    {
                        GnuPlot.HoldOn();
                    }

                    GnuPlot.SPlot(xr, yr, zr, $"title 'Rank 1 individuals' with points pointtype 8 lc rgb 'red'");

                }

                if (rankOtherCount > 0)
                {
                    var xr = new double[rankOtherCount];
                    var yr = new double[rankOtherCount];
                    var zr = new double[rankOtherCount];

                    int index = 0;
                    foreach (var ind in pop.IndList.Where(x => x.Rank != 1))
                    {
                        xr[index] = ind.Obj[GnuplotObjective1];
                        yr[index] = ind.Obj[GnuplotObjective2];
                        zr[index] = ind.Obj[GnuplotObjective3];
                        index++;
                    }
                    GnuPlot.SPlot(xr, yr, zr, $"title 'Rank 2-{pop.IndList.Max(x=>x.Rank)} individuals' with points pointtype 8 lc rgb 'blue'");
                }

                if (best != null)
                {

                    var x = new double[best.Count];
                    var y = new double[best.Count];
                    var z = new double[best.Count];

                    for (int i = 0; i < best.Count; i++)
                    {
                        x[i] = best[i].Obj[GnuplotObjective1];
                        y[i] = best[i].Obj[GnuplotObjective2];
                        z[i] = best[i].Obj[GnuplotObjective3];
                    }

                    GnuPlot.SPlot(x, y, z, $"title 'best ({best.First().TotalResult})' with points pointtype 6 lc rgb 'green'");
                }

                GnuPlot.Set($"title 'Generation #{genNo} of {problem.MaxGeneration}'");

                GnuPlot.Set($"xlabel \"obj[{GnuplotObjective1}]\"");
                GnuPlot.Set($"ylabel \"obj[{GnuplotObjective2}]\"");
                GnuPlot.Set($"zlabel \"obj[{GnuplotObjective3}]\"");
            }
        }
Exemple #37
0
        public UCTProblem(double dSeed, int nPopulation, int nMaxGeneration, int nObjective, int nConstraint, bool useBinary, double crossProbability, double mutateProbability, bool usePlot = false, DisabledCollisions disableOptions = DisabledCollisions.None)
        {
            CurrentGeneration = 0;
            Seed = dSeed;
            if (Seed <= 0.0 || Seed >= 1.0)
            {
                Console.WriteLine("\n Entered seed value is wrong, seed value must be in (0,1) \n");
                Seed = 0.75;
            }

            UsePlot = usePlot;

            var title = RandomTitle.GetRandomTitle();

            ProblemObj = new ProblemDefinition(title)
            {
                PopulationSize = nPopulation,
                MaxGeneration = nMaxGeneration,
                ObjectiveCount = nObjective,
                ConstraintCount = nConstraint,
                BinaryVariableCount = 0,
                RealVariableCount = 0,
                DisabledCollisions = disableOptions
            };

            ProblemObj.TeacherList.Add("ASSISTANT");
            ReadCourseList();

            if (useBinary)
            {
                ProblemObj.BinaryVariableCount = ProblemObj.CourseList.Count;

                PrepareBinaryLimits();

                ProblemObj.BinaryCrossoverProbability = crossProbability;
                if (ProblemObj.BinaryCrossoverProbability < 0.0 || ProblemObj.BinaryCrossoverProbability > 1.0)
                {
                    ProblemObj.BinaryCrossoverProbability = 0.75;
                }

                ProblemObj.BinaryMutationProbability = mutateProbability;
                if (ProblemObj.BinaryMutationProbability < 0.0 || ProblemObj.BinaryMutationProbability > 1.0)
                {
                    ProblemObj.BinaryMutationProbability = 0.0232558;
                }
            }
            else
            {
                ProblemObj.BinaryVariableCount = ProblemObj.CourseList.Count;

                ReadRealValues();

                ProblemObj.BinaryCrossoverProbability = crossProbability;
                if (ProblemObj.BinaryCrossoverProbability < 0.0 || ProblemObj.BinaryCrossoverProbability > 1.0)
                {
                    ProblemObj.BinaryCrossoverProbability = 0.75;
                }

                ProblemObj.BinaryMutationProbability = mutateProbability;
                if (ProblemObj.BinaryMutationProbability < 0.0 || ProblemObj.BinaryMutationProbability > 1.0)
                {
                    ProblemObj.BinaryMutationProbability = 0.0232558;
                }
            }

            WriteStartParams();

            RandomizationObj = new Randomization(dSeed);
            RandomizationObj.Randomize();

            _displayObj = new Display();
            InitDisplay(true, true, new[] { 0, 1, 2 });

            ReadFacultyCourses();
            ReadLab();
            ReadMeeting();

            ReadPreq();

            CreatePopulationObject();
        }