Skip to content

Commit

Permalink
Add support for Boolean argmin/argmax to FlatZinc, and perform offset…
Browse files Browse the repository at this point in the history
… calculation natively rather than through MiniZinc constraints
  • Loading branch information
guidotack committed Apr 1, 2019
1 parent daaaa67 commit d34425b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 8 deletions.
15 changes: 15 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ Date: 2019-??-??
[DESCRIPTION]
Let's see.

[ENTRY]
Module: flatzinc
What: new
Rank: minor
[DESCRIPTION]
Support minimum_arg_bool and maximum_arg_bool constraints.

[ENTRY]
Module: flatzinc
What: change
Rank: minor
[DESCRIPTION]
Use native offset calculation instead of additional constraints
for argmin/argmax constraints.

[ENTRY]
Module: int
What: new
Expand Down
4 changes: 4 additions & 0 deletions gecode/flatzinc/mznlib/arg_max_bool.mzn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
predicate gecode_maximum_arg_bool_offset(array[int] of var bool: x, int: offset, var int: i);

predicate maximum_arg_bool(array[int] of var bool: x, var int: i) =
gecode_maximum_arg_bool_offset(x,min(index_set(x)),i);
4 changes: 2 additions & 2 deletions gecode/flatzinc/mznlib/arg_max_int.mzn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate gecode_maximum_arg_int(array[int] of var int: x, var int: i);
predicate gecode_maximum_arg_int_offset(array[int] of var int: x, int: offset, var int: i);

predicate maximum_arg_int(array[int] of var int: x, var int: i) =
gecode_maximum_arg_int(x,(i-min(index_set(x)))::domain);
gecode_maximum_arg_int_offset(x,min(index_set(x)),i);
4 changes: 4 additions & 0 deletions gecode/flatzinc/mznlib/arg_min_bool.mzn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
predicate gecode_minimum_arg_bool_offset(array[int] of var bool: x, int: offset, var int: i);

predicate minimum_arg_bool(array[int] of var bool: x, var int: i) =
gecode_minimum_arg_bool_offset(x,min(index_set(x)),i);
4 changes: 2 additions & 2 deletions gecode/flatzinc/mznlib/arg_min_int.mzn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
predicate gecode_minimum_arg_int(array[int] of var int: x, var int: i);
predicate gecode_minimum_arg_int_offset(array[int] of var int: x, int: offset, var int: i);

predicate minimum_arg_int(array[int] of var int: x, var int: i) =
gecode_minimum_arg_int(x,(i-min(index_set(x)))::domain);
gecode_minimum_arg_int_offset(x,min(index_set(x)),i);
24 changes: 20 additions & 4 deletions gecode/flatzinc/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,12 +1022,26 @@ namespace Gecode { namespace FlatZinc {

void p_minimum_arg(FlatZincSpace& s, const ConExpr& ce, AST::Node* ann) {
IntVarArgs iv = s.arg2intvarargs(ce[0]);
argmin(s, iv, s.arg2IntVar(ce[1]), true, s.ann2ipl(ann));
int offset = ce[1]->getInt();
argmin(s, iv, offset, s.arg2IntVar(ce[2]), true, s.ann2ipl(ann));
}

void p_maximum_arg(FlatZincSpace& s, const ConExpr& ce, AST::Node* ann) {
IntVarArgs iv = s.arg2intvarargs(ce[0]);
argmax(s, iv, s.arg2IntVar(ce[1]), true, s.ann2ipl(ann));
int offset = ce[1]->getInt();
argmax(s, iv, offset, s.arg2IntVar(ce[2]), true, s.ann2ipl(ann));
}

void p_minimum_arg_bool(FlatZincSpace& s, const ConExpr& ce, AST::Node* ann) {
BoolVarArgs bv = s.arg2boolvarargs(ce[0]);
int offset = ce[1]->getInt();
argmin(s, bv, offset, s.arg2IntVar(ce[2]), true, s.ann2ipl(ann));
}

void p_maximum_arg_bool(FlatZincSpace& s, const ConExpr& ce, AST::Node* ann) {
BoolVarArgs bv = s.arg2boolvarargs(ce[0]);
int offset = ce[1]->getInt();
argmax(s, bv, offset, s.arg2IntVar(ce[2]), true, s.ann2ipl(ann));
}

void p_regular(FlatZincSpace& s, const ConExpr& ce, AST::Node* ann) {
Expand Down Expand Up @@ -1562,8 +1576,10 @@ namespace Gecode { namespace FlatZinc {
&p_global_cardinality_low_up_closed);
registry().add("array_int_minimum", &p_minimum);
registry().add("array_int_maximum", &p_maximum);
registry().add("gecode_minimum_arg_int", &p_minimum_arg);
registry().add("gecode_maximum_arg_int", &p_maximum_arg);
registry().add("gecode_minimum_arg_int_offset", &p_minimum_arg);
registry().add("gecode_maximum_arg_int_offset", &p_maximum_arg);
registry().add("gecode_minimum_arg_bool_offset", &p_minimum_arg_bool);
registry().add("gecode_maximum_arg_bool_offset", &p_maximum_arg_bool);
registry().add("array_int_maximum", &p_maximum);
registry().add("gecode_regular", &p_regular);
registry().add("sort", &p_sort);
Expand Down

0 comments on commit d34425b

Please sign in to comment.