www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptimdataset/cgoptimdataset.m
classdef cgoptimdataset %CGOPTIMDATASET class to encapsulate operating point sets in CAGE optimization. % Copyright 2009 The MathWorks, Inc. and Ford Global Technologies, Inc. properties (SetAccess=protected) %DATASETS array of pointers to datasets for optimization Datasets = mbcpointer(1,0); %VARIABLES cell array containing dataset variables for each dataset Variables = {}; %OPPOINTVARIABLES variables to use as interpolation variables for dataset OpPointVariables = []; %CONSTRAINTS datasets for each constraint Constraints = mbcpointer(1,0); %OBJECTIVES datasets for each objective Objectives = mbcpointer(1,0); %MODEVARIABLE mode variable ModeVariable = mbcpointer(1,1); %USEMODE use mode variable in interpolating application point set UseMode= false(1,0); end properties (Dependent,SetAccess=protected) %CONSTRAINTINDICES indices for dataset used by constraints ConstraintIndices %OBJECTIVEINDICES indices for dataset used by constraints ObjectiveIndices end methods function DS = cgoptimdataset(oppoints,oppointValues) if nargin>1 DS.Datasets = oppoints; DS.Variables = oppointValues(1:length(oppoints)); if ~isempty(DS.Variables)&& length(DS.Variables{1})==2 % likely to be speed load DS.OpPointVariables = DS.Variables{1}; end DS.UseMode = false(size(oppoints)); end end function S = getSummary(DS,Type,items) %getSummary summary for constraint or objective table in CAGE % % S = getSummary(DS,Type,items); % Type must be Objectives or Constraints S= cell(length(DS.(Type)),1); switch Type case 'Constraints' DSindex= DS.ConstraintIndices; case 'Objectives' DSindex= DS.ObjectiveIndices; end pInterp = DS.OpPointVariables; if ~isempty(pInterp) InterpNames = pveceval(pInterp,@getname); end for i=1:length(S) pDS = DS.(Type)(i); if ~isempty(pInterp)&& DSindex(i) pOtherInp = getInputsFromDataset(items{i}); if DS.UseMode(DSindex(i)) pOtherInp = [pOtherInp DS.ModeVariable]; %#ok<AGROW> end if isempty( pOtherInp ) % data set just contains operating point variables S{i} = sprintf('%s(%s)',DS.(Type)(i).getname,... mbcListString(InterpNames)); else % data set contains operating point variables and % other variables (e.g. weights) that will be used % directly in the optimization. DSvarnames = getDSVarNames(pDS); DSvarnames = DSvarnames(~ismember(DSvarnames,InterpNames)); DSvarnames = intersect( DSvarnames , pveceval(pOtherInp,@getname) ); if ~isempty(DSvarnames) S{i} = sprintf('%s(%s;%s)',pDS.getname,... mbcListString(InterpNames),... mbcListString(DSvarnames)); else S{i} = sprintf('%s(%s)',DS.(Type)(i).getname,... mbcListString(InterpNames)); end end else S{i} = ''; end end end function S = get.ObjectiveIndices(DS) [~,S]= ismember(DS.Objectives,DS.Datasets); % make sure null pointers remain null S(DS.Objectives==0) = 0; end function S = get.ConstraintIndices(DS) [~,S]= ismember(DS.Constraints,DS.Datasets); % make sure null pointers remain null S(DS.Constraints==0) = 0; end function DS = addDataset(DS,pData) %addDataset add dataset to cgoptimdataset % % DS = addDataset(DS,pData) if nargin<2 pData = xregpointer; end DS.Datasets = [DS.Datasets pData]; % add new entry for variables DS.Variables = [DS.Variables {[]}]; % augment UseMode DS.UseMode(end+1)= false; end function DS = deleteDataset(DS,Index) %deleteDataset delete dataset Index from cgoptimdataset % % DS = deleteDataset(DS,Index) OldData= DS.Datasets(Index); DS.Datasets(Index) = []; % remove variable DS.Variables(Index) = []; % remove UseMode DS.UseMode(Index) = []; % set Constraints and Objectives to null DS.Constraints(DS.Constraints==OldData) = xregpointer; DS.Objectives(DS.Objectives==OldData) = xregpointer; end function DS = editDataset(DS,Index,pData) %editDataset change dataset Index % % DS = editDataset(DS,Index,pData) OldData= DS.Datasets(Index); DS.Datasets(Index) = pData; % change Constraints and Objectives DS.Constraints(DS.Constraints==OldData) = xregpointer; DS.Objectives(DS.Objectives==OldData) = xregpointer; end function DS = editVariables(DS,index,pVars) %editVariables defined variables for dataset DS.Variables{index} = pVars; end function DS = editUseMode(DS,index,Use) if ~isnull(DS.ModeVariable) DS.UseMode(index) = Use; else DS.UseMode(index) = false; % error('mbc:cgoptimdataset:InvalidState',... % 'The mode variable must be defined before it can be used.'); end end function DS = editModeVariable(DS,pMode) DS.ModeVariable = pMode; end function DS = editOpPointVariables(DS,pVars) %editOpPointVariables define operating point variables DS.OpPointVariables = pVars; end function DS = sortDataPoints(DS,oldopts,opts) %sortDataPoints sort data points during optimization setup % % DS = sortDataPoints(DS,oldopts,opts) % This method is called from cgoptim.setSetup oldOps = getOperatingPointSetLabels(oldopts); newOps = getOperatingPointSetLabels(opts); [keep, loc] = ismember(newOps, oldOps); N = numOperatingPointSets(opts); pNew = mbcpointer(1, N); pNew(keep) = DS.Datasets(loc(keep)); % Do the same for oppoint required variables cellOldVals = DS.Variables; cellNewVals = cell(1, N); for n = 1:length(cellNewVals) newVars = getOperatingPointSetVarLabels(opts, n); cellNewVals{n} = mbcpointer(1, length(newVars)); if keep(n) %Try to keep matches from previous oppoint oldVars = getOperatingPointSetVarLabels(oldopts, loc(n)); [keepvars, locvars] = ismember(newVars, oldVars); cellNewVals{n}(keepvars) = cellOldVals{loc(n)}(locvars(keepvars)); end end DS.Datasets = pNew; DS.Variables = cellNewVals; end function [pDatasets,HasModeVariable,UseMode] = findDatasets(DS,optim,cgp) %findDatasets find datasets in project % % pDatasets = findDatasets(DS,optim,cgp) % [pDatasets,HasModeVariable,UseMode] = findDatasets(DS,optim,cgp) if isempty(DS.OpPointVariables) pDatasets = mbcpointer(1,0); return end if canAddOperatingPointSet(getSetup(optim)) % consider all datasets in project A = cgp.getConnections; pDatasets = A.pData(find(A,'Type','Dataset')); else % data set must already be in optimization and assigned either % in optimization wizard or using data set manager pDatasets = DS.Datasets; pDatasets = pDatasets(~isnull(pDatasets)); end keep = false(size(pDatasets)); HasModeVariable = keep; UseMode = keep; for i=1:length(pDatasets) % check that data set is valid pThisDS = pDatasets(i); keep(i) = checkAppPointSet(DS,pThisDS,optim)==0; if nargout>1 && ~isnull(DS.ModeVariable) % check whether mode variable is in dataset HasModeVariable(i) = keep(i) && any(get(pThisDS.info,'ptrlist')==DS.ModeVariable); DSInd = pThisDS==DS.Datasets; UseMode(i) = HasModeVariable(i) && any(DSInd) && DS.UseMode(DSInd); end end pDatasets = pDatasets(keep); HasModeVariable = HasModeVariable(keep); UseMode = UseMode(keep); end function OK = isAppPointSet(DS,pDS) %isAppPointSet true if data set is being used by an objective %or constraint % % OK = isAppPointSet(DS,pDS) OK = any(DS.Constraints==pDS) || any(DS.Objectives==pDS); end function OK = isAppPointOptim(DS) %isAppPointOptim trif any data set is used by an objective or constraint OK = any(~isnull(DS.Constraints)) || any(~isnull(DS.Objectives)); end function DS = addConstraint(DS,N) %addConstraint add constraint to cgoptimdataset % % DS = addConstraint(DS,N); % add N constraints to cgoptimdataset % DS = addConstraint(DS,pData); % add a constraint with the application point set pData if nargin<2 N = 1; end if isa(N,'xregpointer'); pData = N; if ~isnull(pData) && ~any(pData==DS.Datasets) %don't add null datasets DS=addDataset(DS,pData); end else % use null data set pData = mbcpointer(1,N); end DS.Constraints = [DS.Constraints pData]; end function DS = deleteConstraint(DS,Index) %DELETECONSTRAINT delete constraint Index % % DS = deleteConstraint(DS,Index) DS.Constraints(Index) = []; end function DS = editConstraint(DS,Index,pData) %editConstraint change data set for constraint Index % % DS = editConstraint(DS,Index,pData) DS.Constraints(Index) = pData; if ~isnull(pData) && ~any(pData==DS.Datasets) %new dataset. Don't add null data set DS=addDataset(DS,pData); end end function DS = copyConstraint(DS,DSsource,index) %COPYCONSTRAINT copy constraint from other cgoptimset % DS = copyConstraint(DS,DSsource,index); pData = DSsource.Constraints(index); DS = addConstraint(DS,pData); if ~isnull(pData) && DS.ModeVariable==DSsource.ModeVariable % copy UseMode setting if the mode variables are the same DS.UseMode(pData==DS.Datasets) = DSsource.UseMode(pData==DSsource.Datasets); end end function DS = addObjective(DS,N) %addObjective add objective to cgoptimdataset % % DS = addObjective(DS,N); % add N objectives to cgoptimdataset % DS = addObjective(DS,pData); % add a objective with the application point set pData if nargin<2 N = 1; end if isa(N,'xregpointer'); pData = N; if ~isnull(pData) && ~any(pData==DS.Datasets) %add new dataset but not if it is null DS=addDataset(DS,pData); end else pData = mbcpointer(1,N); end DS.Objectives = [DS.Objectives pData]; end function DS = deleteObjective(DS,Index) %deleteObjective delete objective Index % % DS = deleteObjective(DS,Index) DS.Objectives(Index) = []; end function DS = editObjective(DS,Index,pData) %editObjective change data set for objective Index % % DS = editObjective(DS,Index,pData) DS.Objectives(Index) = pData; if ~isnull(pData) && ~any(pData==DS.Datasets) DS=addDataset(DS,pData); end end function del = cleanup(DS) %cleanup returns indices which data sets are no longer used % % del = cleanup(DS) % del is in reverse order so it is easy to remove datasets from % optimization (using cgoptim.deleteOppoint). del = false(size(DS.Datasets)); for i=1:length(DS.Datasets) pDS = DS.Datasets(i); if ~isAppPointSet(DS,pDS); % dataset not used in any objective or constraint %don't delete it if it has variables del(i) = isempty(DS.Variables{i}); end end del = find(del); del = del(end:-1:1); end function DS = mapptr(DS,RefMap) %mapptr mapping pointers during load DS.Datasets = mapptr(DS.Datasets,RefMap); for i=1:length(DS.Datasets) DS.Variables{i} = mapptr(DS.Variables{i},RefMap); end DS.OpPointVariables = mapptr(DS.OpPointVariables,RefMap); DS.Constraints = mapptr(DS.Constraints,RefMap); DS.Objectives = mapptr(DS.Objectives,RefMap); DS.ModeVariable = mapptr(DS.ModeVariable,RefMap); end function ptrs = getDependentPtrs(DS) %getDependentPtrs list of dependencies for cgprojconnections ptrs = unique([DS.Datasets, DS.Variables{:} DS.OpPointVariables DS.ModeVariable]); ptrs(isnull(ptrs))=[]; end end methods(Static) function DS = loadobj(DS) if isstruct(DS) || isempty(DS.ModeVariable) DS.ModeVariable = xregpointer; end if isstruct(DS) || length(DS.Datasets)~=length(DS.UseMode) DS.UseMode = false(size(DS.Datasets)); end if isstruct(DS) && isfield(DS,'ApplyToAll') DS= rmfield(DS,'ApplyToAll'); end if isstruct(DS) NewDS = cgoptimdataset; props = fieldnames(DS); for i=1:length(props) NewDS.(props{i}) = DS.(props{i}); end DS = NewDS; end end end methods (Static,Access=protected) function [status,msg] = checkVariables(pThisDS,pThisReqVar) %checkVariables check whether required variables are in dataset % % [status,msg] = checkVariables(pThisDS,pThisReqVar) status = 0; msg = ''; if ~isempty(pThisReqVar) MissingVar = ~ismember(pThisReqVar, pThisDS.get('ptrlist')); if any(MissingVar) status = 2; if sum(MissingVar)>1 nms = mbcListString(pveceval(pThisReqVar(MissingVar), @getname),' '); msg = sprintf('Data set does not contain the required variables %s', ... nms); else msg = sprintf('Data set does not contain the required variable %s', ... pThisReqVar(MissingVar).getname); end end end end function pDSinputs = getDatasetVariables(pThisDS) %getDatasetVariables - list of pointers to data set variables % % pDSinputs = getDatasetVariables(pThisDS) pDSinputs = get(pThisDS.info,'ptrlist'); pDSinputs = pDSinputs(get(pThisDS.info,'factor_type')==1); end end end function VarNames = getDSVarNames(pThisDS) %getDSVarNames get dataset variable names % % VarNames = getDSVarNames(pThisDS) % also looks at external data names (no pointers) op = pThisDS.info; pDSinputs = get(op,'ptrlist'); SelVars = get(op,'factor_type')==1 | ... get(op,'factor_type')==2 & isnull(pDSinputs); VarNames = get(op,'factors'); VarNames = VarNames(SelVars); end