📄 cutsdp.m
字号:
function output = cutsdp(p)
% CUTSDP
%
% See also SOLVESDP, BNB, BINVAR, INTVAR, BINARY, INTEGER, LMI
% Author Johan L鰂berg
% $Id: cutsdp.m,v 1.7 2007/01/31 14:00:56 joloef Exp $
% *************************************************************************
%% INITIALIZE DIAGNOSTICS IN YALMIP
% *************************************************************************
bnbsolvertime = clock;
showprogress('Cutting plane solver started',p.options.showprogress);
% *************************************************************************
%% If we want duals, we may not extract bounds. However, bounds must be
% extracted in discrete problems.
% *************************************************************************
if p.options.cutsdp.recoverdual
warning('Dual recovery not implemented yet in CUTSDP')
end
% *************************************************************************
%% Define infinite bounds
% *************************************************************************
if isempty(p.ub)
p.ub = repmat(inf,length(p.c),1);
end
if isempty(p.lb)
p.lb = repmat(-inf,length(p.c),1);
end
% *************************************************************************
%% ADD CONSTRAINTS 0<x<1 FOR BINARY
% *************************************************************************
if ~isempty(p.binary_variables)
p.ub(p.binary_variables) = min(p.ub(p.binary_variables),1);
p.lb(p.binary_variables) = max(p.lb(p.binary_variables),0);
end
% *************************************************************************
%% Extract better bounds from model
% *************************************************************************
if ~isempty(p.F_struc)
[lb,ub,used_rows] = findulb(p.F_struc,p.K);
if ~isempty(used_rows)
lower_defined = find(~isinf(lb));
if ~isempty(lower_defined)
p.lb(lower_defined) = max(p.lb(lower_defined),lb(lower_defined));
end
upper_defined = find(~isinf(ub));
if ~isempty(upper_defined)
p.ub(upper_defined) = min(p.ub(upper_defined),ub(upper_defined));
end
p.F_struc(p.K.f+used_rows,:)=[];
p.K.l = p.K.l - length(used_rows);
end
end
% *************************************************************************
%% ADD CONSTRAINTS 0<x<1 FOR BINARY
% *************************************************************************
if ~isempty(p.binary_variables)
p.ub(p.binary_variables) = min(p.ub(p.binary_variables),1);
p.lb(p.binary_variables) = max(p.lb(p.binary_variables),0);
end
p.ub = min(p.ub,p.options.cutsdp.variablebound');
p.lb = max(p.lb,-p.options.cutsdp.variablebound');
% *************************************************************************
%% PRE-SOLVE (nothing fancy coded)
% *************************************************************************
if isempty(find(isinf([p.ub;p.lb]))) & p.K.l>0
[p.lb,p.ub] = tightenbounds(-p.F_struc(1+p.K.f:p.K.f+p.K.l,2:end),p.F_struc(1+p.K.f:p.K.f+p.K.l,1),p.lb,p.ub,p.integer_variables);
end
% *************************************************************************
%% PERTURBATION OF LINEAR COST
% *************************************************************************
p.corig = p.c;
if nnz(p.Q)~=0
g = randn('seed');
randn('state',1253); %For my testing, I keep this the same...
% This perturbation has to be better. Crucial for many real LP problems
p.c = (p.c).*(1+randn(length(p.c),1)*1e-4);
randn('seed',g);
end
% *************************************************************************
%% We don't need this
% *************************************************************************
p.options.savesolverinput = 0;
p.options.savesolveroutput = 0;
% *************************************************************************
%% Display logics
% 0 : Silent
% 1 : Display cut progress
% 2 : Display node solver prints
% *************************************************************************
switch max(min(p.options.verbose,3),0)
case 0
p.options.cutsdp.verbose = 0;
case 1
p.options.cutsdp.verbose = 1;
p.options.verbose = 0;
case 2
p.options.cutsdp.verbose = 2;
p.options.verbose = 0;
case 3
p.options.cutsdp.verbose = 2;
p.options.verbose = 1;
otherwise
p.options.cutsdp.verbose = 0;
p.options.verbose = 0;
end
% *************************************************************************
%% START CUTTING
% *************************************************************************
[x_min,solved_nodes,lower,feasible,D_struc] = cutting(p);
%% --
% *************************************************************************
%% CREATE SOLUTION
% *************************************************************************
output.problem = 0;
if ~feasible
output.problem = 1;
end
if solved_nodes == p.options.cutsdp.maxiter
output.problem = 3;
end
output.solved_nodes = solved_nodes;
output.Primal = x_min;
output.Dual = D_struc;
output.Slack = [];
output.solverinput = 0;
output.solveroutput =[];
output.solvertime = etime(clock,bnbsolvertime);
%% --
function [x,solved_nodes,lower,feasible,D_struc] = cutting(p)
% *************************************************************************
%% Sanity check
% *************************************************************************
if any(p.lb>p.ub)
x = zeros(length(p.c),1);
solved_nodes = 0;
lower = inf;
feasible = 0;
D_struc = [];
return
end
% *************************************************************************
%% Create function handle to solver
% *************************************************************************
cutsolver = p.solver.cutsolver.call;
% *************************************************************************
%% Create copy of model without
% the SDP part
% *************************************************************************
p_lp = p;
p_lp.F_struc = p_lp.F_struc(1:p.K.l+p.K.f,:);
p_lp.K.s = 0;
p_lp.K.q = 0;
% *************************************************************************
%% DISPLAY HEADER
% *************************************************************************
if p.options.cutsdp.verbose
disp('* Starting YALMIP cutting plane for MISDP based on MILP');
disp(['* Lower solver : ' p.solver.cutsolver.tag]);
disp(['* Max iterations : ' num2str(p.options.cutsdp.maxiter)]);
end
if p.options.bnb.verbose; disp(' Node Infeasibility. Lower LP cuts');end;
%% Initialize diagnostic
infeasibility = -inf;
solved_nodes = 0;
feasible = 1;
lower = -inf;
saveduals = 1;
% *************************************************************************
%% Add diagonal cuts to begin with
% *************************************************************************
savedCuts = [];
savedIndicies = [];
if p.K.q(1) > 0
top = p.K.f+p.K.l+1;
for i = 1:length(p.K.q)
n = p.K.q(i);
newF = p.F_struc(top,:);
% Clean
newF(abs(newF)<1e-12) = 0;
keep=find(any(newF(:,2:end),2));
newF = newF(keep,:);
p_lp.F_struc = [p_lp.F_struc;newF];
p_lp.K.l = p_lp.K.l + size(newF,1);
top = top+n;
end
end
if p.K.s(1)>0
top = p.K.f+p.K.l+p.K.q+1;
for i = 1:length(p.K.s)
n = p.K.s(i);
newF=[];
for m = 1:p.K.s(i)
d = eyev(p.K.s(i),m);
index = (1+(m-1)*(p.K.s(i)+1));
newF = [newF;p.F_struc(top+index-1,:)];
end
% Clean
newF(abs(newF)<1e-12) = 0;
keep=find(any(newF(:,2:end),2));
newF = newF(keep,:);
p_lp.F_struc = [p_lp.F_struc;newF];
p_lp.K.l = p_lp.K.l + size(newF,1);
top = top+n^2;
end
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -