1

I've been working on different genetic algorithm selection methods recently. At the moment I have a RawFitnessSelction class, a RouletteWheelSelection class and a TournamentSelection class. I've been asked to use a factory/abstract factory pattern to choose the different selection methods. I think I'm going to go with a factory pattern as I don't fully understand abstract factory patterns but I'm wondering how to apply the actual pattern to something like GA selection methods?

public static Map<String, Double> rouletteSelect (Map<String, Double> population) {
    //Create a List (ArrayList) called rouletteSelect of type
    //Map<String, Double> and input values from our input Map.
    List<Map.Entry<String,Double>> sorted = new ArrayList<>(population.entrySet());

    //Use collections.sort to sort our "sorted" base on their values
    Collections.sort(sorted, Comparator.comparing(Map.Entry::getValue));

    //Initialize a HashSet
    Set<Integer> usedIndices = new HashSet<>();

    //Initialize our result Map<>, to store our selected individuals
    Map<String, Double> result = new HashMap<>();

    //we want to "select" half the individuals so while the size of our result 
    //is less than half, keep going
    while (result.size() < sorted.size()/2) {
        //pick a random int between 0 and the number of elements we have
        int index = rnd.nextInt(sorted.size());

        //if the element isn't in our set (a set can only contain one
        //occurrence of value x) do what is after our if, else it increments 
        //our while loop (it's already in our set)
        if(!usedIndices.add(index)) {
            continue;
        }
        //create a new Map, which has the <key,vale> of corresponding
        //index of "index" in sorted ArrayList
        Map.Entry<String, Double> survivor =sorted.get(index);
        //pur key and value from survivor into our result HashMap.
        result.put(survivor.getKey(), survivor.getValue());
    }
    return result;
}

Here's the basic structure of my selection methods. Just a basic method stored inside a class. Any help on how to implement the factory pattern to these types of methods would be appreciated.

Note: I know what my actual factory.java file will look like it's more so the abstact Selection.java class and the subclass RouletteWheel.java. Thanks

7
  • I just handled an abstract factory question here. The example lets you switch between 2 factories that each will make 3 different things when asked (so 6 all together). But it sounds like you only have 3 things to switch between. Right? Commented Nov 1, 2017 at 20:49
  • @CandiedOrange Yea I only I have three. I mean I understand the general factory pattern. I've been reading head first design pattern book and looking at tutorials on yt. It's just I find most places treat your abstract class as a place to define setter/getter methods while I don't have any so I'm kind of getting confused in this sense
    – JNMN
    Commented Nov 1, 2017 at 21:09
  • I've been thinking about this for a while and what I'm stuck on is this: how do you want to control the choice of selection? It could be you want to be able to edit a line of code and recompile, It could be you want to process an input string and chose based on the value of the string, It could be you want to process a configuration file that is read when your program loads. Give me a hint about your real needs here. Commented Nov 2, 2017 at 16:55
  • @CandiedOrange So essentially all I've been told is transform the selection methods etc. into a factory pattern. What i've done for seleciton for example is create an interface ISelection, made sure that all selection classes implement the specific methods. Then I created a "SelectionFactory" which creates a new Instance of ISelection and takes user input to see what type of selection they want. I.e if(userinput == "roulettewheel") { return new RouletteWheel(): } " However I'm not sure if that's right. So i think to answer your question it's user input which controls the choice
    – JNMN
    Commented Nov 2, 2017 at 19:43
  • Do you want to ask the user for the choice every time the factory is used or would it be better if the factory just worked when asked (using a default value until told to use another one, whenever the user provides input). Commented Nov 3, 2017 at 3:25

1 Answer 1

0
Map<String, Selection> selectionMap = new HashMap<>();
selectionMap.put("RawFitnessSelction", new RawFitnessSelction());
selectionMap.put("RouletteWheelSelection", new RouletteWheelSelection());
selectionMap.put("TournamentSelection", new TournamentSelection());

AbstractSelectionFactory asf = new DOSInputSelectionFactory(
    selectionMap, 
    "Input choice of selection method: ");

class Client {
    AbstractSelectionFactory asf;
    Client (AbstractSelectionFactory asf) { 
        this.asf = asf;
    }

    public void select(Map<String, Double> population) {
        Selection selection = asf.chooseSelection();
        ...
    }

An abstract factory can be passed to a client and called as many times as you like, or just once. So long as the Client never sees the abstract factory being built, it doesn't care if you're getting input from the command line, a GUI, a file, a database, the internet, or whatever else.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.