⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yalmiptest.m

📁 matlab波形优化算法经常要用到的matlab toolbox工具箱:yalmip
💻 M
📖 第 1 页 / 共 2 页
字号:
function out = yalmiptest(prefered_solver,auto)
%YALMIPTEST Runs a number of test problems.
%
%   YALMIPTEST is recommended when a new solver or a new version
%   of YALMIP installed.
%
%   EXAMPLES
%    YALMIPTEST               % Without argument, default solver used
%    YALMIPTEST('solver tag') % Test with specified solver
%    YALMIPTEST(options)      % Test with specific options structure from
%
%   See also SDPSETTINGS, YALMIPDEMO

% Author Johan L鰂berg 
% $Id: yalmiptest.m,v 1.27 2006/05/16 14:44:27 joloef Exp $


if ~exist('sedumi2pen.m')
    disp('Add /yalmip/extras to your path first...')
    return
end

if ~exist('bmibnb.m')
    disp('Add /yalmip/modules/ with sub-directories to your path first...')
    return
end

if ~exist('pwa_yalmip.m')
    disp('Add /yalmip/operators to your path first...')
    return
end

if ~exist('callsedumi.m')
    disp('Add /yalmip/solvers to your path first...')
    return
end

detected = which('yalmip.m','-all');
if length(detected)>1
    disp('You seem to have multiple installations of YALMIP in your path. Please correct this...');
    detected
    return
end

donttest = 0;
if (nargin==1) & isa(prefered_solver,'char') & strcmp(prefered_solver,'test')
    donttest = 0;
    prefered_solver = '';    
else
    donttest = 1;
end

if nargin==0
    prefered_solver = '';
else
    if ~(isa(prefered_solver,'struct') | isa(prefered_solver,'char'))
        error('Argument should be a solver tag, or a sdpsettings structure');
    end
    if isa(prefered_solver,'char') 
        donttest = 1;
    end
end

if ~(exist('callsedumi')==2)
    disp('The directory yalmip/solvers is not in your path.')
    disp('Put yalmip/, yalmip/solvers, yalmip/extras and yalmip/demos in your MATLAB path.');
    return
end

foundstring = {'not found','found'};
teststring = {'-failed','+passed'};
if ~donttest
    header = {'Solver','Version/module','Status','Unit test'};
else
    header = {'Solver','Version/module','Status'};
end    

[solvers,found] = getavailablesolvers(0);
solvers = solvers([find(found);find(~found)]);
found = [found(find(found));found(find(~found))];
j = 1;
for i = 1:length(solvers)
    if solvers(i).show
        data{j,1} = upper(solvers(i).tag); 
        data{j,2} = solvers(i).version;
        data{j,3} = foundstring{found(i)+1};   
        if ~donttest
            if found(i)
                if options.verbose
                    disp(['Testing ' solvers(i).tag '...']);    
                end
                try
                    if solvers(i).maxdet
                        pass = lyapell(sdpsettings('solver',solvers(i).tag,'verbose',0));            
                    else
                        if solvers(i).sdp
                            pass = stabtest(sdpsettings('solver',solvers(i).tag,'verbose',0));
                        else
                            pass = feasiblelp(sdpsettings('solver',solvers(i).tag,'verbose',0));
                        end
                    end
                    data{j,4} = teststring{pass+1};
                catch
                    data{j,4} = '-failed';
                end
            else
                data{j,4} = 'not tested';
            end
        end
        j = j+1;
    end
end

if isa(prefered_solver,'char')
    ops = sdpsettings('Solver',prefered_solver);
else
    ops = prefered_solver;
end

if ~((nargin==2) & (ops.verbose==0))    
table({'Searching for installed solvers'},header,data);
disp(' ')
end
if nargin<2
    disp('Press any key to continue test')
    pause
end

i=1;
test{i}.fcn  = 'testsdpvar';
test{i}.desc = 'sdpvar/set methods';
i = i+1;

test{i}.fcn  = 'feasiblelp'; 
test{i}.desc = 'LP';
i = i+1;

test{i}.fcn  = 'toepapprox'; 
test{i}.desc = 'LP';
i = i+1;

test{i}.fcn  = 'feasibleqp'; 
test{i}.desc = 'QP';
i = i+1;

test{i}.fcn  = 'toepapprox2'; 
test{i}.desc = 'QP';
i = i+1;


test{i}.fcn  = 'socptest1'; 
test{i}.desc = 'SOCP';
i = i+1;

test{i}.fcn  = 'socptest2'; 
test{i}.desc = 'SOCP'; 
i = i+1;

test{i}.fcn  = 'socptest3'; 
test{i}.desc = 'SOCP';   
i = i+1;

test{i}.fcn  = 'complete';
test{i}.desc = 'SDP';
i = i+1;

test{i}.fcn  = 'complete_2'; 
test{i}.desc = 'SDP';
i = i+1;

test{i}.fcn  = 'maxcut';
test{i}.desc = 'SDP';
i = i+1;

test{i}.fcn  = 'feasible'; 
test{i}.desc = 'SDP';
i = i+1;

test{i}.fcn  = 'lyapell'; 
test{i}.desc = 'MAXDET';
i = i+1;

test{i}.fcn  = 'lyapell2'; 
test{i}.desc = 'MAXDET';
i = i+1;

%test{i}.fcn  = 'circuit1'; 
%test{i}.desc = 'GP';
%i = i+1;

test{i}.fcn  = 'infeasible'; 
test{i}.desc = 'Infeasible LP';
i = i+1;

test{i}.fcn  = 'infeasibleqp'; 
test{i}.desc = 'Infeasible QP';
i = i+1;

test{i}.fcn  = 'infeasiblesdp'; 
test{i}.desc = 'Infeasible SDP';
i = i+1;

test{i}.fcn  = 'momenttest'; 
test{i}.desc = 'Moment relaxation';
i = i+1;

test{i}.fcn  = 'sostest'; 
test{i}.desc = 'Sum-of-squares';
i = i+1;

test{i}.fcn  = 'bmitest'; 
test{i}.desc = 'Bilinear SDP';
i = i+1;



pass_strings = {'Error','Passed','Solver not available'};

tt = cputime;

% Run test-problems
for i = 1:length(test)
    try
        t=cputime;           
        if ops.verbose
            disp(' ');
            disp(['Testing function ' test{i}.fcn]);
            disp(' ');
        end       
        [pp,ss,res] = eval([test{i}.fcn '(ops)']);
        pass(i) = pp;
        sols{i} = ss.info;
        results{i}=res;
        ttime(i) = cputime-t;
    catch
        pass(i) = 0;   
        results{i} = 'NAN';
        sols{i} = 'Unknown problem in YALMIP';
        ttime(i) = cputime-tt;
    end
end
totaltime = cputime-tt;

clear data;
header = {'Test','Solution', 'Solver message'};
for i = 1:length(pass)
    thetime =  num2str(ttime(i),4);
    data{i,1} = test{i}.desc;
    data{i,2} = results{i};
    data{i,3} = sols{i};
end
if ops.verbose
    disp(' ');
end
formats{1}.data.just = 'right';
formats{2}.data.just = 'right';
formats{3}.data.just = 'right';

formats{1}.header.just = 'right';
formats{2}.header.just = 'right';
formats{3}.header.just = 'right';

clc
table([],header,data,formats)


function [pass,sol,result] = testsdpvar(ops)

% Test the sdpvar implementation
pass = 1;
sol.info = yalmiperror(0,'YALMIP');
try
    x = sdpvar(2,2);
    x = sdpvar(2,2,'symmetric');
    x = sdpvar(2,2,'full');
    x = sdpvar(2,2,'toeplitz');
    x = sdpvar(2,2,'hankel');
    x = sdpvar(2,2,'skew');
    if ~ishermitian(sdpvar(2,2,'hermitian','complex'))
        error('bug')
    end
    if ~issymmetric(sdpvar(2,2,'symmetric','complex'))
        error('bug')
    end
    if ~isreal(real(sdpvar(2,2,'symmetric','complex')))
        error('bug')
    end
    if isreal(sqrt(-1)*real(sdpvar(2,2,'symmetric','complex')))
        error('bug')
    end
    x = sdpvar(2,1,'','co');
    if ~isreal(x'*x)
        error('bug')
    end
    x = sdpvar(2,2,'','co');
    if ~isreal(diag(x'*x))
        error('bug')
    end
    x = sdpvar(1,1);
    y = sdpvar(2,2);
    x*eye(2);
    eye(2)*x;
    y*3;
    3*y;
    x = sdpvar(2,3);
    y = sdpvar(2,3);
    setsdpvar(x,randn(2,3));
    z = replace(x,x(1,1:2),[8 9]);
    z = x+y;
    z = x-y;
    z = x+1;
    z = x-1;
    z = x+ones(2,3);
    z = x-ones(2,3);
    z = ones(2,3)-x;
    z = ones(2,3)-x;
    z = eye(2)*x;
    z = x*eye(3);
    z = diag(x);
    z = trace(x);
    z = diff(x);
    z = fliplr(x);
    z = flipud(x);
    z = kron(x,eye(3));
    z = kron(eye(3),x);
    z = rot90(x);
    z = sum(x);
    z = diff(x);
    z = x';
    z = x.';
    z = tril(x);
    z = triu(x);
    z = [x y];
    z = [x;y];
    sdpvar x y
    diag([x y])*[x^-1;y^-1];
    assert(isequal([x x;x x]*x,[x x;x x].*x))
    assert(isequal(trace([x x;x x]*[x y;y x]),x*x+x*y+y*x+x*x))
    
    % Regression ??
    yalmip('clear')
    sdpvar x

    (1+x+x^4)*(1-x^2); 
    
    % Regression complex multiplcation
    A = randn(10,5)+sqrt(-1)*randn(10,5);
    b = randn(10,1)+sqrt(-1)*randn(10,1);
    x = sdpvar(5,1);
    res = A*x-b;
    assert(nnz(clean([res res]'*[res res]-res'*res,1e-8))==0)
    assert(isreal(clean(res'*res,1e-8)))
    
    sdpvar x(1,1,'full','complex');
    assert(isreal(x*x'))
    
%     x = sdpvar(4,1);
%     sdpvar i a   
%     y = [i a];
%     y(1)
%    % i = sdpvar(1,1);
%    % a = sdpvar(1,1);
%    % [i a]
%     
%     
%     
%     size(x([i j]))
%     assert(all(size(x([i j])) == [2 1]))
%     assert(all(size(x(1,[i j])) == [1 2]))
%    % assert(all(size(x(1,[i j])) == [1 2]));
%    % x(1,[i j]);
   % x([i j],1);
   % x([i j],[i j]);
    
    
    
    result = 'N/A';
catch
    sol.info = 'Problems';
    result = 'N/A';
    pass = 0;
end


function [pass,sol,result] = feasible(ops) 
t = sdpvar(1,1);
Y = sdpvar(2,2);
F = set('Y<t*eye(2)');
F = F+set('Y>[1 0.2;0.2 1]');
sol = solvesdp(F,t,ops);
pass = ismember(sol.problem,[0 3 4 5]);
if pass
    result = resultstring(t,1.2);
else
    result = 'N/A';
end
    


function [pass,sol,result] = infeasible(ops)
t = sdpvar(1,1);
Y = sdpvar(2,2);
F = set('t>0');
F = F+set('t<-10');
sol = solvesdp(F,t,ops);
pass = ~(sol.problem==0);
result = 'N/A';

function [pass,sol,result] = lyapell(ops)
A = [1 0;0.4 1];
B = [0.4;0.08]; 
L = [1.9034 1.1501];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -