Skip to content

Commit

Permalink
pow and square refactoring
Browse files Browse the repository at this point in the history
the constraint logic should be in the constraint factory only
  • Loading branch information
jgFages committed Aug 4, 2023
1 parent 3a5a92b commit 1de7267
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,9 @@ default Constraint pow(IntVar base, int exponent, IntVar result) {
b = mm.get(C - mid);
return ref().times(a, b, Y);
/*/
if (exponent == 2) {
return square(result, base);
}
if ((exponent % 2) == 0) {
return new Constraint(ConstraintsName.POWER, new PropPowEven(result, base, exponent));
} else {
Expand All @@ -555,6 +558,23 @@ default Constraint pow(IntVar base, int exponent, IntVar result) {
}
}

default Constraint pow(IntVar base, IntVar exponent, IntVar result) {
if (exponent.isInstantiated()) {
return pow(base, exponent.getValue(), result);
}
// table decomposition todo as intension constraint
Tuples tuples = new Tuples(true);
for (int val1 : base) {
for (int val2 : exponent) {
int res = (int) Math.pow(val1, val2);
if (result.contains(res)) {
tuples.add(val1, val2, res);
}
}
}
return base.getModel().table(new IntVar[]{base, exponent, result}, tuples);
}

//##################################################################################################################
//TERNARIES ########################################################################################################
//##################################################################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package org.chocosolver.solver.expression.discrete.arithmetic;

import org.chocosolver.solver.Model;
import org.chocosolver.solver.constraints.extension.Tuples;
import org.chocosolver.solver.variables.IntVar;
import org.chocosolver.util.tools.VariableUtils;

Expand Down Expand Up @@ -106,19 +105,10 @@ public IntVar intVar() {
me = model.intVar(model.generateName("mod_exp_"), bounds[0], bounds[1]);
model.mod(v1, v2, me).post();
break;
case POW: // todo as intension constraint
case POW:
bounds = VariableUtils.boundsForPow(v1, v2);
me = model.intVar(model.generateName("pow_exp_"), bounds[0], bounds[1]);
Tuples tuples = new Tuples(true);
for(int val1 : v1){
for(int val2 : v2){
int res = (int)Math.pow(val1, val2);
if(me.contains(res)) {
tuples.add(val1, val2, res);
}
}
}
model.table(new IntVar[]{v1, v2, me}, tuples).post();
model.pow(v1, v2, me).post();
break;
case MIN:
bounds = VariableUtils.boundsForMinimum(v1, v2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public IntVar intVar() {
case SQR:
int[] bounds = VariableUtils.boundsForMultiplication(v, v);
me = model.intVar(model.generateName("sqr_exp_"), bounds[0], bounds[1]);
model.times(v, v, me).post();
model.square(me, v).post();
break;
default:
throw new UnsupportedOperationException("Unary arithmetic expressions does not support "+op.name());
Expand Down

0 comments on commit 1de7267

Please sign in to comment.