Search in sources :

Example 6 with Constraint

use of choco.kernel.model.constraints.Constraint in project Malai by arnobl.

the class ChocoWrapper method initialization.

/*
	 * Initialize the warpper.
	 */
public static void initialization() {
    m = new CPModel();
    s = new CPSolver();
    stack = new Stack<Constraint>();
    varMap = new HashMap<String, IntegerVariable>();
    solved = false;
}
Also used : CPSolver(choco.cp.solver.CPSolver) IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) Constraint(choco.kernel.model.constraints.Constraint) CPModel(choco.cp.model.CPModel)

Example 7 with Constraint

use of choco.kernel.model.constraints.Constraint in project abstools by abstools.

the class ProductLineAnalysisHelper method buildAndPrintAllConfigurations.

/*
     * Build all SPL configurations (valid feature selections, ignoring attributes), one by one
     * The purpose is to measure how long this takes, so we can compare it with the performance of type checking the SPL.
     *
     */
public static void buildAndPrintAllConfigurations(Model m) {
    long timeSum = 0;
    for (Product product : m.getProductList()) {
        long time0 = System.currentTimeMillis();
        System.out.println("\u23F1 Flattening product: " + product.getFeatureSetAsString());
        // Find a solution to the feature model that satisfies the product feature selection
        ChocoSolver s = m.instantiateCSModel();
        HashSet<Constraint> newcs = new HashSet<>();
        product.getProdConstraints(s.vars, newcs);
        for (Constraint c : newcs) s.addConstraint(c);
        Map<String, Integer> solution = s.getSolution();
        System.out.println("\u23F1 Full product configuration: " + solution);
        long time1 = System.currentTimeMillis();
        // i.e. add attribute assignments to features
        for (String fname : solution.keySet()) {
            if (// ignore internal ChocoSolver variable
            fname.startsWith("$"))
                continue;
            if (fname.contains(".")) {
                String[] parts = fname.split("\\.");
                String fid = parts[0];
                String aid = parts[1];
                Integer val = solution.get(fname);
                for (Feature feature : product.getFeatures()) {
                    if (feature.getName().equals(fid)) {
                        feature.addAttrAssignment(new AttrAssignment(aid, new IntVal(val)));
                        break;
                    }
                }
            }
        }
        long time2 = System.currentTimeMillis();
        Model thisModel = m.treeCopyNoTransform();
        long time3 = System.currentTimeMillis();
        thisModel.flattenForProduct(product);
        long time4 = System.currentTimeMillis();
        timeSum += (time4 - time3);
        System.out.println("\u23F1 Time: " + (time1 - time0) + " | " + (time2 - time1) + " | " + (time3 - time2) + " | " + (time4 - time3) + " | " + "Total(s): " + ((time4 - time0) / 1000.0));
    }
    System.out.println("\u23F1 Flattening total time (s): " + timeSum / 1000.0);
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) IntVal(org.abs_models.frontend.ast.IntVal) Product(org.abs_models.frontend.ast.Product) Feature(org.abs_models.frontend.ast.Feature) Model(org.abs_models.frontend.ast.Model) AttrAssignment(org.abs_models.frontend.ast.AttrAssignment) ChocoSolver(org.abs_models.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Example 8 with Constraint

use of choco.kernel.model.constraints.Constraint in project abstools by abstools.

the class CheckSPLCommand method analyzeMTVL.

private void analyzeMTVL(Model m) {
    if (m.hasMTVL()) {
        if (solve) {
            if (parent.verbose)
                System.out.println("Searching for solutions for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.getSolutionsAsString());
        }
        if (minimise != null) {
            if (parent.verbose)
                System.out.println("Searching for minimum solutions of " + minimise + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            System.out.print(s.minimiseToString(minimise));
        }
        if (maximise != null) {
            if (parent.verbose)
                System.out.println("Searching for maximum solutions of " + maximise + " for the feature model...");
            ChocoSolver s = m.instantiateCSModel();
            // System.out.print(s.maximiseToInt(product));
            s.addConstraint(ChocoSolver.eqeq(s.vars.get(maximise), s.maximiseToInt(maximise)));
            ChocoSolver s1 = m.instantiateCSModel();
            int i = 1;
            while (s1.solveAgain()) {
                System.out.println("------ " + (i++) + "------");
                System.out.print(s1.getSolutionsAsString());
            }
        }
        if (solveall) {
            if (parent.verbose)
                System.out.println("Searching for all solutions for the feature model...");
            ChocoSolver solver = m.instantiateCSModel();
            System.out.print(solver.getSolutionsAsString());
        }
        if (solveWithProduct != null) {
            ProductDecl solveWithDecl = null;
            try {
                solveWithDecl = m.findProduct(solveWithProduct);
            } catch (WrongProgramArgumentException e) {
            // nothing to do
            }
            if (solveWithDecl != null) {
                if (parent.verbose)
                    System.out.println("Searching for solution that includes " + solveWithProduct + "...");
                ChocoSolver s = m.instantiateCSModel();
                HashSet<Constraint> newcs = new HashSet<>();
                solveWithDecl.getProduct().getProdConstraints(s.vars, newcs);
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution:\n" + s.getSolutionsAsString());
            } else {
                System.out.println("Product '" + solveWithProduct + "' not found.");
            }
        }
        if (minWith != null) {
            ProductDecl minWithDecl = null;
            try {
                minWithDecl = m.findProduct(minWith);
            } catch (WrongProgramArgumentException e) {
            // nothing to do
            }
            if (minWithDecl != null) {
                if (parent.verbose)
                    System.out.println("Searching for solution that includes " + minWith + "...");
                ChocoSolver s = m.instantiateCSModel();
                HashSet<Constraint> newcs = new HashSet<>();
                s.addIntVar("difference", 0, 50);
                m.getDiffConstraints(minWithDecl.getProduct(), s.vars, newcs, "difference");
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution: " + s.minimiseToString("difference"));
            } else {
                System.out.println("Product '" + minWith + "' not found.");
            }
        }
        if (maxProduct) {
            if (parent.verbose)
                System.out.println("Searching for solution with maximum number of features ...");
            ChocoSolver s = m.instantiateCSModel();
            HashSet<Constraint> newcs = new HashSet<>();
            s.addIntVar("noOfFeatures", 0, 50);
            if (m.getMaxConstraints(s.vars, newcs, "noOfFeatures")) {
                for (Constraint c : newcs) s.addConstraint(c);
                System.out.println("checking solution: " + s.maximiseToString("noOfFeatures"));
            } else {
                System.out.println("---No solution-------------");
            }
        }
        if (checkProduct != null) {
            ProductDecl checkProductDecl = null;
            try {
                checkProductDecl = m.findProduct(checkProduct);
            } catch (WrongProgramArgumentException e) {
            // nothing to do
            }
            if (checkProductDecl == null) {
                System.out.println("Product '" + checkProduct + "' not found, cannot check.");
            } else {
                ChocoSolver s = m.instantiateCSModel();
                Map<String, Integer> guess = checkProductDecl.getProduct().getSolution();
                System.out.println("checking solution: " + s.checkSolution(guess, m));
            }
        }
        if (numbersol) {
            ChocoSolver s = m.instantiateCSModel();
            // did we call m.dropAttributes() previously?
            if (ignoreattr) {
                System.out.println("Number of solutions found (without attributes): " + s.countSolutions());
            } else {
                System.out.println("Number of solutions found: " + s.countSolutions());
            }
        }
    }
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) Constraint(choco.kernel.model.constraints.Constraint) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) Constraint(choco.kernel.model.constraints.Constraint) ChocoSolver(org.abs_models.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Example 9 with Constraint

use of choco.kernel.model.constraints.Constraint in project abstools by abstools.

the class ChocoSolverExperiment method othermain.

public static void othermain(final String[] args) throws Exception {
    // Constant declaration
    // Order of the magic square
    int n = 3;
    // Magic sum
    int magicSum = n * (n * n + 1) / 2;
    // Build the model
    CPModel m = new CPModel();
    // Creation of an array of variables
    IntegerVariable[][] var = new IntegerVariable[n][n];
    // For each variable, we define its name and the boundaries of its domain.
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            var[i][j] = Choco.makeIntVar("var_" + i + "_" + j, 1, n * n);
            // Associate the variable to the model.
            m.addVariable(var[i][j]);
        }
    }
    // All cells of the matrix must be different
    for (int i = 0; i < n * n; i++) {
        for (int j = i + 1; j < n * n; j++) {
            Constraint c = (Choco.neq(var[i / n][i % n], var[j / n][j % n]));
            m.addConstraint(c);
        }
    }
    // All rows must be equal to the magic sum
    for (int i = 0; i < n; i++) {
        m.addConstraint(Choco.eq(Choco.sum(var[i]), magicSum));
    }
    IntegerVariable[][] varCol = new IntegerVariable[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            // Copy of var in the column order
            varCol[i][j] = var[j][i];
        }
        // Each column?s sum is equal to the magic sum
        m.addConstraint(Choco.eq(Choco.sum(varCol[i]), magicSum));
    }
    IntegerVariable[] varDiag1 = new IntegerVariable[n];
    IntegerVariable[] varDiag2 = new IntegerVariable[n];
    for (int i = 0; i < n; i++) {
        // Copy of var in varDiag1
        varDiag1[i] = var[i][i];
        // Copy of var in varDiag2
        varDiag2[i] = var[(n - 1) - i][i];
    }
    // Every diagonal?s sum has to be equal to the magic sum
    m.addConstraint(Choco.eq(Choco.sum(varDiag1), magicSum));
    m.addConstraint(Choco.eq(Choco.sum(varDiag2), magicSum));
    // Build the solver
    CPSolver s = new CPSolver();
    // print the problem
    System.out.println(m.pretty());
    // Read the model
    s.read(m);
    // Solve the model
    s.solve();
    // Print the solution
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print(MessageFormat.format("{0} ", s.getVar(var[i][j]).getVal()));
        }
        System.out.println();
    }
}
Also used : CPSolver(choco.cp.solver.CPSolver) IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) Constraint(choco.kernel.model.constraints.Constraint) CPModel(choco.cp.model.CPModel) Constraint(choco.kernel.model.constraints.Constraint)

Example 10 with Constraint

use of choco.kernel.model.constraints.Constraint in project abstools by abstools.

the class ProductLineAnalysisHelper method buildAllConfigurations.

/*
     * Build all SPL configurations (valid feature selections, ignoring attributes), one by one
     * The purpose is to measure how long this takes, so we can compare it with the performance of type checking the SPL.
     *
     */
public static void buildAllConfigurations(Model m) {
    long timeSum = 0;
    for (Product product : m.getProductList()) {
        long time0 = System.currentTimeMillis();
        if (m.debug)
            System.out.println("\u23F1 Flattening product: " + product.getFeatureSetAsString());
        // Find a solution to the feature model that satisfies the product feature selection
        ChocoSolver s = m.instantiateCSModel();
        HashSet<Constraint> newcs = new HashSet<>();
        product.getProdConstraints(s.vars, newcs);
        for (Constraint c : newcs) s.addConstraint(c);
        Map<String, Integer> solution = s.getSolution();
        if (m.debug)
            System.out.println("\u23F1 Full product configuration: " + solution);
        long time1 = System.currentTimeMillis();
        // i.e. add attribute assignments to features
        for (String fname : solution.keySet()) {
            if (// ignore internal ChocoSolver variable
            fname.startsWith("$"))
                continue;
            if (fname.contains(".")) {
                String[] parts = fname.split("\\.");
                String fid = parts[0];
                String aid = parts[1];
                Integer val = solution.get(fname);
                for (Feature feature : product.getFeatures()) {
                    if (feature.getName().equals(fid)) {
                        feature.addAttrAssignment(new AttrAssignment(aid, new IntVal(val)));
                        break;
                    }
                }
            }
        }
        long time2 = System.currentTimeMillis();
        Model thisModel = m.treeCopyNoTransform();
        long time3 = System.currentTimeMillis();
        thisModel.flattenForProduct(product);
        long time4 = System.currentTimeMillis();
        timeSum += (time4 - time3);
        if (m.debug) {
            System.out.println("\u23F1 Time: " + (time1 - time0) + " | " + (time2 - time1) + " | " + (time3 - time2) + " | " + (time4 - time3) + " | " + "Total(s): " + ((time4 - time0) / 1000.0));
        }
    }
    if (m.debug)
        System.out.println("\u23F1 Flattening total time (s): " + timeSum / 1000.0);
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) ChocoSolver(abs.frontend.mtvl.ChocoSolver) HashSet(java.util.HashSet)

Aggregations

Constraint (choco.kernel.model.constraints.Constraint)19 ComponentConstraint (choco.kernel.model.constraints.ComponentConstraint)8 MetaConstraint (choco.kernel.model.constraints.MetaConstraint)8 IntegerVariable (choco.kernel.model.variables.integer.IntegerVariable)6 CPModel (choco.cp.model.CPModel)5 HashSet (java.util.HashSet)4 CPSolver (choco.cp.solver.CPSolver)3 ChocoSolver (abs.frontend.mtvl.ChocoSolver)2 ArrayList (java.util.ArrayList)2 ChocoSolver (org.abs_models.frontend.mtvl.ChocoSolver)2 WrongProgramArgumentException (abs.common.WrongProgramArgumentException)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 AttrAssignment (org.abs_models.frontend.ast.AttrAssignment)1 Feature (org.abs_models.frontend.ast.Feature)1 IntVal (org.abs_models.frontend.ast.IntVal)1 Model (org.abs_models.frontend.ast.Model)1 Product (org.abs_models.frontend.ast.Product)1 ProductDecl (org.abs_models.frontend.ast.ProductDecl)1