Skip to content

add table reformulation for POW + optimized tuples generation#1184

Merged
cprudhom merged 5 commits intodevelopfrom
JG/pow
Mar 10, 2026
Merged

add table reformulation for POW + optimized tuples generation#1184
cprudhom merged 5 commits intodevelopfrom
JG/pow

Conversation

@jgFages
Copy link
Copy Markdown
Contributor

@jgFages jgFages commented Mar 2, 2026

No description provided.

@jgFages jgFages requested a review from cprudhom March 2, 2026 23:13
@jgFages
Copy link
Copy Markdown
Contributor Author

jgFages commented Mar 2, 2026

We could also replace

public static Tuples scalar(IntVar[] VARS, final int[] COEFFS, IntVar SCALAR, final int SCALAR_COEFF) {
Tuples left = generateTuples(TupleValidator.TRUE, true, VARS);
Tuples tuples = new Tuples(true);
int n = VARS.length;
for (int[] tleft : left.tuples) {
int right = 0;
for (int i = 0; i < n; i++) {
right += tleft[i] * COEFFS[i];
}
if (right % SCALAR_COEFF == 0 && SCALAR.contains(right / SCALAR_COEFF)) {
int[] t = new int[n + 1];
System.arraycopy(tleft, 0, t, 0, n);
t[n] = right / SCALAR_COEFF;
tuples.add(t);
}
}
return tuples;
}

by 

public static Tuples scalar(IntVar resultVar, final int resultCoef, IntVar[] VARS, final int[] COEFFS) {
    return generateTuplesWithResult(
            resultVar,
            values -> IntStream.range(0, values.length).map(i -> values[i] * COEFFS[i]).sum() % resultCoef == 0,
            values -> IntStream.range(0, values.length).map(i -> values[i] * COEFFS[i]).sum() / resultCoef,
            VARS
    );
}

for code maintenance but performance might be lower so I did not include it

*/
public static Tuples eucl_div(IntVar DIVIDEND, IntVar DIVISOR, IntVar RESULT) {
return generateTuples(values -> values[0] / values[1] == values[2], true, DIVIDEND, DIVISOR, RESULT);
public static Tuples eucl_div(IntVar result, IntVar dividend, IntVar divisor) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little bit dangerous to change the order of the parameters, isn't it ?

Copy link
Copy Markdown
Contributor Author

@jgFages jgFages Mar 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good remark. The problem is that the result is currently not always in the same position (last argument for modulo, div and times but first position for square) so we would need to make some change anyway if we want to have a clear convention (either all first or all last). I chose the result position at first to allow an IntVar... signature for the other variables (in generateTuplesWithResult and canBeTupledWithResult) :

if (TuplesFactory.canBeTupledWithResult(result, base)) {
       return table(new IntVar[]{result, base}, TuplesFactory.square(result, base));
}

but we may prefer using an explicit array IntVar[] in which case both positions would be fine (yet a convention either first or last would help avoiding errors).

Maybe we could simply rename TuplesFactory to make sure the code is reviewed upon solver update ?

We can also keep existing signatures and be cautious when calling generateTuplesWithResult.

Let me know what you prefer or if you have other suggestions

@cprudhom cprudhom merged commit 732aeea into develop Mar 10, 2026
21 checks passed
@jgFages jgFages deleted the JG/pow branch March 10, 2026 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants