[gecode-users] Sum of IntVarArrays with changing array positions

Guido Tack tack at gecode.org
Wed Oct 12 11:26:05 CEST 2011


Hi Mathias,

you can express the constraint using element constraints.  Say the two sequences are x and y, you create two new sequences xx and yy (with appropriate length) and then post the following constraints:

for (int i=0; i<x.size(); i++) post(home, x[i] == element(xx,m+i))
for (int i=0; i<y.size(); i++) post(home, y[i] == element(yy,n+i))

for (int i=0; i<xx.size(); i++) post(home, xy[i] = x[i]+y[i]);

If you need to constrain the "empty" positions to 0 (e.g. because your optimization function doesn't do that automatically), you can do that in addition:

for (int i=0; i<xx.size(); i++) post(home, (i < m || i > m+x.size()) >> xx[i]==0));
for (int i=0; i<yy.size(); i++) post(home, (i < n || i > n+y.size()) >> yy[i]==0));

I didn't test the code, but I hope you get the idea.

Christian's email just arrived - I think my solution is different in that the time series can be variables, which means you can't use regular.  I understand that correctly from your description?

Cheers,
Guido

On 12 Oct 2011, at 10:57, Mathias Dalheimer wrote:

> Hi,
> 
> I'm having trouble expressing the following constraints: I have two
> IntVarArrays which contain short timeseries (i.e. consumption values for
> one hour, while the remaining hours of one day are empty/not
> constrained). The *position* of the constraints of the consumption can
> move within the day, this is governed by two IntVars (m, n) (which are
> my optimization goal, so the cost function is calculated depending on
> the position variables). The underlying question is where to place the
> consumption in order to optimize some cost function.
> 
> Now I have some other constraints that depend on the position m and n of
> the consumption. I need to sum the timeseries. So, as an example, I have
> something like this (. denotes no constraint):
> 
> 
> . . . 1 2 1 2 3 . . . . . . . . .   // m=3
> . . . . . . . 2 4 5 6 5 . . . . .   // n=8
> 
> Now my question: How do I encode a constraint that depends on the sum of
> these two IntVarArrays while m and n (the position) can change?
> 
> So, in the previous example, this would be:
> 
> . . . 1 2 1 2 3 . . . . . . . . .   // m=3
> . . . . . . . 2 4 5 6 5 . . . . .   // n=8
> . . . 1 2 1 2 5 4 5 6 5 . . . . .   // sum
> 
> In another example with different m and n:
> 
> 1 2 1 2 3 . . . . . . . . . . . .  // m=0
> . . . . . . . 2 4 5 6 5 . . . . .  // n=8
> 1 2 1 2 3 . . 2 4 5 6 5 . . . . .  // sum
> 
> Different m's and n's are being evaluated and an optimal set of m and n
> is the result of the optimization.
> 
> Thanks for any pointers,
> -Mathias
> 
> _______________________________________________
> Gecode users mailing list
> users at gecode.org
> https://www.gecode.org/mailman/listinfo/gecode-users

-- 
Guido Tack, http://people.cs.kuleuven.be/~guido.tack/








More information about the users mailing list