Search in sources :

Example 11 with Constraint

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

the class ChocoSolver method optimise.

public boolean optimise(String var, Boolean minimise) {
    // add the constraints
    if (!solved) {
        for (Constraint c : constraints) cpmodel.addConstraint(c);
    }
    // Impose the feature name to be present
    if (var.contains(".")) {
        String feat = var.split("\\.")[0];
        if (absmodel.debug)
            // and its
            absmodel.println("## including feature '" + feat + "'");
        // parents.");
        if (vars.containsKey(feat)) {
            if (absmodel.debug)
                absmodel.println("  " + feat + " (selected) -> 1");
            cpmodel.addConstraint(Choco.eq(vars.get(feat), 1));
        // // collect parents of 'newFeatures'
        // Set<String> newFeatures = new HashSet<String>();
        // Set<String> newParents = new HashSet<String>();
        // 
        // if (model != null) {
        // newFeatures.add(feat);
        // model.collectParents(newFeatures,newParents);
        // }
        // // add newParents and default values to the solution
        // Iterator<IntegerVariable> it = m.getIntVarIterator();
        // while (it.hasNext()) { // for all variables in the
        // constraints (model): round 2
        // IntegerVariable var2 = it.next();
        // 
        // // If it is a parent to include, set
        // if (newParents.contains(var2.getName())) {
        // if (ast.debug)
        // ast.println("  "+var2+" (parent) -> 1");
        // m.addConstraint(Choco.eq(var2, 1));
        // }
        // }
        }
    }
    // show the problem
    if (absmodel.debug) {
        absmodel.println("## The constraints:");
        // ast.println(m.pretty());
        for (Constraint c : constraints) {
            if (!c.pretty().startsWith("true"))
                absmodel.println(prettyConst(c));
        }
        absmodel.println("-----");
    }
    // Read the model
    solver.read(cpmodel);
    // Minmise the model, if possible
    if (vars.containsKey(var))
        if (solver.contains(vars.get(var))) {
            solved = true;
            if (minimise)
                newsol = solver.minimize(solver.getVar(vars.get(var)), true);
            else
                newsol = solver.maximize(solver.getVar(vars.get(var)), true);
            return newsol;
        }
    return false;
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) MetaConstraint(choco.kernel.model.constraints.MetaConstraint) ComponentConstraint(choco.kernel.model.constraints.ComponentConstraint)

Example 12 with Constraint

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

the class ChocoWrapper method pushOR.

/*
	 * Remove the two top elements (elem1 and elem2) from the stack.
	 * Then add on the top of the stack the "elem1 OR elem2" constraint.
	 */
public static void pushOR() {
    Constraint elem1 = stack.pop();
    Constraint elem2 = stack.pop();
    Constraint res = Choco.or(elem1, elem2);
    stack.push(res);
}
Also used : Constraint(choco.kernel.model.constraints.Constraint)

Example 13 with Constraint

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

the class ChocoWrapper method pushAND.

/*
	 * Remove the two top elements (elem1 and elem2) from the stack.
	 * Then add on the top of the stack the "elem1 AND elem2" constraint.
	 */
public static void pushAND() {
    Constraint elem1 = stack.pop();
    Constraint elem2 = stack.pop();
    Constraint res = Choco.and(elem1, elem2);
    stack.push(res);
}
Also used : Constraint(choco.kernel.model.constraints.Constraint)

Example 14 with Constraint

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

the class ChocoWrapper method pushVariable.

/*
	 * Add the variable at the top of the stack
	 */
public static void pushVariable(String var) {
    IntegerVariable theVar = varMap.get(var);
    if (theVar == null) {
        theVar = Choco.makeIntVar(var, 0, 1);
        varMap.put(var, theVar);
    }
    // FIXME:find a better way
    Constraint res = Choco.or(theVar, theVar);
    stack.push(res);
}
Also used : IntegerVariable(choco.kernel.model.variables.integer.IntegerVariable) Constraint(choco.kernel.model.constraints.Constraint)

Example 15 with Constraint

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

the class ChocoSolver method optimise.

public boolean optimise(String var, Boolean minimise) {
    // add the constraints
    if (!solved) {
        for (Constraint c : constraints) cpmodel.addConstraint(c);
    }
    // Impose the feature name to be present
    if (var.contains(".")) {
        String feat = var.split("\\.")[0];
        if (absmodel.debug)
            // and its
            absmodel.println("## including feature '" + feat + "'");
        // parents.");
        if (vars.containsKey(feat)) {
            if (absmodel.debug)
                absmodel.println("  " + feat + " (selected) -> 1");
            cpmodel.addConstraint(Choco.eq(vars.get(feat), 1));
        // // collect parents of 'newFeatures'
        // Set<String> newFeatures = new HashSet<String>();
        // Set<String> newParents = new HashSet<String>();
        // 
        // if (model != null) {
        // newFeatures.add(feat);
        // model.collectParents(newFeatures,newParents);
        // }
        // // add newParents and default values to the solution
        // Iterator<IntegerVariable> it = m.getIntVarIterator();
        // while (it.hasNext()) { // for all variables in the
        // constraints (model): round 2
        // IntegerVariable var2 = it.next();
        // 
        // // If it is a parent to include, set
        // if (newParents.contains(var2.getName())) {
        // if (ast.debug)
        // ast.println("  "+var2+" (parent) -> 1");
        // m.addConstraint(Choco.eq(var2, 1));
        // }
        // }
        }
    }
    // show the problem
    if (absmodel.debug) {
        absmodel.println("## The constraints:");
        // ast.println(m.pretty());
        for (Constraint c : constraints) {
            if (!c.pretty().startsWith("true"))
                absmodel.println(prettyConst(c));
        }
        absmodel.println("-----");
    }
    // Read the model
    solver.read(cpmodel);
    // Minmise the model, if possible
    if (vars.containsKey(var))
        if (solver.contains(vars.get(var))) {
            solved = true;
            if (minimise)
                newsol = solver.minimize(solver.getVar(vars.get(var)), true);
            else
                newsol = solver.maximize(solver.getVar(vars.get(var)), true);
            return newsol;
        }
    return false;
}
Also used : Constraint(choco.kernel.model.constraints.Constraint) MetaConstraint(choco.kernel.model.constraints.MetaConstraint) ComponentConstraint(choco.kernel.model.constraints.ComponentConstraint)

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