% Fill in default parameters for the MaxEnt algorithm. function algorithmparams =…
- User
% 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;
code% 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);code% 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]).... repmat(Dp,[1 actions transitions])actionsactionstransitions,1),2); diff = max(abs(D-Dp)); end;
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.