[gecode-users] Question about: Float variables, Literals and/or Constants

Holger Winnemoeller holger.winnemoeller at gmail.com
Thu Dec 17 01:36:10 CET 2009


I wonder if I have found the best solution to the following problem (I doubt
it):

inventory = {0,1,2,3,4, ... , N}
subsetA = some subset of inventory

Goal: Give me a resultSet with elements from inventory given the following
constraints

*  fraction X of items in resultSet are from subSetA
*  some other constraints (lets not worry about that here)

pseudocode notation of a solution might be:

IntVar resultSize = cardinality(resultSet);
SetVar mutualSet = intersection(resultSet, subsetA);
IntVar mutualSize = cardinality(mutualSet);

FloatVar f = div(mutualSize, resultSize);
rel(div, EQ, X);

Now, while I did find a FloatVar class, it's in the namespace
Gecode::FlatZinc::AST, not Gecode, and I have a feeling its use is not
analogous to IntVar.

So, fair enough, I can just repose the constraint:

                |mutualSet| / |resultSet| = X, where X can be approximated
by n/d

<==> d * |mutualSet| = n * |resultSet|, all of which can be expressed in
Int's

So, my solution ends up being:

        IntVar lhs (*this, 0, d * setB.size());        // worst-case domain
    IntVar rhs (*this, 0, n * inventory.size());   // worst-case domain

    // Q1: I would love to write the following lines, but I may not use
integer literals in the mult call. Is there a better way?
    //mult(*this, mutualSize, d, lhs);
    //mult(*this, resultSize, n, rhs);

    // Q2: Is this the way to specify the constant that I want to use? It
seems very roundabout...
    IntVar numerator(*this, n,n);
    IntVar denominator(*this, d,d);

    mult(*this, mutualSize, denominator, lhs);
    mult(*this, resultSize, numerator,   rhs);

    rel(*this, lhs, IRT_EQ, rhs);

The thing is, I do get the correct solution using this approach, but I feel
like I am misusing Gecode, and that there is a cleaner way to express what I
want. So to summarize my questions:

   - Is there a way to just use a Float constraint?
   - If not (have to use Quotient approach), is there a way to express the
   multiplication using integer literals?
   - If not, is there a way to specify an integer constant without defining
   a one-element domain for it?
   - Or am I crazy, and the solution I give is adequate?


Thanks for your help,
Holger.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.gecode.org/pipermail/users/attachments/20091216/9c51959f/attachment.htm>


More information about the users mailing list