%Robustness_precooling: Try to optimize the precooling model for various
%starting points and disturbances and record how often it converges to a
%solution.

clc
clear all

global h
h=actxserver('UnisimDesign.Application');

load x0_precooling
load dist_precooling

x0 = x0_precool ;

lb = [12/3600 ; 60.5/3600 ; 1100 ; 420 ; 210 ; 105.0 ] ;
ub = [12.6/3600 ; 64/3600 ; 1300 ; 480 ; 250 ; 125 ] ;

%Scale limits and starting guess.

scale_x = diag(10.^(fix(log10(ub - lb)))) ;

x0 = scale_x\x0 ;
lb = scale_x\lb ;
ub = scale_x\ub ;

%Declare parameters (scaling matrices and constraint type).
parameters.scale_x = scale_x ;
parameters.scale_c=diag([60 60 120 1 1]);
% parameters.constraint = 'enthalpy' ;
parameters.constraint = 'temperature' ;

% NoOfPoints = 10 ;
% X = zeros(6,NoOfPoints) ;
% for i=1:length(lb)
%     X(i,:) = lb(i) + (ub(i)-lb(i))*rand(1,NoOfPoints) ;
% end

%Now we have 50 more or less randomly distributed starting points.

%'interior-point' 'active-set' ,'DiffMinChange',1e-6,'DiffMaxChange',1e-2
options = optimset('Display','final','TolFun',1e-3,'TolCon',1e-1,'Algorithm','interior-point','MaxFunEvals',1000,'MaxIter',100,'DiffMinChange',1e-4);

% if NoOfPoints < 10
%     options.Display = 'iter';
% end

% fail = 0 ; % Failure counter
% for i=1:NoOfPoints
%     x0 = X(:,i) ;
%     [x_opt,J_opt,exitflag,output,lambda] = fmincon(@Precooling_obj,x0,[],[],[],[],lb,ub,@Precooling_constraints,options,d0,parameters) ;
%     if exitflag > 0
%        % disp('Optimization successful')
%     elseif exitflag == 0
%         %disp('Maximum iterations used')
%         fail = fail + 1 ;
%     elseif exitflag < 0
%         %disp('Convergence failure')
%         fail = fail + 1;
%     end
% end

% disp([num2str(fail) ' out of '  num2str(NoOfPoints) ' optimizations failed'])

NoOfPoints = 5 ;
if NoOfPoints < 10
    options.Display = 'iter';
end
chosenones = [7 ] ;
pert=linspace(0.95,1.05,NoOfPoints);
x0(3) = 1500/1100*x0(3) ; % Give a high starting value for ph_c3, to make sure it is above condensation pressure when cooling temperature changes!

lb = [10/3600 ; 55/3600 ; 1100 ; 420 ; 210 ; 102.0 ] ;
ub = [14.5/3600 ; 70/3600 ; 1500 ; 480 ; 250 ; 125 ] ;
lb = scale_x\lb ;
ub = scale_x\ub ;

%Change disturbances
fail = 0 ; % Failure counter
for i=chosenones
    oldfail=fail ;
    disp(['Changing ' num2str(i) 'th disturbance'])
    for j=1:NoOfPoints
        d=d0 ;
        d(i) = d0(i)*pert(j);
        C = Precooling_constraints(x0,d,parameters)
        [x_opt,J_opt,exitflag,output,lambda] = fmincon(@Precooling_obj,x0,[],[],[],[],lb,ub,@Precooling_constraints,options,d,parameters) ;
        C = Precooling_constraints(x_opt,d,parameters)
        if exitflag > 0
           % disp('Optimization successful')
        elseif exitflag == 0
            %disp('Maximum iterations used')
            fail = fail + 1 ;
        elseif exitflag < 0
            %disp('Convergence failure')
            fail = fail + 1;
        end
    end
    newfails = fail - oldfail %In order to get it on screen
end

disp([num2str(fail) ' out of '  num2str(length(chosenones)*j) ' optimizations failed'])