function fval=get_minus_welfare_objective(x_opt,x_opt_name,target_name,oo_,options_,M_)

% function outvalue=get_minus_welfare_objective(x_opt)
% computes minus the welfare objective given the parameters
% Inputs:
% - x_opt       [double]    vector of parameters over which to optimize
% - x_opt_name  [cell]      cell array with name in first column, lower and
%                           upper bounds in second and third
% - target_name [string]    name of the variable storing the welfare

% Copyright (C) 2018 Johannes Pfeifer
%
% This is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% It is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% For a copy of the GNU General Public License,
% see <http://www.gnu.org/licenses/>.

for ii=1:size(x_opt_name,1)
    M_.params(strmatch(x_opt_name{ii,1},M_.param_names,'exact'))=x_opt(ii);
end

if any(x_opt<=cell2mat(x_opt_name(:,2))) || any(x_opt>=cell2mat(x_opt_name(:,3))) %make sure parameters are within bounds
    fval=10e5+sum([x_opt].^2); %penalty function
    return
end

var_list_ = target_name;

[info,oo_]=stoch_simul(M_, options_, oo_, var_list_); %run stoch_simul to generate IRFs with the options specified in the mod-file

if info (1) %solution was not successful
    fval=10e6+sum([x_opt].^2); %return with penalty 
else
    fval=oo_.var(1,1) + oo_.var(2,2) + 0.148 * oo_.var(3,3);
end