IBM ILOG Solver User's Manual > Extending the Library > Advanced Modeling with Set Variables: Configuring Tankers > Model > Defining accessors for attributes of set elements

In this model, as you consider a set of tanks on a truck, there are attributes of a tank (for example, its capacity, its current load, the type of product it currently contains) that you want to access in the constraints. Likewise, as you assign a set of orders to the tanks of a truck, you know that there are attributes of the orders that figure in the problem constraints, so you need ways of accessing those attributes. In short, you need set variables to represent constrained relations between an object (a truck, a tank, an order) and a set of related objects (the set of tanks of a truck, the set of orders fulfilled by a truck, and so on). You also need to access the attributes of the related objects to be able to constrain the relation; for example, the cumulative quantities assigned to a truck must be less than or equal to the capacity of the truck. Solver offers you a useful way of accessing such set object attributes.

To define such accessors, use the predefined Solver class IloFunction.

Accessing the quantity of an order: OQuantityI

To access the quantity of an order, define OQuantityI, like this:

class OQuantityI : public IloFunctionI<IloAny,IloInt> {
public:
  OQuantityI(IloEnvI* e) : IloFunctionI<IloAny,IloInt>(e) {}
  IloInt getValue(IloAny elt);
};

IloInt OQuantityI::getValue(IloAny elt) {
  return ((Order)elt)->getQuantity();
}

IloFunction<IloAny,IloInt> OQuantity(IloEnv env) {
  return new (env) OQuantityI(env.getImpl());
}

Accessing the load in a tank: TankLoadI

To access the load in a tank, define TankLoadI, like this:

class TankLoadI : public IloFunctionI<IloAny,IloIntVar> {
public:
  TankLoadI(IloEnvI* e) : IloFunctionI<IloAny,IloIntVar>(e) {}
  IloIntVar getValue(IloAny elt);
};

IloIntVar TankLoadI::getValue(IloAny elt) {
  return ((Tank)elt)->getLoad();
}

IloFunction<IloAny,IloIntVar> TankLoad(IloEnv env) {
  return new (env) TankLoadI(env.getImpl());
}

Accessing the products in a tank: TankProductI

To access the products in a tank, define TankProductI, like this:

class TankProductI : public IloFunctionI<IloAny,IloAnySetVar> {
public:
  TankProductI(IloEnvI* e) : IloFunctionI<IloAny, IloAnySetVar>(e) {}
  IloAnySetVar getValue(IloAny elt);
};

IloAnySetVar TankProductI::getValue(IloAny elt) {
  return ((Tank)elt)->getProductOrders();
}

IloFunction<IloAny,IloAnySetVar> TankProducts(IloEnv env) {
  return new (env) TankProductI(env.getImpl());
}