Menu

Arrays with same index

Help
2020-03-24
2020-03-24
  • Adriano Cavalheiro Marchesan

    Hi all

    Could anyone help me define the following constraint: I have two coefficient arrays (float) and i need their indexes to change together.

    For example:
    IntVar index = new IntVar(store, 1, 3);
    FloatVar var1 = new FloatVar(store, 3.0, 5.0);
    FloatVar var2 = new FloatVar(store, 3.0, 8.0);
    FloatVar mult = new FloatVar(store, -100.0, 100.0);

    double[] array1 = {3.0, 4.0, 5.0};
    double[] array2 = {6.0, 8.0, 3.0};

    store.impose(new ElementFloat(index, array1, var1));
    store.impose(new ElementFloat(index, array2, var2));
    store.impose(new PmulQeqR(var1, var2, mult)); // var1 * var2 = mult

    I need the answer like that:
    mult = 3.0 x 6.0, 4.0 x 8.0, 5.0 x 3.0 (element by element: position 1 with 1, 2 with 2 and 3 with 3)

    In my model, I have several operations and I need the vector indices to change together always.

    Thank you for your help!

     

    Last edit: Adriano Cavalheiro Marchesan 2020-03-24
  • kris

    kris - 2020-03-24

    Hi!

    I am not sure if I understand your question but if you need the arrays indexes to change together you simply use the same index for both Element constraints. In such case you will get the answer you ask for. The main point is that you need to define your search to search for all solutions and JaCoP will find 18.0 then 32.0 and finally 15.0.
    Bset,
    /Kris

     
  • kris

    kris - 2020-03-24

    Hi!

    I am not sure if I understand your question but if you need the arrays indexes to change together you simply use the same index for both Element constraints. In such case you will get the answer you ask for. The main point is that you need to define your search to search for all solutions and JaCoP will find 18.0 then 32.0 and finally 15.0.
    Bset,
    /Kris

     
  • Adriano Cavalheiro Marchesan

    Hi Kris!

    Thanks for your answer! I summarized my problem, maybe it got confused

    I used the same index on both Element constraints:

    IntVar index = new IntVar(store, 1, 3);
    FloatVar var1 = new FloatVar(store, 3.0, 5.0);
    FloatVar var2 = new FloatVar(store, 3.0, 8.0);
    FloatVar mult = new FloatVar(store, “mult”, -100.0, 100.0);

    double[] array1 = {3.0, 4.0, 5.0};
    double[] array2 = {6.0, 8.0, 3.0};

    store.impose(new ElementFloat(index, array1, var1));
    store.impose(new ElementFloat(index, array2, var2));

    store.impose(new PmulQeqR(var1, var2, mult)); // var1 * var2 = mult

    result = store.consistency();
    if (result){
    System.out.println(mult)
    }

    but my answer is:
    mult::{9.0..40.0}

    This shows what JaCoP is doing:
    array1[1] x array2[3] = 9.0 (min domain)
    array1[3] x array2[2] = 40.0 (max domain)

    I expected the answer:
    mult::{15.0..32.0}
    array1[3] x array2[3] = 15.0 (min domain)
    array1[2] x array2[2] = 32.0 (max domain)

    That is, values less than 15 or greater than 32 should not be in my solution space.

    Thanks for your attention!

     
  • kris

    kris - 2020-03-24

    Hi!

    As I wrote in the previous mail, you need to do search. It is not enough to do consistency. The solver does not "see" that the index is the same in both ElementFloat constraints when pruning domains.

    Possible search can look as follows.

    DepthFirstSearch<FloatVar> label = new DepthFirstSearch<FloatVar>();
    SplitSelectFloat<FloatVar> s = new SplitSelectFloat<FloatVar>(store, new FloatVar[] {var1, var2, mult}, null);
    
    label.setSolutionListener(new PrintOutListener<FloatVar>());
    label.getSolutionListener().searchAll(true);
    
    label.labeling(store, s);
    

    /Kris

     
  • Adriano Cavalheiro Marchesan

    I understand now!

    Thank you very much Kris!

     

Log in to post a comment.