function this = chooseNextPoint(this)
% Set this.Next to the next point to evaluate
%

%   Copyright 2016-2022 The MathWorks, Inc.

if this.NumObjectiveEvaluations < size(this.QueuedX,1)
    this.XNext = this.QueuedX(this.NumObjectiveEvaluations + 1, :);
else
    [X, ChoseRandom, this] = chooseNextPointInternal(this);
    % Perform overexploitation loop. Overexploitation is impossible
    % if we have no IncumbentF, because we haven't found any
    % feasible points yet.
    if ~isnan(this.IncumbentF) && ~ChoseRandom && ...
            ismember(this.PrivOptions.AcquisitionFunctionName,{'expectedimprovementplus','expectedimprovementpersecondplus'}) && ...
            exploitingTooMuch(this, X)
        % Save ObjectiveFcnGP
        ObjFcnGP = this.ObjectiveFcnModel;
        iteration = 1;
        BullAdjustmentN = this.NumObjectiveEvaluations;
        while exploitingTooMuch(this, X) && iteration <= this.PrivOptions.MaxExploitIterations
            if this.PrivOptions.Verbose >= 2
                fprintf('%s\n', overexploitDisplay(this, X));
            end
            % Refit GPs
            this = fitBayesOptModels(this, true, BullAdjustmentN, false);

            % Choose the next point (maximize Acquisition Function)
            [X, ~, this] = chooseNextPointInternal(this);
            % Update loop
            iteration = iteration + 1;
            BullAdjustmentN = BullAdjustmentN*10;
        end
        % Restore ObjectiveFcnModel
        this.ObjectiveFcnModel = ObjFcnGP;
    end
    this.XNext = X;
end
end
