Guest User

Untitled

a guest
May 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. package constraint;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Iterator;
  5. import java.util.Set;
  6. import java.util.StringTokenizer;
  7.  
  8. import search.Assignment;
  9. import search.Bag;
  10. import search.Item;
  11.  
  12. public class FittingLimitConstraint implements Constraint {
  13. private int lowerLimit;
  14. private int upperLimit;
  15.  
  16. public FittingLimitConstraint(String limit) {
  17. StringTokenizer st = new StringTokenizer(limit);
  18. this.lowerLimit = new Integer(st.nextToken());
  19. this.upperLimit = new Integer(st.nextToken());
  20.  
  21. }
  22.  
  23. @Override
  24. public boolean isSatisfied(Assignment assignment, Object variable,
  25. Object value) {
  26. //This will run only if the assignment is complete
  27. ArrayList<Object> uniqueValues = new ArrayList<Object>();
  28. ArrayList<Object> valuesAssigned = new ArrayList<Object>();
  29.  
  30. //get all the non unique bag values that have been assigned
  31. valuesAssigned = (ArrayList<Object>) assignment.assignments.values();
  32. for (int i = 0; i < valuesAssigned.size(); i++) {
  33. Object val = valuesAssigned.get(i);
  34. if (!uniqueValues.contains(val)){
  35. //extract the unique values
  36. uniqueValues.add(val);
  37. }
  38. }
  39.  
  40. //for each unique value, get the number of item variables that are assigned to it
  41. for (int i = 0; i < uniqueValues.size(); i++) {
  42. Object uniqueValue = uniqueValues.get(i);
  43. String uniqueBagName = ((Bag) uniqueValue).getName();
  44.  
  45. Set variables = assignment.assignments.keySet();
  46. Iterator iterator = variables.iterator();
  47.  
  48. int numItemsInBag = 0;
  49. while(iterator.hasNext()){
  50. Object assignedValue = assignment.assignments.get(iterator.next());
  51. String assignedBagName = ((Bag) assignedValue).getName();
  52. if (uniqueBagName.equals(assignedBagName)){
  53. numItemsInBag++;
  54. }
  55. }
  56.  
  57. // if the number of item variables assigned to this bag value is out of range, this constraint is unsatisfied
  58. if (numItemsInBag < this.lowerLimit || numItemsInBag > this.upperLimit){
  59. return false;
  60. }
  61. }
  62. return true;
  63.  
  64. }
  65.  
  66. }
Add Comment
Please, Sign In to add comment