简体   繁体   中英

Sorting Elements in an ArrayList against Elements in the same List

I'm working at implementing the NSGA ii algorithm into my Final Year Project. Part of the algorithm is sorting all possible solutions into fronts, ie dominated solutions and undominated solutions

I'm having problems getting the dominated set.

At the minute I'm using a nested for loop, taking the first element from the unsorted list, checking it against every item in the list. If the element is undominated it means either its soft or hard constraints is less than the element being checked against, or both less than. My idea was to say if this isnt true break out of the nested loop and go to the first loop. If it makes it through the whole list without being dominated it will be checked to see if it has been checked against everything, if it has then it is undominated. I hope this makes sense!

Just looking for any bit of help to get it going, thanks :-)

Basically put all I want to do is check the conditions and do something if the conditions ate met within the nested loop for

public void sortIntoFronts(ArrayList<Chromosome> c )
    {

    ArrayList<Chromosome> UnsortedSet =  new ArrayList<Chromosome>();
    ArrayList<Chromosome> UndominatedSet = new ArrayList<Chromosome>();
    ArrayList<Chromosome> DominatedSet = new ArrayList<Chromosome>();

    UnsortedSet = c;

    Chromosome a = new Chromosome();
    Chromosome b = new Chromosome();

    for (int x = 0; x<=UnsortedSet.size()-1; x++)
    {

        a=UnsortedSet.get(x);

        for(int y = 0; y<=UnsortedSet.size()-1;)
        {

            b=UnsortedSet.get(y);

            if(a.SoftConstraints<=b.SoftConstraints || a.hardConstraints<=b.hardConstraints)
            {
                y++;
            } else
            {
                break;
            }

            if(y==UnsortedSet.size()-1)
            {
                UndominatedSet.add(a);   
            }
        }
    }
}

Basically put all I want to do is check the conditions and do something if the conditions ate met within the nested loop for, using this code could some1 help me

for (int x = 0; x<=UnsortedSet.size()-1; x++)
{

    a=UnsortedSet.get(x);

    for(int y = 0; y<=UnsortedSet.size()-1;y++)
    {

    b=UnsortedSet.get(y);

    }
}

I haven't really understood what your question really is. But to recap:

  1. a is sad to dominate b , if and only if a.SoftConstraints<=b.SoftConstraints || a.hardConstraints<=b.hardConstraints a.SoftConstraints<=b.SoftConstraints || a.hardConstraints<=b.hardConstraints holds.
  2. a is sad to be in the same dominated front as b if and only if a.SoftConstraints==b.SoftConstraints || a.hardConstraints==b.hardConstraints a.SoftConstraints==b.SoftConstraints || a.hardConstraints==b.hardConstraints holds
  3. in the other cases b dominates a

Based on this you can define a Comparator to create an ordered list of your unsorted set. Then you can just iterate over that sorted list. Your inner loop is just the iteration over the sublist to which the outer loop already proceeded (or depending on sort order from where the outer loop already proceeded). You have to take your action than for every iteration of that inner loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM