Going back now to our small example, we are ready to solve it using simulated annealing. The code for the model and a solution, are described as follows:
IloEnv env;
IloModel mdl(env);
IloIntVarArray a(env, 2, 0, 1);
IloInt C = 1;
IloIntVar cost(env, 0, 2 * C + 1);
mdl.add(cost == C * (a[0] + a[1]) + (C + 1.0) * (a[0] != a[1]));
IloSolution soln(env);
soln.add(a);
soln.add(IloMinimize(env, cost));
|
Our neighborhood is one which flips a single bit of the solution, and so we can use IloFlip
for this. However, what we eventually want is a random neighbor which is termed legal by the simulated annealing rule to be accepted. There is a special neighborhood modifier defined in Solver, IloRandomize
, which, given a neighborhood, jumbles the order in a random fashion. This jumbling takes place for each neighborhood move made. We specify our neighborhood:
IloRandom rand(env);
IloNHood nh = IloRandomize(env, IloFlip(env, a), rand);
|
IloRandom
is a Concert Technology class for generating pseudo-random numbers. It is passed to the IloRandomize
neighborhood which then uses rand
to generate the randomized neighborhood.