/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fuzzysat;
import csp.ChocoSolver;
import csp.EnumeratedChocoSolver;
import finiteReduction.DomainFinder;
import finiteReduction.FiniteReductionConfig;
import java.util.*;
/**
*
* @author steven
*/
public class IncompleteChocoSolver implements FuzzySATSolver {
static Random rand = new Random();
int maxDegrees;
int k;
List<FuzzyClause> constraints;
Map<String, Double> trivialAssignments;
long timeout = 10000000;
long startTime = -1;
public IncompleteChocoSolver(List<FuzzyClause> constraints) {
this(constraints, 10);
}
/*
* the number of truth degrees that will be tried by the algorithm is k, 2*k, ..., maxDegrees*k
*/
public IncompleteChocoSolver(List<FuzzyClause> constraints, int k) {
this.k = k;
this.maxDegrees = maxDegrees;
this.constraints = constraints;
trivialAssignments = new HashMap();
startTime = System.currentTimeMillis();
// System.out.println("Start preprocessing");
if (FiniteReductionConfig.optimizeClauses) {
boolean changed = true;
while (changed) {
// System.out.println("...");
DomainFinder.simplify(constraints);
changed = DomainFinder.eliminateTriviallySatisfiableClauses(constraints, trivialAssignments);
}
}
}
public Map<String, Double> findFuzzyModel() {
for (int numberOfDegrees = 1;!timesUp(); numberOfDegrees++) {
EnumeratedChocoSolver solv = new EnumeratedChocoSolver(numberOfDegrees, new HashMap(),k);
for (FuzzyClause fc : constraints) {
solv.addFuzzyClause(fc);
}
/*Map<String, Double> model = solv.getModel();
model.putAll(trivialAssignments);
if (model != null){
return model;
}*/
// // Safer code added by Jeroen:
// Map<String, Double> model = solv.getModel();
// if (!trivialAssignments.isEmpty()) {
// if (model == null) {
// model = new HashMap<String, Double>();
// }
// model.putAll(trivialAssignments);
// return model;
// }
Map<String, Double> model = solv.getModel();
if (model!=null) {
model.putAll(trivialAssignments);
return model;
}
}
return null;
}
public void setTimeout(long l) {
timeout = l;
}
private boolean timesUp() {
if (timeout < 0 || startTime < 0)
return false;
long time = System.currentTimeMillis() - startTime;
// System.out.println("time = " + time);
return (time > timeout * 1000);
}
}