% Fill in default parameters for the MaxEnt algorithm.
function algorithm_params = maxentdefaultparams(algorithm_params)

% Create default parameters.
default_params = struct(...
    'seed',0,...
    'laplace_prior',0,...
    'all_features',1,...
    'true_features',0);

% Set parameters.
algorithm_params = filldefaultparams(algorithm_params,default_params);

% Run value iteration to solve a linear MDP.
function [v,q,p,logp] = linearvalueiteration(mdp_data,r,vinit)

states = mdp_data.states;
actions = mdp_data.actions;
VITR_THRESH = 1e-4;
%VITR_THRESH = 1e-10;

% Compute log state partition function V.
if (nargin == 3)
    v = vinit;
else
    v = zeros(states,1);
end;
diff = 1.0;
while diff >= VITR_THRESH,
    % Initialize new v.
    vp = v;
    
    % Compute q function.
    q = r + mdp_data.discount*sum(mdp_data.sa_p.*vp(mdp_data.sa_s),3);
    
    % Compute softmax.
    v = maxentsoftmax(q);
    
    diff = max(abs(v-vp));
end;

% Compute policy.
logp = q - repmat(v,1,actions);
p = exp(logp);

% Compute MaxEnt objective and gradient. Discounted infinite-horizon version.
function [val,dr] = maxentdiscounted(r,F,muE,mu_sa,mdp_data,initD,laplace_prior)

if nargin < 7,
    laplace_prior = 0;
end;

% Compute constants.
actions = mdp_data.actions;

% Convert to full reward.
wts = r;
r = F*r;

[~,~,policy,logpolicy] = linearvalueiteration(mdp_data,repmat(r,1,actions));

% Compute value by adding up log example probabilities.
val = sum(sum(logpolicy.*mu_sa));

% Add laplace prior.
if laplace_prior,
    val = val - laplace_prior*sum(abs(wts));
end;

% Invert for descent.
val = -val;

if nargout >= 2,    
    % Compute state visitation count D.
    D = linearmdpfrequency(mdp_data,struct('p',policy),initD);

    % Compute gradient.
    dr = muE - F'*D;
    
    % Laplace prior.
    if laplace_prior,
        dr = dr - laplace_prior*sign(wts);
    end;
    
    % Invert for descent.
    dr = -dr;
end;

% Compute the occupancy measure of the linear MDP given a policy.
function D = linearmdpfrequency(mdp_data,mdp_solution,initD,prevD)

[states,actions,transitions] = size(mdp_data.sa_p);
VITR_THRESH = 1e-4;
%VITR_THRESH = 1e-10;

if nargin >= 4,
    D = prevD;
else
    D = zeros(states,1);
end;

if nargin < 3 || isempty(initD),
    % Initialize uniform initial state distribution.
    initD = (1/states)*ones(states,1);
end;

diff = 1.0;
while diff >= VITR_THRESH,
    Dp = D;
    Dpi = repmat(mdp_solution.p,[1 1 transitions]).*mdp_data.sa_p.*...
        repmat(Dp,[1 actions transitions])*mdp_data.discount;
    D = initD + sum(sparse(mdp_data.sa_s(:),1:states*actions*transitions,...
        Dpi(:),states,states*actions*transitions)*ones(states*actions*transitions,1),2);
    diff = max(abs(D-Dp));
end;

% Compute column-wise softmax of a matrix.
function v = maxentsoftmax(q)

% Find maximum elements.
maxx = max(q,[],2);

% Compute safe softmax.
v = maxx + log(sum(exp(q - repmat(maxx,1,size(q,2))),2));

% Ziebart's Maximum Entropy IRL, with optional prior on reward values.
function irl_result = maxentrun(algorithm_params,mdp_data,mdp_model,...
    feature_data,example_samples,true_features,verbosity)

% algorithm_params - parameters of the FIRL algorithm:
%       seed (0) - initialization for random seed
%       laplace_prior (0) - use Laplace prior for regularization
%       true_features (0) - use true features as a basis
%       all_features (1) - use the provided features as a basis
% mdp_data - definition of the MDP to be solved
% example_samples - cell array containing examples
% irl_result - result of IRL algorithm, generic and algorithm-specific:
%       r - inferred reward function
%       v - inferred value function.
%       q - corresponding q function.
%       p - corresponding policy.
%       time - total running time

% Fill in default parameters.
algorithm_params = maxentdefaultparams(algorithm_params);

% Set random seed.
rand('seed',algorithm_params.seed);

% Initialize variables.
[states,actions,transitions] = size(mdp_data.sa_p);
[N,T] = size(example_samples);
mdp_solve = str2func(strcat(mdp_model,'solve'));

% Build feature membership matrix.
if algorithm_params.all_features,
    F = feature_data.splittable;
    % Add dummy feature.
    F = horzcat(F,ones(states,1));
elseif algorithm_params.true_features,
    F = true_features;
else
    F = eye(states);
end;

% Count features.
features = size(F,2);

% Compute feature expectations.
muE = zeros(features,1);
ex_s = zeros(N,T);
ex_a = zeros(N,T);
mu_sa = zeros(states,actions);
for i=1:N,
    for t=1:T,
        ex_s(i,t) = example_samples{i,t}(1);
        ex_a(i,t) = example_samples{i,t}(2);
        mu_sa(ex_s(i,t),ex_a(i,t)) = mu_sa(ex_s(i,t),ex_a(i,t)) + 1;
        state_vec = zeros(states,1);
        state_vec(ex_s(i,t)) = 1;
        muE = muE + F'*state_vec;
    end;
end;

% Generate initial state distribution for infinite horizon.
initD = sum(sparse(ex_s(:),1:N*T,ones(N*T,1),states,N*T)*ones(N*T,1),2);
for i=1:N,
    for t=1:T,
        s = ex_s(i,t);
        a = ex_a(i,t);
        for k=1:transitions,
            sp = mdp_data.sa_s(s,a,k);
            initD(sp) = initD(sp) - mdp_data.discount*mdp_data.sa_p(s,a,k);
        end;
    end;
end;

fun = @(r)maxentdiscounted(r,F,muE,mu_sa,mdp_data,initD,algorithm_params.laplace_prior);

% Set up optimization options.
options = struct();
options.Display = 'iter';
options.LS_init = 2;
options.LS = 2;
options.Method = 'lbfgs';
if verbosity == 0,
    options.display = 'none';
end;

tic;

% Initialize reward.
r = rand(features,1);

% Run unconstrainted non-linear optimization.
[r,~] = minFunc(fun,r,options);

% Print timing.
time = toc;
if verbosity ~= 0,
    fprintf(1,'Optimization completed in %f seconds.\n',time);
end;

% Convert to full tabulated reward.
wts = r;
r = F*r;

% Return corresponding reward function.
r = repmat(r,1,actions);
soln = mdp_solve(mdp_data,r);
v = soln.v;
q = soln.q;
p = soln.p;

% Construct returned structure.
irl_result = struct('r',r,'v',v,'p',p,'q',q,'r_itr',{{r}},'model_itr',{{wts}},...
    'model_r_itr',{{r}},'p_itr',{{p}},'model_p_itr',{{p}},...
    'time',time);

% Clean up.
clear global prev_v;
clear global prev_d;

Yukarıdaki kodları Python'a uygun hale getirebilir misin? Bu bir MaxEnt IRL uygulması ve yukarıda yazılan kodlara bağlı olarak çalışan bir uygulama hazırlamak istiyorum.