Package csp

Source Code of csp.MinionTest

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

import csp.backends.MinionSolver;
import csp.convertors.FuzzyToIntegerConvertor;
import csp.convertors.IntegerToMinionConvertor;
import csp.datatypes.*;
import csp.datatypes.minion.MinionCSPProblem;
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 MinionTest {

  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;

    FuzzyToIntegerConvertor ficonv = new FuzzyToIntegerConvertor(defaultBound, k);
    //ficonv.addFuzzyClause(null)
    List<FuzzyClause> clauses = RandomProblemGenerator.readConstraintsFromFile(
        new File("/home/jeroen/Doctoraat/Implementatie/minionsmalltest.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());
    MinionSolver solv = new MinionSolver("/home/jeroen/programming/minion-0.10/bin/minion");
    System.out.println("Reading file...");
    solv.read(mProb);
    System.out.println("Ok, read file. Now solving ...");
    CSPSolution solution = solv.solve();
    System.out.println("Solving ended!");
    if (solution != null) {
      Map<String, Double> fsol = ficonv.convertCSPSolutionToFuzzyModel(solution);
      solution.getSolution().putAll(trivialAssignments);
      checkModel(clauses, fsol);
      for (String key : fsol.keySet()) {
        System.out.println(key + ": " + fsol.get(key));
      }
    } else {
      if (!trivialAssignments.isEmpty()) {
        for (Object key : trivialAssignments.keySet()) {
          System.out.println(key + ": " + trivialAssignments.get(key));
        }
      } else {
        System.out.println("No solution.");
      }
    }
    //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.MinionTest

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.