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 java.util.ArrayList;
- /**
- *
- * @author Jan
- */
- public class rSequence2 {
- public ArrayList<Integer> piratesIdxs = new ArrayList<>();
- public ArrayList<Double> extendedUtility = new ArrayList<>();
- public int gold = 0;
- public String sequence;
- public double probability = 0;
- public boolean endsInDestination = false;
- 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(maze[y][x].isTerminal()) endsInDestination = true;
- }
- 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(!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;
- }
- }
- /*
- * 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();
- // for sequences to destination
- for(int i = 0; i < finalSequences.size(); i++){
- rSequence2 seq = new rSequence2(this.maze, this.startSquare, finalSequences.get(i).actionsString, this.probability);
- seq.computeExtendedUtility(allSequences2);
- IloNumVar var = cplex.numVar(0, 1, finalSequences.get(i).actionsString);
- variables.add(var);
- sequences.add(seq);
- }
- // add sequences not to destination
- ArrayList<String> haveStr = new ArrayList<>();
- haveStr.add("");
- for(int i = 0; i < finalSequences.size(); i++){
- haveStr.add(sequences.get(i).sequence);
- for(int j = 0; j < sequences.get(i).sequence.length(); j++){
- String str = sequences.get(i).sequence.substring(0, j);
- if(!haveStr.contains(str)){
- haveStr.add(str);
- rSequence2 seq = new rSequence2(this.maze, this.startSquare, str, this.probability);
- seq.computeExtendedUtility(allSequences2);
- IloNumVar var = cplex.numVar(0, 1, str);
- variables.add(var);
- sequences.add(seq);
- }
- }
- }
- // create empty sequence
- IloNumVar var = cplex.numVar(1, 1, "var_empty");
- variables.add(var);
- rSequence2 seq = new rSequence2(this.maze, this.startSquare, "", this.probability);
- 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.0, variables.get(j));
- }
- }
- cplex.addEq(lhs, variables.get(i));
- }
- }
- 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 < finalSequences.size(); i++){
- lhs.addTerm(sequences.get(i).extendedUtility.get(a), variables.get(i));
- }
- cplex.addGe(lhs, gameValue);
- }
- cplex.addMaximize(gameValue);
- cplex.solve();
- System.out.println("THE TOTAL VALUE IS " + cplex.getValue(gameValue));
- for(int i = 0; i < finalSequences.size(); i++){
- System.out.println(variables.get(i).getName() + " " + cplex.getValue(variables.get(i)));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment