/** * 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; } }
/** * 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.DSet} object. * @param map * @return {@link solver.variables.Variable}. */ private static SetVar buildWithSet(String name, DSet type, THashMap<String, Object> map) { // final Declaration what = type.getWhat(); // final SetVariable sv; // switch (what.typeOf) { // case INT: // LOGGER.severe("PVariable#buildWithSet INT: unknown constructor for " + name); // throw new UnsupportedOperationException(); // case INT2: // DInt2 bounds = (DInt2) what; // sv = Choco.makeSetVar(name, bounds.getLow(), bounds.getUpp()); // map.put(name, sv); // return sv; // case INTN: // DManyInt values = (DManyInt) what; // sv = Choco.makeSetVar(name, values.getValues()); // map.put(name, sv); // return sv; // } // return null; Exit.log("SET VAR"); return null; }
private static IntVar buildOnExpression( String name, Expression expression, THashMap<String, Object> map, Solver solver) { final IntVar iv; switch (expression.getTypeOf()) { case BOO: iv = expression.boolVarValue(solver); break; case INT: iv = VariableFactory.fixed(name, expression.intValue(), solver); break; case IDE: iv = VariableFactory.eq((IntVar) map.get(expression.toString())); break; case IDA: EIdArray eida = (EIdArray) expression; iv = ((IntVar[]) map.get(eida.name))[eida.index - 1]; break; default: iv = null; Exit.log("Unknown expression"); } return iv; }
/** * 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; } }