LINE Solver
MATLAB API documentation
Loading...
Searching...
No Matches
rewardModel_multiclass.m
1%% Example: Multi-Class Reward Analysis
2% This example demonstrates reward-based CTMC analysis in a multi-class queueing
3% network, showing how to define per-class metrics and analyze class-specific behavior.
4%
5% Copyright (c) 2012-2026, Imperial College London
6% All rights reserved.
7
8clear; clc;
9
10%% Model Definition
11% Create an M/M/2 queue with two job classes (prioritized service)
12
13model = Network('MultiClassRewardExample');
14
15% Nodes
16source = Source(model, 'Source');
17queue = Queue(model, 'Queue', SchedStrategy.PS); % Processor Sharing
18sink = Sink(model, 'Sink');
19
20% Server capacity and buffer limit
21queue.setNumberOfServers(2);
22queue.setCapacity(6); % Limit state space
23
24% Job classes with different characteristics
25class_interactive = OpenClass(model, 'Interactive');
26class_batch = OpenClass(model, 'Batch');
27
28% Different arrival rates
29source.setArrival(class_interactive, Exp(2.0)); % Interactive: λ = 2.0
30source.setArrival(class_batch, Exp(1.5)); % Batch: λ = 1.5
31
32% Different service time requirements
33queue.setService(class_interactive, Exp(0.5)); % Interactive: μ = 2.0 (fast)
34queue.setService(class_batch, Exp(1.0)); % Batch: μ = 1.0 (slow)
35
36% Topology (same for both classes)
37P = model.initRoutingMatrix;
38P{class_interactive} = Network.serialRouting(source, queue, sink);
39P{class_batch} = Network.serialRouting(source, queue, sink);
40model.link(P);
41
42%% Define Per-Class Rewards
43fprintf('Defining per-class reward metrics:\n\n');
44
45% === Interactive Class Metrics ===
46
47% Jobs of Interactive class
48model.setReward('Interactive_QLen', @(state) state.at(queue, class_interactive));
49fprintf(' ✓ Interactive_QLen = state.at(queue, Interactive)\n');
50
51% Utilization contributed by Interactive class
52model.setReward('Interactive_Util', Reward.utilization(queue, class_interactive));
53fprintf(' ✓ Interactive_Util = Reward.utilization(queue, Interactive)\n');
54
55% === Batch Class Metrics ===
56
57% Jobs of Batch class
58model.setReward('Batch_QLen', @(state) state.at(queue, class_batch));
59fprintf(' ✓ Batch_QLen = state.at(queue, Batch)\n');
60
61% Utilization contributed by Batch class
62model.setReward('Batch_Util', Reward.utilization(queue, class_batch));
63fprintf(' ✓ Batch_Util = Reward.utilization(queue, Batch)\n');
64
65% === Comparative Metrics ===
66
67% Total queue length (both classes)
68model.setReward('Total_QLen', @(state) state.at(queue).total());
69fprintf(' ✓ Total_QLen = state.at(queue).total()\n');
70
71% Total system utilization
72model.setReward('Total_Util', Reward.utilization(queue));
73fprintf(' ✓ Total_Util = Reward.utilization(queue)\n');
74
75% Ratio of Interactive to Batch jobs
76model.setReward('Interactive_Ratio', @(state) ...
77 state.at(queue, class_interactive) / ...
78 max(state.at(queue, class_batch), 0.001)); % Avoid division by zero
79fprintf(' ✓ Interactive_Ratio = Interactive / Batch\n');
80
81% === Priority-Aware Metrics ===
82
83% Weighted response time cost (Interactive weighted 3x, Batch weighted 1x)
84% Approximates weighted system delay
85model.setReward('Weighted_Cost', @(state) ...
86 3.0 * state.at(queue, class_interactive) + ...
87 1.0 * state.at(queue, class_batch));
88fprintf(' ✓ Weighted_Cost = 3.0*Interactive + 1.0*Batch\n');
89
90% Service fairness indicator: 1 if both classes present, 0 otherwise
91model.setReward('Fairness', @(state) ...
92 double(state.at(queue, class_interactive) > 0 && ...
93 state.at(queue, class_batch) > 0));
94fprintf(' ✓ Fairness = (Interactive > 0) AND (Batch > 0)\n');
95
96%% Solve with CTMC Solver
97fprintf('\nSolving with CTMC solver...\n\n');
98
99options = Solver.defaultOptions;
100options.verbose = 0;
101
102solver = CTMC(model, options);
103
104%% Get Steady-State Expected Rewards
105[R, names] = solver.getAvgReward();
106
107fprintf('=== Steady-State Expected Rewards (Multi-Class) ===\n\n');
108
109fprintf('Interactive Class:\n');
110fprintf(' %-25s: %10.6f (jobs)\n', 'Interactive_QLen', R(1));
111fprintf(' %-25s: %10.6f (server capacity)\n', 'Interactive_Util', R(2));
112
113fprintf('\nBatch Class:\n');
114fprintf(' %-25s: %10.6f (jobs)\n', 'Batch_QLen', R(3));
115fprintf(' %-25s: %10.6f (server capacity)\n', 'Batch_Util', R(4));
116
117fprintf('\nComparative Metrics:\n');
118fprintf(' %-25s: %10.6f (both classes)\n', 'Total_QLen', R(5));
119fprintf(' %-25s: %10.6f (2 servers)\n', 'Total_Util', R(6));
120fprintf(' %-25s: %10.6f (ratio)\n', 'Interactive_Ratio', R(7));
121
122fprintf('\nPriority-Aware Metrics:\n');
123fprintf(' %-25s: %10.6f (weighted cost)\n', 'Weighted_Cost', R(8));
124fprintf(' %-25s: %10.6f (fraction of time both present)\n', ...
125 'Fairness', R(9));
126
127%% Analytical Validation
128fprintf('\n=== Analysis ===\n');
129
130lambda_int = 2.0;
131mu_int = 2.0;
132lambda_batch = 1.5;
133mu_batch = 1.0;
134c = 2; % Number of servers
135
136rho_int = lambda_int / (c * mu_int); % 2 / (2*2) = 0.5
137rho_batch = lambda_batch / (c * mu_batch); % 1.5 / (2*1) = 0.75
138rho_total = rho_int + rho_batch; % 1.25
139
140fprintf('System Characteristics:\n');
141fprintf(' Interactive: λ=%.1f, μ=%.1f per server\n', lambda_int, mu_int);
142fprintf(' Batch: λ=%.1f, μ=%.1f per server\n', lambda_batch, mu_batch);
143fprintf(' Servers: %d\n', c);
144fprintf(' Total utilization: %.3f (%.1f%% capacity)\n', ...
145 rho_total, 100*rho_total);
146
147% Class composition
148total_qlen = R(5);
149if total_qlen > 0
150 pct_int = 100 * R(1) / total_qlen;
151 pct_batch = 100 * R(3) / total_qlen;
152 fprintf('\nQueue Composition:\n');
153 fprintf(' Interactive jobs: %.2f%% of queue\n', pct_int);
154 fprintf(' Batch jobs: %.2f%% of queue\n', pct_batch);
155end
156
157% Response time estimation (Little's Law)
158total_arrival = lambda_int + lambda_batch;
159est_resp_time_int = R(1) / lambda_int;
160est_resp_time_batch = R(3) / lambda_batch;
161
162fprintf('\nEstimated Response Times (Little''s Law):\n');
163fprintf(' Interactive: %.6f time units\n', est_resp_time_int);
164fprintf(' Batch: %.6f time units\n', est_resp_time_batch);
165
166fprintf('\n✓ Example completed successfully.\n');