Skip to content

Commit

Permalink
[FZ] change the way we support element and element2d
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 18, 2024
1 parent 6e1fdea commit dd9e4e0
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 742 deletions.
16 changes: 0 additions & 16 deletions ortools/flatzinc/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,6 @@ cc_library(
],
)

cc_library(
name = "presolve",
srcs = ["presolve.cc"],
hdrs = ["presolve.h"],
deps = [
":model",
"//ortools/base",
"//ortools/base:hash",
"//ortools/graph:cliques",
"//ortools/util:logging",
"//ortools/util:saturated_arithmetic",
"@com_google_absl//absl/strings",
],
)

cc_library(
name = "checker",
srcs = ["checker.cc"],
Expand Down Expand Up @@ -172,7 +157,6 @@ cc_binary(
":cp_model_fz_solver",
":model",
":parser_lib",
":presolve",
"//ortools/base",
"//ortools/base:path",
"//ortools/base:threadpool",
Expand Down
107 changes: 56 additions & 51 deletions ortools/flatzinc/checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ bool CheckArrayBoolXor(const Constraint& ct,

bool CheckArrayIntElement(const Constraint& ct,
const std::function<int64_t(Variable*)>& evaluator) {
if (ct.arguments[0].variables.size() == 2) {
// TODO(user): Check 2D element.
return true;
}
// Flatzinc arrays are 1 based.
const int64_t shifted_index = Eval(ct.arguments[0], evaluator) - 1;
const int64_t element = EvalAt(ct.arguments[1], shifted_index, evaluator);
Expand All @@ -172,17 +168,22 @@ bool CheckArrayIntElementNonShifted(

bool CheckArrayVarIntElement(
const Constraint& ct, const std::function<int64_t(Variable*)>& evaluator) {
if (ct.arguments[0].variables.size() == 2) {
// TODO(user): Check 2D element.
return true;
}
// Flatzinc arrays are 1 based.
const int64_t shifted_index = Eval(ct.arguments[0], evaluator) - 1;
const int64_t element = EvalAt(ct.arguments[1], shifted_index, evaluator);
const int64_t target = Eval(ct.arguments[2], evaluator);
return element == target;
}

bool CheckOrtoolsArrayIntElement(
const Constraint& ct, const std::function<int64_t(Variable*)>& evaluator) {
const int64_t min_index = ct.arguments[1].values[0];
const int64_t index = Eval(ct.arguments[0], evaluator) - min_index;
const int64_t element = EvalAt(ct.arguments[2], index, evaluator);
const int64_t target = Eval(ct.arguments[3], evaluator);
return element == target;
}

bool CheckAtMostInt(const Constraint& ct,
const std::function<int64_t(Variable*)>& evaluator) {
const int64_t expected = Eval(ct.arguments[0], evaluator);
Expand Down Expand Up @@ -1184,7 +1185,6 @@ using CallMap =
// They are created at compilation time when using the or-tools mzn library.
CallMap CreateCallMap() {
CallMap m;
m["fzn_all_different_int"] = CheckAllDifferentInt;
m["alldifferent_except_0"] = CheckAlldifferentExcept0;
m["among"] = CheckAmong;
m["array_bool_and"] = CheckArrayBoolAnd;
Expand All @@ -1193,130 +1193,135 @@ CallMap CreateCallMap() {
m["array_bool_xor"] = CheckArrayBoolXor;
m["array_int_element"] = CheckArrayIntElement;
m["array_int_element_nonshifted"] = CheckArrayIntElementNonShifted;
m["array_int_maximum"] = CheckMaximumInt;
m["array_int_minimum"] = CheckMinimumInt;
m["array_var_bool_element"] = CheckArrayVarIntElement;
m["array_var_int_element"] = CheckArrayVarIntElement;
m["at_most_int"] = CheckAtMostInt;
m["bool_and"] = CheckBoolAnd;
m["bool_clause"] = CheckBoolClause;
m["bool_eq"] = CheckIntEq;
m["bool2int"] = CheckIntEq;
m["bool_eq_imp"] = CheckIntEqImp;
m["bool_eq_reif"] = CheckIntEqReif;
m["bool_ge"] = CheckIntGe;
m["bool_eq"] = CheckIntEq;
m["bool_ge_imp"] = CheckIntGeImp;
m["bool_ge_reif"] = CheckIntGeReif;
m["bool_gt"] = CheckIntGt;
m["bool_ge"] = CheckIntGe;
m["bool_gt_imp"] = CheckIntGtImp;
m["bool_gt_reif"] = CheckIntGtReif;
m["bool_le"] = CheckIntLe;
m["bool_gt"] = CheckIntGt;
m["bool_le_imp"] = CheckIntLeImp;
m["bool_le_reif"] = CheckIntLeReif;
m["bool_le"] = CheckIntLe;
m["bool_left_imp"] = CheckIntLe;
m["bool_lin_eq"] = CheckIntLinEq;
m["bool_lin_le"] = CheckIntLinLe;
m["bool_lt"] = CheckIntLt;
m["bool_lt_imp"] = CheckIntLtImp;
m["bool_lt_reif"] = CheckIntLtReif;
m["bool_ne"] = CheckIntNe;
m["bool_lt"] = CheckIntLt;
m["bool_ne_imp"] = CheckIntNeImp;
m["bool_ne_reif"] = CheckIntNeReif;
m["bool_ne"] = CheckIntNe;
m["bool_not"] = CheckBoolNot;
m["bool_or"] = CheckBoolOr;
m["bool_right_imp"] = CheckIntGe;
m["bool_xor"] = CheckBoolXor;
m["ortools_circuit"] = CheckCircuit;
m["bool2int"] = CheckIntEq;
m["count_eq"] = CheckCountEq;
m["count"] = CheckCountEq;
m["count_geq"] = CheckCountGeq;
m["count_gt"] = CheckCountGt;
m["count_leq"] = CheckCountLeq;
m["count_lt"] = CheckCountLt;
m["count_neq"] = CheckCountNeq;
m["count_reif"] = CheckCountReif;
m["fzn_cumulative"] = CheckCumulative;
m["var_cumulative"] = CheckCumulative;
m["variable_cumulative"] = CheckCumulative;
m["fixed_cumulative"] = CheckCumulative;
m["ortools_cumulative_opt"] = CheckCumulativeOpt;
m["fzn_diffn"] = CheckDiffn;
m["count"] = CheckCountEq;
m["diffn_k_with_sizes"] = CheckDiffnK;
m["fzn_diffn_nonstrict"] = CheckDiffnNonStrict;
m["diffn_nonstrict_k_with_sizes"] = CheckDiffnNonStrictK;
m["fzn_disjunctive"] = CheckDisjunctive;
m["fzn_disjunctive_strict"] = CheckDisjunctiveStrict;
m["ortools_disjunctive_strict_opt"] = CheckDisjunctiveStrictOpt;
m["false_constraint"] = CheckFalseConstraint;
m["global_cardinality"] = CheckGlobalCardinality;
m["fixed_cumulative"] = CheckCumulative;
m["fzn_all_different_int"] = CheckAllDifferentInt;
m["fzn_cumulative"] = CheckCumulative;
m["fzn_diffn_nonstrict"] = CheckDiffnNonStrict;
m["fzn_diffn"] = CheckDiffn;
m["fzn_disjunctive_strict"] = CheckDisjunctiveStrict;
m["fzn_disjunctive"] = CheckDisjunctive;
m["global_cardinality_closed"] = CheckGlobalCardinalityClosed;
m["global_cardinality_low_up"] = CheckGlobalCardinalityLowUp;
m["global_cardinality_low_up_closed"] = CheckGlobalCardinalityLowUpClosed;
m["global_cardinality_low_up"] = CheckGlobalCardinalityLowUp;
m["global_cardinality_old"] = CheckGlobalCardinalityOld;
m["global_cardinality"] = CheckGlobalCardinality;
m["int_abs"] = CheckIntAbs;
m["int_div"] = CheckIntDiv;
m["int_eq"] = CheckIntEq;
m["int_eq_imp"] = CheckIntEqImp;
m["int_eq_reif"] = CheckIntEqReif;
m["int_ge"] = CheckIntGe;
m["int_eq"] = CheckIntEq;
m["int_ge_imp"] = CheckIntGeImp;
m["int_ge_reif"] = CheckIntGeReif;
m["int_gt"] = CheckIntGt;
m["int_ge"] = CheckIntGe;
m["int_gt_imp"] = CheckIntGtImp;
m["int_gt_reif"] = CheckIntGtReif;
m["int_le"] = CheckIntLe;
m["int_gt"] = CheckIntGt;
m["int_in"] = CheckSetIn;
m["int_le_imp"] = CheckIntLeImp;
m["int_le_reif"] = CheckIntLeReif;
m["int_lin_eq"] = CheckIntLinEq;
m["int_le"] = CheckIntLe;
m["int_lin_eq_imp"] = CheckIntLinEqImp;
m["int_lin_eq_reif"] = CheckIntLinEqReif;
m["int_lin_ge"] = CheckIntLinGe;
m["int_lin_eq"] = CheckIntLinEq;
m["int_lin_ge_imp"] = CheckIntLinGeImp;
m["int_lin_ge_reif"] = CheckIntLinGeReif;
m["int_lin_le"] = CheckIntLinLe;
m["int_lin_ge"] = CheckIntLinGe;
m["int_lin_le_imp"] = CheckIntLinLeImp;
m["int_lin_le_reif"] = CheckIntLinLeReif;
m["int_lin_ne"] = CheckIntLinNe;
m["int_lin_le"] = CheckIntLinLe;
m["int_lin_ne_imp"] = CheckIntLinNeImp;
m["int_lin_ne_reif"] = CheckIntLinNeReif;
m["int_lt"] = CheckIntLt;
m["int_lin_ne"] = CheckIntLinNe;
m["int_lt_imp"] = CheckIntLtImp;
m["int_lt_reif"] = CheckIntLtReif;
m["int_lt"] = CheckIntLt;
m["int_max"] = CheckIntMax;
m["int_min"] = CheckIntMin;
m["int_minus"] = CheckIntMinus;
m["int_mod"] = CheckIntMod;
m["int_ne"] = CheckIntNe;
m["int_ne_imp"] = CheckIntNeImp;
m["int_ne_reif"] = CheckIntNeReif;
m["int_ne"] = CheckIntNe;
m["int_negate"] = CheckIntNegate;
m["int_not_in"] = CheckSetNotIn;
m["int_plus"] = CheckIntPlus;
m["int_times"] = CheckIntTimes;
m["ortools_inverse"] = CheckInverse;
m["lex_less_bool"] = CheckLexLessInt;
m["lex_less_int"] = CheckLexLessInt;
m["lex_lesseq_bool"] = CheckLexLesseqInt;
m["lex_lesseq_int"] = CheckLexLesseqInt;
m["maximum_arg_int"] = CheckMaximumArgInt;
m["maximum_int"] = CheckMaximumInt;
m["array_int_maximum"] = CheckMaximumInt;
m["minimum_arg_int"] = CheckMinimumArgInt;
m["minimum_int"] = CheckMinimumInt;
m["array_int_minimum"] = CheckMinimumInt;
m["ortools_network_flow"] = CheckNetworkFlow;
m["ortools_network_flow_cost"] = CheckNetworkFlowCost;
m["nvalue"] = CheckNvalue;
m["ortools_array_bool_element"] = CheckOrtoolsArrayIntElement;
m["ortools_array_int_element"] = CheckOrtoolsArrayIntElement;
m["ortools_array_var_bool_element"] = CheckOrtoolsArrayIntElement;
m["ortools_array_var_int_element"] = CheckOrtoolsArrayIntElement;
m["ortools_circuit"] = CheckCircuit;
m["ortools_cumulative_opt"] = CheckCumulativeOpt;
m["ortools_disjunctive_strict_opt"] = CheckDisjunctiveStrictOpt;
m["ortools_inverse"] = CheckInverse;
m["ortools_network_flow_cost"] = CheckNetworkFlowCost;
m["ortools_network_flow"] = CheckNetworkFlow;
m["ortools_regular"] = CheckRegular;
m["ortools_subcircuit"] = CheckSubCircuit;
m["ortools_table_bool"] = CheckTableInt;
m["ortools_table_int"] = CheckTableInt;
m["regular_nfa"] = CheckRegularNfa;
m["set_in_reif"] = CheckSetInReif;
m["set_in"] = CheckSetIn;
m["int_in"] = CheckSetIn;
m["set_not_in"] = CheckSetNotIn;
m["int_not_in"] = CheckSetNotIn;
m["set_in_reif"] = CheckSetInReif;
m["sliding_sum"] = CheckSlidingSum;
m["sort"] = CheckSort;
m["ortools_subcircuit"] = CheckSubCircuit;
m["symmetric_all_different"] = CheckSymmetricAllDifferent;
m["ortools_table_bool"] = CheckTableInt;
m["ortools_table_int"] = CheckTableInt;
m["var_cumulative"] = CheckCumulative;
m["variable_cumulative"] = CheckCumulative;
return m;
}

Expand Down
3 changes: 2 additions & 1 deletion ortools/flatzinc/cp-sat.msc.in
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"tags": ["cp-sat", "cp", "lcg", "int"],
"stdFlags": ["-a", "-i", "-f", "-p", "-r", "-s", "-v"],
"extraFlags": [
["--params", "Provide parameters interpreted as a text SatParameters proto", "string", ""]
["--params", "Provide parameters interpreted as a text SatParameters proto", "string", ""],
["--fz_int_max", "Provide the maximum value for integer variables", "int", "1125899906842624"]
],
"supportsMzn": false,
"supportsFzn": true,
Expand Down
Loading

0 comments on commit dd9e4e0

Please sign in to comment.