Emania

gt 2

Dec 4th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.11 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 ilog.concert.IloException;
  9. import ilog.concert.IloLinearNumExpr;
  10. import ilog.concert.IloNumVar;
  11. import ilog.cplex.IloCplex;
  12. import java.util.ArrayList;
  13.  
  14. /**
  15.  *
  16.  * @author Jan
  17.  */
  18. public class LP2 {
  19.    
  20.     ArrayList<Sequence> finalSequences = new ArrayList<>();
  21.     ArrayList<rSequence2> sequences = new ArrayList<>();
  22.     ArrayList<Integer[]> allSequences2 = new ArrayList<>();
  23.     Square[][] maze;
  24.     public Square startSquare;
  25.     public double probability = 0;
  26.     ArrayList<IloNumVar> variables = new ArrayList<>();
  27.    
  28.    
  29.     public LP2(Start start) throws IloException{
  30.         this.finalSequences = start.finalSequences;
  31.         this.allSequences2 = start.allSequences2;
  32.         this.maze = start.maze;
  33.         this.startSquare = start.startSquare;
  34.         this.probability = start.attackP;
  35.        
  36.         IloCplex cplex = new IloCplex();
  37.        
  38.         //extend sequences
  39.        
  40.        
  41.        
  42.        
  43.         System.out.println("ALL SEQUENCES, size " + finalSequences.size());
  44.         for(int i = 0; i < finalSequences.size(); i++){
  45.             System.out.println(finalSequences.get(i).actionsString);
  46.         }
  47.        
  48.        
  49.        
  50.        
  51.         ArrayList<String> allExtendedSequencesStr = extendSequences(finalSequences, true);
  52. //        ArrayList<String> allExtendedSequencesStr = extendSequences(finalSequences);
  53.        
  54.         System.out.println("ALL EXTENDED SEQUENCES, size " + allExtendedSequencesStr.size());
  55.         for(int i = 0; i < allExtendedSequencesStr.size(); i++){
  56.             System.out.println(allExtendedSequencesStr.get(i));
  57.         }
  58.        
  59.        
  60.        
  61.        
  62.        
  63.         // for all string sequences create objects
  64.         for(int i = 0; i < allExtendedSequencesStr.size(); i++){
  65.             rSequence2 seq = new rSequence2(this.maze, this.startSquare, allExtendedSequencesStr.get(i), this.probability);
  66.             seq.computeExtendedUtility(allSequences2);
  67.             IloNumVar var = cplex.numVar(0, 1, allExtendedSequencesStr.get(i));
  68.             seq.var = var;
  69.             sequences.add(seq);
  70.         }
  71.        
  72.        
  73.         // create empty sequence
  74.         IloNumVar var = cplex.numVar(1, 1, "var_empty");
  75.         rSequence2 seq = new rSequence2(this.maze, this.startSquare, "", this.probability);
  76.         seq.computeExtendedUtility(allSequences2);
  77.         seq.var = var;
  78.         sequences.add(seq);
  79.        
  80. //        System.out.println("ALL SEQUENCES");
  81. //        for(int i = 0; i < sequences.size(); i++){
  82. //            System.out.println(sequences.get(i).sequence);
  83. //        }
  84.  
  85.        
  86.         // constraint 4
  87.         for(int i = 0; i < sequences.size(); i++){
  88.             int followsNum = 0;
  89.             for(int j = 0; j < sequences.size(); j++){
  90.                 if(sequences.get(j).follows(sequences.get(i))){
  91.                     followsNum++;
  92.                 }
  93.             }
  94.            
  95.             if(followsNum >= 1){
  96.                 IloLinearNumExpr lhs = cplex.linearNumExpr();
  97.                 for(int j = 0; j < sequences.size(); j++){
  98.                     if(sequences.get(j).follows(sequences.get(i))){
  99.                         lhs.addTerm(1, sequences.get(j).var);
  100.                     }
  101.                 }
  102.                
  103.                 cplex.addEq(lhs, sequences.get(i).var);
  104.             }
  105.            
  106.            
  107.  
  108.         }
  109.        
  110. //        System.out.println("PERMUTING STRING " + finalSequences.get(0).actionsString);
  111. //        ArrayList<String> permStr = perm(finalSequences.get(0).actionsString, 0, startSquare.x, startSquare.y);
  112. //        System.out.println(permStr);
  113.        
  114.        
  115.         // constraint 5
  116.         IloNumVar gameValue = cplex.numVar(Double.MIN_VALUE, Double.MAX_VALUE, "game_value");
  117.         for(int a = 0; a < allSequences2.size(); a++){
  118.             IloLinearNumExpr lhs = cplex.linearNumExpr();
  119.             for(int i = 0; i < sequences.size(); i++){
  120. //                System.out.println("sequences size = " +sequences.size() + ", i = " + i + ", a = " + a);
  121.                 lhs.addTerm(sequences.get(i).extendedUtility.get(a), sequences.get(i).var);
  122.             }
  123.             cplex.addGe(lhs, gameValue);
  124. //            System.out.println("Adding 5. constaint: " + lhs + " > " + gameValue);
  125.         }
  126.        
  127.         cplex.addMaximize(gameValue);
  128.         cplex.solve();
  129.         System.out.println("THE TOTAL VALUE IS " + cplex.getValue(gameValue));
  130.        
  131.         for(int i = 0; i < sequences.size(); i++){
  132.             if(cplex.getValue(sequences.get(i).var) > 0)
  133.             System.out.println(i + ": " + sequences.get(i).var.getName() + " : " + cplex.getValue(sequences.get(i).var));
  134.         }
  135.        
  136.        
  137.        
  138.        
  139.     }
  140.    
  141.    
  142.    
  143.     public final ArrayList<String> extendSequences(ArrayList<Sequence> finalSequences, boolean extend){
  144.         ArrayList<String> haveStr = new ArrayList<>();
  145.         haveStr.add("");
  146.         for(int i = 0; i < finalSequences.size(); i++){
  147.             haveStr.add(finalSequences.get(i).actionsString);
  148.             for(int j = 0; j < finalSequences.get(i).actionsString.length(); j++){
  149.                 String str = finalSequences.get(i).actionsString.substring(0, j);
  150.                 if(!haveStr.contains(str)){
  151.                     haveStr.add(str);
  152.                 }
  153.             }
  154.         }
  155.        
  156. //        System.out.println("ALL SEQUENCES");
  157. //        for(int i = 0; i < haveStr.size(); i++){
  158. //            System.out.println(haveStr.get(i));
  159. //        }
  160. //        System.out.println("DONE SEQUENCES");
  161.        
  162. //        return perm("RRR", 0, startSquare.x, startSquare.y);
  163.  
  164.         ArrayList<String> allExtendedSequences = new ArrayList<>();
  165.        
  166.         if(extend){
  167.             for(String s : haveStr){
  168.                 allExtendedSequences.addAll(perm(s, 0, startSquare.x, startSquare.y));
  169.             }
  170.         } else{
  171.             return haveStr;
  172.         }
  173.         return allExtendedSequences;
  174.     }
  175.    
  176.     public ArrayList<String> perm(String s, int n, int x, int y){
  177.        
  178. //        System.out.println(x + ", " + y);
  179.         ArrayList<String> ret = new ArrayList();
  180. //        System.out.println(s + " [" + x + ", " + y + "], " + n + " - " + maze[y][x].c);
  181.         if(maze[y][x].isDanger() && n == s.length()){  //  && !("P").equals(s.charAt(n-1)) && ("W").equals(s.charAt(n-1))
  182.             ret.add(s + "W");
  183.             ret.add(s + "P");
  184.             ret.add(s);
  185.         }else if(n == s.length()) {
  186.             ret.add(s);
  187.             return ret;
  188.         } else{
  189.             int thisX = x;
  190.             int thisY = x;
  191.             if(!maze[y][x].isDanger() || ("P").equals(s.charAt(n-1)) || ("W").equals(s.charAt(n-1))){ //  || ("P").equals(s.charAt(n-1)) || ("W").equals(s.charAt(n-1))
  192. //                System.out.println("not danger, moving " + s.charAt(n));
  193.                 String tmp = "" + s.charAt(n);
  194.                 if(tmp.equals("R")) x++;
  195.                 if(tmp.equals("L")) x--;
  196.                 if(tmp.equals("U")) y--;
  197.                 if(tmp.equals("D")) y++;
  198.                 return perm(s, n + 1, x, y);
  199.             } else{
  200. //                System.out.println("danger2, moving " + s.charAt(n));
  201.                 String tmp = "" + s.charAt(n);
  202.                 if(tmp.equals("R")) x++;
  203.                 if(tmp.equals("L")) x--;
  204.                 if(tmp.equals("U")) y--;
  205.                 if(tmp.equals("D")) y++;
  206.                 ArrayList<String> alW = perm(s.substring(0, n) + "W" + s.substring(n, s.length()), n + 2, x, y);
  207.                 ArrayList<String> alP = perm(s.substring(0, n) + "P" + s.substring(n, s.length()), n + 2, x, y);
  208.                 ret.addAll(alP);
  209.                 ret.addAll(alW);
  210.             }
  211.         }
  212.         return ret;
  213.     }
  214.    
  215. }
  216.  
  217.  
  218.  
  219. // else{
  220. //                System.out.println("danger");
  221. //                String tmp = "" + s.charAt(n);
  222. //                if(tmp.equals("R")) x++;
  223. //                if(tmp.equals("L")) x--;
  224. //                if(tmp.equals("U")) y--;
  225. //                if(tmp.equals("D")) y++;
  226. //                ArrayList<String> alW = perm(s.substring(0, n) + "W" + s.substring(n, s.length()), n + 1, x, y);
  227. //                ArrayList<String> alP = perm(s.substring(0, n) + "P" + s.substring(n, s.length()), n + 1, x, y);
  228. //                ret.addAll(alP);
  229. //                ret.addAll(alW);
  230. //            }
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249. /*
  250.  * To change this license header, choose License Headers in Project Properties.
  251.  * To change this template file, choose Tools | Templates
  252.  * and open the template in the editor.
  253.  */
  254. package gt;
  255.  
  256. import ilog.concert.IloNumVar;
  257. import java.util.ArrayList;
  258.  
  259. /**
  260.  *
  261.  * @author Jan
  262.  */
  263. public class rSequence2 {
  264.    
  265.     public ArrayList<Integer> piratesIdxs = new ArrayList<>();
  266.     public ArrayList<Boolean> piratesKill = new ArrayList<>();
  267.     public ArrayList<Double> extendedUtility = new ArrayList<>();
  268.     public int gold = 0;
  269.     public String sequence;
  270.     public double probability = 0;
  271.     public boolean endsInDestination = false;
  272.     public IloNumVar var;
  273.    
  274.     public rSequence2(Square[][] maze, Square startSquare, String s, double p){
  275.        
  276.         this.sequence = s;
  277.         this.probability = p;
  278.        
  279.         int x = startSquare.x;
  280.         int y = startSquare.y;
  281.        
  282.         for(int i = 0; i < s.length(); i++){
  283.             String tmp = "" + s.charAt(i);
  284.             if(maze[y][x].isGold()) gold++;
  285.             if(maze[y][x].isDanger()) piratesIdxs.add(maze[y][x].dangerIdx);
  286.            
  287.             if(tmp.equals("R")) x++;
  288.             if(tmp.equals("L")) x--;
  289.             if(tmp.equals("U")) y--;
  290.             if(tmp.equals("D")) y++;
  291.            
  292.             if(tmp.equals("W")) piratesKill.add(false);
  293.             if(tmp.equals("P")) piratesKill.add(true);
  294.         }
  295.         if(maze[y][x].isTerminal()) endsInDestination = true;
  296.        
  297.         System.out.println(s);
  298. //        System.out.println("final destination is [" + x + ", " + y + "] end is destination " + endsInDestination + " - " + maze[y][x].c);
  299.        
  300.        
  301.     }
  302.    
  303.     public void computeExtendedUtility(ArrayList<Integer[]> pirateMove){
  304.        
  305.        
  306.         for(int i = 0; i < pirateMove.size(); i++){
  307.             double ut = 10 + gold;
  308.             for(int j = 0; j < pirateMove.get(0).length; j++){
  309.                 if(piratesIdxs.contains(pirateMove.get(i)[j])) ut *= (1 - probability);
  310.             }
  311.            
  312.            
  313.             if(sequence.contains("P") || !endsInDestination){
  314.                 ut = 0;
  315.             }
  316.            
  317.             extendedUtility.add(ut);
  318.         }
  319.        
  320. //        System.out.println(sequence + " : " + extendedUtility);
  321.     }
  322.    
  323.     //if this is after s
  324.     public boolean follows(rSequence2 s){
  325.         if(s.sequence.length() + 1 == this.sequence.length()){
  326.             return this.sequence.substring(0, s.sequence.length()).equals(s.sequence);
  327.         } else return false;
  328.     }
  329.    
  330.    
  331. }
Advertisement
Add Comment
Please, Sign In to add comment