IBM ILOG Solver User's Manual > The Basics > Using Arrays and Basic Search: Changing Money > Model

Once you have written a description of your problem, you can use Concert Technology classes to model it. Before declaring the variables and adding constraints, you must create an environment and a model. Once you create a model of your problem, you can use Solver classes to search for a solution.

Step 2   -  

Open the example file

Open the example file YourSolverHome/examples/src/tutorial/money_partial.cpp in your development environment. In this exercise, you use arrays of constrained integer variables and therefore you use the include file <ilsolver/ilosolverint.h>. The code for printing out the solution is provided. The number of types of coins, nCoins, is set to 4 in this problem and the amount of the purchase, Sum, is set to 123.

The first step in converting your natural language description of the problem into code using Concert Technology classes is to create an environment and a model.

Step 3   -  

Create the environment

Add the following code after the comment //Create the environment

  IloEnv env;

Step 4   -  

Create the model

Add the following code after the comment //Create the model

    IloModel model(env);

Concert Technology gives you the means to represent the unknowns in this problem--the number of coins of each type--as an array of constrained integer variables. You associate one constrained integer variable in the array with each type of coin.

Arrays of constrained integer variables are represented by the class IloIntVarArray in Concert Technology. The first parameter of the constructor is the environment. The second parameter is the number of variables in the array. Arrays are extensible, so you can later increase or reduce this number. The third and fourth parameters are the lower and upper bounds of the domain of possible values for each variable in the array. When you use an array, you can access a variable in that array by its index, and the operator[] is overloaded for this purpose. Here is a constructor:

     IloIntVarArray (const IloEnv env,
                     IloInt n,
                     IloNum lb,
                     IloNum ub);

After you create an environment and a model, you declare the array of constrained integer variables. Each variable in the array represents the unknown information--the number of coins of that type. These decision variables will contain the solutions to the problem, once it is solved.

Step 5   -  

Declare the decision variables

Add the following code after the comment //Declare the decision variables

    IloIntVarArray coins(env, nCoins, 0, Sum);

The number of variables in the array can be no larger than the number of different types of coins, nCoins, which is set to 4 in this problem. The lower bound of the domain of possible values for each variable is 0, meaning that no coin of that type is used. The upper bound of the domain of possible values for each variable is Sum, set to 123 in this problem, which would be the largest possible value if the entire purchase amount were composed only of 1 cent coins. (This is not a possible solution in this problem because there is a constraint that only 5 coins of this type are available.)

Now you add the constraint that the sum of the coins must equal 1.23 euros (or 123 cents). To express this constraint, you use the Concert Technology function IloScalProd. IloScalProd takes two arguments, both arrays, and returns an instance of IloExprBase that represents the scalar product of the two arrays. The scalar product is also called the inner product or the weighted sum. The class IloExprBase is used internally by Concert Technology to build expressions. You should not use IloExprBase directly. Here is a constructor for IloScalProd:

IloExprBase IloScalProd(const IloNumArray vals, const IloNumVarArray vars); 

IloNum

IloNum is a type definition that represents numeric values as floating-point numbers in Concert Technology. By its nature, IloNum can include IloInt.

Likewise, the class IloIntArray is a subclass of IloNumArray and the class IloIntVarArray is a subclass of IloNumVarArray.

You will learn more about using floating-point numbers and variables in Chapter 11, Using Constrained Floating-Point Variables: Modeling Equations.

You use the array of constrained variables that you just created, coins, as one argument. The array represents the number of each type of coin. What are you going to use as the other argument? You need to create another array that represents the value of each type of coin. Since you know the value of each type of coin, this array is not an array of variables, but instead an array of integer values. This array represents the coefficients of the equation you used to model the problem.

Arrays of integer values are represented in Concert Technology by the class IloIntArray. The first parameter of the constructor is the environment. The second parameter is the number of integer values in the array, which is extensible. The elements of the new array take the corresponding values: v0, v1, ..., v(n-1). When you use an array, you can access a value in that array by its index, and the operator[] is overloaded for this purpose. Here is a constructor:

     IloIntArray(const IloEnv env, IloInt n, const IloInt v0, ...);

Step 6   -  

Declare the array of coefficients

Add the following code after the comment //Declare the array of coefficients

    IloIntArray coeffs(env, nCoins, 1, 10, 20, 100);

Now that you have declared the array of variables coins and the array of coefficients coeffs that are used in IloScalProd, you can add constraints to the model. You must explicitly add a constraint to the model or Solver will not be able to use it in the search for a solution.

Step 7   -  

Add the constraint using IloScalProd

Add the following code after the comment //Add the constraint using IloScalProd

    model.add(IloScalProd(coins, coeffs) == Sum);

Now you add the second constraint that the number of coins of 1 cent must be less than or equal to 5. You used the operator != in Chapter 2, Modeling and Solving a Simple Problem: Map Coloring to express the idea that two or more variables cannot assume the same value. In this lesson, you use the operator <= to express the idea that the number of coins of 1 cent must be less than or equal to 5.

Step 8   -  

Add the constraint on the number of 1 cent coins

Add the following code after the comment
//Add the constraint on the number of 1 cent coins

    model.add(coins[0] <= 5);