function dresult = dmatrix(dproblem)
%% Function dresult = dmatrix(dproblem) returns a struct type variable dresult,
%% whose field dvalues tells the values of the decisions and the field doptim
%% tells the index of the optimal decision.  If the decision rule if minimax
%% regret, i.e. "regret", then the output field rmat will be the regret matrix.
%% If the decision rule is constant absolut risk aversion, or "cara", then the
%% output field umat returns the utility matrix.
%%
%% The argument dproblem is also a struct variable.  Its fields are:
%%
%% dmat is the decision (or reward) matrix.
%%
%% drule is the string that describes the decision rule.  Supported rules are
%% "opt" (optimist), "pess" (pessimist), "Hur" (Hurwicz), "regret" (minimax
%% regret), "Lap" (Laplace), "ev" (expected value), "cara" (constant absolute
%% risk aversion).
%%
%% odegree is the degree of optimism in Hurwicz rule.
%%
%% prob is the column vector containing the probabilites of the scenarios,
%%
%% rtol is the risk tolerance related to the "cara" rule.

    %% Lyhennysmerkintoja
    R = droblem.dmat;       %% Decision matrix
    s = droblem.drule;      %% Decision rule
    I = 1:size(R,1);        %% R's rows
    J = 1:size(R,2);        %% R's columns

    if strcmp(s,"opt")
        dresult.dvalues = max(R')';
        [tmp, dresult.doptim] = max(dresult.dvalues);
    elseif strcmp(s,"pess")
        dresult.dvalues = min(R')';
        [tmp, dresult.doptim] = max(dresult.dvalues);
    elseif strcmp(s,"Hur")
        w = dproblem.odegree;
        dresult.dvalues = w*max(R')' + (1-w)*min(R')';
        [tmp, dresult.doptim] = max(dresult.dvalues);
    elseif strcmp(s,"regret")
        %% Build the regret matrix
        for i=I
            for j=J
                K(i,j) = max(R(:,j)) - R(i,j);
            end
        end
        dresult.dvalues = max(K')';
        [tmp, dresult.doptim] = min(dresult.dvalues);
        dresult.rmat = K;
    elseif strcmp(s,"Lap")
        dresult.dvalues = (1/size(R,2))*sum(R')';
        [tmp, dresult.doptim] = max(dresult.dvalues);
    elseif strcmp(s,"ev")
        p = dproblem.prob;
        dresult.dvalues = R*p;        %%  p has to be a clolumn vector!
        [tmp, dresult.doptim] = max(dresult,dvalues);
    elseif strcmp(s,"cara")
        p = dproblem.prob;
        r0 = dproblem.rtol;
        %% Build the utility matrix
        for i=I
            for j=J
               U(i,j) = 1 - exp(-R(i,j)/r0);
            end
        end
        dresult.dvalues = U*p;        %% p has to be a column vector!
        [tmp, dresult.doptim] = max(dresult.dvalues);
        dresult.umat = U;
    end
end
