i am trying to make a model in minizinc that finds 3 numbers in sequence with the MaxSUM, in arrays below:
[2,3,4,**10,22,11**,17]).
[1,2,3,4,**10,22,11**,11,10,24]).
[2,3,4,5,10,23,**10,22,11**,17]).
I want my model to output these numbers, their indices and their sum.
I tried this:
array[int] of int : list1 = [2,3,4,10,22,11,17];
array[int] of int : list2 = [1,2,3,4,10,22,11,11,10,24];
array[int] of int : list3 = [2,3,4,5,10,23,10,22,11,17];
array[1..3] of var int: values;
array[1..3] of var int: indices;
constraint forall(i in 1..3, j in list1)(
values[i]=list1[j]
);
constraint exists (i in 1..length(list1)-2)(
exists(j in 1..length(list2)-2)(
exists(k in 1..length(list3)-2)
(list1[i]=list2[j]/\list2[j]=list3[k] /\
list1[i+1]=list2[j+1]/\list2[j+1]=list3[k+1]/\
list1[i+2]=list2[j+2]/\list2[j+2]=list3[k+2]
/\values[1]=list1[i]/\values[2]=list1[i+1]/\values[3]=list1[i+2]
/\indices[1]=i/\indices[2]=j/\indices[3]=k
)));
var int: max_sum;
constraint max_sum=sum(values);
solve maximize max_sum;
but, UNSATISFIABLE :(
Here are two hints.
Hint 1: The error states that there's something wrong at line 8, where j=3. If you comment out this constraint then there's no syntax error. Since I don't understand the purpose of this constraint I can't help you there.
Hint 2: Also, if you comment out that constraint (at line 8) then it might take a long time to get a solution - depending on the solver - since you have the decision variables values and indices set to var int. It's much better to state a positive domain for these two decision variables.
Related
I am new to constraint programming and to Minizinc.
I have look for a solution to this not relly hard task but I found nothing.
I want to count the number of different elements that appear in an array:
This is the declaration of my array:
array[1..n,1..n] of var 1..n: countLeft;
I have try to do like this:
constraint
forall(j in 1..n) (
length(array2set([countLeft[i,j]|i in 1..stopCountLeft[j]]) )==left_vision[j]
);
But apparently my array is of type: array[int]of var opt int and is not accept by the function array2set.
Any ideas?
There might be different approaches you could take, but an approach that is similar to what you try would be to split the counting of different elements in an array into two steps:
Counting the occurrence of the values in the domain.
Counting the amount of times the occurrence is higher than zero.
We can use the global global_cardinality constraint to count the occurrences and then use a simply count constraint over its result.
include "global_cardinality_fn";
array[1..n] of var int: occurrences = global_cardinality(YOURARRAY, [i | i in 1..n]);
var int: num_diff = count(o in occurrences) (o > 0);
Note, however, that this might not be the best code for your model. For some solvers global_cardinality might not be perform well enough. Similarly if your stopCountLeft contains variables, then that means that you are creating a array of optional variables, and global_cardinality might not be defined for optional variables.
Instead we can write an implication graph instead. The idea is still the same, but instead of counting the number of a value occurring, we just use a boolean value to signal wether the value is in use.
array[1..n] of var bool: occurs;
constraint forall(i,j in 1..n) (YOURARRAY[i] = j -> occurs[j]);
var int: num_diff = count(occurs);
Note that the problem with this approach is the exponential number of implications posted in the forall loop. However, I suspect that, with implications being small, it would perform reasonably well.
In MiniZinc 2.5.0, you can do something like this:
array[int, int] of int: a =
[| 1, 1,
| 2, 3,
| 3, 4 |];
set of int: Rows = index_set_1of2(a);
set of int: Cols = index_set_2of2(a);
int: values = card({ a[r, c] | r in Rows, c in Cols });
output ["\(values) values in "] ++
[if c == 1 then "\n" else "" endif ++
"\(a[r, c]) " | r in Rows, c in Cols];
I am trying to find solutions to a matrix where I know the row and column sums and the maximum value a cell can have. I want to find possible solutions that are within the constraints. I've already tried various things like constructing an array of all cell values and picking picking from each cell in sequence but whatever I try I always run into the problem where I run out of values for a cell.
I also tried a recursive algorithm but that I only managed to get the first result or it failed to get any solution. I think I have to do this with a backtracking algorithm? Not sure...
Any help or pointers would be appreciated.
Row sums A, B, C, column sums X, Y, Z as well as the maximum value for each ? are known. All values are are positive integers.
C1 | C2 | C3
-----------------
R1 | ? | ? | ? | A
-----------------
R2 | ? | ? | ? | B
-----------------
R3 | ? | ? | ? | C
-----------------
X | Y | Z
If you heard about linear programming (LP) and its 'cousins' (ILP, MILP), that could be a good approach to help you solve your problem with a great efficiency.
A linear program consists in a set of variables (your matrix unknowns), constraints (maximum values, sum of rows and columns), and an objective function (here none) to minimize or maximize.
Let's call x[i][j] the values you are looking for.
With the following data:
NxM the dimensions of your matrix
max_val[i][j] the maximum value for the variable x[i][j]
row_val[i] the sum of the values on the row i
col_val[j] the sum of the values on the column j
Then a possible linear program that could solve your problem is:
// declare variables
int x[N][M] // or eventually float x[N][M]
// declare constaints
for all i in 1 .. N, j in 1 .. M, x[i][j] <= max_val[i][j]
for all i in 1 .. N, sum[j in 1 .. M](x[i][j]) == row_val[i]
for all j in 1 .. M, sum[i in 1 .. N](x[i][j]) == col_val[j]
// here the objective function is useless, but you still will need one
// for instance, let's minimize the sum of all variables (which is constant, but as I said, the objective function does not have to be useful)
minimize sum[i in 1 .. N](sum[j in 1 .. M](x[i][j]))
// you could also be more explicit about the uselessness of the objective function
// minimize 0
Solvers such as gurobi or Cplex (but there are much more of them, see here for instance) can solve this kind of problems incredibly fast, especially if your solutions do not need to be integer, but can be float (that makes the problem much, much easier). It also have the advantage to not only be faster t execute, but faster and simpler to code. They have APIs in several common programming languages to ease their use.
For example, you can reasonably expect to solve this kind of problem in less than a minute, with hundreds of thousands of variables in the integer case, millions in the real variables case.
Edit:
In response to the comment, here is a piece of code in OPL (the language Cplex and other LP solvers use) that would solve your problem. We consider a 3x3 case.
// declare your problem input
int row_val[1..3] = [7, 11, 8];
int col_val[1..3] = [14, 6, 6];
int max_val[1..3][1..3] = [[10, 10, 10], [10, 10, 10], [10, 10, 10]];
// declare your decision variables
dvar int x[1..3][1..3];
// objective function
minimize 0;
// constraints
subject to {
forall(i in 1..3, j in 1..3) x[i][j] <= max_val[i][j];
forall(i in 1..3) sum(j in 1..3) x[i][j] == row_val[i];
forall(j in 1..3) sum(i in 1..3) x[i][j] == col_val[j];
}
The concept of a LP solver is that you only describe the problem you want to solve, then the solver solves it for you. The problem must be described according to a certain set of rules. In the current case (Integer Linear Programming, or ILP), the variables must all be integers, and the constraints and objective function must be linear equalities (or inequalities) with regards to the decision variables.
The solver will then work as a black box. It will analyse the problem, and run algorithms that can solve it, with a ton of optimizations, and output the solution.
As you wrote in a comment, that you want to come up an own solution, here's some guideline:
Use a Backtrack algorithm to find a solution. Your value-space consists of 3*3=9 independent values, each of them are between 1 and maxval[i][j]. Your constraints will be the row and column sums (all of them must match)
Intitalize your space with all 1s, then increment them, until they reach the maxval. Evaluate the conditions only after each value is covered for that condition (particularly, after 3 values you can evaluate the first row, after 6 the second row, after 7 the first col, after 8 the second col, and after 9 the third row and the third col)
If you reach the 9th, with all conditions passing, you've got a solution. Otherwise try the values from 1 till maxval, if neither matches, step back. If the first value was iterated through, then there's no solution.
That's all.
More advanced backtracking:
Your moving values are only the top-left 2*2=4 values. The third column is calculated, the condition is that it must be between 1 and the maxval for that particular element.
After defining the [1][1] element, you need to calculate the [2][2] index by using the column sum, and validate its value by the row sum (or vica versa). The same processing rules apply as above: iterate through all possible values, step back if none matches, and check rules only if they can be applied.
It is a way faster method, since you have 5 bound variables (the bottom and right rows), and only 4 unbound. These are optimizations from your particular rules. A bit more complex to implement, though.
PS: 1 is used because you have positive integers. If you have non-negative integers, you need to start with 0.
I am working on the guide.elm-lang website on the Random example.
I am trying to add a feature that shows you the total number of times you threw the dice, and some stats on how many times you got each face.
To do this I've changed the model to look like this:
type alias Model =
{
die_face : Int,
total_throws : Int,
stats: Array.Array Int
}
and this is what I do to update the model:
{ model |
die_face = face,
total_throws = model.total_throws + 1,
stats = Array.set face ((Array.get face model.stats) + 1) model.stats
}
this throws an error that tells me:
This get call produces:
#Maybe# Int
But (+) only works with #Int# and #Float# values.
Which refers to Array.get not returning a Int but a Maybe and therefore I can't add it to the number 1.
I have tried using lists to achieve the same purpose but since they are not indexed I am not sure what to increment when I map over it. I am thinking of using records to do this and figure out a way to map the record keys to the face Int.
In general my question is. What is a good method to increment an element at index [x] of an Array in elm ? Or if I am just thinking about this wrong, what would be the elm way ?
Array.get returns a Maybe because it has to account for the case where the index is outside the array. The simplest way of getting around that is using Maybe.withDefault with a reasonable default value:
Array.set face (((Array.get face model.stats) |> Maybe.withDefault 0) + 1) model.stats
It might be a good idea to write a helper function for this though, to clean up the code a bit. Something like this:
incrementAt : Int -> Array Int -> Array Int
incrementAt index array =
case Array.get index array of
Just value ->
Array.set index (value + 1) array
Nothing ->
array
You can also use Array.Extra.update if you don't mind the extra dependency.
I would like to know if it’s possible to have array of (multidimensional) array in MiniZinc language.
Indeed, I would like to resolve a timetabling problem with workers. My goal is to check if they are available at least 1 day per week. Each worker is indexed by an integer and I have their schedule per week.
For me, an array like : [[number_of_week, weekday]] could be a solution.
For example, worker 1 who is available monday/friday in week 1 and tuesday/thursday in week 2 can be modeled by the following array : (« 1 » means that the worker is available)
[[| 1,0,0,0,1,
| 0,1,0,1,0 |],
[...], ...]
If it's possible, how to declare this kind of array ? And I’m also open to any advice on modeling this constraint.
Sorry for my imperfect English and thank you in advance,
Nicolas.
I'm not sure I understand your question fully, but you can have multi dimensional arrays as follows:
array[1..3,1..3] of int: a = array2d(1..3,1..3,
[1,0,0,
0,1,0,
1,1,0]);
or using another syntax:
array[1..3,1..3] of int: a =
[|1,0,0
|0,1,0
|1,1,0|];
However, you cannot have arrays in arrays, i.e. this is not allowed:
% This is NOT valid MiniZinc code!
array[1..3,1..3] of int: a =
[[[1,0,0],[1,0,1]],
[[0,1,0],[0,1,1]],
[[1,1,0],[0,0,0]]];
Note also that one cannot have "ragged" arrays: the number of element in each row and each column must be the same.
Regarding you specific constraint you might - if I understand your description - define a 3D array like this:
int:_num_workers = 2;
int: num_weeks = 3;
int: num_days = 4;
array[1..num_workers,1..num_weeks,1..num_days] of int: a =
array3d(1..num_workers,1..num_weeks,1..num_days,
[
% worker 1
% week1 week2 week3
1,0,0,1, 1,1,0,1, 0,0,1,1,
% worker 2
% week1 week2 week3
1,0,0,0, 0,0,0,1, 1,1,1,0
]);
Note especially the array3d construct which creates a 3D array. So you can access day d of week e for worker w with a[w,e,d].
I have an array: array[backpacks] of int: capacity specifying the capacity of each backpack.
Now I want to create an array of variables which the constraint solver will have to satisfy. I want each variable to take values in domain 1..capacity where capacity corresponds to the one specified in the above array.
Would something like this work: array[backpacks] of var capacity: bagcaps ?
Or do I have to do something like: array[backpacks] of var 1..MAX: bagcaps
and then add constraints: constraint forall(i in backpacks) bagcaps[i] <= capacity[i] ?
Thank you.
There is no short cut to restrict the domain of specific element in the array declaration. The traditional version is the one you wrote last:
constraint forall(i in backpacks) bagcaps[i] <= capacity[i]);
However, you can make this as an predicate (and place it in a separate file which is then imported into the model with include). E.g. some thing like this:
set of int: backpacks = 1..6;
array[backpacks] of int: capacity = [10,4,3,7,5,3];
array[backpacks] of var 1..max(capacity): bagcaps;
solve satisfy;
predicate restrict_domains(array[int] of var int: x, array[int] of int: d) =
forall(i in index_set(x)) ( x[i] <= d[i] );
constraint
% forall(i in backpacks) ( bagcaps[i] <= capacity[i] ) % original
restrict_domains(bagcaps,capacity)
;
% output [];
Note that you must still use restrict_domains as a constraint. And I recommend that you always restrict the domain in the declaration as much as possible, i.e. use the declaration using var 1..max(capacity) instead of var int.