Package csp

Source Code of csp.TailorTest2

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package csp;

import csp.backends.TailorSolver;
import csp.convertors.FuzzyToIntegerConvertor;
import csp.datatypes.CSPSolution;
import evaluation.RandomProblemGenerator;
import expressions.DoubleNegationEliminator;
import expressions.FuzzyExpression;
import finiteReduction.DomainFinder;
import finiteReduction.FiniteReductionConfig;
import fuzzysat.FuzzyClause;
import fuzzysat.FuzzyLiteral;
import fuzzysat.Literal;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*
* @author Jeroen Janssen <Jeroen.Janssen@vub.ac.be>
*/
public class TailorTest2 {
  public static void main(String[] args) {
    //CSPProblem model = new CSPProblem();
    /*IntegerBinaryExpression exp1 = new IntegerBinaryExpression(
    IntegerBinaryExpression.Operator.MAX,
    new IntegerBinaryExpression(IntegerBinaryExpression.Operator.SUM,
    new IntegerDomainVariable("a",0,10),
    new IntegerBinaryExpression(IntegerBinaryExpression.Operator.SUM,
    new IntegerDomainVariable("b",0,10),new IntegerConstant(-10))),
    new IntegerConstant(0));
    model.addConstraint(new Constraint(Constraint.Operator.GEQ,exp1,5));*/

    int defaultBound = 4;
    int k = 1;

    long initTime = System.currentTimeMillis();

    FuzzyToIntegerConvertor ficonv =
        new FuzzyToIntegerConvertor(defaultBound, k);
    //ficonv.addFuzzyClause(null)
    List<FuzzyClause> clauses = RandomProblemGenerator.readConstraintsFromFile(
        new File("/home/jeroen/Doctoraat/Implementatie/godel.txt"));
    //List<FuzzyClause> clauses = RandomProblemGenerator.readConstraintsFromFile(
    //    new File("/home/jeroen/Doctoraat/Implementatie/miniontest"));

//    DoubleNegationEliminator dnegElim = new DoubleNegationEliminator();
//    ArrayList<FuzzyClause> newClauses = new ArrayList<FuzzyClause>();
//    for (FuzzyClause c : clauses) {
//      ArrayList<Literal> newLiterals = new ArrayList<Literal>();
//      for (Literal l : c.getDisjuncts()) {
//        FuzzyLiteral flit = (FuzzyLiteral) l;
//        FuzzyExpression newExp = flit.getExpression().accept(dnegElim);
//        newLiterals.add(new FuzzyLiteral(flit.getLowerBound(),
//            flit.getUpperBound(), newExp));
//      }
//      newClauses.add(new FuzzyClause(newLiterals));
//    }

    // overwrite the old clauses with the simplified ones ...
//    clauses = newClauses;

    // DEBUG output for checking DoubleNegationEliminator
    //System.out.println("After DoubleNegationEliminator");
    //for (FuzzyClause c : clauses) {
    //  System.out.println(c);
    //}

    // optimization
    HashMap trivialAssignments = new HashMap();
//    if (FiniteReductionConfig.optimizeClauses) {
//      boolean changed = true;
//      while (changed) {
////                System.out.println("...");
//        DomainFinder.simplify(clauses);
//        changed = DomainFinder.eliminateTriviallySatisfiableClauses(clauses, trivialAssignments);
//        System.out.println("Eliminated trivially satisfiable clause");
//      }
//      System.out.println("Optimized the clauses");
//    }

    //System.out.println("After optimization");
    //for (FuzzyClause c : clauses) {
    //  System.out.println(c);
    //}

    for (FuzzyClause c : clauses) {
      ficonv.addFuzzyClause(c);
    }
    //IntegerToMinionConvertor imconv = new IntegerToMinionConvertor(defaultBound);
    //MinionCSPProblem mProb = imconv.convertCSPToMinion(ficonv.convertToCSPProblem());
    TailorSolver solv = new TailorSolver("/home/jeroen/programming/tailorV0.3.2/tailor.jar",
        "/home/jeroen/programming/minion-0.10/bin/minion");
    solv.read(ficonv.convertToCSPProblem());
   
    System.out.println("Ok, read file. Now solving ...");
    solv.PPLEVELNUMBER = TailorSolver.PPLEVELNUMBER.SAC;
    solv.VARORDERNUMBER = TailorSolver.VARORDERNUMBER.LDF;
    CSPSolution solution = solv.solve();
    System.out.println("Solving ended!");
    if (solution != null) {
      solution.getSolution().putAll(trivialAssignments);
      Map<String, Double> fsol = ficonv.convertCSPSolutionToFuzzyModel(solution);
      for (String key : fsol.keySet()) {
        System.out.println(key + ": " + fsol.get(key));
      }
      if(checkModel(clauses, fsol)) {
        System.out.println("Model found!");
      }
    } else {
      if (!trivialAssignments.isEmpty()) {
        for (Object key : trivialAssignments.keySet()) {
          System.out.println(key + ": " + trivialAssignments.get(key));
        }
        checkModel(clauses, trivialAssignments);
      } else {
        System.out.println("No solution.");
      }
    }
    long elapsedTime = System.currentTimeMillis() - initTime;
    System.out.println("Time: " + elapsedTime + "ms");
    //System.out.println(solution);
  }

  public static boolean checkModel(List<FuzzyClause> problem, Map<String, Double> model) {
    for (FuzzyClause cl : problem) {
      if (!cl.isSatisfied(model)) {
        System.out.println("WARNING: incorrect model!");
        System.out.println("     model  = " + model);
        System.out.println("     clause = " + cl);
        return false;
      }
    }
    return true;
  }
}
TOP

Related Classes of csp.TailorTest2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.