Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package lab1;
- import org.jacop.constraints.*;
- import org.jacop.core.*;
- import org.jacop.search.*;
- public class Photo2 {
- public static void main(String[] args) {
- int n = 9;
- int n_prefs = 17;
- int[][] prefs = {{1,3}, {1,5}, {1,8},
- {2,5}, {2,9}, {3,4}, {3,5}, {4,1},
- {4,5}, {5,6}, {5,1}, {6,1}, {6,9},
- {7,3}, {7,8}, {8,9}, {8,7}};
- int n2 = 11;
- int n_prefs2 = 20;
- int[][] prefs2 = {{1,3}, {1,5}, {2,5},
- {2,8}, {2,9}, {3,4}, {3,5}, {4,1},
- {4,5}, {4,6}, {5,1}, {6,1}, {6,9},
- {7,3}, {7,5}, {8,9}, {8,7}, {8,10},
- {9, 11}, {10, 11}};
- int n3 = 15;
- int n_prefs3 = 20;
- int[][] prefs3 = {{1,3}, {1,5}, {2,5},
- {2,8}, {2,9}, {3,4}, {3,5}, {4,1},
- {4,15}, {4,13}, {5,1}, {6,10}, {6,9},
- {7,3}, {7,5}, {8,9}, {8,7}, {8,14},
- {9, 13}, {10, 11}};
- photo(n2, prefs2);
- }
- private static void photo(int n, int[][] prefs){
- Store store = new Store();
- //Create a list of persons and don't let them stand in the same spot
- IntVar[] ps = new IntVar[n];
- for(int i = 0; i < n; i++){
- ps[i] = new IntVar(store, "p"+(i+1), 0, n); //index for each person, value for their place
- }
- store.impose(new Alldifferent(ps));
- BooleanVar[] costs = new BooleanVar[prefs.length];
- for(int i = 0; i < prefs.length; i++){
- costs[i] = new BooleanVar(store);
- //index of the two people in this preference, adjusted for 0 indexing
- int person1 = prefs[i][0] - 1;
- int person2 = prefs[i][1] - 1;
- //Create FDV for distance between persons
- IntVar distance = new IntVar(store, 0, n);
- store.impose(new Distance(ps[person1], ps[person2], distance));
- //if distance is greater than 1, costs[i] is 1
- store.impose(new Reified(new XgtC(distance, 1) , costs[i]));
- }
- IntVar cost = new IntVar(store, "cost", 0, prefs.length);
- store.impose(new SumBool(costs, "==", cost));
- Search<IntVar> search = new DepthFirstSearch<IntVar>();
- SelectChoicePoint<IntVar> select =
- new InputOrderSelect<IntVar>(store, ps,
- new IndomainMin<IntVar>());
- boolean result = search.labeling(store, select, cost);
- //some printing stuff. This seems pretty stupid
- if(result){
- String costString = cost.toString();
- char s = costString.charAt(costString.length() - 1);
- int i = Integer.parseInt(String.valueOf(s));
- System.out.println(prefs.length - i + " of " + prefs.length + " preferences satisfied");
- } else {
- System.out.println("Nein");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement