function control
global Tk
Tk = 273.15;
name = label;
fd = fopen('con.csv', 'w');
for i=1:length(name)
  fprintf(fd, '\t%s', name(i, :));
end
fprintf(fd, '\n');
controller(fd);
fclose(fd);

function controller(fd)
global Tk
load optimal_operation
load scaling
load conditions
load data

i_com = length(sc.com.x);
i_con = length(sc.con.x);
i_val = length(sc.val.x);
i_vap = length(sc.vap.x);
con = x(1:i_con);
val = x(i_con+1:i_con+i_val);
vap = x(i_con+i_val+1:i_con+i_val+i_vap);
com = x(i_con+i_val+i_vap+1:end);

Avap = vap(2);
Acon = sum(con(6:10));
UA = vap(3)/(u(2)-u(1));
Ntot = sum(con(16:21))+sum(vap(4:5));
z = val(1);
Vl_vap = vap(6);
Ph = con(end);
Pl = vap(end);
Nl_con = sum(con((16:19)));
DTsub = con(2)-con(5);
Tcon = con(1:5);
Xcon_L = [zeros(5,1); zeros(5,1); -inf*ones(5,1); zeros(4,1); zeros(2,1); 0; 0]./sc.con.x;
Xcon_U = [inf*ones(5,1); inf*ones(5,1); inf*ones(5,1); inf*ones(4,1); inf*ones(2,1); 1; inf]./sc.con.x;
Xval_L = [zeros(2,1); -inf]./sc.val.x;
Xval_U = [1; inf; inf]./sc.val.x;
Xvap_L = [0; 0; -inf; zeros(2,1); 0; 0]./sc.vap.x;
Xvap_U = [inf; inf; inf; inf*ones(2,1); inf; inf]./sc.vap.x;
Xcom_L = [0; 0]./sc.com.x;
Xcom_U = [inf; inf]./sc.com.x;
f = 'objfun';
g = [];
H = [];
HessPattern = [];
x_L = [Xcon_L; Xval_L; Xvap_L; Xcom_L];
x_U = [Xcon_U; Xval_U; Xvap_U; Xcom_U];
pSepFunc = [];
fLowBnd = [];
dc = [];
c = 'nonlcon';
d2c = [];
ConsPattern = [];
c_L = zeros(size(sc.res));
c_U = zeros(size(sc.res));
x_min = [];
x_max = [];
f_opt = [];
x_opt = [];
for nc = 0:7%[0 1 5]
x0=x;
load conditions
deler=10;
  for i=1:deler
    A = xlsread('A.xls', 'operation');
    % Disturbance
    %%%%%%%%%%%%%%%%%%%%%%%%
%    u(2)=u(2)-10/deler;
    u(1) = u(1)-5/deler;
    Qvap = UA*(u(2)-u(1));
    DTout = Tcon(end)-u(2);
    %%%%%%%%%%%%%%%%%%%%%%%%
    b_L = [Qvap;
           Acon;
           Avap;
           Ntot;
           -inf];
    b_U = [Qvap;
           Acon;
           Avap;
           Ntot;
           inf];
  if nc==0
    Name = 'Re-optimized'
  elseif nc==1
    Name = 'Constant z'
    b_L(end)=z;
    b_U(end)=z;
    A(end, i_con+1)=1;
  elseif nc==2
    Name = 'Constant Vl_vap'
    b_L(end)=Vl_vap;
    b_U(end)=Vl_vap;
    A(end, i_con+i_val+6)=1;
  elseif nc==3
    Name = 'Constant Ph'
    b_L(end)=Ph;
    b_U(end)=Ph;
    A(end, i_con)=1;
  elseif nc ==4
    Name = 'Constant temperature out of condenser'
    b_L(end)=Tcon(end);
    b_U(end)=Tcon(end);
    A(end, 5)=1;
  elseif nc==5
    Name = 'Constant Nl_con'
    b_L(end)=Nl_con;
    b_U(end)=Nl_con;
    A(end, 16:19)=1;
  elseif nc==6
    Name = 'Constant DTsub'
    b_L(end)=DTsub;
    b_U(end)=DTsub;
    A(end, 2)=1;
    A(end, 5)=-1;
  elseif nc==7
    Name = 'Constant DTout'
    b_L(end)=DTout+u(2);
    b_U(end)=DTout+u(2);
    A(end, 5)=1;
  end
  SCALER = [];
  for i=1:size(A,1)
    SCALER = [SCALER; sc.x'];
  end
  A=A.*SCALER;
  Prob = ProbDef;
  Prob = conAssign(f, g, H, HessPattern, x_L, x_U, Name, x0./sc.x, pSepFunc, fLowBnd, A, b_L, b_U, c, dc, d2c, ConsPattern, c_L, c_U, x_min, x_max, f_opt, x_opt);
  Prob.d = d;
  Prob.sc = sc;
  Prob.u = u;
  Result = tomRun('minos', Prob);
  Result.ExitText
  x0 = Result.x_k.*sc.x;
end
xtmp=x0;
  fprintf(fd, '%s\t', Name);
  for i=1:length(sc.x)
     fprintf(fd, '%0.5f\t', xtmp(i));
  end
  fprintf(fd, '%s', Result.ExitText);
  fprintf(fd, '\n');
end
