function PerfLevel = perfwc(hFuncObj, hFuncPerf, ProbEst, Delta, varargin)
%PERFWC returns worst-case performance level estimation
%
% PerfLevel = PERFWC(HANDLEOBJ, HANDLEPERF, PROBEST, DELTA)
% PerfLevel = PERFWC(HANDLEOBJ, HANDLEPERF, PROBEST, DELTA, VECOBJ)
% PerfLevel = PERFWC(HANDLEOBJ, HANDLEPERF, PROBEST, DELTA, VECOBJ, VECPERF)
%
% returns a performance level PerfLevel such that
% with probability at least 1-DELTA
% Prob{ J(.} <= PerfLevel } >= PROBEST
% where
% HANDLEOBJ - user-defined function with uncertain object definition,
% HANDLEPERF - performance function ('J' in formulae above), built-in
% function names like 'ident' etc allowed,
% PROBEST - (high) probability to achieve,
% VECOBJ = {-1, 0, 1} - flag to show if user-defined uncertain object allow
% vectorization, (default = -1)
% VECPERF = {-1, 0, 1} - flag to show if performance function allow
% vectorization, (default = -1)
%
% EXAMPLE
% tic; PerfLevel = perfwc(@probworstcase_demo_ex, @probworstcase_demo_aux, 0.95, 0.01, 0); t_elapsed = toc;
%
% See also UTEST3.
%
% Calafiore, Dabbene, Tempo, "Randomized Algorithms in Robust Control", CDC42, 2003
% $Id$
% parameter check
if any([Delta >= 1, Delta <= 0])
error(['RACT:perfwc:InputType', 'Delta should be in (0, 1) interval.']);
end
if any([ProbEst >= 1, ProbEst <= 0])
error(['RACT:perfwc:InputType', 'ProbEst should be in (0, 1) interval.']);
end
switch nargin
case 4
VecObj = -1;
VecPerf = -1;
case 5
VecObj = varargin{1};
VecPerf = -1;
case 6
VecObj = varargin{1};
VecPerf = varargin{2};
otherwise
error(['RACT:perfwc:InputSize', ['Incorrect number of parameters: ' num2str(nargin)]]);
end
% estimate number of samples by log-over-log bound
nSamples = ceil(log(1/Delta)/log(1/ProbEst));
disp(['Number of samples by ''log-over-log'' bound is: ' num2str(nSamples)]);
% calculate maximum of performance level
PerfLevel = utest3(hFuncObj, hFuncPerf, 'maxval', nSamples, VecObj, VecPerf);
% no vectorization by object sampling, but vectorization on second crit.
disp(['With probability ' num2str(1-Delta) ', Prob{Performance <= ' num2str(PerfLevel) '} >= ' num2str(ProbEst)]);