I am very interested in search algorithms and return path programming. At the moment, I have performed Algorithm X (see My other post here: Define sets without conflicts? ) To solve the problem with the exact shell. This works very well, but now I'm interested in solving this problem with a more basic retreat option. I just can't figure out how to do this. The problem description is the same as before:
Suppose you have many sets, while each set has several subsets.
Set1 = {(banana, pineapple, orange), (apple, cabbage, cucumber), (onion, garlic)}
Set2 = {(banana, cucumber, garlic), (avocado, tomato)}
...
SetN = {...}
Now you need to select one subset from each set, while each subset must be in conflict with any other selected subset (one element is not contained in any of the other selected subsets).
As an example, I wrote two Java classes. Sets are identified by a single character, and elements are prime numbers.
I have two problems:
- How to iterate over all sets / subsets in such a way that heuristics can be used (select a subset with minimal elements, minimum cost, ...)
- How to save selected sets + subsets and their contained elements.
All other examples I can find are Sudoku or n-Queens, and they all use fixed for-loops. In addition to this, I was thinking of a fairly general approach where the "isPossiblePartialSolution" function can be used to check if the selected path / set may contradict a previously selected subset / element.
Here are two Java classes:
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Set> allSets = buildRandomTest(); for(Set r : allSets) { System.out.print("Set with id: " + r.id + " is subset in collection: " + r.name + " and contains: "); for(Integer n : r.listOfElements) { System.out.print(" " + n + " "); } System.out.println(); } } public static int myRandomRange(int low, int high) { return (int) (Math.random() * (++high - low) + low); } public static ArrayList<Set> buildRandomTest() { ArrayList<Set> mySet = new ArrayList<Set>(); int numberOfSets = myRandomRange(10, 12); for(int i=0; i<numberOfSets; i++) { String nameOfSet = Character.toString((char) myRandomRange(65, 67)); Set tmp = new Set(nameOfSet, i); ArrayList<Integer> listOfElements = new ArrayList<Integer>(); int elementsInList = myRandomRange(2, 4); for(int j=0; j<elementsInList; j++) { listOfElements.add(myRandomRange(1,30)); } tmp.listOfElements = listOfElements; mySet.add(tmp); } return mySet; } }
and
import java.util.ArrayList; public class Set { public String name; public int id; public ArrayList<Integer> listOfElements; public Set(String name, int id) { this.name = name; this.id = id; listOfElements = new ArrayList<Integer>(); } }