
function [pts,value]=optimal_process_symm(...
    pts_in,deg,mildness,family_type,unc_method,de_parms)

if nargin < 6
    unc_method=0;
end

display_stats=1;

%-------------------------
% BUILDING INITIAL VALUE.
%-------------------------

if length(pts_in) == 0
    
    fprintf('\n \t %% WARNING: RANDOM INITIAL VALUE.')
    
%    if n3 > 0 % ORBIT 3 (-1,1).
%        x3=rand(n3,1); x3=2*x3-1;
%    else
%        x3=[];
%    end
%    if n6 > 0 % ORBIT 6 (0,1).
%        x6x=rand(n6,1); x6y=(1-x6x).*rand(n6,1); x6=[x6x; x6y];
%    else
%        x6=[];
%    end
%    tot = (deg^2+3*deg+2)/2;
%    pts_add_x = rand(tot,1).*2.-1;
%    pts_add_y = rand(tot,1).*2.-1;
%    pts_in=[pts_add_x pts_add_y];
end

%-------------------------
% OPTIMIZATION.
%-------------------------

[options,degree_leb,maxits,maxtol,tolx]=option_settings_unconstrained(...
    deg,mildness,unc_method,pts_in);

switch family_type
    case 1 % FEKETE.
        V_leb=[];
    case 2 % LEBESGUE.
        pts_leb=tens_cub_sq(250);

     V_leb = chebvand2d(deg,pts_leb);		
    case 3 % L2 OPTIMALITY. CHEN-BABUSKA.
        V_leb=[];
end

% OPTIMIZATION.
unc_method=abs(unc_method);

switch unc_method

    case 0
        pts_out = fminsearch(@(x) target_orbits(x,...
            deg,V_leb,family_type),pts_in,options);



    case 6 % DIFFERENTIAL EVOLUTION.
%		x_in = [pts_in(:,1); pts_in(:,2)];
        F_VTR=de_parms(3);
        I_D=length(pts_in);
        F_weight = 0.5;
        F_CR = 0.9;
        mydelta=de_parms(2);
        xlow=(pts_in-mydelta)';
        xup=(pts_in+mydelta)';
%		xlow = max(xlow,-1);
%		xup = min(xup,1);
        maxit=de_parms(1);

        %fprintf('\n \t DE IN')
        [pts_out,S_y,I_nf]=myRundeopt(F_VTR,I_D,F_weight,F_CR,xlow,...
            xup,maxit,deg,V_leb,...
            family_type);
        %fprintf('\n \t DE OUT')
%		pts_out = [x_out(1,1:I_D/2)' x_out(1,I_D/2+1:length(x_out))'];
        if size(pts_out,1) < size(pts_out,2)
            pts_out = pts_out';
        end
        
    case 7 % CONSTRAINED (ACTIVE SET).

        LB=[-ones(n3,1)+10^(-6); zeros(2*n6,1)+10^(-6)];
        UB=[ones(n3,1)-10^(-6); ones(2*n6,1)-10^(-6)];
        A=[zeros(n6,n3) eye(n6) eye(n6)];
        B=ones(n6,1);

        pts_out = fmincon(@(x) target_orbits(x,deg,V_leb,...
            family_type),pts_in,A,B,[],[],LB,UB,[],options);
        
    case 8 % GENETIC ALGORITHM.

        
        LB=[-ones(n3,1)+10^(-6); zeros(2*n6,1)+10^(-6)];
        UB=[ones(n3,1)-10^(-6); ones(2*n6,1)-10^(-6)];
        A=[zeros(n6,n3) eye(n6) eye(n6)];
        B=ones(n6,1);

        NVARS=length(x_in);
        pts_out = ga(@(x) target_orbits(x,deg,V_leb,...
            family_type),NVARS,A,B,[],[],LB,UB,[],options);

    case 9
        LB=[-ones(n3,1)+10^(-6); zeros(2*n6,1)+10^(-6)];
        UB=[ones(n3,1)-10^(-6); ones(2*n6,1)-10^(-6)];
        A=[zeros(n6,n3) eye(n6) eye(n6)];
        B=ones(n6,1);

        pts_out = simulannealbnd(@(x) target_orbits(x,deg,V_leb,...
            family_type),pts_in,A,B,[],[],LB,UB,[],options);

    case 10
        LB=[-ones(n3,1)+10^(-6); zeros(2*n6,1)+10^(-6)];
        UB=[ones(n3,1)-10^(-6); ones(2*n6,1)-10^(-6)];
        A=[zeros(n6,n3) eye(n6) eye(n6)];
        B=ones(n6,1);

        pts_out = patternsearch(@(x) target_orbits(x,deg,V_leb,...
            family_type),pts_in,A,B,[],[],LB,UB,[],options);

    otherwise
        pts_out = fminsearch(@(x) target_orbits(x,...
            deg,V_leb,family_type),pts_in,options);
end


%----------------------------------
% SEE IF THERE ARE IMPROVEMENTS.
%----------------------------------

switch family_type

    case 1

        value_in=target_orbits(pts_in,deg,V_leb,...
            family_type); value_in=abs(value_in);
        value_out=target_orbits(pts_out,deg,V_leb,...
            family_type); value_out=abs(value_out);

        if display_stats == 1
            fprintf('\n \t %% [Meth: %2.0f / Mild: %2.0f] IN: %2.5e OUT: %2.5e DIFF: %1.1e',...
                unc_method,mildness,value_in,value_out,abs(value_in-value_out));
        end

        if value_in > value_out
            pts_out=pts_in;
            value=value_in;
        else
            value=value_out;
        end
        pts=pts_out;

    case 2

		pts_to_in = [cos(pts_in(1:(length(pts_in)/2),1)) cos(pts_in((length(pts_in)/2+1):end,1))];
		pts_to_out = [cos(pts_out(1:(length(pts_out)/2),1)) cos(pts_out((length(pts_out)/2+1):end,1))];
%		V_pts_in=dubiner_leg(deg,pts_to_in);
        V_pts_in = chebvand2d(deg,pts_to_in);		
        value_in=norm(V_pts_in'\V_leb',1);

%		V_pts_out = dubiner_leg(deg,pts_to_out);
        V_pts_out = chebvand2d(deg,pts_to_out);		
        value_out=norm(V_pts_out'\V_leb',1);

        if display_stats == 1
            fprintf('\n \t %% [Meth: %2.0f / Mild: %2.0f] IN: %2.5e OUT: %2.5e DIFF: %1.1e',...
                unc_method,mildness,value_in,value_out,abs(value_in-value_out));
        end

        if value_in < value_out
            pts_out=pts_in; value=value_in; pts=pts_in;
        else
            value=value_out; pts=pts_out;
        end

    case 3

        value_in=target_orbits(pts_in,deg,V_leb,...
            family_type); value_in=abs(value_in);
        value_out=target_orbits(pts_out,deg,V_leb,...
            family_type); value_out=abs(value_out);

        if display_stats == 1
            fprintf('\n \t %% [Meth: %2.0f / Mild: %2.0f] IN: %2.5e OUT: %2.5e DIFF: %1.1e',...
                unc_method,mildness,value_in,value_out,abs(value_in-value_out));
        end

        if value_in < value_out
            pts_out=pts_in;
            value=value_in;
        else
            value=value_out;
        end
        pts=pts_out;

end
















%-------------------------
% ATTACHED FUNCTIONS.
%-------------------------

function y=target_orbits(pts_in,deg,V_leb,...
    family_type)


% BARYCENTRIC COORDINATES SET.
pts=g(pts_in);

% SYMPLEX COORDINATES.
%pts=pts_bar(:,1:2);

% SEE IF THE POINTS BELONG TO THE SIMPLEX.
XV=[1 -1 -1 1 1]; YV=[1 1 -1 -1 1]; X=pts(:,1); Y=pts(:,2); L=length(X);
[IN,ON]=inpolygon(X,Y,XV,YV);
if (sum(IN)+sum(ON)) < L % SOME POINTS ARE OUTSIDE THE SIMPLEX.
    switch family_type
        case 1
            y=eps;
        case 2
            y=10^20;
        case 3
            y=10^20;
    end
    return
end

% TARGET EVALUATION.
%V=dubiner_leg(deg,pts);
V = chebvand2d(deg,pts);		

%table=find_orbits(deg);
[lebs,dets,conds,errs,lagrs,l2opts]=do_stats_sh(deg,pts,1,1);
%pause


switch family_type
    case 1 % FEKETE.
        y=-(abs(det(V)));
    case 2 % LEBESGUE.
        y=norm(V'\V_leb',1);
    case 3 % L2 OPTIMAL: CHEN-BABUSKA.
        y=sum(diag(inv(V'*V)));
end

% DEFAULT VALUE CORRECTION IN CASE OF "y=NaN"or .
if (isnan(y) == 1) |  (abs(y) == Inf)
    switch family_type
        case 1
            y=eps;
        case 2
            y=10^20;
        case 3
            y=10^20;
    end
end

% fprintf('\n \t LOCAL VALUE: %2.5e',y); 
















function [options,lebdeg,maxfuns,maxtol,tolx]=option_settings_unconstrained(...
    deg,mildness,unc_method,x_in)

switch mildness

    case -2
        if deg < 7
            lebdeg=250;
        else
            if deg < 12
                lebdeg=250;
            else
                lebdeg=250;
            end
        end
        maxfuns=2000; maxtol=10^(-400); tolx=maxtol;

    case -1
        if deg < 7
            lebdeg=70;
        else
            if deg < 12
                lebdeg=50;
            else
                lebdeg=30;
            end
        end
        maxfuns=500; maxtol=10^(-400); tolx=maxtol;

    case 0
        if deg < 7
            lebdeg=30;
        else
            if deg < 12
                lebdeg=25;
            else
                lebdeg=15;
            end
        end
        maxfuns=100; maxtol=10^(-12); tolx=maxtol;

    case 1
        if deg < 7
            lebdeg=35;
        else
            if deg < 12
                lebdeg=30;
            else
                lebdeg=20;
            end
        end
        maxfuns=100; maxtol=10^(-12); tolx=maxtol;

    case 2
        if deg < 7
            lebdeg=50;
        else
            if deg < 12
                lebdeg=40;
            else
                lebdeg=25;
            end
        end
        maxfuns=75; maxtol=10^(-12); tolx=maxtol;



    case 3
        if deg < 7
            lebdeg=70;
        else
            if deg < 12
                lebdeg=50;
            else
                lebdeg=30;
            end
        end
        maxfuns=50; maxtol=10^(-12); tolx=maxtol;

    case 4

        if deg < 7
            lebdeg=80;
        else
            if deg < 12
                lebdeg=60;
            else
                lebdeg=40;
            end
        end

        maxfuns=50; maxtol=10^(-12);  tolx=maxtol;

    case 5
        if deg < 7
            lebdeg=100;
        else
            if deg < 12
                lebdeg=70;
            else
                lebdeg=50;
            end
        end
        maxfuns=50; maxtol=10^(-12); tolx=maxtol;

    case 6
        if deg < 7
            lebdeg=135;
        else
            if deg < 12
                lebdeg=85;
            else
                lebdeg=60;
            end
        end
        maxfuns=50; maxtol=10^(-12); tolx=maxtol;

    case 7
        if deg < 7
            lebdeg=170;
        else
            if deg < 12
                lebdeg=120;
            else
                lebdeg=80;
            end
        end
        maxfuns=50; maxtol=10^(-14); tolx=maxtol;

    case 8

        if deg < 7
            lebdeg=250;
        else
            if deg < 12
                lebdeg=150;
            else
                lebdeg=100;
            end
        end
        maxfuns=15; maxtol=10^(-400);  tolx=10^(-60);

    case 9
        if deg < 7
            lebdeg=270;
        else
            if deg < 12
                lebdeg=170;
            else
                lebdeg=120;
            end
        end
        maxfuns=15; maxtol=10^(-400); tolx=maxtol;

    case 10

        if deg < 7
            lebdeg=300;
        else
            if deg < 12
                lebdeg=200;
            else
                lebdeg=140;
            end
        end
        maxfuns=15; maxtol=10^(-400);  tolx=10^(-60);

    case 11

        if deg < 7
            lebdeg=325;
        else
            if deg < 12
                lebdeg=250;
            else
                lebdeg=170;
            end
        end
        maxfuns=15; maxtol=10^(-400);  tolx=10^(-60);

    case 12

        if deg < 7
            lebdeg=350;
        else
            if deg < 12
                lebdeg=275;
            else
                lebdeg=185;
            end
        end
        maxfuns=20; maxtol=10^(-400);  tolx=10^(-60);

    case 13

        if deg < 7
            lebdeg=400;
        else
            if deg < 12
                lebdeg=300;
            else
                lebdeg=200;
            end
        end
        maxfuns=50; maxtol=10^(-400);  tolx=10^(-60);

    case 14

        if deg < 7
            lebdeg=425;
        else
            if deg < 12
                lebdeg=325;
            else
                lebdeg=250;
            end
        end
        maxfuns=50; maxtol=10^(-400);  tolx=10^(-60);

    case 15

        if deg < 7
            lebdeg=450;
        else
            if deg < 12
                lebdeg=350;
            else
                lebdeg=300;
            end
        end
        maxfuns=50; maxtol=10^(-400);  tolx=10^(-60);

    case 16

        if deg < 7
            lebdeg=500;
        else
            if deg < 12
                lebdeg=400;
            else
                lebdeg=350;
            end
        end
        maxfuns=50; maxtol=10^(-400);  tolx=10^(-60);

    otherwise

        if deg < 7
            lebdeg=600;
        else
            if deg < 12
                lebdeg=500;
            else
                lebdeg=400;
            end
        end
        maxfuns=50; maxtol=10^(-400);  tolx=10^(-60);

end

switch unc_method

    case  0 % fminsearch/fminunc basics.
        options = optimset('MaxIter',max(15,ceil(maxfuns/3)),'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','iter','Diagnostic','off');

    case -1 % BFGS (QUIET).
        options = optimset('MaxIter',maxfuns,'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','off','LargeScale','off',...
            'Hessian','off','HessUpdate','bfgs',...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case 1 % BFGS.
        options = optimset('MaxIter',maxfuns,'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','iter','LargeScale','off',...
            'Hessian','off','HessUpdate','bfgs',...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case  -2 % DFP (QUIET).
        options = optimset('MaxIter',maxfuns,'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','off','LargeScale','off',...
            'Hessian','off','HessUpdate','dfp',...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case  2 % DFP.
        options = optimset('MaxIter',maxfuns,'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','iter','LargeScale','off',...
            'Hessian','off','HessUpdate','dfp',...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case -3 % STEEPDESC.
        options = optimset('MaxIter',maxfuns,'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','off','LargeScale','off',...
            'Hessian','off','HessUpdate','steepdesc',...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case 3 % STEEPDESC.
        options = optimset('MaxIter',maxfuns,'MaxFunEvals',1000*maxfuns,...
            'TolX',tolx,'TolFun',tolx,'Display','iter','LargeScale','off',...
            'Hessian','off','HessUpdate','steepdesc',...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case -7 % CONSTRAINED.
        options=optimset('MaxIter',1e4,'MaxFunEvals',...
            1e4,'Display','off','Algorithm','active-set','TolCon',...
            1e-400,'ObjectiveLimit',1e300,'TolX',1e-60,'TolFun',1e-60,...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));


    case 7 % CONSTRAINED.
        options=optimset('MaxIter',1e4,'MaxFunEvals',...
            1e4,'Display','iter','Algorithm','active-set','TolCon',...
            1e-400,'ObjectiveLimit',1e300,'TolX',1e-60,'TolFun',1e-60,...
            'FunValCheck','on','RelLineSrchBnd',10^(-2),...
            'RelLineSrchBndDuration',maxfuns,'DiffMaxChange',10^(-3));

    case -8 % GENETIC ALGORITHM (QUIET).
        options=gaoptimset('Generations',100);


    case 8 % GENETIC ALGORITHM.
        options=gaoptimset('Generations',100);

    case -9 % SIMULATED ANNEALING ALGORITHM. (QUIET).
        options=saoptimset;


    case 9 % SIMULATED ANNEALING ALGORITHM.
        options=saoptimset;
        
    case -10 % PATTERN SEARCH ALGORITHM (QUIET).
        options=psoptimset('MaxIter',100,'MaxFunEvals',5000,'Display','off');


    case 10 % PATTERN SEARCH ALGORITHM.
        options=psoptimset('MaxIter',100,'MaxFunEvals',5000,'Display','iter');

    otherwise
        options=[];

end























