% function uOpt = optCtrl(obj, ~, ~, deriv, uMode)
function uOpt = optCtrl(obj, ~, xs, deriv, uMode)
% uOpt = optCtrl(obj, t, y, deriv, uMode)
global u_optimal

%% Input processing
if nargin < 5
  uMode = 'min';
end

if ~iscell(deriv)
  deriv = num2cell(deriv);
end

% uOpt = cell(obj.nu, 1);

% xyz = load('C:\Users\admin\Downloads\helperOC-master\helperOC-master\controller_matlab.mat');
% W1 = xyz.fc1_weight;
% b1 = xyz.fc1_bias';
% W2 = xyz.fc_2_weight;
% b2 = xyz.fc_2_bias';

% W1 = rand(16, 2);
% b1 = rand(16, 1);
% W2 = rand(2, 16);
% b2 = rand(2, 1);

% for i = 1:length(xs{1,1})
%     for j = 1:length(xs{2,1})
%         s_inp = [xs{1,1}(i, j); xs{2, 1}(i, j); 1];
%         l1 = vpa(tanh([W1, b1]*s_inp));
%         temp = [W2, b2]*[l1; 1];
%         sig = vpa((1./(1 + exp(-temp))));
%         tn = vpa(tanh(temp));
%         uOpt{1,1}(i, j) = vpa( sig(1) )*5;
%         uOpt{2,1}(i, j) = vpa( tn(2) )*3.14;
%     end
% end

%% Optimal control
%if strcmp(uMode, 'max')
  %uOpt = (deriv{obj.dims==3}>=0)*obj.wRange(2) + (deriv{obj.dims==3}<0)*(obj.wRange(1));
%  for i = 1:2
%     if any(obj.dims == i)
%       uOpt{i} = (deriv{obj.dims==i}>=0)*obj.uRange{2}(i) + ...
%         (deriv{obj.dims==i}<0)*(obj.uRange{1}(i));
%     end
%  end
%elseif strcmp(uMode, 'min')
  %uOpt = (deriv{obj.dims==3}>=0)*(obj.wRange(1)) + (deriv{obj.dims==3}<0)*obj.wRange(2);
%else
%  error('Unknown uMode!')
%end
uOpt = u_optimal;

end