Ejemplo n.º 1
0
  /**
   * Build an array of <? extends {@link solver.variables.Variable}>. </br>WARNING: array's indice
   * are from 1 to n.
   *
   * @param name name of the array of variables.</br> Each variable is named like {@code name}_i.
   * @param type {@link parser.flatzinc.ast.declaration.DArray} object.
   * @param earr array of {@link parser.flatzinc.ast.expression.Expression}
   * @param map
   * @param solver
   */
  private static void buildWithDArray(
      String name,
      DArray type,
      Expression expression,
      EArray earr,
      THashMap<String, Object> map,
      Solver solver) {
    final DInt2 index = (DInt2) type.getIndex(0);
    // no need to get lowB, it is always 1 (see specification of FZN for more informations)
    final int size = index.getUpp();
    final Declaration what = type.getWhat();

    switch (what.typeOf) {
      case BOOL:
        final BoolVar[] bs = new BoolVar[size];
        for (int i = 0; i < size; i++) {
          bs[i] = earr.getWhat_i(i).boolVarValue(solver);
        }
        map.put(name, bs);
        break;
      case INT:
      case INT2:
      case INTN:
        final IntVar[] vs = new IntVar[size];
        for (int i = 0; i < size; i++) {
          vs[i] = earr.getWhat_i(i).intVarValue(solver);
        }
        map.put(name, vs);
        break;
      case SET:
        //                final SetVariable[] svs = new SetVariable[size];
        //                for (int i = 0; i < size; i++) {
        //                    svs[i] = earr.getWhat_i(i).setVarValue();
        //                }
        //                map.put(name, svs);
        Exit.log("SET VAR");
        break;
      default:
        break;
    }
  }
Ejemplo n.º 2
0
 /**
  * Build a {@link solver.variables.Variable} named {@code name}, defined by {@code type}.
  *
  * @param name name of the variable
  * @param type {@link parser.flatzinc.ast.declaration.DInt2} object
  * @param map
  * @param solver
  * @return {@link solver.variables.Variable}
  */
 private static IntVar buildWithInt2(
     String name, DInt2 type, Expression expression, THashMap<String, Object> map, Solver solver) {
   final IntVar iv;
   if (expression != null) {
     iv = buildOnExpression(DEBUG ? name : NO_NAME, expression, map, solver);
     int lb = type.getLow();
     int ub = type.getUpp();
     solver.post(IntConstraintFactory.member(iv, lb, ub));
   } else {
     int size = type.getUpp() - type.getLow() + 1;
     if (size < 256) {
       iv =
           VariableFactory.enumerated(
               DEBUG ? name : NO_NAME, type.getLow(), type.getUpp(), solver);
     } else {
       iv = VariableFactory.bounded(DEBUG ? name : NO_NAME, type.getLow(), type.getUpp(), solver);
     }
   }
   map.put(name, iv);
   return iv;
 }
Ejemplo n.º 3
0
 /**
  * Build an array of <? extends {@link solver.variables.Variable}>. </br>WARNING: array's indice
  * are from 1 to n.
  *
  * @param name name of the array of variables.</br> Each variable is named like {@code name}_i.
  * @param type {@link parser.flatzinc.ast.declaration.DArray} object.
  * @param map
  * @param solver
  */
 private static void buildWithDArray(
     String name,
     DArray type,
     Expression expression,
     THashMap<String, Object> map,
     Solver solver) {
   final DInt2 index = (DInt2) type.getIndex(0);
   // no need to get lowB, it is always 1 (see specification of FZN for more informations)
   final int size = index.getUpp();
   final Declaration what = type.getWhat();
   final IntVar[] vs;
   switch (what.typeOf) {
     case BOOL:
       BoolVar[] bs = new BoolVar[size];
       if (expression == null) {
         for (int i = 1; i <= size; i++) {
           bs[i - 1] = buildWithBool(name + '_' + i, expression, map, solver);
         }
       } else if (expression.getTypeOf().equals(Expression.EType.ARR)) {
         EArray array = (EArray) expression;
         // build the array
         for (int i = 0; i < size; i++) {
           bs[i] = array.getWhat_i(i).boolVarValue(solver);
         }
       }
       map.put(name, bs);
       break;
     case INT:
       vs = new IntVar[size];
       if (expression == null) {
         for (int i = 1; i <= size; i++) {
           vs[i - 1] = buildWithInt(name + '_' + i, null, map, solver);
         }
       } else if (expression.getTypeOf().equals(Expression.EType.ARR)) {
         buildFromIntArray(vs, (EArray) expression, size, solver);
       }
       map.put(name, vs);
       break;
     case INT2:
       vs = new IntVar[size];
       if (expression == null) {
         for (int i = 1; i <= size; i++) {
           vs[i - 1] = buildWithInt2(name + '_' + i, (DInt2) what, expression, map, solver);
         }
       } else if (expression.getTypeOf().equals(Expression.EType.ARR)) {
         buildFromIntArray(vs, (EArray) expression, size, solver);
       }
       map.put(name, vs);
       break;
     case INTN:
       vs = new IntVar[size];
       if (expression == null) {
         for (int i = 1; i <= size; i++) {
           vs[i - 1] = buildWithManyInt(name + '_' + i, (DManyInt) what, expression, map, solver);
         }
       } else if (expression.getTypeOf().equals(Expression.EType.ARR)) {
         buildFromIntArray(vs, (EArray) expression, size, solver);
       }
       map.put(name, vs);
       break;
     case SET:
       //                final SetVariable[] svs = new SetVariable[size];
       //                for (int i = 1; i <= size; i++) {
       //                    svs[i - 1] = buildWithSet(name + '_' + i, (DSet) what, map);
       //                }
       //                map.put(name, svs);
       Exit.log("SET VAR");
       break;
     default:
       break;
   }
 }