www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cgexprconstraint/createOptimConstraint.m

    function [optimitem, optim] = createOptimConstraint(obj, optim)
%CREATEOPTIMCONSTRAINT Create an optimization constraint object.
%
%   [OPTIMITEM, OPTIM] = CREATEOPTIMCONSTRAINT(OBJ, OPTIM) creates and
%   returns an appropriate optimization object from the cgconstraint.  The
%   new item may or may not contain the address to this cgconstraint.

%   Copyright 2005 The MathWorks, Inc.


if isempty(obj.V0SumUpgradeData)
    % Create a standard model constraint
    pNew = xregpointer(1);
    obj = setaddress(obj, pNew);
    optimitem = cgpointconstraint(getname(obj), pNew);
else
    % Create a summation constraint
    pM = getinputs(obj);
    if ~isempty(pM)
        pM = pM(1);
    else
        pM = xregpointer;
    end
    
    comp = getcomparisonoperator(obj);
    switch comp
        case '<='
            op = 'le';
        case '=='
            op = 'eq';
        case '>='
            op = 'ge';
    end
    optimitem = cgsumconstraint(getname(obj), pM, op, obj.CompValue);
    
    SumData = obj.V0SumUpgradeData;
    
    % Set weights data via the cgoptim
    pWts = getInputs(optimitem);
    pWts = pWts(1);
    nR = getNumRuns(optim);

    % Copy same weights in for each run
    WtsVals = SumData.Weights(:)';
    WtsVals = repmat(WtsVals, nR,1);

    setinitialvaluedata(optim, pWts, 1:nR, WtsVals);
end