[gecode-users] Re: Var indexes-help

Mikael Zayenz Lagerkvist zayenz at gmail.com
Tue Jan 9 08:10:37 CET 2007


On 1/8/07, penche <penche21 at hotmail.com> wrote:
> <pekczynski at ...> writes:
>
> >
> > Hi there,
> > I think using the element constraint is the correct choice.
> > Maybe the little example I attached will help you out.
> > At least you can use the element constraint to model
> >
> > B[C[i-1]]=A[i]
> >
> > and indeed the alg also does
> >
> > A[i]=B[C[i-1]]
> >
> > see http://www.gecode.org/gecode-doc-latest/group__TaskIntIntElement.html
> >
> > > 2) I also tried 'element' to express this constraint
> > >
> > > Int Var x;
> > > element(this, B, C.get(i - 1), x);
> > > rel(this,A.get(i),IRT_EQ,x);
> >
> > So this was quite right, but you can directly use
> >
> >  element(this, B, C[i - 1], A[i]);
> >
> > so you don't need the rel constraint.
> >
> > If there are any further questions, let us know.
> > :-D
> > Cheers
> > Patrick
> >
> >
> > Attachment (arrayex.cc): application/octet-stream, 1932 bytes
> >
> > _______________________________________________
> > Gecode users mailing list
> > users at ...
> > https://www.gecode.org/mailman/listinfo/gecode-users
> >
>
> Thank you, I now see rel is useless and element is just enough.
> But one point is; we will need Var indexes inevitable when the relations
> get nested.
>
> For example; A[i] = B [ C [A[j]] ]
>
> 1) obviously rel does not work.
> rel(this, A.get(i), IRT_EQ, B.get(C.get(A.get(j))))
>
> 2) I tried to introduce new Vars, but below did not work also.
>
> IntVar intermediate1= new IntVar(this,min,max);
> IntVar intermediate2= new IntVar(this,min, max);
>
> element(this, C, A.get(j), intermediate1);
> element(this, B, intermediate1, intermediate2);
>
> rel(this, A.get(i), IRT_EQ, intermediate2);
>
> 3) Any way to write this constraint?

The element constraint implements A[I]=V where A is a VarArray of
integer variables and I and V are integer variables. If you want your
index variable to be looked up in another variable array, then this is
another element constraint. Thus something like 2 is the correct way
to model your desired constraints.

One thing to note is that you can skip intermediate2, and directly
replace it with A.get(i). This gives the following code:

  IntVar intermediate1= new IntVar(this,min,max);
  element(this, C, A.get(j), intermediate1);
  element(this, B, intermediate1, A.get(j));

this implements B[C[A[i]]] = A[j]. The only direct reason I see that
this would not work is if min and max are wrong. Safe, unconstraining
bounds are min=0 and max=B.size().

Cheers,
Mikael

-- 
Mikael Zayenz Lagerkvist, http://www.ict.kth.se/~zayenz/




More information about the gecode-users mailing list