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

    function out = mbcOSworkedexample(action, in)
% MBCOSWORKEDEXAMPLE Worked example for CAGE Optimization
%
%  This is an example Optimization script. 
%
%  See also MBCOSTEMPLATE

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Deal with the two action cases
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
switch lower(action)  
    case 'options'
        % Return the updated options object 
        out = i_Options(in);
    case 'evaluate' 
        % Return a handle to the main evaluation routine
        out = i_Evaluate(in);
end

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Options Function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function opt = i_Options(opt)

% Add a name
opt = setName(opt, 'WorkedExample');

% Add a description
opt = setDescription(opt, 'A simple worked example to maximize torque');

% Set up the free variables
opt = setFreeVariablesMode(opt, 'fixed');
opt = addFreeVariable(opt, 'afr');
opt = addFreeVariable(opt, 'spk');

% Set up the objective functions
opt = setObjectivesMode(opt, 'fixed');
opt = addObjective(opt, 'Torque', 'max');

% Set up the constraints
opt = setConstraintsMode(opt, 'fixed');
% There are no constraints for this example

% Set up the operating point sets
opt = setOperatingPointsMode(opt, 'fixed');
% No datasets are needed for this example

% Set up the optimization parameters
opt = addParameter(opt, 'Resolution', {'integer', 'positive'}, 25);
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Evaluation Function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function optimstore = i_Evaluate(optimstore)
% Optimization routine must conform to the following API
% optimstore = <OPTIMIZATION_FUNCTION>(optimstore)
%
% Inputs: optimstore: store of all the CAGE data needed
% Outputs: optimstore: updated object containing all the CAGE data, including outputs

% Get the ranges for SPK and AFR
lb = getLB(optimstore);
ub =  getUB(optimstore);
minAFR = lb(1);
maxAFR = ub(1);
minSPK = lb(2);
maxSPK = ub(2);

% Get the resolution
res = getParam(optimstore, 'Resolution');

% For every fixed point, find the optimum (afr, spk) using
% the mbcweoptimizer routine you have written
[bestafr, bestspk] = mbcweoptimizer(@n_evalTQ, [minAFR, maxAFR], ...
    [minSPK, maxSPK], res);

% Set the best values calculated for the free variable(s) into the output
% data set
optimstore = setFreeVariables(optimstore, [bestafr, bestspk]);

% Return some information about the optimization
OUTPUT.Algorithm = 'Brute force search';
OUTPUT.Resolution = res;

% Set all information in the optimstore
optimstore = setExitStatus(optimstore, 1, 'Optimization Completed');
optimstore = setOutput(optimstore, OUTPUT);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Objective evaluation function 
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    function y = n_evalTQ(afr, spk)

        y = evaluate(optimstore, [afr, spk]);

    end

end