Advertisement
Guest User

Untitled

a guest
Sep 16th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. package Prac2;
  2.  
  3.  
  4. import org.jacop.*;
  5. import org.jacop.constraints.*;
  6. import org.jacop.core.*;
  7. import org.jacop.search.DepthFirstSearch;
  8. import org.jacop.search.IndomainMin;
  9. import org.jacop.search.PrintOutListener;
  10. import org.jacop.search.Search;
  11. import org.jacop.search.SelectChoicePoint;
  12. import org.jacop.search.SimpleSelect;
  13. import org.jacop.search.SmallestDomain;
  14.  
  15. public class Prac2 {
  16.  
  17.     public static void main(String args[]) {
  18.  
  19.             int GMAX = 5; //max gold
  20.             int GP = 9;
  21.  
  22.             int SMAX = 4; //max sacks
  23.             int SP = 3;
  24.  
  25.             int MPMAX = 5; //max magic potion
  26.             int MP = 5;
  27.  
  28.             int AngryNum = 3;
  29.             int GreedyNum = 1;
  30.             int UglyNum = 1;
  31.  
  32.             int carryCap = 6; //warrior carry capacity
  33.  
  34.             int[] itemPrices = {GP, SP, MP}; //costs of Gold,Sack,Magic Potion respectively
  35.  
  36.             Store constraintStore = new Store();
  37.  
  38.             // All of the declared vars and domains for quantity of items.
  39.             IntVar Gold = new IntVar(constraintStore, "Gold",0, GMAX);
  40.             IntVar Sack = new IntVar(constraintStore, "Sack",0, SMAX);
  41.             IntVar MagicPotion = new IntVar(constraintStore, "Magic Potion",0, MPMAX);
  42.  
  43.            
  44.             //variables for number of sacks/gold to defeat angry/greedy
  45.             IntVar SAngry = new IntVar(constraintStore, "SAngry",0,SMAX);
  46.             IntVar SGreedy = new IntVar(constraintStore, "SGreedy",0,SMAX);
  47.             IntVar GAngry= new IntVar(constraintStore, "GAngry",0,GMAX);
  48.             IntVar GGreedy = new IntVar(constraintStore, "GGreedy",0,GMAX);
  49.  
  50.             IntVar SAngryAux = new IntVar(constraintStore, "SAngryAux",0,SMAX);
  51.            
  52.             //Total # Items and Cost
  53.             IntVar totalItems = new IntVar(constraintStore, "Total Items",0,carryCap);
  54.             IntVar totalCost = new IntVar(constraintStore, "Total Cost",0,82);
  55.  
  56.            
  57.            
  58.             // all items
  59.             IntVar[] consumables = {Gold, Sack, MagicPotion};
  60.            
  61.             //XeqC,XlteqC,XmulCeqZ,XplusYeqC,XplusYlteqZ,Sum,SumWeight.
  62.            
  63.             //# Items to carry
  64.             Constraint sum = new Sum(consumables,totalItems);
  65.  
  66.            
  67.             //total#items < carry capacity
  68.             Constraint a = new XlteqC(totalItems,carryCap);
  69.            
  70.             //total cost of items
  71.             Constraint b = new SumWeight(consumables,itemPrices,totalCost);
  72.            
  73.             //Constraints for number of items
  74.             Constraint c = new XlteqC(Gold,GMAX);
  75.             Constraint d = new XlteqC(Sack,SMAX);
  76.             Constraint e = new XlteqC(MagicPotion,MPMAX);
  77.            
  78.             //constraints for sack/gold vs angry/greedy
  79.             Constraint f = new XlteqC(SAngry,SMAX);
  80.             Constraint g = new XlteqC(SGreedy,SMAX);
  81.             Constraint h = new XlteqC(GAngry,GMAX);
  82.             Constraint i = new XlteqC(GGreedy,GMAX);
  83.            
  84.             Constraint j = new XplusYlteqZ(SAngry,SGreedy,Sack); //correct number of sacks
  85.             Constraint k = new XplusYlteqZ(GAngry,GGreedy,Gold); //correct number of gold
  86.            
  87.             //all trolls of each type must equal number of trolls on path
  88.             Constraint l = new XplusYeqC(SAngryAux,GAngry,(AngryNum%2)+AngryNum);
  89.             Constraint m = new XplusYeqC(SGreedy,GGreedy,GreedyNum);
  90.             Constraint n = new XeqC(MagicPotion,UglyNum); //used to make sure 1 potion is used for 1 ugly
  91.            
  92.             //possibly for the 1/2 sack issue?
  93.             //Constraint o = new XmulCeqZ(SAngry,2,Sack); //this is wrong - this is basically saying to take number of angry trolls * 2 and that's the number of sacks
  94.             //Constraint o = new XmulCeqZ(SAngry,AngryNum%2,SAngryAux);
  95.            
  96.             //Constraint p = new XlteqC(SAngryAux,SMAX);
  97.             Constraint q = new XmulCeqZ(SAngry,2,SAngryAux);
  98.            
  99.             //
  100.            
  101.            
  102.             constraintStore.impose(a);
  103.             constraintStore.impose(b);
  104.             constraintStore.impose(c);
  105.             constraintStore.impose(d);
  106.             constraintStore.impose(e);
  107.             constraintStore.impose(f);
  108.             constraintStore.impose(g);
  109.             constraintStore.impose(h);
  110.             constraintStore.impose(i);
  111.             constraintStore.impose(j);
  112.             constraintStore.impose(k);
  113.             constraintStore.impose(l);
  114.             constraintStore.impose(m);
  115.             constraintStore.impose(n);
  116.             //constraintStore.impose(o);
  117.             //constraintStore.impose(p);
  118.             constraintStore.impose(q);
  119.             constraintStore.impose(sum);
  120.            
  121.            
  122.             //all information req'd
  123.             IntVar[] allVars = {Gold,Sack,MagicPotion,totalCost};
  124.            
  125.            
  126.             //search shit
  127.             Search<IntVar> label = new DepthFirstSearch<IntVar>();
  128.            
  129.             label.getSolutionListener().searchAll(true);
  130.             label.getSolutionListener().recordSolutions(true);
  131.  
  132.            
  133.             SelectChoicePoint<IntVar> select = new SimpleSelect<IntVar>(allVars,new SmallestDomain<IntVar>(),new IndomainMin<IntVar>());
  134.            
  135.             label.setSolutionListener(new PrintOutListener<IntVar>());
  136.             label.labeling(constraintStore,select,totalCost);
  137.            
  138.             label.printAllSolutions();
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement