Menu

how to define this jacop constraint

shahab
2014-06-30
2014-08-14
  • shahab

    shahab - 2014-06-30

    could anyone help me define the following constraint. i have a matrix m[n][n] in which each m[i][j] is a domain variable of type IntVar. Now i want to define the following constraint:
    m[m[i][j]][k] = X (X is any integer value)
    i have not been able to define this constraint as index of a matrix should be a regular integer whereas in my constraint the first index m[i][j] in m[...][k] is a domain variable.
    will really appreciate any help.

     
  • kris

    kris - 2014-07-01

    Hi!

    No, you cannot use element constraint in m[m[i][j]][k] = X since the first index m[i][j] selects a vector and then integer index k is supposed to select a variable. Try to rewrite matrix m and then use indexing of the form m[k][m[i][j]]. In this case, integer index k will select a vector and then index m[i][j] can be used in element constraint.

    Best,
    /Kris

     
    • shahab

      shahab - 2014-07-01

      thanks Kris very much for your reply. basically whether i use m[m[i][j]][k] or m[k][m[i][j]], since m[i][j] is a domain variable of type IntVar, it won't allow me to use it as an index in m again. i am getting the following error:
      incompatible types: store.impose(new XeqC(m[k][m[i][j]], i));
      required: int
      found: IntVar

       
  • kris

    kris - 2014-07-02

    You should use element constraint with code that looks like that

    IntVar[] x = m[k];
    store.impose(new Element(m[i][j], x, result));
    store.impose(new XeqC(result, i));

    Alternatively, you can flatten your two dimensional array into one dimensional vector, compute (using constraints) your index and then use element constraint. I mean that your index will be something line that index = N*k + m[i][j] (!!! this has to be done with constraints.

    /Kris

     
    • shahab

      shahab - 2014-07-03

      thanks a lot Kris. Let me use Element constraint. Hopefully it will solve the issue i am facing.

      shahab

       
  • shahab

    shahab - 2014-08-14

    Hi,
    I tried the element constraint and it works perfectly. However, I have two other questions and i hope you can help me out.
    1) I implemented a constraint with and without using Element constraint. I was hoping Element constraint would speed up the performance, but I am getting results in the same time. This is how I am getting the matrix[i][j] value at run time without Element contraint:
    //there are two loops here for i and j that scans the entire matrix
    for(int m = 0; m < n; m++) //this loop is the 3rd nested loop within i and j
    store.impose(new IfThen(new XeqC(matrix[i][j], m), new XeqC(matrix[m][3], 5)));
    Here, I am getting the matrix value matrix[i][j] in m and then I use this m as index in matrix[m][3].
    Now, when I use Element constraint, the for loop for index m is not needed, so I hoped it will speed up the Jacop results, but I got results in the same time in either case.

    2) I have a matrix[][] of nxn domain variables in which I applied many constraints and after I ran Jacop I got the answer within T seconds. Now, when I reduced some of the domains of these nxn variables, say initially each element matrix[i][j] has a domain value 0--->n, but in my second experiment, i reduced the domain to 2---n for instance. I was expecting reduced time when running Jacop with reduced domain values. But i am getting same running time T. By the way I am also using AllDiff constraint so that each row and column of matrix is all different numbers from 0 to n. I am wondering what could be the possible explanation that reduced values in domain variables is not making Jacop produce faster results.

    many thanks,
    Shahab

     
  • kris

    kris - 2014-08-14

    Hi!

    I do not know why the time is similar but I can guess a little bit. First, the element constraint is in fact implemented for two different situations. One for the list of integers and the other one for the list of IntVar. As I see your description your constraint is the list of IntVar.This constraint is rather complicated since it need to propagate in all directions from index to the value from value to the index and finally to variables on the list. It needs to do it every time any variable changes. In some cases it is a useless job since there will not be any change at all in domains of variables. On the other hand IfThen constraint is directional. It basically checks whether the first constraint is satisfied and enforces consistency of the second constraint if necessary. Otherwise it does nothing more. In some situations it might be more efficient. Specially for large sizes of the matrix from your example.

    BTW, note that the index in element constraint starts at one, if there is no offset specified.

    Best regards,
    /Kris

     

Log in to post a comment.