Example usage for org.apache.commons.math3.util Combinations iterator

List of usage examples for org.apache.commons.math3.util Combinations iterator

Introduction

In this page you can find the example usage for org.apache.commons.math3.util Combinations iterator.

Prototype

public Iterator<int[]> iterator() 

Source Link

Usage

From source file:ai_coursework.HWState.java

/**
 * Finds all possible and valid successor states from this.state
 * @return A list of all valid successor states
 *//*w  w  w.  ja va 2  s  .com*/
@Override
public List<ActionStatePair> successor() {
    List<ActionStatePair> result = new ArrayList<>();
    ArrayList<Person> bank = raftLocation == RiverBank.NORTH ? northBank : southBank;

    if (this.isInvalid()) // If the current state is invalid
        return result; // Return an emtpy set

    for (int i = 0; i <= HusbandWives.RAFTSIZE && i <= bank.size(); i++) {
        Combinations combo = new Combinations(bank.size(), i);
        Iterator<int[]> iterator = combo.iterator();

        while (iterator.hasNext()) {
            int[] selected = iterator.next();
            ArrayList<Person> raft = new ArrayList<>();

            for (int index : selected) {
                raft.add(bank.get(index));
            }

            if (raft.isEmpty())
                break;

            HWAction action = new HWAction(raft, oppositeBank(raftLocation));
            HWState nextState = this.applyAction(action);
            if (!nextState.isInvalid())
                result.add(new ActionStatePair(action, nextState));
        }
    }

    return result;
}

From source file:lovinghusbandpuzzle.LoHusbState.java

/**
 * This method finds all the valid successors of the current state.
 *
 * @return a list containing the valid children of the current state
 *//*from  www .j a v a 2  s  .  c o m*/
@Override
public List<ActionStatePair> successor() {
    List<ActionStatePair> result = new ArrayList<>();
    if (this.isInvalid()) //if current state is invalid
    {
        return result; //return an empty set
    }
    //an array of all of the people, that are on the side of the raft
    //therefore they will be moved and create children
    ArrayList<Character> toMove = new ArrayList<>();
    for (int j = 0; j < couplesN.length; j++) {
        if (raftLocation == RiverBank.NORTH) {//if the raft is on the north
            if (couplesN[j] != '0') {//people from the north are added to the arraylist
                toMove.add(couplesN[j]);
            }
        } else {
            if (couplesS[j] != '0') {//if the raft is on the south
                toMove.add(couplesS[j]);//people from the north are added to the arraylist
            }
        }
    }
    //start with moving a number of people as the raft size
    //until it gets to 1(moving 1 person)
    for (int i = LovingHusbandProblem.raftSize; i > 0; i--) {
        if (toMove.size() < i) {
            //if the people that need to be moved are less than the raft size
            //then i starts with the size of the arraylist
            i = toMove.size();
        }
        //make a new combination every time
        Combinations comb = new Combinations(toMove.size(), i);
        Iterator<int[]> iterator = comb.iterator();
        while (iterator.hasNext()) //while there is a combination left
        {
            int[] selected = iterator.next();//get the next one
            char[] temp = new char[i];//make a temporary array
            int count = 0;
            for (int index : selected) //Go through elements in the "selected" array. Each one is an index into the original array.
            {
                temp[count] = toMove.get(index);//fill the temp array with combination people
                count++;
            }
            LoHusbAction action = new LoHusbAction(temp, oppositeBank(this.raftLocation));
            if (action.peopleMoved.length > 0) {
                LoHusbState nextState = this.applyAction(action);
                if (!nextState.isInvalid()) {
                    result.add(new ActionStatePair(action, nextState));
                }
            }

        }
    }
    return result;
}