1function [Rt, t, names] = getTranReward(self, rewardName)
2% [
RT, T, NAMES] = GETTRANREWARD([REWARDNAME]) Transient reward r(X(t)) via LDES
4% Returns
the transient reward trajectory r(X(t))
for each reward function defined
5% on
the model via setReward, sampled along
the simulated path. The LDES simulator
6% exports
the integer joint-state trajectory;
the reward functions are evaluated here
7% on each state, so
the result
is correct also
for nonlinear rewards.
10% Rt - Cell array {nRewards x 1} of structs with fields .t, .metric, .name
11% (or a single
struct if REWARDNAME
is given)
12% t - Time vector [nPoints x 1]
13% names - Cell array of reward names (or a single name
if REWARDNAME
is given)
15% Copyright (c) 2012-2026, Imperial College London
22if GlobalConstants.DummyMode
23 Rt = {}; t = []; names = {};
27sn = self.model.getStruct(
true);
29 line_error(mfilename, 'No rewards defined. Use model.setReward(name, @(state) ...) before calling getTranReward.');
32% Run
the LDES simulation (fully JSON-mediated) requesting
the time-ordered
33% joint-state trajectory via --export-histogram --trajectory.
34data = self.solveCli(self.getOptions, {
'--export-histogram',
'--trajectory'});
37if isstruct(data) && isfield(data,
'stateHistogram') && ~isempty(data.stateHistogram)
38 space = ldesJson2mat(ldesGetField(data.stateHistogram, 'trajSpace', []), [], []);
39 t = ldesJson2mat(ldesGetField(data.stateHistogram, 'trajTime', []), [], []);
43nRewardsAll = length(sn.reward);
44allNames = cell(nRewardsAll, 1);
46 allNames{r} = sn.reward{r}.name;
49% Build index maps
for RewardState (station-major aggregated layout)
50nodeToStationMap = containers.Map(
'KeyType',
'int32',
'ValueType',
'int32');
51classToIndexMap = containers.Map(
'KeyType',
'int32',
'ValueType',
'int32');
54 nodeToStationMap(int32(ind)) = sn.nodeToStation(ind);
58 classToIndexMap(int32(r)) = r;
61nPoints = size(space, 1);
62Rt = cell(nRewardsAll, 1);
65 rewardFn = sn.reward{r}.fn;
66 metric = zeros(nPoints, 1);
68 stateRow = space(s, :);
69 rewardState = RewardState(stateRow, sn, nodeToStationMap, classToIndexMap);
71 metric(s) = rewardFn(rewardState);
74 metric(s) = rewardFn(stateRow, sn);
80 Rt{r} =
struct(
't', t,
'metric', metric,
'name', allNames{r});
83% Filter to a single named reward
if requested
84if ~isempty(rewardName)
85 idx = find(strcmp(allNames, rewardName), 1);
87 line_error(mfilename, sprintf(
'Reward ''%s'' not found.', rewardName));
90 names = allNames{idx};