Ejemplo n.º 1
0
        private static void SolveNSGA()
        {
            NSGA nsga = new NSGA();

            Console.WriteLine("WA NSGA Solver Started");
            Console.WriteLine("Press ESC to stop");

            nsga.Solve();

            Console.WriteLine("WA NSGA Solver Finished");
        }
Ejemplo n.º 2
0
 void UpdateSD(List<MoChromosome> pop)
 {
     List<MoChromosome> temp = NSGA.FastNonDominatedSort(pop)[0];
     GetDintercept(temp);
     for (int i = 0; i < this.numObjectives; i++) sd[i] = Double.MaxValue;
     for (int i = 0; i < temp.Count(); i++)
     {
         double[] tp = ProjPoint(temp[i]);
         for (int j = 0; j < this.numObjectives; j++)
         {
             if (sd[j] > tp[j]) sd[j] = tp[j];
         }
     }
 }
Ejemplo n.º 3
0
        protected override void DoSolve()
        {
            Initial();

            string prob = mop.GetName();

            pofData = FileTool.ReadData(pofPath + prob);
            igdValue.Add(QulityIndicator.QulityIndicator.IGD(mainpop, pofData));

            frm = new plotFrm(mainpop, mop.GetName());
            frm.Show();
            frm.Refresh();
            while (!Terminated())
            {
                List <MoChromosome> offsPop = new List <MoChromosome>();

                for (int i = 0; i < K; i++)
                {
                    for (int j = 0; j < Pop[i].Count; j++)
                    {
                        MoChromosome offspring;
                        offspring = GeneticOPDE(i, j);//GeneticOPDE//GeneticOPSBXCrossover
                        this.Evaluate(offspring);
                        offsPop.Add(offspring);
                        UpdateReference(offspring);
                    }
                }

                offsPop.AddRange(mainpop);
                EnviromentSelection(offsPop);

                if (this.ItrCounter % 10 == 0)
                {
                    frm.refereshPlot(this.ItrCounter, mainpop);
                    frm.Refresh();
                    igdValue.Add(QulityIndicator.QulityIndicator.IGD(mainpop, pofData));
                }

                this.ItrCounter++;
            }
            List <MoChromosome> result = new List <MoChromosome>();

            result.AddRange(mainpop);
            mainpop.Clear();
            mainpop.AddRange(NSGA.FastNonDominatedSort(result)[0]);
            Common.FileTool.WritetoFile(mainpop, "gen", 1);
            Common.FileTool.WritetoFile(mainpop, "obj", 2);
            Common.FileTool.WritetoFile(igdValue, "igdCurve");
        }
Ejemplo n.º 4
0
        private void EnviromentSelection(List <MoChromosome> pop)
        {
            List <MoChromosome>         result        = new List <MoChromosome>();
            List <List <MoChromosome> > dominatedSet0 = NSGA.fastNonDominatedSort(pop);

            computeFitness(ref pop);

            int cnt = 0;

            while (result.Count() + dominatedSet0[cnt].Count() <= this.popsize)
            {
                for (int r = 0; r < dominatedSet0[cnt].Count(); r++)
                {
                    dominatedSet0[cnt][r].selected = true;
                }

                result.AddRange(dominatedSet0[cnt]);
                cnt++;
            }
            if (result.Count() == this.popsize)
            {
                //return result;
                mainpop.Clear();
                mainpop.AddRange(result);
                return;
            }

            List <MoChromosome> Fl = new List <MoChromosome>();

            Fl.AddRange(dominatedSet0[cnt]);

            Boolean[] flag = new Boolean[Fl.Count];
            for (int i = 0; i < flag.Length; i++)
            {
                flag[i] = false;
            }

            Associate(ref flag, ref result, ref Fl);

            //List<double[]> P = GetVector(ref result, Fl);

            Niching(ref flag, ref result, Fl);

            mainpop.Clear();
            mainpop.AddRange(result);
        }
Ejemplo n.º 5
0
        public Boolean contrained_dominates(MoChromosome another)
        {
            Boolean m1 = NSGA.isFeasibleSolutions(this);
            Boolean m2 = NSGA.isFeasibleSolutions(another);

            if (m1 == true && m2 == false)
            {
                return(true);
            }
            if (m1 == false && m2 == true)
            {
                return(false);
            }
            if (m1 == false && m2 == false)
            {
                if (NSGA.getCVValue(this) < NSGA.getCVValue(another))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }

            Boolean flag = false;

            for (int i = 0; i < objectDimension; i++)
            {
                if (this.objectivesValue[i] > another.objectivesValue[i])
                {
                    return(false);
                }
                if (this.objectivesValue[i] < another.objectivesValue[i])
                {
                    flag = true;
                }
            }
            return(flag);
        }
Ejemplo n.º 6
0
        protected void EnviromentSelection(List <MoChromosome> pop)
        {
            List <MoChromosome> result = new List <MoChromosome>();
            //List<List<MoChromosome>> dominatedSet0 = NSGA.fastNonDominatedSort(pop);
            List <List <MoChromosome> > dominatedSet0 = NSGA.fastConstrainedNonDominatedSort(pop);

            int cnt = 0;

            while (result.Count() + dominatedSet0[cnt].Count() < this.popsize)
            {
                for (int r = 0; r < dominatedSet0[cnt].Count(); r++)
                {
                    dominatedSet0[cnt][r].selected = true;
                }

                result.AddRange(dominatedSet0[cnt]);
                cnt++;
            }
            if (result.Count() == this.popsize)
            {
                //return result;
                mainpop.Clear();
                mainpop.AddRange(result);
                return;
            }

            NSGA.crowdingDistanceAssignment(dominatedSet0[cnt]);
            MoChromosome[] arr = NSGA.sort(dominatedSet0[cnt]);
            int            i   = 0;

            while (result.Count() < popsize)
            {
                result.Add(arr[i]);
                i++;
            }
            mainpop.Clear();
            mainpop.AddRange(result);
            return;
        }
Ejemplo n.º 7
0
        protected void EnviromentSelection(List <MoChromosome> pop)
        {
            RawFitness(pop);
            List <MoChromosome> result = new List <MoChromosome>();

            for (int i = 0; i < pop.Count; i++)
            {
                if (pop[i].fitnessValue < 1)
                {
                    result.Add(pop[i]);
                }
            }

            if (result.Count < popsize)
            {
                pop = pop.OrderBy(r => r.fitnessValue).ToList();
            }

            int cnt = result.Count;

            while (result.Count < popsize)
            {
                result.Add(pop[cnt]);
                cnt++;
            }
            if (result.Count > popsize)
            {
                NSGA.crowdingDistanceAssignment(result);
                result = result.OrderByDescending(r => r.crdistance).ToList();
            }

            mainpop.Clear();
            for (int r = 0; r < popsize; r++)
            {
                mainpop.Add(result[r]);
            }
        }
Ejemplo n.º 8
0
        protected override void doSolve()
        {
            initial();

            string prob = mop.getName();

            pofData = FileTool.readData(pofPath + prob);
            igdValue.Add(QulityIndicator.QulityIndicator.IGD(mainpop, pofData));
            frm = new plotFrm(mainpop, mop.getName());
            frm.Show();
            frm.Refresh();
            while (!terminated())
            {
                for (int i = 0; i < popsize; i++)
                {
                    MoChromosome offSpring = SBXCrossover(i, true);//GeneticOPDE//GeneticOPSBXCrossover
                    this.evaluate(offSpring);
                    updateNeighbours(i, offSpring);
                    updateReference(offSpring);
                    offSpring = null;
                }
                if (this.ItrCounter == 0.7 * this.TotalItrNum)
                {
                    isCave = isConcave();
                    //isCave = false;
                    if (isCave == false)
                    {
                        exterSet.Clear();
                        for (int i = 0; i < this.mainpop.Count; i++)
                        {
                            MoChromosome offSpring = this.createChromosome();
                            this.mainpop[i].copyTo(offSpring);
                            exterSet.Add(offSpring);
                        }

                        for (int i = 0; i < this.weights.Count(); i++)
                        {
                            for (int j = 0; j < this.numObjectives; j++)
                            {
                                this.weights[i][j] = 1 - this.weights[i][j];
                            }
                        }
                        getTransweight();
                        this.neighbourTable.Clear();
                        initNeighbour();

                        for (int i = 0; i < popsize; i++)
                        {
                            for (int j = 0; j < this.numObjectives; j++)
                            {
                                narpoint[j] = 1;
                            }
                        }
                    }
                }

                if (this.ItrCounter % 10 == 0)
                {
                    List <MoChromosome> union = new List <MoChromosome>();
                    union.AddRange(mainpop);
                    union.AddRange(exterSet);
                    frm.refereshPlot(this.ItrCounter, union);
                    frm.Refresh();
                    igdValue.Add(QulityIndicator.QulityIndicator.IGD(mainpop, pofData));
                }

                this.ItrCounter++;
            }
            mainpop.AddRange(exterSet);
            List <MoChromosome> result = new List <MoChromosome>();

            result.AddRange(mainpop);
            mainpop.Clear();
            mainpop.AddRange(NSGA.fastNonDominatedSort(result)[0]);

            Common.FileTool.WritetoFile(mainpop, "gen", 1);
            Common.FileTool.WritetoFile(mainpop, "obj", 2);
            Common.FileTool.WritetoFile(igdValue, "igdCurve");
        }
Ejemplo n.º 9
0
        protected void EnviromentSelection(List <MoChromosome> pop)
        {
            List <MoChromosome>         result        = new List <MoChromosome>();
            List <List <MoChromosome> > dominatedSet0 = NSGA.FastNonDominatedSort(pop);

            if (GlobalValue.IsNormalization)
            {
                UpdateNadirPoint(dominatedSet0[0]);
            }
            //updateNadirPoint(dominatedSet0[0]);

            int cnt = 0;

            while (result.Count() + dominatedSet0[cnt].Count() <= this.popsize)
            {
                for (int r = 0; r < dominatedSet0[cnt].Count(); r++)
                {
                    dominatedSet0[cnt][r].selected = true;
                }

                result.AddRange(dominatedSet0[cnt]);
                cnt++;
            }
            if (result.Count() == this.popsize)
            {
                //return result;
                mainpop.Clear();
                mainpop.AddRange(result);
                return;
            }

            int[] count = new int[popsize];
            for (int i = 0; i < popsize; i++)
            {
                count[i] = 0;
            }

            for (int i = 0; i < result.Count(); i++)
            {
                double dist = Double.MaxValue, dt;
                int    pos = -1;
                for (int j = 0; j < this.weights.Count(); j++)
                {
                    dt = GetAngle(j, result[i], GlobalValue.IsNormalization);
                    if (dt < dist)
                    {
                        dist = dt;
                        pos  = j;
                    }
                }
                count[pos]++;
            }

            List <List <MoChromosome> > associatedSolution;

            associatedSolution = new List <List <MoChromosome> >();
            for (int i = 0; i < this.weights.Count(); i++)
            {
                List <MoChromosome> temp = new List <MoChromosome>();
                associatedSolution.Add(temp);
            }

            for (int i = 0; i < dominatedSet0[cnt].Count(); i++)
            {
                double dist = Double.MaxValue, dt;
                int    pos = -1;
                for (int j = 0; j < this.weights.Count(); j++)
                {
                    dt = GetAngle(j, dominatedSet0[cnt][i], GlobalValue.IsNormalization);
                    if (dt < dist)
                    {
                        dist = dt;
                        pos  = j;
                    }
                }
                dominatedSet0[cnt][i].tchVal    = PbiScalarObj(pos, dominatedSet0[cnt][i], GlobalValue.IsNormalization);//dist;
                dominatedSet0[cnt][i].subProbNo = pos;
                associatedSolution[pos].Add(dominatedSet0[cnt][i]);
            }

            for (int i = 0; i < this.weights.Count(); i++)
            {
                associatedSolution[i].Sort();
            }

            List <int> itr = new List <int>();

            while (result.Count < popsize)
            {
                itr.Clear();
                int min = popsize;
                for (int i = 0; i < popsize; i++)
                {
                    if (min > count[i] && associatedSolution[i].Count > 0)
                    {
                        itr.Clear();
                        min = count[i];
                        itr.Add(i);
                    }
                    else if (min == count[i] && associatedSolution[i].Count() > 0)
                    {
                        itr.Add(i);
                    }
                }

                int pos = random.Next(itr.Count());
                count[itr[pos]]++;
                result.Add(associatedSolution[itr[pos]][0]);
                associatedSolution[itr[pos]].RemoveAt(0);
            }
            mainpop.Clear();
            mainpop.AddRange(result);
            return;
        }
Ejemplo n.º 10
0
        protected void EnviromentSelection(List <MoChromosome> pop)
        {
            for (int i = 0; i < K; i++)
            {
                Pop[i].Clear();
            }

            for (int i = 0; i < pop.Count; i++)
            {
                double min = Double.MaxValue;
                int    pos = -1;
                for (int j = 0; j < K; j++)
                {
                    double tp = GetAngle(j, pop[i]);
                    if (min > tp)
                    {
                        min = tp;
                        pos = j;
                    }
                }
                Pop[pos].Add(pop[i]);
            }

            mainpop.Clear();
            List <MoChromosome> result = new List <MoChromosome>();

            for (int i = 0; i < K; i++)
            {
                if (Pop[i].Count < S)
                {
                    int total1 = Pop[i].Count;
                    result.AddRange(Pop[i]);
                    while (total1 < S)
                    {
                        result.Add(pop[random.Next() % pop.Count]);
                        total1++;
                    }
                    continue;
                }

                List <List <MoChromosome> > dominatedSet0 = NSGA.FastNonDominatedSort(Pop[i]);

                int cnt   = 0;
                int total = 0;
                while (total + dominatedSet0[cnt].Count() < this.S)
                {
                    for (int r = 0; r < dominatedSet0[cnt].Count(); r++)
                    {
                        dominatedSet0[cnt][r].selected = true;
                    }
                    total += dominatedSet0[cnt].Count();
                    result.AddRange(dominatedSet0[cnt]);
                    cnt++;
                }
                if (total < S)
                {
                    NSGA.CrowdingDistanceAssignment(dominatedSet0[cnt]);
                    MoChromosome[] arr = NSGA.Sort(dominatedSet0[cnt]);

                    for (int r = 0; r < S - total; r++)
                    {
                        result.Add(arr[r]);
                    }
                }
            }

            for (int i = 0; i < K; i++)
            {
                Pop[i].Clear();
            }
            mainpop.AddRange(result);

            for (int i = 0; i < result.Count; i++)
            {
                double min = Double.MaxValue;
                int    pos = -1;
                for (int j = 0; j < K; j++)
                {
                    double tp = GetAngle(j, result[i]);
                    if (min > tp)
                    {
                        min = tp;
                        pos = j;
                    }
                }
                Pop[pos].Add(result[i]);
            }
        }
Ejemplo n.º 11
0
        protected void EnviromentSelection(List<MoChromosome> pop)
        {
            List<MoChromosome> result = new List<MoChromosome>();
            //List<List<MoChromosome>> dominatedSet0 = NSGA.fastNonDominatedSort(pop);
            List<List<MoChromosome>> dominatedSet0 = NSGA.FastConstrainedNonDominatedSort(pop);

            int cnt = 0;
            while (result.Count() + dominatedSet0[cnt].Count() <= this.popsize)
            {
                for (int r = 0; r < dominatedSet0[cnt].Count(); r++)
                {
                    dominatedSet0[cnt][r].selected = true;
                }

                result.AddRange(dominatedSet0[cnt]);
                cnt++;
            }
            if (result.Count() == this.popsize)
            {
                mainpop.Clear();
                mainpop.AddRange(result);
                return;
            }

            if(this.ItrCounter < Mr * this.TotalItrNum)
            {
                UpdateSD(pop);
                UpdateDintercept();
            }


            List<List<MoChromosome>> associatedSolution = Clustering(dominatedSet0[cnt]);

            for (int i = 0; i < this.weights.Count(); i++)
            {
                associatedSolution[i].Sort();
            }

            cnt = 0;
            while (true)
            {
                int size = 0;
                for (int i = 0; i < this.weights.Count(); i++)
                {
                    if (associatedSolution[i].Count() > cnt)
                    {
                        size++;
                    }
                }
                if (result.Count() + size <= this.popsize)
                {
                    for (int i = 0; i < this.weights.Count(); i++)
                    {
                        if (associatedSolution[i].Count() > cnt)
                        {
                            result.Add(associatedSolution[i][cnt]);
                        }
                    }
                }
                else
                {
                    break;
                }

                if (result.Count() == this.popsize) break;
                cnt++;
            }
            if (result.Count() < this.popsize)
            {
                List<MoChromosome> temp = new List<MoChromosome>();
                for (int i = 0; i < this.weights.Count(); i++)
                {
                    if (associatedSolution[i].Count() > cnt)
                    {
                        temp.Add(associatedSolution[i][cnt]);
                    }
                }

                Boolean[] flag = new Boolean[temp.Count()];
                for (int i = 0; i < flag.Length; i++) flag[i] = false;
                while (result.Count() < popsize)
                {
                    int pos = random.Next(temp.Count());
                    while (flag[pos] == true)
                    {
                        pos = random.Next(temp.Count());
                    }

                    flag[pos] = true;
                    result.Add(temp[pos]);
                }

            }
            mainpop.Clear();
            mainpop.AddRange(result);
            return;
        }
Ejemplo n.º 12
0
        protected override void DoSolve()
        {
            initial();

            string prob = mop.GetName();

            if (prob.IndexOf("DTLZ") != -1)
            {
                igdValue.Add(QulityIndicator.QulityIndicator.DTLZIGD(mainpop, prob, this.numObjectives));
            }
            else
            {
                pofData = FileTool.ReadData(pofPath + prob);
                igdValue.Add(QulityIndicator.QulityIndicator.IGD(mainpop, pofData));
            }

            if (GlobalValue.IsShowProcess)
            {
                frm = new plotFrm(mainpop, mop.GetName());
                frm.Show();
                frm.Refresh();
            }
            while (!Terminated())
            {
                List <MoChromosome> offsPop = new List <MoChromosome>();
                for (int i = 0; i < popsize; i++)
                {
                    MoChromosome offspring;
                    if (GlobalValue.CrossoverType.Equals("SBX"))
                    {
                        offspring = SBXCrossover(i, true);//GeneticOPDE//GeneticOPSBXCrossover
                    }
                    else
                    {
                        offspring = DECrossover(i, true);
                    }
                    this.Evaluate(offspring);
                    offsPop.Add(offspring);
                    UpdateReference(offspring);
                }

                List <MoChromosome> Pop = new List <MoChromosome>();
                Pop.AddRange(mainpop);
                Pop.AddRange(offsPop);

                EnviromentSelection(Pop);

                if (this.ItrCounter % 10 == 0)
                {
                    if (GlobalValue.IsShowProcess)
                    {
                        frm.refereshPlot(this.ItrCounter, mainpop);
                        frm.Refresh();
                    }

                    if (prob.IndexOf("DTLZ") != -1)
                    {
                        igdValue.Add(QulityIndicator.QulityIndicator.DTLZIGD(mainpop, prob, this.numObjectives));
                    }
                    else
                    {
                        igdValue.Add(QulityIndicator.QulityIndicator.IGD(mainpop, pofData));
                    }
                }

                ItrCounter++;
            }


            Common.FileTool.WritetoFile(NSGA.NonDominatedSolutions(mainpop), "gen", 1);
            Common.FileTool.WritetoFile(NSGA.NonDominatedSolutions(mainpop), "obj", 2);
            Common.FileTool.WritetoFile(igdValue, "igdCurve");
        }
Ejemplo n.º 13
0
        protected void EnviromentSelection(List <MoChromosome> pop)
        {
            mainpop.Clear();

            if (GlobalValue.IsNormalization)
            {
                UpdateNadirPoint(NSGA.NonDominatedSolutions(pop));
            }

            List <List <PairRelation> > aggr = new List <List <PairRelation> >();

            for (int i = 0; i < popsize; i++)
            {
                List <PairRelation> li = new List <PairRelation>();
                for (int j = 0; j < pop.Count(); j++)
                {
                    PairRelation pair = new PairRelation(j, updateCretia(i, pop[j]));
                    li.Add(pair);
                }
                aggr.Add(li);
            }
            for (int i = 0; i < popsize; i++)
            {
                aggr[i] = aggr[i].OrderBy(r => r.val).ToList();
            }

            List <MoChromosome> result = new List <MoChromosome>();

            int cnt = 0;

            for (int i = 0; i < popsize; i++)
            {
                for (int j = 0; j < pop.Count(); j++)
                {
                    if (j >= topK)
                    {
                        break;
                    }
                    if (pop[aggr[i][j].pos].selected != true)
                    {
                        mainpop.Add(pop[aggr[i][j].pos]);
                        pop[aggr[i][j].pos].selected = true;
                        //pop[aggr[i][j].pos].crdistance = dist(pop.get(aggr[i][j].pos));
                        if (j == 0)
                        {
                            cnt++;
                        }
                        result.Add(pop[aggr[i][j].pos]);
                        //cnt++;
                        break;
                    }
                }
            }


            if (result.Count() != popsize)
            {
                int[] count = new int[popsize];
                for (int i = 0; i < popsize; i++)
                {
                    count[i] = 0;
                }
                for (int i = 0; i < result.Count(); i++)
                {
                    double min  = Double.MaxValue;
                    int    pos  = -1;
                    double temp = 0;
                    for (int j = 0; j < popsize; j++)
                    {
                        temp = this.GetAngle(j, result[i], GlobalValue.IsNormalization); //getAngle1
                        if (temp < min)
                        {
                            min = temp;
                            pos = j;
                        }
                    }
                    Distance(this.idealpoint, this.narpoint);
                    result[i].angle = min;
                    count[pos]++;
                }
                List <int> minPos = new List <int>();
                while (result.Count() < popsize)
                {
                    minPos.Clear();
                    int min = count[0];
                    minPos.Add(0);
                    for (int i = 1; i < popsize; i++)
                    {
                        if (count[i] < min)
                        {
                            min = count[i];
                            minPos.Clear();
                            minPos.Add(i);
                        }
                        else if (count[i] == min)
                        {
                            minPos.Add(i);
                        }
                    }

                    int    pos    = minPos[random.Next(minPos.Count())];
                    double valmin = Double.MaxValue;
                    int    _pos   = -1;
                    for (int i = 0; i < pop.Count(); i++)
                    {
                        if (pop[i].selected == true)
                        {
                            continue;
                        }
                        double tp = this.GetAngle(pos, pop[i], GlobalValue.IsNormalization); //getAngle1
                        if (tp < valmin)
                        {
                            valmin = tp;
                            _pos   = i;
                        }
                    }
                    count[pos]++;
                    pop[_pos].selected = true;
                    pop[_pos].angle    = valmin;
                    result.Add(pop[_pos]);
                }
            }

            mainpop.Clear();
            mainpop.AddRange(result);

            for (int i = 0; i < popsize; i++)
            {
                mainpop[i].selected = false;
            }
        }