Emania

gt

Dec 4th, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.35 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package gt;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. /**
  11. *
  12. * @author Jan
  13. */
  14. public class rSequence2 {
  15.  
  16. public ArrayList<Integer> piratesIdxs = new ArrayList<>();
  17. public ArrayList<Double> extendedUtility = new ArrayList<>();
  18. public int gold = 0;
  19. public String sequence;
  20. public double probability = 0;
  21. public boolean endsInDestination = false;
  22.  
  23. public rSequence2(Square[][] maze, Square startSquare, String s, double p){
  24.  
  25. this.sequence = s;
  26. this.probability = p;
  27.  
  28. int x = startSquare.x;
  29. int y = startSquare.y;
  30.  
  31. for(int i = 0; i < s.length(); i++){
  32. String tmp = "" + s.charAt(i);
  33. if(maze[y][x].isGold()) gold++;
  34. if(maze[y][x].isDanger()) piratesIdxs.add(maze[y][x].dangerIdx);
  35.  
  36. if(tmp.equals("R")) x++;
  37. if(tmp.equals("L")) x--;
  38. if(tmp.equals("U")) y--;
  39. if(tmp.equals("D")) y++;
  40. }
  41. if(maze[y][x].isTerminal()) endsInDestination = true;
  42.  
  43.  
  44. }
  45.  
  46. public void computeExtendedUtility(ArrayList<Integer[]> pirateMove){
  47.  
  48.  
  49. for(int i = 0; i < pirateMove.size(); i++){
  50. double ut = 10 + gold;
  51. for(int j = 0; j < pirateMove.get(0).length; j++){
  52. if(piratesIdxs.contains(pirateMove.get(i)[j])) ut *= (1 - probability);
  53. }
  54. if(!endsInDestination) ut = 0;
  55. extendedUtility.add(ut);
  56. }
  57.  
  58. System.out.println(sequence + " : " + extendedUtility);
  59. }
  60.  
  61. //if this is after s
  62. public boolean follows(rSequence2 s){
  63. if(s.sequence.length() + 1 == this.sequence.length()){
  64. return this.sequence.substring(0, s.sequence.length()).equals(s.sequence);
  65. } else return false;
  66. }
  67.  
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. /*
  83. * To change this license header, choose License Headers in Project Properties.
  84. * To change this template file, choose Tools | Templates
  85. * and open the template in the editor.
  86. */
  87. package gt;
  88.  
  89. import ilog.concert.IloException;
  90. import ilog.concert.IloLinearNumExpr;
  91. import ilog.concert.IloNumVar;
  92. import ilog.cplex.IloCplex;
  93. import java.util.ArrayList;
  94.  
  95. /**
  96. *
  97. * @author Jan
  98. */
  99. public class LP2 {
  100.  
  101. ArrayList<Sequence> finalSequences = new ArrayList<>();
  102. ArrayList<rSequence2> sequences = new ArrayList<>();
  103. ArrayList<Integer[]> allSequences2 = new ArrayList<>();
  104. Square[][] maze;
  105. public Square startSquare;
  106. public double probability = 0;
  107. ArrayList<IloNumVar> variables = new ArrayList<>();
  108.  
  109.  
  110. public LP2(Start start) throws IloException{
  111. this.finalSequences = start.finalSequences;
  112. this.allSequences2 = start.allSequences2;
  113. this.maze = start.maze;
  114. this.startSquare = start.startSquare;
  115. this.probability = start.attackP;
  116.  
  117. IloCplex cplex = new IloCplex();
  118.  
  119. // for sequences to destination
  120. for(int i = 0; i < finalSequences.size(); i++){
  121. rSequence2 seq = new rSequence2(this.maze, this.startSquare, finalSequences.get(i).actionsString, this.probability);
  122. seq.computeExtendedUtility(allSequences2);
  123. IloNumVar var = cplex.numVar(0, 1, finalSequences.get(i).actionsString);
  124. variables.add(var);
  125. sequences.add(seq);
  126. }
  127.  
  128. // add sequences not to destination
  129. ArrayList<String> haveStr = new ArrayList<>();
  130. haveStr.add("");
  131. for(int i = 0; i < finalSequences.size(); i++){
  132. haveStr.add(sequences.get(i).sequence);
  133. for(int j = 0; j < sequences.get(i).sequence.length(); j++){
  134. String str = sequences.get(i).sequence.substring(0, j);
  135. if(!haveStr.contains(str)){
  136. haveStr.add(str);
  137. rSequence2 seq = new rSequence2(this.maze, this.startSquare, str, this.probability);
  138. seq.computeExtendedUtility(allSequences2);
  139. IloNumVar var = cplex.numVar(0, 1, str);
  140. variables.add(var);
  141. sequences.add(seq);
  142. }
  143. }
  144. }
  145.  
  146. // create empty sequence
  147. IloNumVar var = cplex.numVar(1, 1, "var_empty");
  148. variables.add(var);
  149. rSequence2 seq = new rSequence2(this.maze, this.startSquare, "", this.probability);
  150. sequences.add(seq);
  151.  
  152. System.out.println("ALL SEQUENCES");
  153. for(int i = 0; i < sequences.size(); i++){
  154. System.out.println(sequences.get(i).sequence);
  155. }
  156.  
  157.  
  158. // constraint 4
  159. for(int i = 0; i < sequences.size(); i++){
  160. int followsNum = 0;
  161. for(int j = 0; j < sequences.size(); j++){
  162. if(sequences.get(j).follows(sequences.get(i))){
  163. followsNum++;
  164. }
  165. }
  166.  
  167. if(followsNum >= 1){
  168. IloLinearNumExpr lhs = cplex.linearNumExpr();
  169. for(int j = 0; j < sequences.size(); j++){
  170. if(sequences.get(j).follows(sequences.get(i))){
  171. lhs.addTerm(1.0, variables.get(j));
  172. }
  173. }
  174. cplex.addEq(lhs, variables.get(i));
  175. }
  176. }
  177.  
  178.  
  179.  
  180. IloNumVar gameValue = cplex.numVar(Double.MIN_VALUE, Double.MAX_VALUE, "game_value");
  181. for(int a = 0; a < allSequences2.size(); a++){
  182. IloLinearNumExpr lhs = cplex.linearNumExpr();
  183. for(int i = 0; i < finalSequences.size(); i++){
  184. lhs.addTerm(sequences.get(i).extendedUtility.get(a), variables.get(i));
  185. }
  186. cplex.addGe(lhs, gameValue);
  187. }
  188.  
  189. cplex.addMaximize(gameValue);
  190. cplex.solve();
  191. System.out.println("THE TOTAL VALUE IS " + cplex.getValue(gameValue));
  192.  
  193. for(int i = 0; i < finalSequences.size(); i++){
  194. System.out.println(variables.get(i).getName() + " " + cplex.getValue(variables.get(i)));
  195. }
  196.  
  197.  
  198. }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment