00001 package edu.tum.cs.logic.sat.weighted;
00002
00003 import java.util.Vector;
00004
00005 import edu.tum.cs.logic.Formula;
00006 import edu.tum.cs.logic.GroundAtom;
00007 import edu.tum.cs.logic.PossibleWorld;
00008 import edu.tum.cs.logic.WorldVariables;
00009 import edu.tum.cs.srl.Database;
00010
00015 public class MaxWalkSATRoom extends MaxWalkSAT {
00016
00020 public boolean alternativeMethod = true;
00021
00031 public MaxWalkSATRoom(WeightedClausalKB kb, PossibleWorld state, WorldVariables vars, Database evidence) throws Exception {
00032 super(kb, state, vars, evidence);
00033 }
00034
00038 @Override
00039 public void setState() {
00040
00041 super.setState();
00042
00043 int count = 0;
00044 for(int i = 0; i < vars.size(); i++) {
00045 if(this.evidence.containsKey(i))
00046 continue;
00047 GroundAtom ga = vars.get(i);
00048 String gaName = ga.toString();
00049 GroundAtom vorher;
00050 if(gaName.startsWith("workplaceAfter"))
00051 vorher = vars.get(gaName.replace("After", "Before"));
00052 else if(gaName.startsWith("employeeIn"))
00053 vorher = vars.get(gaName.replace("employeeIn", "roomBefore"));
00054 else
00055 continue;
00056 state.set(i, vorher.isTrue(state));
00057 count++;
00058 }
00059 System.out.printf("reinitialized %d ground atoms according to room planning heuristic.\n", count);
00060 }
00061
00065 public void printBestState() {
00066 System.out.println("Methode: " + deltaCostCalcMethod);
00067 System.out.println("Steps: " + this.maxSteps);
00068 System.out.println("*********** Best State after " + lastMinStep + " Steps *************");
00069 boolean[] s = bestState.getState();
00070 for (int c = 0; c < s.length; c++) {
00071 String temp = "";
00072 if (!s[c]) {
00073 temp = "!";
00074 }
00075 System.out.println(temp += vars.get(c).toString());
00076 System.out.println(vars.get(c).toString() + " - > " + s[c]);
00077 }
00078 System.out.println("Unsatisfied Sum: " + minSum);
00079
00080 }
00081
00082 protected void SAMove() {
00083 if(alternativeMethod)
00084 walkSATMove();
00085 else
00086 super.SAMove();
00087 }
00088
00093 @Override
00094 protected void walkSATMove() {
00095 if(!alternativeMethod) {
00096 super.walkSATMove();
00097 return;
00098 }
00099 boolean found;
00100 double formulaDelta = 0;
00101
00102 int x = rand.nextInt(unsatisfiedConstraints.size());
00103 Formula parent;
00104
00105 do {
00106
00107 if (x == unsatisfiedConstraints.size()) {
00108 SAMoves++;
00109 super.SAMove();
00110 return;
00111 } else {
00112
00113 Vector<Object> bestGAinFormula = new Vector<Object>();
00114 formulaDelta = 0;
00115
00116 parent = cl2Formula.get(unsatisfiedConstraints.get(x));
00117 do {
00118 x++;
00119
00120 for (WeightedClause con : formula2clauses.get(parent)) {
00121 if (unsatisfiedConstraints.contains(con)) {
00122 bestGAinFormula.addAll(con.greedySatisfy());
00123 }
00124 }
00125
00126 for (Object o : bestGAinFormula) {
00127 if (o instanceof Double) {
00128 formulaDelta += ((Double) o).doubleValue();
00129 }
00130 }
00131
00132 if (formulaDelta < 0) {
00133 for (Object o : bestGAinFormula) {
00134 if (o instanceof GroundAtom) {
00135 flipGndAtom((GroundAtom) o);
00136 }
00137 }
00138 found = true;
00139 greedyMoves++;
00140 break;
00141 } else {
00142
00143 found = false;
00144 break;
00145 }
00146
00147 } while (!parent.isTrue(state));
00148 }
00149 } while (!found);
00150 }
00151
00152 }