Add a different objective to the original changing money problem you worked on in Chapter 3, Using Arrays and Basic Search: Changing Money. This time, instead of trying to pay for the item using as many of your lower denomination coins as possible as in Exercise 3, try to use as few coins as possible. Assume that you want to pay for an item that costs 1.23 euros. You have a mixture of coins of the following types: 1 euro cent, 10 euro cents, 20 euro cents, and 1 euro. To make the problem more interesting, assume that you have only 5 coins of 1 euro cent. Create an objective that minimizes the number of coins using the function IloMinimize
. Change the code so that you only search for one minimized solution.
Suggested Answer
The code that has changed from money.cpp
follows. You can view the complete program online in the file YourSolverHome/examples/src/colormin_ex4.cpp
.
You add the following objective:
IloObjective obj = IloMinimize(env, coins[0] + coins[1] + coins[2] +
coins[3]);
|
You change the following code to display the solution:
if (solver.solve())
{
for (IloInt i = 0; i < nCoins; i++)
solver.out() << solver.getValue(coins[i]) << " ";
solver.out() << endl;
}
else
solver.out() << "No Solution" << endl;
|
You should get the following results: