LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
getTranReward.m
1function [Rt, t, names] = getTranReward(self, rewardName)
2% [RT, T, NAMES] = GETTRANREWARD([REWARDNAME]) Transient reward r(X(t)) via LDES
3%
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.
8%
9% OUTPUTS:
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)
14%
15% Copyright (c) 2012-2026, Imperial College London
16% All rights reserved.
17
18if nargin < 2
19 rewardName = [];
20end
21
22if GlobalConstants.DummyMode
23 Rt = {}; t = []; names = {};
24 return
25end
26
27sn = self.model.getStruct(true);
28if isempty(sn.reward)
29 line_error(mfilename, 'No rewards defined. Use model.setReward(name, @(state) ...) before calling getTranReward.');
30end
31
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'});
35space = [];
36t = [];
37if isstruct(data) && isfield(data, 'stateHistogram') && ~isempty(data.stateHistogram)
38 space = ldesJson2mat(ldesGetField(data.stateHistogram, 'trajSpace', []), [], []);
39 t = ldesJson2mat(ldesGetField(data.stateHistogram, 'trajTime', []), [], []);
40end
41t = t(:);
42
43nRewardsAll = length(sn.reward);
44allNames = cell(nRewardsAll, 1);
45for r = 1:nRewardsAll
46 allNames{r} = sn.reward{r}.name;
47end
48
49% Build index maps for RewardState (station-major aggregated layout)
50nodeToStationMap = containers.Map('KeyType', 'int32', 'ValueType', 'int32');
51classToIndexMap = containers.Map('KeyType', 'int32', 'ValueType', 'int32');
52for ind = 1:sn.nnodes
53 if sn.isstation(ind)
54 nodeToStationMap(int32(ind)) = sn.nodeToStation(ind);
55 end
56end
57for r = 1:sn.nclasses
58 classToIndexMap(int32(r)) = r;
59end
60
61nPoints = size(space, 1);
62Rt = cell(nRewardsAll, 1);
63names = allNames;
64for r = 1:nRewardsAll
65 rewardFn = sn.reward{r}.fn;
66 metric = zeros(nPoints, 1);
67 for s = 1:nPoints
68 stateRow = space(s, :);
69 rewardState = RewardState(stateRow, sn, nodeToStationMap, classToIndexMap);
70 try
71 metric(s) = rewardFn(rewardState);
72 catch ME
73 try
74 metric(s) = rewardFn(stateRow, sn);
75 catch
76 rethrow(ME);
77 end
78 end
79 end
80 Rt{r} = struct('t', t, 'metric', metric, 'name', allNames{r});
81end
82
83% Filter to a single named reward if requested
84if ~isempty(rewardName)
85 idx = find(strcmp(allNames, rewardName), 1);
86 if isempty(idx)
87 line_error(mfilename, sprintf('Reward ''%s'' not found.', rewardName));
88 end
89 Rt = Rt{idx};
90 names = allNames{idx};
91end
92
93end
Definition Station.m:245