Discussion:
[gecode-users] Extra level of variables needed for count?
Neill Clift
2018-03-10 19:36:59 UTC
Permalink
Hi,
I want to restrict the values of an array to members of a multiset. This is a bit like distinct but can have repeated values.
So for example I want the values of b[0..7] to come from the multiset {5,5,5,4,3,2,1,0}. The b’s are essentially a permutation of the multiset
Count seems to be the way to achieve this but I have to add a whole new set of variables (the v’s below) that contain the counts of the multiplicities.
I cut out a bunch of stuff that’s not relevant to the code below so I hope it still makes sense.
bp.MaxBit is the number of distinct values in the multiset. And bp.Bits[i] is the multiplicity for the multiset value i.
Is this the expected way to do what I am trying to do here?
Thanks.
Neill.

public:
IntVarArray b;

public:
PartialOrderSort(LPTYPER n, BIT_PATTERN &bp, LPTYPER TopIndex) :
b(*this, n, 0, TopIndex)
{
IntVarArgs x(n);
IntVarArgs c((LPTYPER)bp.MaxBit);
for (int i = 0; i < n; i++) {
x[i] = b[i];
}
IntVarArray v(*this, (LPTYPER)bp.MaxBit, 0, n - 1);
for (int i = 0; i < bp.MaxBit; i++) {
c[i] = v[i];
rel(*this, v[i] == (LPTYPER)bp.Bits[i]);
}

count(*this, x, c, IPL_DOM);


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Christian Schulte
2018-03-12 09:31:26 UTC
Permalink
Hi,

I think you stopped reading a little too early. MPG says that you can also use integer sets instead of variables.

Then, in your example you do not need x and c, just pass b and v directly! IntVarArray is automatically casted to IntVarArgs.

Cheers
Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: users-***@gecode.org [mailto:users-***@gecode.org] On Behalf Of Neill Clift
Sent: Saturday, March 10, 2018 20:37
To: ***@gecode.org
Subject: [gecode-users] Extra level of variables needed for count?

Hi,
I want to restrict the values of an array to members of a multiset. This is a bit like distinct but can have repeated values.
So for example I want the values of b[0..7] to come from the multiset {5,5,5,4,3,2,1,0}. The b's are essentially a permutation of the multiset
Count seems to be the way to achieve this but I have to add a whole new set of variables (the v's below) that contain the counts of the multiplicities.
I cut out a bunch of stuff that's not relevant to the code below so I hope it still makes sense.
bp.MaxBit is the number of distinct values in the multiset. And bp.Bits[i] is the multiplicity for the multiset value i.
Is this the expected way to do what I am trying to do here?
Thanks.
Neill.

public:
IntVarArray b;

public:
PartialOrderSort(LPTYPER n, BIT_PATTERN &bp, LPTYPER TopIndex) :
b(*this, n, 0, TopIndex)
{
IntVarArgs x(n);
IntVarArgs c((LPTYPER)bp.MaxBit);
for (int i = 0; i < n; i++) {
x[i] = b[i];
}
IntVarArray v(*this, (LPTYPER)bp.MaxBit, 0, n - 1);
for (int i = 0; i < bp.MaxBit; i++) {
c[i] = v[i];
rel(*this, v[i] == (LPTYPER)bp.Bits[i]);
}

count(*this, x, c, IPL_DOM);


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Neill Clift
2018-03-12 16:29:30 UTC
Permalink
OK thanks for that. This makes the code simpler but I do still have to make a set of variables to contain the multiplicities (the v’s).
Of course they immediately become assigned to a single value. Would it be right in assuming the cost of that on the model is very small?

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Christian Schulte <***@kth.se>
Sent: Monday, March 12, 2018 2:31:26 AM
To: Neill Clift; ***@gecode.org
Subject: RE: Extra level of variables needed for count?

Hi,

I think you stopped reading a little too early. MPG says that you can also use integer sets instead of variables.

Then, in your example you do not need x and c, just pass b and v directly! IntVarArray is automatically casted to IntVarArgs.

Cheers
Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: users-***@gecode.org [mailto:users-***@gecode.org] On Behalf Of Neill Clift
Sent: Saturday, March 10, 2018 20:37
To: ***@gecode.org
Subject: [gecode-users] Extra level of variables needed for count?

Hi,
I want to restrict the values of an array to members of a multiset. This is a bit like distinct but can have repeated values.
So for example I want the values of b[0..7] to come from the multiset {5,5,5,4,3,2,1,0}. The b’s are essentially a permutation of the multiset
Count seems to be the way to achieve this but I have to add a whole new set of variables (the v’s below) that contain the counts of the multiplicities.
I cut out a bunch of stuff that’s not relevant to the code below so I hope it still makes sense.
bp.MaxBit is the number of distinct values in the multiset. And bp.Bits[i] is the multiplicity for the multiset value i.
Is this the expected way to do what I am trying to do here?
Thanks.
Neill.

public:
IntVarArray b;

public:
PartialOrderSort(LPTYPER n, BIT_PATTERN &bp, LPTYPER TopIndex) :
b(*this, n, 0, TopIndex)
{
IntVarArgs x(n);
IntVarArgs c((LPTYPER)bp.MaxBit);
for (int i = 0; i < n; i++) {
x[i] = b[i];
}
IntVarArray v(*this, (LPTYPER)bp.MaxBit, 0, n - 1);
for (int i = 0; i < bp.MaxBit; i++) {
c[i] = v[i];
rel(*this, v[i] == (LPTYPER)bp.Bits[i]);
}

count(*this, x, c, IPL_DOM);


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Christian Schulte
2018-03-12 16:31:36 UTC
Permalink
No, the point is to not use variables, you can use sets with a single element instead. Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: Neill Clift [mailto:***@live.com]
Sent: Monday, March 12, 2018 17:30
To: Christian Schulte <***@kth.se>; ***@gecode.org
Subject: RE: Extra level of variables needed for count?

OK thanks for that. This makes the code simpler but I do still have to make a set of variables to contain the multiplicities (the v's).
Of course they immediately become assigned to a single value. Would it be right in assuming the cost of that on the model is very small?

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Christian Schulte <***@kth.se<mailto:***@kth.se>>
Sent: Monday, March 12, 2018 2:31:26 AM
To: Neill Clift; ***@gecode.org<mailto:***@gecode.org>
Subject: RE: Extra level of variables needed for count?

Hi,

I think you stopped reading a little too early. MPG says that you can also use integer sets instead of variables.

Then, in your example you do not need x and c, just pass b and v directly! IntVarArray is automatically casted to IntVarArgs.

Cheers
Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: users-***@gecode.org<mailto:users-***@gecode.org> [mailto:users-***@gecode.org] On Behalf Of Neill Clift
Sent: Saturday, March 10, 2018 20:37
To: ***@gecode.org<mailto:***@gecode.org>
Subject: [gecode-users] Extra level of variables needed for count?

Hi,
I want to restrict the values of an array to members of a multiset. This is a bit like distinct but can have repeated values.
So for example I want the values of b[0..7] to come from the multiset {5,5,5,4,3,2,1,0}. The b's are essentially a permutation of the multiset
Count seems to be the way to achieve this but I have to add a whole new set of variables (the v's below) that contain the counts of the multiplicities.
I cut out a bunch of stuff that's not relevant to the code below so I hope it still makes sense.
bp.MaxBit is the number of distinct values in the multiset. And bp.Bits[i] is the multiplicity for the multiset value i.
Is this the expected way to do what I am trying to do here?
Thanks.
Neill.

public:
IntVarArray b;

public:
PartialOrderSort(LPTYPER n, BIT_PATTERN &bp, LPTYPER TopIndex) :
b(*this, n, 0, TopIndex)
{
IntVarArgs x(n);
IntVarArgs c((LPTYPER)bp.MaxBit);
for (int i = 0; i < n; i++) {
x[i] = b[i];
}
IntVarArray v(*this, (LPTYPER)bp.MaxBit, 0, n - 1);
for (int i = 0; i < bp.MaxBit; i++) {
c[i] = v[i];
rel(*this, v[i] == (LPTYPER)bp.Bits[i]);
}

count(*this, x, c, IPL_DOM);


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Neill Clift
2018-03-12 17:27:33 UTC
Permalink
Thanks.
I think I might now be seeing what you are saying. Sorry to be so stupid. I found this count stuff to be mind boggling.

IntSetArgs s((LPTYPER)bp.MaxBit);
for (LPTYPER i = 0; i < (LPTYPER)bp.MaxBit; i++) {
s[i] = IntSet((LPTYPER)bp.Bits[i], (LPTYPER)bp.Bits[i]);
}
count(*this, b, s, IPL_DOM);

I guess I was feeling I was using an overly more general mechanism with variables for counts when I have constants for counts. With the sets I am still using something which allows much more general situations but since it’s constants it might be optimized away.
So I assume this would be the recommended way to code this?
Neill.

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Christian Schulte <***@kth.se>
Sent: Monday, March 12, 2018 9:31:36 AM
To: Neill Clift; ***@gecode.org
Subject: RE: Extra level of variables needed for count?

No, the point is to not use variables, you can use sets with a single element instead. Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: Neill Clift [mailto:***@live.com]
Sent: Monday, March 12, 2018 17:30
To: Christian Schulte <***@kth.se>; ***@gecode.org
Subject: RE: Extra level of variables needed for count?

OK thanks for that. This makes the code simpler but I do still have to make a set of variables to contain the multiplicities (the v’s).
Of course they immediately become assigned to a single value. Would it be right in assuming the cost of that on the model is very small?

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Christian Schulte <***@kth.se<mailto:***@kth.se>>
Sent: Monday, March 12, 2018 2:31:26 AM
To: Neill Clift; ***@gecode.org<mailto:***@gecode.org>
Subject: RE: Extra level of variables needed for count?

Hi,

I think you stopped reading a little too early. MPG says that you can also use integer sets instead of variables.

Then, in your example you do not need x and c, just pass b and v directly! IntVarArray is automatically casted to IntVarArgs.

Cheers
Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: users-***@gecode.org<mailto:users-***@gecode.org> [mailto:users-***@gecode.org] On Behalf Of Neill Clift
Sent: Saturday, March 10, 2018 20:37
To: ***@gecode.org<mailto:***@gecode.org>
Subject: [gecode-users] Extra level of variables needed for count?

Hi,
I want to restrict the values of an array to members of a multiset. This is a bit like distinct but can have repeated values.
So for example I want the values of b[0..7] to come from the multiset {5,5,5,4,3,2,1,0}. The b’s are essentially a permutation of the multiset
Count seems to be the way to achieve this but I have to add a whole new set of variables (the v’s below) that contain the counts of the multiplicities.
I cut out a bunch of stuff that’s not relevant to the code below so I hope it still makes sense.
bp.MaxBit is the number of distinct values in the multiset. And bp.Bits[i] is the multiplicity for the multiset value i.
Is this the expected way to do what I am trying to do here?
Thanks.
Neill.

public:
IntVarArray b;

public:
PartialOrderSort(LPTYPER n, BIT_PATTERN &bp, LPTYPER TopIndex) :
b(*this, n, 0, TopIndex)
{
IntVarArgs x(n);
IntVarArgs c((LPTYPER)bp.MaxBit);
for (int i = 0; i < n; i++) {
x[i] = b[i];
}
IntVarArray v(*this, (LPTYPER)bp.MaxBit, 0, n - 1);
for (int i = 0; i < bp.MaxBit; i++) {
c[i] = v[i];
rel(*this, v[i] == (LPTYPER)bp.Bits[i]);
}

count(*this, x, c, IPL_DOM);


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Christian Schulte
2018-03-12 19:43:38 UTC
Permalink
Yep, that looks good. Gecode will choose the most efficient representation anyway at runtime. Cheers Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: Neill Clift <***@live.com>
Sent: 12 March 2018 18:28
To: Christian Schulte <***@kth.se>; ***@gecode.org
Subject: RE: Extra level of variables needed for count?

Thanks.
I think I might now be seeing what you are saying. Sorry to be so stupid. I found this count stuff to be mind boggling.

IntSetArgs s((LPTYPER)bp.MaxBit);
for (LPTYPER i = 0; i < (LPTYPER)bp.MaxBit; i++) {
s[i] = IntSet((LPTYPER)bp.Bits[i], (LPTYPER)bp.Bits[i]);
}
count(*this, b, s, IPL_DOM);

I guess I was feeling I was using an overly more general mechanism with variables for counts when I have constants for counts. With the sets I am still using something which allows much more general situations but since it’s constants it might be optimized away.
So I assume this would be the recommended way to code this?
Neill.

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Christian Schulte <***@kth.se<mailto:***@kth.se>>
Sent: Monday, March 12, 2018 9:31:36 AM
To: Neill Clift; ***@gecode.org<mailto:***@gecode.org>
Subject: RE: Extra level of variables needed for count?

No, the point is to not use variables, you can use sets with a single element instead. Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: Neill Clift [mailto:***@live.com]
Sent: Monday, March 12, 2018 17:30
To: Christian Schulte <***@kth.se<mailto:***@kth.se>>; ***@gecode.org<mailto:***@gecode.org>
Subject: RE: Extra level of variables needed for count?

OK thanks for that. This makes the code simpler but I do still have to make a set of variables to contain the multiplicities (the v’s).
Of course they immediately become assigned to a single value. Would it be right in assuming the cost of that on the model is very small?

Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10

________________________________
From: Christian Schulte <***@kth.se<mailto:***@kth.se>>
Sent: Monday, March 12, 2018 2:31:26 AM
To: Neill Clift; ***@gecode.org<mailto:***@gecode.org>
Subject: RE: Extra level of variables needed for count?

Hi,

I think you stopped reading a little too early. MPG says that you can also use integer sets instead of variables.

Then, in your example you do not need x and c, just pass b and v directly! IntVarArray is automatically casted to IntVarArgs.

Cheers
Christian

--
Christian Schulte, https://chschulte.github.io/
Professor of Computer Science, KTH, ***@kth.se<mailto:***@kth.se>
Expert Researcher, RISE SICS, ***@ri.se<mailto:***@ri.se>

From: users-***@gecode.org<mailto:users-***@gecode.org> [mailto:users-***@gecode.org] On Behalf Of Neill Clift
Sent: Saturday, March 10, 2018 20:37
To: ***@gecode.org<mailto:***@gecode.org>
Subject: [gecode-users] Extra level of variables needed for count?

Hi,
I want to restrict the values of an array to members of a multiset. This is a bit like distinct but can have repeated values.
So for example I want the values of b[0..7] to come from the multiset {5,5,5,4,3,2,1,0}. The b’s are essentially a permutation of the multiset
Count seems to be the way to achieve this but I have to add a whole new set of variables (the v’s below) that contain the counts of the multiplicities.
I cut out a bunch of stuff that’s not relevant to the code below so I hope it still makes sense.
bp.MaxBit is the number of distinct values in the multiset. And bp.Bits[i] is the multiplicity for the multiset value i.
Is this the expected way to do what I am trying to do here?
Thanks.
Neill.

public:
IntVarArray b;

public:
PartialOrderSort(LPTYPER n, BIT_PATTERN &bp, LPTYPER TopIndex) :
b(*this, n, 0, TopIndex)
{
IntVarArgs x(n);
IntVarArgs c((LPTYPER)bp.MaxBit);
for (int i = 0; i < n; i++) {
x[i] = b[i];
}
IntVarArray v(*this, (LPTYPER)bp.MaxBit, 0, n - 1);
for (int i = 0; i < bp.MaxBit; i++) {
c[i] = v[i];
rel(*this, v[i] == (LPTYPER)bp.Bits[i]);
}

count(*this, x, c, IPL_DOM);


Sent from Mail<https://go.microsoft.com/fwlink/?LinkId=550986> for Windows 10
Loading...