Documentation |
On this page… |
---|
Solving Mixed Integer Optimization Problems |
ga can solve problems when certain variables are integer-valued. Give IntCon, a vector of the x components that are integers:
[x,fval,exitflag] = ga(fitnessfcn,nvars,A,b,[],[],... lb,ub,nonlcon,IntCon,options)
IntCon is a vector of positive integers that contains the x components that are integer-valued. For example, if you want to restrict x(2) and x(10) to be integers, set IntCon to [2,10].
Note: Restrictions exist on the types of problems that ga can solve with integer variables. In particular, ga does not accept any equality constraints when there are integer variables. For details, see Characteristics of the Integer ga Solver. |
This example shows how to find the minimum of Rastrigin's function restricted so the first component of x is an integer. The components of x are further restricted to be in the region .
Set up the bounds for your problem
lb = [5*pi,-20*pi]; ub = [20*pi,-4*pi];
Set a plot function so you can view the progress of ga
opts = gaoptimset('PlotFcns',@gaplotbestf);
Call the ga solver where x(1) has integer values
rng(1,'twister') % for reproducibility IntCon = 1; [x,fval,exitflag] = ga(@rastriginsfcn,2,[],[],[],[],... lb,ub,[],IntCon,opts)
Optimization terminated: average change in the penalty fitness value less than options.TolFun and constraint violation is less than options.TolCon. x = 16.0000 -12.9325 fval = 424.1355 exitflag = 1
ga converges quickly to the solution.
There are some restrictions on the types of problems that ga can solve when you include integer constraints:
No linear equality constraints. You must have Aeq = [] and beq = []. For a possible workaround, see No Equality Constraints.
No nonlinear equality constraints. Any nonlinear constraint function must return [] for the nonlinear equality constraint. For a possible workaround, see Example: Integer Programming with a Nonlinear Equality Constraint.
Only doubleVector population type.
No custom creation function (CreationFcn option), crossover function (CrossoverFcn option), mutation function (MutationFcn option), or initial scores (InitialScores option). If you supply any of these, ga overrides their settings.
ga uses only the binary tournament selection function (SelectionFcn option), and overrides any other setting.
No hybrid function. ga overrides any setting of the HybridFcn option.
ga ignores the ParetoFraction, DistanceMeasureFcn, InitialPenalty, and PenaltyFactor options.
The listed restrictions are mainly natural, not arbitrary. For example:
There are no hybrid functions that support integer constraints. So ga does not use hybrid functions when there are integer constraints.
To obtain integer variables, ga uses special creation, crossover, and mutation functions.
You cannot use equality constraints and integer constraints in the same problem. You can try to work around this restriction by including two inequality constraints for each linear equality constraint. For example, to try to include the constraint
3x_{1} – 2x_{2} = 5,
create two inequality constraints:
3x_{1} –
2x_{2} ≤ 5
3x_{1} –
2x_{2} ≥ 5.
To write these constraints in the form A x ≤ b, multiply the second inequality by -1:
–3x_{1} + 2x_{2} ≤ –5.
You can try to include the equality constraint using A = [3,-2;-3,2] and b = [5;-5].
Be aware that this procedure can fail; ga has difficulty with simultaneous integer and equality constraints.
Example: Integer Programming with a Nonlinear Equality Constraint. This example attempts to locate the minimum of the Ackley function in five dimensions with these constraints:
x(1), x(3), and x(5) are integers.
norm(x) = 4.
The Ackley function, described briefly in Resuming ga From the Final Population, is difficult to minimize. Adding integer and equality constraints increases the difficulty.
To include the nonlinear equality constraint, give a small tolerance tol that allows the norm of x to be within tol of 4. Without a tolerance, the nonlinear equality constraint is never satisfied, and the solver does not realize when it has a feasible solution.
Write the expression norm(x) = 4 as two "less than zero" inequalities:
norm(x) - 4 ≤ 0
-(norm(x) - 4) ≤ 0.
Allow a small tolerance in the inequalities:
norm(x) - 4 - tol ≤ 0
-(norm(x) - 4) - tol ≤ 0.
Write a nonlinear inequality constraint function that implements these inequalities:
function [c, ceq] = eqCon(x) ceq = []; rad = 4; tol = 1e-3; confcnval = norm(x) - rad; c = [confcnval - tol;-confcnval - tol];
Set options:
StallGenLimit = 50 — Allow the solver to try for a while.
TolFun = 1e-10 — Specify a stricter stopping criterion than usual.
Generations = 300 — Allow more generations than default.
PlotFcns = @gaplotbestfun — Observe the optimization.
opts = gaoptimset('StallGenLimit',50,'TolFun',1e-10,... 'Generations',300,'PlotFcns',@gaplotbestfun);
Set lower and upper bounds to help the solver:
nVar = 5; lb = -5*ones(1,nVar); ub = 5*ones(1,nVar);
Solve the problem:
rng(1,'twister') % for reproducibility [x,fval,exitflag] = ga(@ackleyfcn,nVar,[],[],[],[], ... lb,ub,@eqCon,[1 3 5],opts); Optimization terminated: average change in the penalty fitness value less than options.TolFun and constraint violation is less than options.TolCon.
Examine the solution:
x,fval,exitflag,norm(x) x = -2.0000 -0.0000 1.0000 -2.6488 -2.0000 fval = 5.7769 exitflag = 1 ans = 4.0020
The odd x components are integers, as specified. The norm of x is 4, to within the given relative tolerance of 1e-3.
Despite the exit flag of 1, the solution is not the global optimum. Run the problem again and examine the solution:
opts = gaoptimset(opts,'Display','off'); [x2,fval2,exitflag2] = ga(@ackleyfcn,nVar,[],[],[],[], ... lb,ub,@eqCon,[1 3 5],opts);
Examine the second solution:
x2,fval2,exitflag2,norm(x2) x2 = -1.0000 -2.9977 2.0000 -0.9989 1.0000 fval2 = 4.2355 exitflag2 = 0 ans = 3.9980
The second run gives a better solution (lower fitness function value). Again, the odd x components are integers, and the norm of x2 is 4, to within the given relative tolerance of 1e-3.
Be aware that this procedure can fail; ga has difficulty with simultaneous integer and equality constraints.
Integer programming with ga involves several modifications of the basic algorithm (see How the Genetic Algorithm Works). For integer programming:
Special creation, crossover, and mutation functions enforce variables to be integers. For details, see Deep et al. [2].
The genetic algorithm attempts to minimize a penalty function, not the fitness function. The penalty function includes a term for infeasibility. This penalty function is combined with binary tournament selection to select individuals for subsequent generations. The penalty function value of a member of a population is:
If the member is feasible, the penalty function is the fitness function.
If the member is infeasible, the penalty function is the maximum fitness function among feasible members of the population, plus a sum of the constraint violations of the (infeasible) point.
For details of the penalty function, see Deb [1].
[1] Deb, Kalyanmoy. An efficient constraint handling method for genetic algorithms. Computer Methods in Applied Mechanics and Engineering, 186(2–4), pp. 311–338, 2000.
[2] Deep, Kusum, Krishna Pratap Singh, M.L. Kansal, and C. Mohan. A real coded genetic algorithm for solving integer and mixed integer optimization problems. Applied Mathematics and Computation, 212(2), pp. 505–518, 2009.