Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package gt;
- import ilog.concert.IloException;
- import ilog.concert.IloLinearNumExpr;
- import ilog.concert.IloNumVar;
- import ilog.cplex.IloCplex;
- import java.util.ArrayList;
- /**
- *
- * @author Jan
- */
- public class LP2 {
- ArrayList<Sequence> finalSequences = new ArrayList<>();
- ArrayList<rSequence2> sequences = new ArrayList<>();
- ArrayList<Integer[]> allSequences2 = new ArrayList<>();
- Square[][] maze;
- public Square startSquare;
- public double probability = 0;
- ArrayList<IloNumVar> variables = new ArrayList<>();
- public LP2(Start start) throws IloException{
- this.finalSequences = start.finalSequences;
- this.allSequences2 = start.allSequences2;
- this.maze = start.maze;
- this.startSquare = start.startSquare;
- this.probability = start.attackP;
- IloCplex cplex = new IloCplex();
- //extend sequences
- System.out.println("ALL SEQUENCES, size " + finalSequences.size());
- for(int i = 0; i < finalSequences.size(); i++){
- System.out.println(finalSequences.get(i).actionsString);
- }
- ArrayList<String> allExtendedSequencesStr = extendSequences(finalSequences, true);
- // ArrayList<String> allExtendedSequencesStr = extendSequences(finalSequences);
- System.out.println("ALL EXTENDED SEQUENCES, size " + allExtendedSequencesStr.size());
- for(int i = 0; i < allExtendedSequencesStr.size(); i++){
- System.out.println(allExtendedSequencesStr.get(i));
- }
- // for all string sequences create objects
- for(int i = 0; i < allExtendedSequencesStr.size(); i++){
- rSequence2 seq = new rSequence2(this.maze, this.startSquare, allExtendedSequencesStr.get(i), this.probability);
- seq.computeExtendedUtility(allSequences2);
- IloNumVar var = cplex.numVar(0, 1, allExtendedSequencesStr.get(i));
- seq.var = var;
- sequences.add(seq);
- }
- // create empty sequence
- IloNumVar var = cplex.numVar(1, 1, "var_empty");
- rSequence2 seq = new rSequence2(this.maze, this.startSquare, "", this.probability);
- seq.computeExtendedUtility(allSequences2);
- seq.var = var;
- sequences.add(seq);
- // System.out.println("ALL SEQUENCES");
- // for(int i = 0; i < sequences.size(); i++){
- // System.out.println(sequences.get(i).sequence);
- // }
- // constraint 4
- for(int i = 0; i < sequences.size(); i++){
- int followsNum = 0;
- for(int j = 0; j < sequences.size(); j++){
- if(sequences.get(j).follows(sequences.get(i))){
- followsNum++;
- }
- }
- if(followsNum >= 1){
- IloLinearNumExpr lhs = cplex.linearNumExpr();
- for(int j = 0; j < sequences.size(); j++){
- if(sequences.get(j).follows(sequences.get(i))){
- lhs.addTerm(1, sequences.get(j).var);
- }
- }
- cplex.addEq(lhs, sequences.get(i).var);
- }
- }
- // System.out.println("PERMUTING STRING " + finalSequences.get(0).actionsString);
- // ArrayList<String> permStr = perm(finalSequences.get(0).actionsString, 0, startSquare.x, startSquare.y);
- // System.out.println(permStr);
- // constraint 5
- IloNumVar gameValue = cplex.numVar(Double.MIN_VALUE, Double.MAX_VALUE, "game_value");
- for(int a = 0; a < allSequences2.size(); a++){
- IloLinearNumExpr lhs = cplex.linearNumExpr();
- for(int i = 0; i < sequences.size(); i++){
- // System.out.println("sequences size = " +sequences.size() + ", i = " + i + ", a = " + a);
- lhs.addTerm(sequences.get(i).extendedUtility.get(a), sequences.get(i).var);
- }
- cplex.addGe(lhs, gameValue);
- // System.out.println("Adding 5. constaint: " + lhs + " > " + gameValue);
- }
- cplex.addMaximize(gameValue);
- cplex.solve();
- System.out.println("THE TOTAL VALUE IS " + cplex.getValue(gameValue));
- for(int i = 0; i < sequences.size(); i++){
- if(cplex.getValue(sequences.get(i).var) > 0)
- System.out.println(i + ": " + sequences.get(i).var.getName() + " : " + cplex.getValue(sequences.get(i).var));
- }
- }
- public final ArrayList<String> extendSequences(ArrayList<Sequence> finalSequences, boolean extend){
- ArrayList<String> haveStr = new ArrayList<>();
- haveStr.add("");
- for(int i = 0; i < finalSequences.size(); i++){
- haveStr.add(finalSequences.get(i).actionsString);
- for(int j = 0; j < finalSequences.get(i).actionsString.length(); j++){
- String str = finalSequences.get(i).actionsString.substring(0, j);
- if(!haveStr.contains(str)){
- haveStr.add(str);
- }
- }
- }
- // System.out.println("ALL SEQUENCES");
- // for(int i = 0; i < haveStr.size(); i++){
- // System.out.println(haveStr.get(i));
- // }
- // System.out.println("DONE SEQUENCES");
- // return perm("RRR", 0, startSquare.x, startSquare.y);
- ArrayList<String> allExtendedSequences = new ArrayList<>();
- if(extend){
- for(String s : haveStr){
- allExtendedSequences.addAll(perm(s, 0, startSquare.x, startSquare.y));
- }
- } else{
- return haveStr;
- }
- return allExtendedSequences;
- }
- public ArrayList<String> perm(String s, int n, int x, int y){
- // System.out.println(x + ", " + y);
- ArrayList<String> ret = new ArrayList();
- // System.out.println(s + " [" + x + ", " + y + "], " + n + " - " + maze[y][x].c);
- if(maze[y][x].isDanger() && n == s.length()){ // && !("P").equals(s.charAt(n-1)) && ("W").equals(s.charAt(n-1))
- ret.add(s + "W");
- ret.add(s + "P");
- ret.add(s);
- }else if(n == s.length()) {
- ret.add(s);
- return ret;
- } else{
- int thisX = x;
- int thisY = x;
- 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))
- // System.out.println("not danger, moving " + s.charAt(n));
- String tmp = "" + s.charAt(n);
- if(tmp.equals("R")) x++;
- if(tmp.equals("L")) x--;
- if(tmp.equals("U")) y--;
- if(tmp.equals("D")) y++;
- return perm(s, n + 1, x, y);
- } else{
- // System.out.println("danger2, moving " + s.charAt(n));
- String tmp = "" + s.charAt(n);
- if(tmp.equals("R")) x++;
- if(tmp.equals("L")) x--;
- if(tmp.equals("U")) y--;
- if(tmp.equals("D")) y++;
- ArrayList<String> alW = perm(s.substring(0, n) + "W" + s.substring(n, s.length()), n + 2, x, y);
- ArrayList<String> alP = perm(s.substring(0, n) + "P" + s.substring(n, s.length()), n + 2, x, y);
- ret.addAll(alP);
- ret.addAll(alW);
- }
- }
- return ret;
- }
- }
- // else{
- // System.out.println("danger");
- // String tmp = "" + s.charAt(n);
- // if(tmp.equals("R")) x++;
- // if(tmp.equals("L")) x--;
- // if(tmp.equals("U")) y--;
- // if(tmp.equals("D")) y++;
- // ArrayList<String> alW = perm(s.substring(0, n) + "W" + s.substring(n, s.length()), n + 1, x, y);
- // ArrayList<String> alP = perm(s.substring(0, n) + "P" + s.substring(n, s.length()), n + 1, x, y);
- // ret.addAll(alP);
- // ret.addAll(alW);
- // }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package gt;
- import ilog.concert.IloNumVar;
- import java.util.ArrayList;
- /**
- *
- * @author Jan
- */
- public class rSequence2 {
- public ArrayList<Integer> piratesIdxs = new ArrayList<>();
- public ArrayList<Boolean> piratesKill = new ArrayList<>();
- public ArrayList<Double> extendedUtility = new ArrayList<>();
- public int gold = 0;
- public String sequence;
- public double probability = 0;
- public boolean endsInDestination = false;
- public IloNumVar var;
- public rSequence2(Square[][] maze, Square startSquare, String s, double p){
- this.sequence = s;
- this.probability = p;
- int x = startSquare.x;
- int y = startSquare.y;
- for(int i = 0; i < s.length(); i++){
- String tmp = "" + s.charAt(i);
- if(maze[y][x].isGold()) gold++;
- if(maze[y][x].isDanger()) piratesIdxs.add(maze[y][x].dangerIdx);
- if(tmp.equals("R")) x++;
- if(tmp.equals("L")) x--;
- if(tmp.equals("U")) y--;
- if(tmp.equals("D")) y++;
- if(tmp.equals("W")) piratesKill.add(false);
- if(tmp.equals("P")) piratesKill.add(true);
- }
- if(maze[y][x].isTerminal()) endsInDestination = true;
- System.out.println(s);
- // System.out.println("final destination is [" + x + ", " + y + "] end is destination " + endsInDestination + " - " + maze[y][x].c);
- }
- public void computeExtendedUtility(ArrayList<Integer[]> pirateMove){
- for(int i = 0; i < pirateMove.size(); i++){
- double ut = 10 + gold;
- for(int j = 0; j < pirateMove.get(0).length; j++){
- if(piratesIdxs.contains(pirateMove.get(i)[j])) ut *= (1 - probability);
- }
- if(sequence.contains("P") || !endsInDestination){
- ut = 0;
- }
- extendedUtility.add(ut);
- }
- // System.out.println(sequence + " : " + extendedUtility);
- }
- //if this is after s
- public boolean follows(rSequence2 s){
- if(s.sequence.length() + 1 == this.sequence.length()){
- return this.sequence.substring(0, s.sequence.length()).equals(s.sequence);
- } else return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment