Decision Optimization

Decision Optimization

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

 View Only
Expand all | Collapse all

Modeling Count (Cardinality) Constraint .Net

  • 1.  Modeling Count (Cardinality) Constraint .Net

    Posted Tue August 13, 2013 09:17 AM

    Originally posted by: thisisNeil


    Hi All,

    The following snippet taken from the IBM docs is exactly the sort of thing I've been asked to model within our .Net (c-sharp) application.

    Would someone please be able to provide some guidance on how to build it up?  Specifically how each of the x[i] > 20 tests can be constructed and then summed within the LHS of the constraint.  I've seen references to indicator vars in other APIs and I think that's the area I need to head in, but can find the .Net equivalent.

    Thank you,

    Neil

    IloNumVarArray x(env, 3, 0, 1000);
    model.add((x[0] >= 20) + (x[1] >= 20) + (x[2] >= 20) >= 2);
    

    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: Modeling Count (Cardinality) Constraint .Net

    Posted Thu August 15, 2013 11:37 AM

    Originally posted by: Petr Vilím


    Hello Neil,

    the var >= n constraints can be constructed using function Ge on CP class:

            public virtual IConstraint Le(IIntExpr e, int v)
    

    The function retuns IConstraint, however IConstraint is derived from IIntExpr. Therefore one of the following functions of class CP can be used to produce the LHS of the constraint:

            public virtual IIntExpr Sum(IIntExprArray expr)      
    
            public override IIntExpr Sum(IIntExpr[] exprs)
    

    Finally, to create the whole constraint, you can use Ge function again.

    Best regards, Petr


    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: Modeling Count (Cardinality) Constraint .Net

    Posted Tue August 20, 2013 11:18 AM

    Originally posted by: thisisNeil


    Great, thanks.  The trick I was missing was to use Le rather than AddLe to get a handle on the indicator constraint without adding it directly to the model.  Works a treat.


    #CPOptimizer
    #DecisionOptimization