1% Example 11: Random environments and SolverENV
2% This tutorial illustrates how to model a queueing system operating in
3% a random environment, where system parameters (e.g., service rates)
4% change according to an underlying environmental process.
6% Scenario: A server that alternates between
"Fast" and
"Slow" modes.
7% In Fast mode, service rate
is 4.0. In Slow mode, service rate
is 1.0.
8% The environment switches from Fast->Slow at rate 0.5 and Slow->Fast at rate 1.0.
12%% Block 1: Create base network model
13% First, we define a closed queueing network with a delay and a queue
14baseModel = Network(
'BaseModel');
15delay = Delay(baseModel,
'ThinkTime');
16queue = Queue(baseModel,
'Fast/Slow Server', SchedStrategy.FCFS);
18% Closed
class with 5 jobs
20jobclass = ClosedClass(baseModel,
'Jobs', N, delay);
21delay.setService(
jobclass, Exp(1.0)); % Think time = 1.0
22queue.setService(
jobclass, Exp(2.0)); % Placeholder service rate
24% Connect
nodes in a cycle
25baseModel.link(Network.serialRouting(delay, queue));
27%% Block 2: Create the random environment
28% Define two stages:
"Fast" and
"Slow" with different service rates
29env = Environment(
'ServerModes');
31% Stage 1: Fast mode (service rate = 4.0)
32fastModel = baseModel.copy();
33fastQueue = fastModel.getNodeByName('Fast/Slow Server');
34fastQueue.setService(fastModel.
classes{1}, Exp(4.0));
35env.addStage(
'Fast',
'operational', fastModel);
37% Stage 2: Slow mode (service rate = 1.0)
38slowModel = baseModel.copy();
39slowQueue = slowModel.getNodeByName('Fast/Slow Server');
40slowQueue.setService(slowModel.
classes{1}, Exp(1.0));
41env.addStage(
'Slow',
'degraded', slowModel);
43% Define transitions between stages
44% Fast -> Slow at rate 0.5 (mean time in Fast mode = 2.0)
45env.addTransition(
'Fast',
'Slow', Exp(0.5));
46% Slow -> Fast at rate 1.0 (mean time in Slow mode = 1.0)
47env.addTransition(
'Slow',
'Fast', Exp(1.0));
49%% Block 3: Inspect the environment structure
50% Display the environment stages and their probabilities
52stageTable = env.getStageTable()
54%% Block 4: Solve
using SolverENV
55% SolverENV
requires a solver factory that creates solvers
for each stage
56% We use the Fluid solver (FLD) with transient analysis
58options = Solver.defaultOptions;
60options.iter_tol = 0.01;
61options.verbose =
false;
63% Solver factory: creates a Fluid solver
for each stage model
64fldOptions = FLD.defaultOptions;
65fldOptions.timespan = [0, 100];
66fldOptions.verbose =
false;
67solverFactory = @(m) FLD(m, fldOptions);
69% Create and run the ENV solver
70envSolver = ENV(env, solverFactory, options);
71[QN, UN, TN] = envSolver.getAvg();
73% Display average results weighted by environment probabilities
74fprintf(
'\n--- Environment-Averaged Results ---\n');
75envAvgTable = envSolver.getAvgTable()
77%% Block 5: Compare with individual stage analysis
78% Analyze each stage network in steady-state
using MVA
79fprintf(
'\n--- Individual Stage Analysis (MVA) ---\n');
80for e = 1:height(env.envGraph.Nodes)
81 stageName = env.envGraph.Nodes.Name{e};
82 stageModel = env.envGraph.Nodes.Model{e};
83 fprintf(
'\nStage: %s (prob = %.4f)\n', stageName, env.probEnv(e));
84 mvaSolver = MVA(stageModel);
85 stageTable = mvaSolver.getAvgTable();