/** * 此方法,将root涉及到的符号变量初始化为约束求解中的基本变量 * * @param root */ public void updateVariableAndPrintExpression(ExpressionNode root) { HashMap<ExpressionOperator, String> expression_operator = new HashMap<>(); expression_operator.put(ExpressionOperator.div, "\\"); expression_operator.put(ExpressionOperator.plus, "+"); expression_operator.put(ExpressionOperator.minus, "-"); expression_operator.put(ExpressionOperator.multi, "*"); if (root != null) { // if(root.operator!=null){ if (root.getType() == ExpressionType.expression) { print("("); updateVariable(root.getLeft()); // print(root.getOperator()); print(expression_operator.get(root.getOperator())); updateVariable(root.getRight()); print(")"); } else { print(root.getValue()); if (root.getType() == ExpressionType.single_variable) { String variableName = root.getValue(); if (!envVariableValueHashMap.containsKey(variableName)) { envVariableValueHashMap.put(variableName, Choco.makeIntVar(variableName)); } } } } }
private IntegerVariable createAttributeValueVariable(Attribute attribute, String attributeId) { // String attrOptions = "cp:no_decision"; String attrOptions = ""; IntegerVariable attributeVariable = null; // (1) if attribute value is set, then attribute can either be this value or the disabled // attribute value if (FeatureModelHelper.isAttributeValueSet(attribute)) { int value = FeatureModelHelper.getAttributeValue(attribute); int[] attributeValues = new int[] {value, attributeDisabled}; attributeVariable = Choco.makeIntVar(attributeId, attributeValues, attrOptions); } else { Domain attributeDomain = attribute.getDomain(); // (2) if attribute domain is discrete if (attributeDomain instanceof DiscreteDomain) { DiscreteDomain discreteDomain = (DiscreteDomain) attributeDomain; int[] domainValues = getDomainValues(attribute, discreteDomain); attributeVariable = Choco.makeIntVar(attributeId, domainValues, attrOptions); // todo: check numerical domain and use intervals instead // (3) if attribute domain is integer } else if (attributeDomain instanceof NumericalDomain) { NumericalDomain numericalDomain = (NumericalDomain) attributeDomain; int lowestBoundofNumericalDomain = getLowestBoundofNumericalDomain(numericalDomain); int highestBoundofNumericalDomain = getHighestBoundofNumericalDomain(numericalDomain); if (lowestBoundofNumericalDomain > attributeDisabled) { lowestBoundofNumericalDomain = attributeDisabled; } attributeVariable = Choco.makeIntVar( attributeId, lowestBoundofNumericalDomain, highestBoundofNumericalDomain, attrOptions); } } return attributeVariable; }
public void initializeTasks() { if (starts == null) { tasks = Choco.makeTaskVarArray("T", 0, horizon, durations); } else { tasks = new TaskVariable[durations.length]; for (int i = 0; i < tasks.length; i++) { tasks[i] = Choco.makeTaskVar( String.format("T_%d", i), starts[i], Choco.makeIntVar(String.format("end-%d", i), 0, horizon, Options.V_BOUND), durations[i]); } } }
private IntegerVariable createFeatureVariable(Feature feature) { String id = feature.getId(); // an unbound feature has cardinality [0..1], // a selected feature has cardinality [1..1], // a deselected feature has cardinality [0..0] int minCardinality = (FeatureState.SELECTED.equals(feature.getConfigurationState())) ? 1 : 0; int maxCardinality = (FeatureState.DESELECTED.equals(feature.getConfigurationState())) ? 0 : 1; log.debug( "Create IntegerVariable for '" + id + "' [" + minCardinality + "," + maxCardinality + "]."); IntegerVariable intNodeVariable = Choco.makeIntVar(id, minCardinality, maxCardinality); getModel().addVariable(intNodeVariable); nodeVariables.put(id, intNodeVariable); return intNodeVariable; }
public Constraint generateConstraint( ExpressionNode leftNode, ExpressionNode rightNode, InfixExpression.Operator operator) { IntegerExpressionVariable leftVariable = null, rightVariable = null; if (leftNode.getType() != ExpressionType.single_int) { leftVariable = constraintVariableModeling(leftNode); } if (rightNode.getType() != ExpressionType.single_int) { rightVariable = constraintVariableModeling(rightNode); } Constraint constraint = null; if (leftVariable != null && rightVariable != null) { if (operator.equals(InfixExpression.Operator.LESS)) { constraint = Choco.lt(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { constraint = Choco.leq(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER)) { constraint = Choco.gt(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { constraint = Choco.geq(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.EQUALS)) { constraint = Choco.eq(leftVariable, rightVariable); } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { constraint = Choco.neq(leftVariable, rightVariable); } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } else if (leftVariable != null && rightVariable == null) { int rightValue = Integer.parseInt(rightNode.getValue()); if (operator.equals(InfixExpression.Operator.LESS)) { constraint = Choco.lt(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { constraint = Choco.leq(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.GREATER)) { constraint = Choco.gt(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { constraint = Choco.geq(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.EQUALS)) { constraint = Choco.eq(leftVariable, rightValue); } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { constraint = Choco.neq(leftVariable, rightValue); } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } else if (leftVariable == null && rightVariable != null) { int leftValue = Integer.parseInt(leftNode.getValue()); if (operator.equals(InfixExpression.Operator.LESS)) { constraint = Choco.lt(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { constraint = Choco.leq(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER)) { constraint = Choco.gt(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { constraint = Choco.geq(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.EQUALS)) { constraint = Choco.eq(leftValue, rightVariable); } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { constraint = Choco.neq(leftValue, rightVariable); } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } else if (leftVariable == null && rightVariable == null) { int leftValue = Integer.parseInt(leftNode.getValue()), rightValue = Integer.parseInt(rightNode.getValue()); boolean logicResult; Constraint falseConstraint = Choco.lt(Choco.makeIntVar("bigger", 5, 6), Choco.makeIntVar("smaller", 1, 2)), trueConstraint = Choco.gt(Choco.makeIntVar("bigger", 5, 6), Choco.makeIntVar("smaller", 1, 2)); if (operator.equals(InfixExpression.Operator.LESS)) { if (leftValue < rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.LESS_EQUALS)) { if (leftValue <= rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.GREATER)) { if (leftValue > rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.GREATER_EQUALS)) { if (leftValue >= rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.EQUALS)) { if (leftValue == rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else if (operator.equals(InfixExpression.Operator.NOT_EQUALS)) { if (leftValue != rightValue) { constraint = trueConstraint; } else { constraint = falseConstraint; } } else { System.out.println("Constraint solver can't do with the operator: " + operator); } } if (constraint == null) { System.out.println("can't solve this kind of constraint"); } return constraint; }
public Object makeIntVar(String name, int min, int max) { return Choco.makeIntVar(name, min, max); }