www.gusucode.com > mbcdesign 工具箱 matlab 源码程序 > mbcdesign/@des_constraints/getOptimConstraints.m
function [consFcn, A, b] = getOptimConstraints(des, model) %GETOPTIMCONSTRAINTS Get constraints for use in optimization % % [CONSFCN, A, B] = GETOPTIMCONSTRAINTS(DES) % [CONSFCN, A, B] = GETOPTIMCONSTRAINTS(DES, MODEL) % % % See also DES_CONSTRAINTS, XREGDESIGN/GETOPTIMCONSTRAINTS. % Copyright 2005 The MathWorks, Inc. nConstraints = length( des.Constraints ); consFcn = ''; A = []; b = []; if nConstraints >= 1, % If there is no model then we use the constraint in natural units and % we can always use linear constraints if nargin < 2, consFcn = @n_Constraint; isAllowedLinear = true; else % But if there is a model, then we need to use the coded constraint % and check that linear coding is being used before using lienar % constraints consFcn = @n_CodedConstraint; [src, g, tgt] = getcode( model ); isAllowedLinear = all( cellfun( 'isempty', g ) ) ... && ~any( isinf( src(:) ) ) && ~any( isinf( tgt(:) ) ); end % Linear constraints tfIsLinear = false( 1, nConstraints ); if isAllowedLinear, for i = 1:nConstraints, tfIsLinear(i) = islinear( des.Constraints{i} ); end end if any( tfIsLinear ), [A, b] = i_MakeLinearConstraints( des.Constraints(tfIsLinear) ); if nargin >= 2, [A, b] = i_CodeLinearConstraints( src, tgt, A, b ); end end % Non-linear constraints if all( tfIsLinear ), consFcn = ''; else nonLinearConstraints = des.Constraints(~tfIsLinear); end end function [g, geq] = n_Constraint( X ) geq = []; dim = find( size( X ) == length( des.Factors ) ); g = zeros( size( X, dim ), length( nonLinearConstraints ) ); if dim == 1; X = X.'; end for i = 1:size( X, 1 ), for j = 1:length( nonLinearConstraints ), g(i,j) = constraintDistance( nonLinearConstraints{j}, X(i,:) ); end end end function [g, geq] = n_CodedConstraint( X ) geq = []; g = n_Constraint( invcode( model, X ) ); end end %------------------------------------------------------------------------------| function [A, b] = i_MakeLinearConstraints( cons ) % CONS should be a cell array of linear constraints A = cell( size( cons ) ); b = cell( size( cons ) ); for i = 1:length( cons ), [A{i}, b{i}] = getLinearForm( cons{i} ); end A = cat( 1, A{:} ); b = cat( 1, b{:} ); end %------------------------------------------------------------------------------| function [A, b] = i_CodeLinearConstraints( src, tgt, A, b ) % The given constraint is A*x <= b, where x is natural units. For % y in coded units the corresponding point x in natural units is % given by x = D*y + C where D is a diagonal matrix of half-widths % and C a vector of centers of the range of the factors. Thus the % constraint is % % A*x <= b % A*(D*y + C) <= b % A*D*y + A*C <= b % A*D*y <= b - A*C % i.e., % new.A = A * D % new.b = b - A*C D = (src(:,2) - src(:,1))./(tgt(:,2) - tgt(:,1)); C = (src(:,2) + src(:,1))./(tgt(:,2) - tgt(:,1)); b = b - A * C; A = A * diag( D ); end %------------------------------------------------------------------------------| % EOF %------------------------------------------------------------------------------|