Commit 881e0504 authored by Argyris Kalogeratos's avatar Argyris Kalogeratos
Browse files

init

parents
Loading
Loading
Loading
Loading

DRAsimulator.m

0 → 100644
+107 −0
Original line number Diff line number Diff line
%epidemicControl - This is the main script that initializes the environment,
%performs simulations, and reports comparative results.
%---
% This is part of the DRA Simulator package for Dynamic Resource Allocation 
% strategies aiming to suppress SIS epidemics. See README.md for more 
% details. If you make use of this package, or part of it, please consider 
% citing the paper:
%
%   "A Greedy Approach for Dynamic Control of Diffusion Processes in
%   Networks", K. Scaman, A. Kalogeratos, N Vayatis, IEEE International 
%   Conference on Tools with Artificial Intelligence (ICTAI), 2015.
%
% DRA Simulator is free software: redistribute them and/or modify them 
% under the terms of the GNU General Public License as published by the 
% Free Software Foundation, either version 3 of the License, or (at your 
% option) any later version.
%
% DRA Simulator package is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
% Public License for more details. You should have received a copy of the 
% GNU General Public License along with DRA Simulator. If not, see 
% <http://www.gnu.org/licenses/>.
%
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

% INITIALIZATION (run only once - to force initialization, clear the environment first)
if (~exist('MATLAB_WORKERS', 'var')) % avoid re-initializations 
    prepareWorkspace();
end

% Initialize the random number generator (RNG) for reproducibility of the results
if (rng_seed >= 0), rng(rng_seed); end
rngInfo = rng;

%% PARAMETER SETUP
% Graph parameters
    graphPars = struct;
    
    % a. Generate graph
        graphPars.N           = 100;           % the size of graph to generate
        graphPars.graphType = 'erdosRenyi';    % available: 'erdosRenyi', 'preferentialAttachment', 'smallWorld', 'adjacencyMatrix'
        
        if (strcmp(graphPars.graphType, 'erdosRenyi') == 1)
            graphPars.prob_arete_graph = 0.1;  % only for erdosRenyiGraph() generation
        end
            
    % b. Use an input graph
    %  - enter 'adjacencyMatrix' in graphPars.graphType
    %  - include the following:
    %  - load or create your graph G 
        %    G = erdosRenyiGraph(graphPars.N, graphPars.prob_arete_graph);   % this is just an example
        %    graphPars.adjacencyMatrix = G;
        %    graphPars.N = size(G,1);

% Infection seeds parameters
    infectionSeedPars      = struct;
    infectionSeedPars.size = 0.9;   % the percentage of initial seeds distributed uniformly at random

% Epidemic parameters
    epidemicPars       = struct;
    epidemicPars.beta  = 3;         % independent infection rate for each edge
    epidemicPars.delta = 1;         % recovery rate for each infected node
    epidemicPars.rho   = 120;       % increase in recovery rate for each treated node

% Control strategy parameters
    btot = 5;                       % number of available treatment resources 
    strategies = createStrategies(btot, 'LRIE');%'RAND', 'LRSR', 'LRIE'); % which strategies to test (baseline, static, dynamic): for more info see the documentation of this function
    
% Simulation and experimentation parameters
    simulationPars          = struct;
    simulationPars.T        = 2;           % simulation time
    simulationPars.noSlopeT = Inf;         % characteristic time for the non stationarity test
    simulationPars.timeStep = 0.01;        % time step for results
    simulationPars.stateSnapshots = false; % record the infection state and the resource distribution for every change that happens

    numTests = 10;
    
    % automatically defined
    simulationPars.runInParallel  = (MATLAB_WORKERS > 0);       % use multiple Matlab workers
    simulationPars.consistentRNG  = (rngInfo.Seed == rng_seed); % preserve the consistency of random number generator if the seed is the one given by the user
    if (simulationPars.runInParallel == simulationPars.consistentRNG)
        simulationPars.runInParallel = 0;
        warning('Warning: RNG cannot be consistent when multiple workers are used (simulationPars.runInParallel turned to false)');
    end

%% EPIDEMIC SIMULATION
results = cell(numTests, 1);
tic;
for iTest=1:numTests,
    fprintf('\n=== iTest: %g ==================================================\n', iTest);
    results{iTest} = simulateEpidemic(graphPars, infectionSeedPars, epidemicPars, simulationPars, strategies, 'verbosityLevel', 2);
end
toc;

%% COMPUTE AND SHOW METRICS
fResults = invertCellStruct(results);   % make a strcture with proper format for exploring the results
fResults = applyMetric(fResults, @(s) 100 * double(s.numInfected) / graphPars.N, 'percentageInfected');
fResults = applyMetric(fResults, @(s) 100 * double(s.numInfected(end)) / graphPars.N, 'finalPercentageInfected');
fResults = applyMetric(fResults, @(s) sum(s.numInfected) * simulationPars.timeStep, 'AUC');
fResults = applyMetric(fResults, @(s) timeOfExtinction(s), 'timeExtinction');
fResults = applyMetric(fResults, @(s) double(s.numInfected(end)), 'finalNumInfected');

%% SHOW OUTPUT RESULTS & PLOTS
showMeasures(fResults, 'AUC', 'timeExtinction', 'finalPercentageInfected');
plotCurves(fResults, 'percentageInfected', 'title', 'Comparison of resource allocation strategies', 'xlabel', 'time', 'ylabel', 'percentage of infected nodes');

Datasets/USAir97.mat

0 → 100644
+38.4 KiB

File added.

No diff preview for this file type.

+41 −0
Original line number Diff line number Diff line
%simulationResult - This function applies a function that computes an 
%evaluation metric on the simulation result and store it as a field in the 
%results' structure.
%
% function sResult = applyMetric (simulationResult, ...
%                                 metricFunction, metricName)
%
% Input:
%   - sResult: a structure with the simulation results.
%   - metricFunction: a function handler that computes an evaluation metric
%     on the recorded simulation results. This is defined through the call.
%   - metricName: the name of computed evaluation metric.
%
% Output:
%   - sResult: the udated structure with the simulation results.
%---
% This is part of the DRA Simulator package for Dynamic Resource Allocation
% strategies aiming to suppress SIS epidemics. See the comments header 
% of the main script DRAsimulator.m and the README.md for more details.
% This package is distributed under GNU GPL v.3 or later.
%
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

function sResult = applyMetric (sResult, metricFunction, metricName)

sResult = structfun(@(strategy) addMetricToStrategy(strategy), sResult, 'UniformOutput', false);

function strategy = addMetricToStrategy(strategy)
    if (iscell(strategy))
        strategy = cellfun(@(test) addMetricValue(test, metricFunction(test), metricName), strategy, 'UniformOutput', false);
    else
        strategy = addMetricValue(strategy, metricFunction(strategy), metricName);
    end
end

function strategy = addMetricValue(strategy, value, name)
    strategy.(name) = value;
end
end
 No newline at end of file
+35 −0
Original line number Diff line number Diff line
%computeMeanAndStd - This is a helper function which is called by the 
%showMeasures(). It computes mean and std values for given fields of the 
%structure with the simulation results.
%
% function meanAndStd = computeMeanAndStd (fRresults, fieldName)
%
% Input:
%   - fRresults: a structure with the simulation results.
%   - fieldName: the field to compute the values (should be a name of an
%     evaluation metric existing in the structure).
%
% Output:
%   - meanAndStd: the resulting values in a structure.
%---
% This is part of the DRA Simulator package for Dynamic Resource Allocation
% strategies aiming to suppress SIS epidemics. See the comments header 
% of the main script DRAsimulator.m and the README.md for more details.
% This package is distributed under GNU GPL v.3 or later.
%
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

function meanAndStd = computeMeanAndStd (fRresults, fieldName)

valuesStruct = structfun(@(s) cellfun(@(t) t.(fieldName)(end), s), fRresults, 'UniformOutput', false);
meanAndStd   = structfun(@(v) createStruct(v), valuesStruct, 'UniformOutput', false);

function meanAndStd = createStruct(values)
    meanAndStd      = struct;
    meanAndStd.mean = mean(values(values ~= Inf));
    meanAndStd.std  = std(values(values ~= Inf));
end

end
+39 −0
Original line number Diff line number Diff line
%smallWorldGraph - Shows a selection of evaluation metrics stored in the 
%simulation results structure.
%
% function showMeasures (fRresults, varargin)
%
% Input:
%   - fRresults: a structure with the simulation results.
%   - varargin : a list of names of the evaluation metrics to show.
%---
% This is part of the DRA Simulator package for Dynamic Resource Allocation
% strategies aiming to suppress SIS epidemics. See the comments header 
% of the main script DRAsimulator.m and the README.md for more details.
% This package is distributed under GNU GPL v.3 or later.
%
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---

function showMeasures (fRresults, varargin)

methodNames = fieldnames(fRresults);
numMeasures = length(varargin);
numMethods  = length(methodNames);

measures = cell(numMeasures, 1);
for j=1:numMeasures
    measure = computeMeanAndStd(fRresults, varargin{j});
    measures{j} = structfun(@(strategy) strategy.mean, measure);
end

fprintf('-----------------------\n Comparing strategies\n-----------------------\n');
for i=1:numMethods
    for j=1:numMeasures
        measure = measures{j};
        fprintf('%s: %10.2f (%2.2f), ', varargin{j}, measure(i), measure(i) / min(measure));
    end
    fprintf('-- [%20s]\n', methodNames{i});
end
end
 No newline at end of file