Advertisement
Guest User

Untitled

a guest
Nov 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. package lab1;
  2.  
  3. import org.jacop.constraints.*;
  4. import org.jacop.core.*;
  5. import org.jacop.search.*;
  6.  
  7. public class Photo2 {
  8. public static void main(String[] args) {
  9.  
  10. int n = 9;
  11. int n_prefs = 17;
  12. int[][] prefs = {{1,3}, {1,5}, {1,8},
  13. {2,5}, {2,9}, {3,4}, {3,5}, {4,1},
  14. {4,5}, {5,6}, {5,1}, {6,1}, {6,9},
  15. {7,3}, {7,8}, {8,9}, {8,7}};
  16.  
  17. int n2 = 11;
  18. int n_prefs2 = 20;
  19. int[][] prefs2 = {{1,3}, {1,5}, {2,5},
  20. {2,8}, {2,9}, {3,4}, {3,5}, {4,1},
  21. {4,5}, {4,6}, {5,1}, {6,1}, {6,9},
  22. {7,3}, {7,5}, {8,9}, {8,7}, {8,10},
  23. {9, 11}, {10, 11}};
  24.  
  25. int n3 = 15;
  26. int n_prefs3 = 20;
  27. int[][] prefs3 = {{1,3}, {1,5}, {2,5},
  28. {2,8}, {2,9}, {3,4}, {3,5}, {4,1},
  29. {4,15}, {4,13}, {5,1}, {6,10}, {6,9},
  30. {7,3}, {7,5}, {8,9}, {8,7}, {8,14},
  31. {9, 13}, {10, 11}};
  32.  
  33.  
  34. photo(n2, prefs2);
  35.  
  36. }
  37.  
  38. private static void photo(int n, int[][] prefs){
  39.  
  40. Store store = new Store();
  41. //Create a list of persons and don't let them stand in the same spot
  42. IntVar[] ps = new IntVar[n];
  43. for(int i = 0; i < n; i++){
  44. ps[i] = new IntVar(store, "p"+(i+1), 0, n); //index for each person, value for their place
  45. }
  46. store.impose(new Alldifferent(ps));
  47.  
  48.  
  49. BooleanVar[] costs = new BooleanVar[prefs.length];
  50.  
  51. for(int i = 0; i < prefs.length; i++){
  52.  
  53. costs[i] = new BooleanVar(store);
  54. //index of the two people in this preference, adjusted for 0 indexing
  55. int person1 = prefs[i][0] - 1;
  56. int person2 = prefs[i][1] - 1;
  57.  
  58. //Create FDV for distance between persons
  59. IntVar distance = new IntVar(store, 0, n);
  60. store.impose(new Distance(ps[person1], ps[person2], distance));
  61.  
  62. //if distance is greater than 1, costs[i] is 1
  63. store.impose(new Reified(new XgtC(distance, 1) , costs[i]));
  64. }
  65. IntVar cost = new IntVar(store, "cost", 0, prefs.length);
  66. store.impose(new SumBool(costs, "==", cost));
  67.  
  68. Search<IntVar> search = new DepthFirstSearch<IntVar>();
  69. SelectChoicePoint<IntVar> select =
  70. new InputOrderSelect<IntVar>(store, ps,
  71. new IndomainMin<IntVar>());
  72. boolean result = search.labeling(store, select, cost);
  73.  
  74. //some printing stuff. This seems pretty stupid
  75. if(result){
  76. String costString = cost.toString();
  77. char s = costString.charAt(costString.length() - 1);
  78. int i = Integer.parseInt(String.valueOf(s));
  79. System.out.println(prefs.length - i + " of " + prefs.length + " preferences satisfied");
  80. } else {
  81. System.out.println("Nein");
  82. }
  83.  
  84. }
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement