Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.

 View Only
Expand all | Collapse all

Constrain Intersection of two var arrays to be of given size

  • 1.  Constrain Intersection of two var arrays to be of given size

    Posted Tue November 07, 2017 04:00 AM

    Originally posted by: Fab10


    Hello,

    I'm new to ILOG CP optimizer and to constraint programming in general and I'm trying to model and solve a simple problem.

    I'm looking for a special construct or a method to let me constrain the intersection of two int var arrays to be of a given size, something like

    Intersection(intVarArray vars1, intVarArrays vars2, iloInt size)

    In other words, the cardinality of the intersection of vars1 and vars2 should be  exactly of size "size".

    Is there a specific constraint or should I have to construct it?

     

    Thank you for your help.

    Fabio


    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Constrain Intersection of two var arrays to be of given size

    Posted Thu November 09, 2017 05:00 AM

    Hi,

    let me give you 2 starting points in OPL

    If you use arrays then you could start with

    using CP;

    dvar int A[1..5] in 1..10;
    dvar int B[1..5] in 1..10;

    dvar int cardIntersection;

    subject to
    {
    cardIntersection==sum(i in 1..5) and(k in 1..i-1) ((A[i]!=A[k]) &&  or(j in 1..5) (A[i]==B[j]));
    cardIntersection==3;
    }

    and if you manage to have a set like description:

    using CP;

    dvar boolean inA[1..10];
    dvar boolean inB[1..10];

    subject to
    {
    3==sum(i in 1..10) ((inA[i]==1) && (inB[i]==1));
    }

    regards


    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: Constrain Intersection of two var arrays to be of given size

    Posted Thu November 09, 2017 07:35 AM

    Originally posted by: Fab10


    Dear ,

    than you for your answer.

    I'm not using OPL but the C++ API of CP.

    I am in trouble in "translating" your code with the C++ API, especially the part "((A[i]!=A[k]) &&  or(j in 1..5) (A[i]==B[j]))".

    Could you please show me how I can deal with it.

    Thank you.

    Cheers


    #CPOptimizer
    #DecisionOptimization


  • 4.  Re: Constrain Intersection of two var arrays to be of given size

    Posted Thu November 09, 2017 07:54 AM

    Hi,

    first do you know you could call OPL from C++ ?

    second, you could try with OPL that is part of CPLEX Optimization Studio and translate to C++ when it is ok

    third, you could do all this in C++

    See

     

    regards


    #DecisionOptimization


  • 5.  Re: Constrain Intersection of two var arrays to be of given size

    Posted Mon November 13, 2017 07:48 AM

    Originally posted by: Fab10


    Thank you very much for your reply and patience.

     

    Fabio


    #CPOptimizer
    #DecisionOptimization