代码实现
%main.m
%分支定界法
%求解模型:
%min z = f * x
%A * x <= b
%x >= 0,且为整数
clear gloal
global result;%存储所有整数解
global lowerBound;%下界
global upperBound;%上界
global count; %用来判断是否为第一次分支
f = [-40, -90];
A = [9, 7;
7, 20;];
b = [56;70];
Aeq = [];
beq = [];
lb = [0; 0];
ub = [inf; inf]
[x, fval] = linprog(f, A, b, Aeq, beq, lb, ub);
x
abs(fval)
maxz = (-f) * x
%对x1进行分支 x1<=4
c = [40, 90];
A = [9, 7; 7, 20];
b = [56, 70];
Aeq = [];beq = [];
lb = [0;0];
ub = [4;inf];
[x, fval] = linprog(-c, A, b, Aeq, beq, lb, ub);
x
maxz = abs(fval)
%x1分支+x2分支 x1<=4,x2<=2
c = [40, 90];
A = [9, 7; 7, 20];
b = [56, 70];
Aeq = [];beq = [];
lb = [0;0];
ub = [4;2];
[x, fval] = linprog(-c, A, b, Aeq, beq, lb, ub);
x
maxz = abs(fval)
%x1+x2分支 x1 <=4, x2>=3
c = [40, 90];
A = [9, 7; 7, 20];
b = [56, 70];
Aeq = [];beq = [];
lb = [0;3];
ub = [4;inf];
[x, fval] = linprog(-c, A, b, Aeq, beq, lb, ub);
x
maxz = abs(fval)
%对x1分支 x1>=5
c = [40, 90];
A = [9, 7; 7, 20];
b = [56, 70];
Aeq = [];beq = [];
lb = [5;0];
ub = [inf;inf];
[x, fval] = linprog(-c, A, b, Aeq, beq, lb, ub);
x
maxz = abs(fval)
%x1+x2分支 x1>=5+x2<=2
c = [40, 90];
A = [9, 7; 7, 20];
b = [56, 70];
Aeq = [];beq = [];
lb = [5;0];
ub = [inf;2];
[x, fval] = linprog(-c, A, b, Aeq, beq, lb, ub);
x
maxz = abs(fval)
%x1+x2分支,x1>=5+x2>=3
c = [40, 90];
A = [9, 7; 7, 20];
b = [56, 70];
Aeq = [];beq = [];
lb = [5;3];
ub = [inf;inf];
[x, fval] = linprog(-c, A, b, Aeq, beq, lb, ub);
x
maxz = abs(fval)
运行结果
线性规划运行结果
然后对变量x1,x2进行分支计算
x1分支1(x1<=4)
x1,x2分支(x1<=4, x2<=2)
x1,x2分支(x1<=4, x2>=3)
x1分支2(x1>=5)
x1,x2分支(x1>=5, x2<=2)
x1,x2分支(x1>=5, x2>=3)
所以这个题目整数规划结果为