⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fgoalattain.m

📁 遗传算法的小程序
💻 M
📖 第 1 页 / 共 2 页
字号:
else
   constflag = 1;
end

line_search = strcmp(optimget(options,'LargeScale',defaultopt,'fast'),'off'); % 0 means trust-region, 1 means line-search
if ~line_search
   warning('Large-scale algorithm not currently available for this problem type.')
   line_search = 1;
end

% If nonlinear constraints exist, need 
%  either both function and constraint gradients, or not
if constflag
   if gradflag & ~gradconstflag
      gradflag = 0;
   elseif ~gradflag & gradconstflag
      gradconstflag = 0;
   end
end

% Convert to inline function as needed
% FUN is called from goalcon; goalfun is based only on x
if ~isempty(FUN)  % will detect empty string, empty matrix, empty cell array
   [funfcn, msg] = optimfcnchk(FUN,'goalcon',length(varargin),gradflag,hessflag);
else
   errmsg = sprintf('%s\n%s', ...
      'FUN must be a function name or inline object;', ...
      ' or, FUN may be a cell array that contains these type of objects.');
   error(errmsg)
end
[ffun, msg] = optimfcnchk(@goalfun,'fgoalattain',lenVarIn+goalargs,gradflag);


if constflag % NONLCON is non-empty
   [confcn, msg] = ...
      optimfcnchk(NONLCON,'goalcon',length(varargin),gradconstflag,0,1);
else
   confcn{1} = '';
end
[cfun, msg]= optimfcnchk(@goalcon,'fgoalattain',lenVarIn+goalargs,gradconstflag,0,1); 

lenvlb=length(l);
lenvub=length(u);
CHG = 1e-7*abs(xnew)+1e-7*ones(numberOfVariablesplus1,1);
i=1:lenvlb;
lindex = xnew(i)<l(i);
if any(lindex),
   xnew(lindex)=l(lindex)+1e-4; 
end
i = 1:lenvub;
uindex = xnew(i)>u(i);
if any(uindex)
   xnew(uindex)=u(uindex);
   CHG(uindex)=-CHG(uindex);
end
x(:) = xnew(1:end-1);

% Evaluate user function to get number of function values at x, not xnew!
switch funfcn{1}
case 'fun'
   user_f = feval(funfcn{3},x,varargin{:});
case 'fungrad'
   user_f = feval(funfcn{3},x,varargin{:});
   
case 'fungradhess'
   user_f = feval(funfcn{3},x,varargin{:});
   
case 'fun_then_grad'
   user_f = feval(funfcn{3},x,varargin{:}); 
case 'fun_then_grad_then_hess'
   user_f = feval(funfcn{3},x,varargin{:}); 
otherwise
   error('Undefined calltype in FGOALATTAIN');
end
user_f = user_f(:);
len_user_f = length(user_f);

% error checking
if length(GOAL) ~= len_user_f
    error('Size of GOAL must be equal to the size of F returned by FUN')
end
if length(WEIGHT) ~= length(GOAL)
    error('Size of WEIGHT must be equal to the size of GOAL.')
end    

GRAD=zeros(numberOfVariablesplus1,1);
HESS = [];
extravarargin= {neqgoals,funfcn,confcn,WEIGHT,GOAL,x,varargin{:}}; 
% Evaluate goal function
switch ffun{1}
case 'fun'
   f = feval(ffun{3},xnew,extravarargin{:});
case 'fungrad'
   [f,GRAD] = feval(ffun{3},xnew,extravarargin{:});
 case 'fungradhess'
   [f,GRAD,HESS] = feval(ffun{3},xnew,extravarargin{:});
case 'fun_then_grad'
   f = feval(ffun{3},xnew,extravarargin{:}); 
   GRAD = feval(ffun{4},xnew,extravarargin{:});
 case 'fun_then_grad_then_hess'
   f = feval(ffun{3},xnew,extravarargin{:}); 
   GRAD = feval(ffun{4},xnew,extravarargin{:});
   HESS = feval(ffun{5},xnew,extravarargin{:});
 otherwise
   error('Undefined calltype in FGOALATTAIN');
end

% Evaluate goal constraints
switch cfun{1}
case 'fun'
   [ctmp,ceqtmp] = feval(cfun{3},xnew,extravarargin{:});
   c = ctmp(:); ceq = ceqtmp(:);
   cGRAD = zeros(numberOfVariablesplus1,length(c));
   ceqGRAD = zeros(numberOfVariablesplus1,length(ceq));
case 'fungrad'
   [ctmp,ceqtmp,cGRAD,ceqGRAD] = feval(cfun{3},xnew,extravarargin{:});
   c = ctmp(:); ceq = ceqtmp(:);
   case 'fun_then_grad'
   [ctmp,ceqtmp] = feval(cfun{3},xnew,extravarargin{:});
   c = ctmp(:); ceq = ceqtmp(:);
   [cGRAD,ceqGRAD] = feval(cfun{4},xnew,extravarargin{:});
 case ''
   c=[]; ceq =[];
   cGRAD = zeros(numberOfVariablesplus1,length(c));
   ceqGRAD = zeros(numberOfVariablesplus1,length(ceq));
otherwise
   error('Undefined calltype in FGOALATTAIN');
end

non_eq = length(ceq);
non_ineq = length(c);
[lin_eq,Aeqcol] = size(Aeq);
[lin_ineq,Acol] = size(A);
[cgrow, cgcol]= size(cGRAD);
[ceqgrow, ceqgcol]= size(ceqGRAD);

eq = non_eq + lin_eq;
ineq = non_ineq + lin_ineq;

if ~isempty(Aeq) & Aeqcol ~= numberOfVariables
   error('Aeq has the wrong number of columns.')
end
if ~isempty(A) & Acol ~= numberOfVariables
   error('A has the wrong number of columns.')
end
if  cgrow~=numberOfVariables & cgcol~=non_ineq
   error('Gradient of the nonlinear inequality constraints is the wrong size.')
end
if ceqgrow~=numberOfVariables & ceqgcol~=non_eq
   error('Gradient of the nonlinear equality constraints is the wrong size.')
end

just_user_constraints = non_ineq - len_user_f - neqgoals;
OUTPUT.algorithm = 'goal attainment SQP, Quasi-Newton, line_search';  % override nlconst output

if diagnostics > 0
   % Do diagnostics on information so far
   msg = diagnose('fgoalattain',OUTPUT,gradflag,hessflag,constflag,gradconstflag,...
      line_search,options,defaultopt,xnew(1:end-1),non_eq,...
      just_user_constraints,lin_eq,lin_ineq,LB,UB,funfcn,confcn,f,GRAD,HESS, ...
      c(1:just_user_constraints),ceq,cGRAD(1:just_user_constraints,:),ceqGRAD);
end

% add extra column to account for extra xnew component
A =[A,zeros(lin_ineq,1)];
Aeq =[Aeq,zeros(lin_eq,1)];

[xnew,ATTAINFACTOR,lambda,EXITFLAG,OUTPUT]=...
   nlconst(ffun,xnew,l,u,full(A),B,full(Aeq),Beq,cfun,options,defaultopt, ...
   verbosity,gradflag,gradconstflag,hessflag,meritFunctionType, ...
   CHG,f,GRAD,HESS,c,ceq,cGRAD,ceqGRAD,neqgoals,funfcn,confcn,WEIGHT,GOAL,x,varargin{:});
just_user_constraints = length(lambda.ineqnonlin) - len_user_f - neqgoals;
lambda.ineqnonlin = lambda.ineqnonlin(1:just_user_constraints);
LAMBDA=lambda;

OUTPUT.algorithm = 'goal attainment SQP, Quasi-Newton, line_search';  % override nlconst output

% compute FVAL since it is attainfactor instead of F(x)
% Evaluate user function to get number of function values at x, not xnew!
x(:)=xnew(1:end-1);
switch funfcn{1}
case 'fun'
   user_f = feval(funfcn{3},x,varargin{:});
case 'fungrad'
   user_f = feval(funfcn{3},x,varargin{:});
case 'fungradhess'
   user_f = feval(funfcn{3},x,varargin{:}); 
case 'fun_then_grad'
   user_f = feval(funfcn{3},x,varargin{:}); 
case 'fun_then_grad_then_hess'
   user_f = feval(funfcn{3},x,varargin{:}); 
otherwise
   error('Undefined calltype in FGOALATTAIN');
end
FVAL = user_f;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -