Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package constraint;
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.Set;
- import java.util.StringTokenizer;
- import search.Assignment;
- import search.Bag;
- import search.Item;
- public class FittingLimitConstraint implements Constraint {
- private int lowerLimit;
- private int upperLimit;
- public FittingLimitConstraint(String limit) {
- StringTokenizer st = new StringTokenizer(limit);
- this.lowerLimit = new Integer(st.nextToken());
- this.upperLimit = new Integer(st.nextToken());
- }
- @Override
- public boolean isSatisfied(Assignment assignment, Object variable,
- Object value) {
- //This will run only if the assignment is complete
- ArrayList<Object> uniqueValues = new ArrayList<Object>();
- ArrayList<Object> valuesAssigned = new ArrayList<Object>();
- //get all the non unique bag values that have been assigned
- valuesAssigned = (ArrayList<Object>) assignment.assignments.values();
- for (int i = 0; i < valuesAssigned.size(); i++) {
- Object val = valuesAssigned.get(i);
- if (!uniqueValues.contains(val)){
- //extract the unique values
- uniqueValues.add(val);
- }
- }
- //for each unique value, get the number of item variables that are assigned to it
- for (int i = 0; i < uniqueValues.size(); i++) {
- Object uniqueValue = uniqueValues.get(i);
- String uniqueBagName = ((Bag) uniqueValue).getName();
- Set variables = assignment.assignments.keySet();
- Iterator iterator = variables.iterator();
- int numItemsInBag = 0;
- while(iterator.hasNext()){
- Object assignedValue = assignment.assignments.get(iterator.next());
- String assignedBagName = ((Bag) assignedValue).getName();
- if (uniqueBagName.equals(assignedBagName)){
- numItemsInBag++;
- }
- }
- // if the number of item variables assigned to this bag value is out of range, this constraint is unsatisfied
- if (numItemsInBag < this.lowerLimit || numItemsInBag > this.upperLimit){
- return false;
- }
- }
- return true;
- }
- }
Add Comment
Please, Sign In to add comment