0

I am trying to build a model with a parameter n = 5 which represent the length of an array. The model also have a decision variable that takes the range 0..9 and involves two constraints. The first of all is that the sum of the values of the array must be equal to the product of values of this array and the last one must be that the order of the values should be sorted increasing.

I attach the code:

int: n = 5;    % Array length
var 0..9: x;

array[1..n] of var 1..n: V;

% Constraints
constraint sum(V) = product(V);    
constraint increasing(array[1..n] of var 1..n: V);    % Sort problem

solve satisfy;
output[show(V)];

2 Answers 2

4

If you want to add the constraint that the array V is ordered, you can use the increasing constraint from the library

include "globals.mzn";
% ...
constraint increasing(V);

You must also include the file "globals.mzn" which includes the definition of increasing.

The complete model is thus:

include "globals.mzn";
int: n = 5;    % Array length
var 0..9: x;

array[1..n] of var 1..n: V;

% Constraints
constraint sum(V) = product(V);    
constraint increasing(V);

solve satisfy;
output[show(V)];
Sign up to request clarification or add additional context in comments.

Comments

2

When possible, using an existing constraint as indicated in the accepted answer is great. If there is no available constraint, it can be good to also know how to specify your own predicate.

A complete model including your own specification of increasing could look like the following.

int: n = 5;    % Array length
var 0..9: x;

array[1..n] of var 1..n: V;

predicate increasing(array[1..n] of var 1..n: V) =
    forall(i in 1..n-1) (
        V[i] <= V[i+1]
    );

% Constraints
constraint sum(V) = product(V);    
constraint increasing(V);

solve satisfy;
output[show(V)];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.