www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@xregdesign/generateConstrainedSpaceFill.m

    function [des, cs]=generateConstrainedSpaceFill(des, cs, targetPoints, maxIter)
%generateConstrainedSpaceFill Make a space filling (Halton or Sobal)
% sequence with a specified number of constrained points

%  Copyright 2015 The MathWorks, Inc.

% error if it is not a space filling design
if nargin < 4
    maxIter = 10;
end
if isempty(cs)
    [~, cs] = getstyle(des);
end

if ~(isa(cs,'cset_spacefillhalton') || isa(cs,'cset_spacefillsobol') ...
        || isa(cs,'cset_lhs') || isa(cs,'cset_lattice') || isa(cs, 'cset_spacefilllatt'))
    error('mbc:xregdesign:CannotGenerate', 'Can only generate a space filling design')
end

if isa(cs,'cset_spacefillhalton') || isa(cs,'cset_spacefillsobol')
    % increase for sobal or halton sequence
    [des,cs] = increaseSobolOrHalton(des,cs,targetPoints,maxIter);
else
    [des,cs] = increaseSequence(des,cs,targetPoints,maxIter);
end

function [des,cs] = increaseSobolOrHalton(des,cs,targetPoints,maxIter)
currIter = 0;
pointsInDesign = 0;
nextGuess = targetPoints;

% increase points by increasing total points and iterating
while pointsInDesign<targetPoints && currIter<maxIter
    if nextGuess > 1e8
        error('mbc:xregdesign:ExceededMaxIter', ...
        'Reached maximum number of iteration without finding the required number of points')
    end
    % update size of candidate set and generate design
    [des,cs] = createDesignWithNPoints(des,cs,nextGuess);
    pointsInDesign = des.npoints;
    % make next guess based on points currently in
    nextGuess = ceil(nextGuess * targetPoints/max(pointsInDesign,1));
    currIter = currIter+1;
end

if currIter==maxIter && pointsInDesign<targetPoints
    error('mbc:xregdesign:ExceededMaxIter', ...
        'Reached maximum number of iteration without finding the required number of points')
end

if pointsInDesign>targetPoints
    % if more valid points were found than needed, trim the size of the candidate set
    allCandPoints = fullset(cs);
    allValidPoints = des.design;
    [~, ind] = ismembertol(allValidPoints(targetPoints, :), allCandPoints,'ByRows',true);
    [des,cs] = createDesignWithNPoints(des,cs,ind);
end

function [closestdes,closestcs] = increaseSequence(des,cs,targetPoints,maxIter)
currIter = 0;
pointsInDesign = des.npoints;
nextGuess = targetPoints;

rngSettings = rng();

closestConstrainedPoints = Inf;
closestdes = [];
closestcs = [];
% increase points by increasing total points and iterating
while (currIter==0 || pointsInDesign~=targetPoints) && currIter<maxIter
    % reset rng so that all designs will use the same initial seed
    rng(rngSettings);
    % update size of candidate set and generate design
    [des,cs] = createDesignWithNPoints(des,cs,nextGuess);
    pointsInDesign = des.npoints;
    
    if abs(pointsInDesign-targetPoints) < closestConstrainedPoints
        closestConstrainedPoints = pointsInDesign;
        closestdes = des;
        closestcs = cs;
    end
    % make next guess based on points currently in
    currGuess = nextGuess;
    nextGuess = ceil(currGuess * targetPoints/max(pointsInDesign,1));
    currIter = currIter+1;
end


function [des,cs] = createDesignWithNPoints(des,cs,nextGuess)
cs = set(cs, 'n', nextGuess);
cs = generate(cs);
des = SpaceFillDesign(des,cs,'replace','constrain');