0

I'm new to constraint programming and toying around with some basic operations. I want to count the number of occurrences of an arbitrary element x in an array of pairs.

For instance, the following array has 2 eights, and 1 of every other element.

sampleArray = [{8,13}, {21,34}, {8,55}]

I wonder how I am to extract this information, possibly using built-in functions.

1 Answers1

0

I'm not sure I understand exactly what you want to do here. Do you want to count only the first element in the pair?

Note that the example you show is an array of sets, not a 2 dimensional matrix. Extracting and count the first(?) element in each pair is probably easier if you have a two dimensional matrix (constructed with array2d).

In general there are at least two global constraints that you can use for this: "count" and perhaps also "global_cardinality". See http://www.minizinc.org/2.0/doc-lib/doc-globals-counting.html

hakank
  • 6,629
  • 1
  • 17
  • 27
  • Clarification: I want to get the number of sets which contain a certain number, say 8. So the example above would return 2. –  Apr 30 '16 at 15:29
  • Let "int: n = 8;" (the number you want to check). Then the number of occurrences of "n" in the "sampleArray" pairs is: sum([n in sampleArray[i] | i in 1..3 ]). – hakank May 01 '16 at 04:56
  • I have some [code](http://pastebin.com/Z6PTrCU6) with one constraint. However, it's not working as intended. When using the input: solutions=5; solution_lab=[1,1,2,2,2]; solution_group=[{1,2},{3,4},{5,6},{1,2,3},{7,8}]; teachers=2; timeslots=3; I get: Time Teacher Lab Students 1: 1 1 1 1..2 2: 1 1 1 3..4 3: 1 1 2 5..6 4: 1 1 2 1..3 5: 1 1 2 7..8 But I want to merge the sets of students who have done a lab (so the last column would have 1..4 on rows 1 and 2, and {1,2,3,5,6,7,8} on rows 3-5). What should I do? –  May 06 '16 at 21:06