Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Prac2;
- import org.jacop.*;
- import org.jacop.constraints.*;
- import org.jacop.core.*;
- import org.jacop.search.DepthFirstSearch;
- import org.jacop.search.IndomainMin;
- import org.jacop.search.PrintOutListener;
- import org.jacop.search.Search;
- import org.jacop.search.SelectChoicePoint;
- import org.jacop.search.SimpleSelect;
- import org.jacop.search.SmallestDomain;
- public class Prac2 {
- public static void main(String args[]) {
- int GMAX = 5; //max gold
- int GP = 9;
- int SMAX = 4; //max sacks
- int SP = 3;
- int MPMAX = 5; //max magic potion
- int MP = 5;
- int AngryNum = 3;
- int GreedyNum = 1;
- int UglyNum = 1;
- int carryCap = 6; //warrior carry capacity
- int[] itemPrices = {GP, SP, MP}; //costs of Gold,Sack,Magic Potion respectively
- Store constraintStore = new Store();
- // All of the declared vars and domains for quantity of items.
- IntVar Gold = new IntVar(constraintStore, "Gold",0, GMAX);
- IntVar Sack = new IntVar(constraintStore, "Sack",0, SMAX);
- IntVar MagicPotion = new IntVar(constraintStore, "Magic Potion",0, MPMAX);
- //variables for number of sacks/gold to defeat angry/greedy
- IntVar SAngry = new IntVar(constraintStore, "SAngry",0,SMAX);
- IntVar SGreedy = new IntVar(constraintStore, "SGreedy",0,SMAX);
- IntVar GAngry= new IntVar(constraintStore, "GAngry",0,GMAX);
- IntVar GGreedy = new IntVar(constraintStore, "GGreedy",0,GMAX);
- IntVar SAngryAux = new IntVar(constraintStore, "SAngryAux",0,SMAX);
- //Total # Items and Cost
- IntVar totalItems = new IntVar(constraintStore, "Total Items",0,carryCap);
- IntVar totalCost = new IntVar(constraintStore, "Total Cost",0,82);
- // all items
- IntVar[] consumables = {Gold, Sack, MagicPotion};
- //XeqC,XlteqC,XmulCeqZ,XplusYeqC,XplusYlteqZ,Sum,SumWeight.
- //# Items to carry
- Constraint sum = new Sum(consumables,totalItems);
- //total#items < carry capacity
- Constraint a = new XlteqC(totalItems,carryCap);
- //total cost of items
- Constraint b = new SumWeight(consumables,itemPrices,totalCost);
- //Constraints for number of items
- Constraint c = new XlteqC(Gold,GMAX);
- Constraint d = new XlteqC(Sack,SMAX);
- Constraint e = new XlteqC(MagicPotion,MPMAX);
- //constraints for sack/gold vs angry/greedy
- Constraint f = new XlteqC(SAngry,SMAX);
- Constraint g = new XlteqC(SGreedy,SMAX);
- Constraint h = new XlteqC(GAngry,GMAX);
- Constraint i = new XlteqC(GGreedy,GMAX);
- Constraint j = new XplusYlteqZ(SAngry,SGreedy,Sack); //correct number of sacks
- Constraint k = new XplusYlteqZ(GAngry,GGreedy,Gold); //correct number of gold
- //all trolls of each type must equal number of trolls on path
- Constraint l = new XplusYeqC(SAngryAux,GAngry,(AngryNum%2)+AngryNum);
- Constraint m = new XplusYeqC(SGreedy,GGreedy,GreedyNum);
- Constraint n = new XeqC(MagicPotion,UglyNum); //used to make sure 1 potion is used for 1 ugly
- //possibly for the 1/2 sack issue?
- //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
- //Constraint o = new XmulCeqZ(SAngry,AngryNum%2,SAngryAux);
- //Constraint p = new XlteqC(SAngryAux,SMAX);
- Constraint q = new XmulCeqZ(SAngry,2,SAngryAux);
- //
- constraintStore.impose(a);
- constraintStore.impose(b);
- constraintStore.impose(c);
- constraintStore.impose(d);
- constraintStore.impose(e);
- constraintStore.impose(f);
- constraintStore.impose(g);
- constraintStore.impose(h);
- constraintStore.impose(i);
- constraintStore.impose(j);
- constraintStore.impose(k);
- constraintStore.impose(l);
- constraintStore.impose(m);
- constraintStore.impose(n);
- //constraintStore.impose(o);
- //constraintStore.impose(p);
- constraintStore.impose(q);
- constraintStore.impose(sum);
- //all information req'd
- IntVar[] allVars = {Gold,Sack,MagicPotion,totalCost};
- //search shit
- Search<IntVar> label = new DepthFirstSearch<IntVar>();
- label.getSolutionListener().searchAll(true);
- label.getSolutionListener().recordSolutions(true);
- SelectChoicePoint<IntVar> select = new SimpleSelect<IntVar>(allVars,new SmallestDomain<IntVar>(),new IndomainMin<IntVar>());
- label.setSolutionListener(new PrintOutListener<IntVar>());
- label.labeling(constraintStore,select,totalCost);
- label.printAllSolutions();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement