Decision Optimization

 View Only
Expand all | Collapse all

modelisation problem: sum constraint over indices of a set variable

  • 1.  modelisation problem: sum constraint over indices of a set variable

    Posted Sat January 19, 2008 03:19 PM

    Originally posted by: SystemAdmin


    [pierre schaus said:]

    Hello,

    I have little modelisation problem:

    An IloIntSetVar "myset" represents a subset of (indices) of {1..n}
    and a cost array of constant values [c1,...,cn] are the cost of selecting an indices.
    I want to compute the cost of selecting the indices of myset:

    totcost=sum_{i in myset} c_i


    I could make a vector of n variables cost[] with cost[ i ]=ci if i in myset and cost[ i ]=0 otherwise.
    Then totcost=sum_i cost[ i ]

    Unfortunately, this is not very efficient to create n additional variables for this.

    Is there a sum constraint where the indices of the sum are taken on an IloIntSetVar?

    Thank you,

    Pierre. 







    #CPOptimizer
    #DecisionOptimization


  • 2.  Re: modelisation problem: sum constraint over indices of a set variable

    Posted Sun January 20, 2008 01:30 PM

    Originally posted by: SystemAdmin


    [pierre schaus said:]

    I think I have now the answer:

    1) Extend the IloFunction class allows to make the mapping between the indices and the costs.
    2) Use the IloEqSum to make the sum. This constraint takes the function in argument.

    The filtering is stronger and faster than with the naive solution described above.

    Note that IloEqSum is not in the reference manual of the version Ilog CP1.1 I'm using but it is available in the library.

    Pierre.

    #CPOptimizer
    #DecisionOptimization


  • 3.  Re: modelisation problem: sum constraint over indices of a set variable

    Posted Tue January 22, 2008 06:58 PM

    Originally posted by: SystemAdmin


    [Olivier Lhomme said:]

    Hello Pierre,
    This is a documentation problem in the CP 1.1 release.
    Here is the missing documentation for IloEqSum:

    --------------------------------
    IloEqSum
    public IloConstraint IloEqSum(const IloEnv, const IloIntSetVar var1, const IloIntVar var2, const IloIntToIntFunction f)
    public IloConstraint IloEqSum(const IloEnv, const IloIntSetVar var1, const IloIntVar var2, const IloIntToIntVarFunction f)
    Definition file: ilconcert/iloset.h
    For ILOG Solver: a constraint forcing a variable to the sum of returned values.
    This function creates and returns a constraint (an instance of IloConstraint) for use in a model. The constraint forces var2 to the sum of the values returned by the function f when it is applied to the variable var1.
    In order for the constraint to take effect, you must add it to a model with the template IloAdd or the member function IloModel::add and extract the model for an algorithm with the member function IloAlgorithm::extract.
    --------------------------------

    In your case you can define an IloIntToIntFunction in the following way:

    class MyFunctionI : public IloFunctionI<IloInt, IloInt>{
    public:
      MyFunctionI(IloEnvI* e): IloFunctionI<IloInt, IloInt>( e) {};
      virtual IloInt getValue(IloInt a){return the cost of a;}
    };


    int main(int argc, char** argv) {
      IloEnv env;
      IloIntToIntFunction myFunction(new (env) MyFunctionI(env.getImpl()));
    }


    Hope it helps,
    Olivier


    #CPOptimizer
    #DecisionOptimization


  • 4.  Re: modelisation problem: sum constraint over indices of a set variable

    Posted Wed January 23, 2008 12:31 PM

    Originally posted by: SystemAdmin


    [pierre schaus said:]

    Thank you Olivier,

    I finally found an example of  IloFunction in the user manual at section "Advanced Modeling with Set Variables".

    This constraint IloEqSum combined with the function are very usefull and much more efficient than a model with binary variables!

    Bye,

    Pierre.

    #CPOptimizer
    #DecisionOptimization