function [x, OPTIONS] = attgoal(FUN,x,GOAL,WEIGHT,OPTIONS,VLB,VUB,GRADFUN,varargin)
%ATTGOAL Solves the multi-objective goal attainment optimization problem.
%   ATTGOAL has been replaced with FGOALATTAIN.  ATTGOAL currently works but
%   will be removed in the future.  Use FGOALATTAIN instead.
%
%   X = ATTGOAL('FUN',X0,GOAL,WEIGHT)
%   tries to make the objective functions (F) supplied by FUN
%   (usually an M-file: FUN.M)  attain the goals (GOAL) by varying X.
%
%   In doing so the following nonlinear programming problem is solved:
%            min     { LAMBDA :  F(X)-WEIGHT.*LAMBDA<=GOAL } 
%          X,LAMBDA  
%
%   The function 'FUN' should return the values of the objectives, F.
%   F=FUN(X).
%
%   X=ATTGOAL('FUN',X0,GOAL,WEIGHT,OPTIONS) allows a vector of optional 
%   parameters to be defined. For more information type HELP FOPTIONS.
%   
%   X=ATTGOAL('FUN',X0,GOAL,WEIGHT,OPTIONS,VLB,VUB) defines a set of 
%   lower and upper bounds on the design variables, X, so that the 
%   solution is in the range VLB <= X <= VUB. 
%
%   X=ATTGOAL('FUN',X0,GOAL,WEIGHT,OPTIONS,VLB,VUB,'GRADFUN') allows a 
%   function 'GRADFUN' to be entered which returns the partial derivatives 
%   of the objective functions:  gf = GRADFUN(X).
%
%   X=ATTGOAL('FUN',X0,GOAL,WEIGHT,OPTIONS,VLB,VUB,'GRADFUN',P1,P2,...) 
%   passes the problem-dependent parameters P1,P2,... directly to the 
%   functions FUN and GRADFUN: FUN(X,P1,P2,...) and GRADFUN(X,P1,P2,...).  
%   Pass empty matrices for OPTIONS, VLB, VUB, and 'GRADFUN' to use the 
%   default values.
%
%   [X,OPTIONS]=ATTGOAL('FUN',X0,GOAL,WEIGHT...) returns the parameters 
%   used in the optimization method.  For example, options(10) contains 
%   the number of function evaluations used.
%   
%   For more details, see the M-file ATTGOAL.M.
%
%   See also FOPTIONS.

%   Copyright (c) 1990-98 by The MathWorks, Inc.
%   $Revision: 1.15 $  $Date: 1998/08/31 22:29:19 $
%   Andy Grace 7-9-90.

         
% ---------------------More Details---------------------------
% [x]=attgoal(x,F,GOAL,WEIGHT,OPTIONS)
% Solves the goal attainment problem where:
%
%  X  Is a set of design parameters which can be varied.
%  F  Is a set of objectives which are dependent on X.
%  GOAL Set of design goals. The optimizer will try to make 
%         F<GOAL, F=GOAL, or F>GOAL depending on the formulation.
%  WEIGHT Set of weighting parameters which determine the 
%         relative under or over achievement of the objectives.
%         Notes:
%           1.Setting WEIGHT=abs(GOAL)  will try to make the objectives
%             less than the goals resulting  in roughly the same 
%             percentage under or over achievement of the goals.
%           2. Setting WEIGHT=-abs(GOAL) will try to make the objectives
%              greater then the goals resulting in roughly the same percentage 
%              under- or over-achievement in the goals.
%           3. Setting WEIGHT(i)=0  indicates a hard constraint.
%              i.e. F<GOAL.
%  OPTIONS OPTIONS(15) indicates the number of objectives for which it is
%      required for the objectives (F) to equal the goals (GOAL). 
%          Such objectives should be partitioned into the first few 
%      elements of F.
%          The remaining parameters determine tolerance settings.
%          For more information type HELP FOPTIONS.
%
%
%   X=ATTGOAL('FUN',X0,GOAL,WEIGHT,OPTIONS,VLB,VUB,'GRADFUN') allows a 
%   function 'GRADFUN' to be entered which returns the partial derivatives 
%   of the function and the  constraints at X:  GRADS = GRADFUN(X).
%
%   X=ATTGOAL('FUN',X0,GOAL,WEIGHT,OPTIONS,VLB,VUB,[],P1,P2,..) allows
%   coefficients, P1, P2, P3 to be passed directly to FUN:
%   F=FUN(X,P1,P2,...).

caller='attgoal';
if nargin < 4, error('attgoal requires four input arguments'); end       
if nargin < 5, OPTIONS=[]; end
if nargin < 6, VLB=[]; end
if nargin < 7, VUB=[]; end
if nargin < 8, GRADFUN=[]; end

lenopt = length(OPTIONS);
if ~lenopt, OPTIONS=0; end

xnew=x(:);
WEIGHT=WEIGHT(:);
GOAL=GOAL(:);
OPTIONS=foptions(OPTIONS);
OPTIONS(7) = ~OPTIONS(7);
neqcstr=OPTIONS(15); 

lenVarIn = length(varargin);
% goalgra and goalobj also take: neqcstr,funfcn,gradfcn,WEIGHT,GOAL,x
goalargs = 6; 

% Convert to inline function as needed.
gradflag = 0;
[funfcn, msg] = prefcnchk(FUN,'attgoal',lenVarIn,gradflag);
if ~isempty(msg)
  error(msg);
end
[ffun, msg] = prefcnchk('goalobj','goal',lenVarIn+goalargs,gradflag);
if ~isempty(msg)
  error(msg);
end
if ~isempty(GRADFUN)
  gradflag = 1;
  [gradfcn, msg] = prefcnchk(GRADFUN,'attgoal',lenVarIn,gradflag);
  if ~isempty(msg)
    error(msg);
  end
  [gfun, msg] = prefcnchk('goalgra','goal',lenVarIn+goalargs,gradflag);
  if ~isempty(msg)
    error(msg);
  end
else
  gradfcn = [];
  gfun = [];
end


[xnew, OPTIONS]=nlconstold(ffun,[xnew;0],OPTIONS,VLB,VUB,gfun,neqcstr,funfcn,gradfcn,WEIGHT,GOAL,x,varargin{:});


OPTIONS(7) = ~OPTIONS(7);
x(:) = xnew(1:length(xnew)-1);
