www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimstore/setFreeVariables.m

    function optimstore = setFreeVariables(optimstore, data,Selected)
%SETFREEVARIABLES Set the optimal values of the free variables
%    OPTIMSTORE = SETFREEVARIABLES(OPTIMSTORE, RESULTS) sets the optimal values of
%    the free variables, as returned by the optimization, into the
%    OPTIMSTORE. RESULTS is a NSOL by NFREEVAR matrix containing many
%    solutions for the optimal values of the free variables. NSOL is the
%    number of solutions and NFREEVAR is the number of free variables. 
%
%    OPTIMSTORE = SETFREEVARIABLES(OPTIMSTORE, RESULTS,SELECTED) sets the selected
%    solution for algorithms with multiple solutions such as multiobjective
%    and multimodal optimization problems. The index must be between 1 and
%    the maximum number of solutions.
% 
%    IMPORTANT NOTE : This function MUST be called at the end of the
%    optimization for the optimal values to be stored.
%
%  See also: CGOPTIMSTORE/GETFREEVARIABLES.

%  Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc.

% Sanity checks - ensure data is numeric. 
if ~isnumeric(data)
    error(message('mbc:cgoptimstore:InvalidArgument12')); 
end

if isv2mode(optimstore)
    % MBC Version 2 script has been run. Need to permute data into
    % NSOL-by-NFREE-by-NRUN
    data = permute(data, [3 2 1]);
else
    % Check data size.
    if ndims(data)==3
        if size(data, 1)==1
            % Convert 3rd dimension to first
            data = permute(data, [3 2 1]);
        else
            error(message('mbc:cgoptimstore:InvalidArgument13'));
        end
    end
end

x0 = getInitialFreeValues(optimstore.OptimRunner);
nFreeVar = size(x0, 2);
if size(data, 2) ~= nFreeVar
    error(message('mbc:cgoptimstore:InvalidArgument14'));
end

optimstore.OptimalFreeValues = data;

if nargin>2
    NumSolutions = size(data,1);
    if ~isempty(Selected) && (~isnumeric(Selected) || ~isscalar(Selected) || ...
            Selected~=fix(Selected) || Selected<0 || Selected>NumSolutions)
        error(message('mbc:cgoptimstore:InvalidArgument15'));
    end
    
    optimstore.SelectedSolution = Selected;
end