Commit 3a339480 authored by Argyris Kalogeratos's avatar Argyris Kalogeratos

minor code improvements

parent cae19c5c
......@@ -39,18 +39,24 @@ rngInfo = rng;
graphPars = struct;
% a. Generate graph
graphPars.N = 100; % the size of graph to generate
graphPars.graphType = 'erdosRenyi'; % available: 'erdosRenyi', 'preferentialAttachment', 'smallWorld', 'adjacencyMatrix'
graphPars.N = 100; % the size of graph to generate
graphPars.graphType = 'preferentialAttachment'; % available: 'erdosRenyi', 'preferentialAttachment', 'smallWorld', 'adjacencyMatrix'
switch graphPars.graphType, % specific parameters needed for some random graph models
case 'erdosRenyi'
graphPars.edge_prob = 0.1;
case 'preferentialAttachment'
graphPars.edge_num = 5;
graphPars.delta = 0.01;
case 'smallWorld'
graphPars.edge_num = 5;
graphPars.edge_prob = 0.05;
end
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
% - load your graph G or create it include the following:
% G = erdosRenyiGraph(graphPars.N, graphPars.edge_prob); % this is just an example
% graphPars.adjacencyMatrix = G;
% graphPars.N = size(G,1);
......
......@@ -18,8 +18,8 @@
function showMeasures (fRresults, varargin)
methodNames = fieldnames(fRresults);
numMeasures = length(varargin);
numMethods = length(methodNames);
numMeasures = numel(varargin);
numMethods = numel(methodNames);
measures = cell(numMeasures, 1);
for j=1:numMeasures
......
%erdosRenyiGraph - A generator for Erdos Renyi graphs.
%
% function G = erdosRenyiGraph (N, p)
% function G = erdosRenyiGraph (N, edge_num)
%
% Input:
% - N: the size of the graph to generate.
% - p: the probability of creating each edge (i,j).
% - edge_prob: the probability of creating each edge (i,j).
%
% Output:
% - G: the generated graph.
......@@ -17,14 +17,14 @@
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---
function G = erdosRenyiGraph (N, p)
function G = erdosRenyiGraph (N, edge_num)
G = rand(N);
G = G + G';
if (p <= 0.5)
G = G < sqrt(2 * p);
if (edge_num <= 0.5)
G = G < sqrt(2 * edge_num);
else
G = G >= sqrt(2 * (1 - p));
G = G >= sqrt(2 * (1 - edge_num));
end
G = G - diag(diag(G));
G = sparse(G);
......
%preferentialAttachmentGraph - A generator for Preferential Attachment graphs.
%
% function G = preferentialAttachmentGraph (N, M, delta)
% function G = preferentialAttachmentGraph (N, num_edges, delta)
%
% Input:
% - N: the size of the graph to generate.
% - M: is the number of esges to generate.
% - num_edges: is the number of edges to generate.
% - delta: is an extra additive weight that increases the probability of
% creating an edge, for each node.
%
......@@ -19,19 +19,19 @@
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---
function G = preferentialAttachmentGraph (N, M, delta)
function G = preferentialAttachmentGraph (N, num_edges, delta)
if (nargin < 3)
delta = 0;
end
G = spalloc(N, N, N*M);
G = spalloc(N, N, N*num_edges);
G(1, 2) = 1;
G(2, 1) = 1;
for i = 3:N
numNeighbors = full(sum(G,1));
newNeighbors = randp(max(0, numNeighbors + delta), [M 1]);
newNeighbors = randp(max(0, numNeighbors + delta), [num_edges 1]);
G(i, newNeighbors) = 1;
G(newNeighbors, i) = 1;
end
......
%smallWorldGraph - A generator for Small World graphs.
%
% function G = smallWorldGraph (N, k, p)
% function G = smallWorldGraph (N, edge_num, edge_prob)
%
% Input:
% - N: the size of the graph to generate.
% - k: the size of the neighbors to connect each node (it assumes that
% - edge_num: the size of the neighbors to connect each node (it assumes that
% each node is located on the periphery of a ring, then k is the number
% of nearest neighbors to connect with each node)
% - p: the probability of creating each edge (i,j) of the Erdos Renyi model.
% - edge_prob: the probability of creating each edge (i,j) of the Erdos Renyi model.
% the generator makes an OR between the graph generated with the ring
% layout and an Erdos Renyi graph.
%
......@@ -22,18 +22,18 @@
% Copyright (c) by Argyris Kalogeratos and Kevin Scaman, 2015.
%---
function G = smallWorldGraph (N, k, p)
function G = smallWorldGraph (N, edge_num, edge_prob)
G = zeros(N,N);
for i = 1:k
for i = 1:edge_num
G = G + diag(ones(N - i, 1), i);
end
G = G + G';
G = max(G, erdosRenyiGraph(N, p));
p = randperm(N);
G = G(p,:);
G = G(:,p);
G = max(G, erdosRenyiGraph(N, edge_prob));
edge_prob = randperm(N);
G = G(edge_prob,:);
G = G(:,edge_prob);
G = sparse(G);
end
......
%allStrategy - It creates a strategy object for the ALL strategy.
%allStrategy - Creates a strategy object for the ALL strategy.
%
% function [treatments, strategyInternalState] = ...
% allStrategy (~, strategyInternalState)
%
% Input:
% - <void par>: this is a dummy parameter for reasons of being compatible
% with other functions that create a strategy object.
% - <void par>: this is a dummy parameter for the function to be compatible
% with other functions that create strategy objects.
% - strategyInternalState: a structure with all the internal information
% of the strategy state.
%
......
......@@ -77,7 +77,7 @@ numNodesToUpdate = nnz(idxUpdatedNodeScores); % old: full(sum(idxUpdatedNodeScor
if (numNodesToUpdate)
% Update sorting:
% idxSorted is a vector (e.g. [5; 10; 2]) with the ids of infected nodes ranked
% acording to the nodeScore value. The ids correspond to the internal numbering of the subset
% according to the nodeScore value. The ids correspond to the internal numbering of the subset
% of infected nodes. So the 3rd id might be the 15th node in the original 1:N numbering.
if (idxVariation ~= -1 && infections(idxVariation))
idxRanked = [idxRanked; idxVariation];
......
......@@ -29,18 +29,18 @@
function [treatments, strategyInternalState] = randomStrategy (idxVariation, strategyInternalState, btot, amongInfected)
% Initialization: computing scores.
% Initialization: computing scores
if(isfield(strategyInternalState, 'infectionSeed'))
strategyInternalState.N = size(strategyInternalState.graph, 1);
strategyInternalState.infections = strategyInternalState.infectionSeed;
strategyInternalState.numInfected = nnz(strategyInternalState.infections);
% Cleaning the internal state.
% Cleaning the internal state
strategyInternalState = rmfield(strategyInternalState, 'graph');
strategyInternalState = rmfield(strategyInternalState, 'infectionSeed');
end
% Update infections if this is not the initialization step.
% Update infections, if this is not the initialization step
if(idxVariation ~= -1)
nextState = ~strategyInternalState.infections(idxVariation);
if (nextState)
......
......@@ -32,7 +32,7 @@
function [treatments, strategyInternalState] = staticStrategy(idxVariation, strategyInternalState, scoringFunction, btot, strategyName, amongInfected, isOrder)
% Initialization: computing scores.
% Initialization: computing scores
if(isfield(strategyInternalState, 'infectionSeed'))
strategyInternalState.N = size(strategyInternalState.graph, 1);
strategyInternalState.infections = strategyInternalState.infectionSeed;
......@@ -45,12 +45,12 @@ if(isfield(strategyInternalState, 'infectionSeed'))
strategyInternalState.idxRanked = scores;
end
% Cleaning the internal state.
% Cleaning the internal state
strategyInternalState = rmfield(strategyInternalState, 'graph');
strategyInternalState = rmfield(strategyInternalState, 'infectionSeed');
end
% Update infections if this is not the initialization step.
% Update infections if this is not the initialization step
if(idxVariation ~= -1)
nextState = ~strategyInternalState.infections(idxVariation);
if (nextState)
......
%hasselfloops - Calculates the density of the non-zero entries of a matrix.
%density - Calculates the density of the non-zero entries of a matrix.
%
% function val = density (M)
%
......
......@@ -17,7 +17,7 @@ function graphProperties (G)
N = size(G,1);
[E, sym] = numGraphEdges(G);
k = 1;
fprintf('-----------------------------------------------------------------\n');
fprintf('Properties | N: %g, E: %g\n', N, E);
......
......@@ -44,6 +44,7 @@ if (size(graphCurves, 3) > 1)
curveStds = 2 * std(graphCurves, 0, 3) / sqrt(numTests);
showStd = 1;
else showStd = 0;
curveStds = [];
end
plotMeanAndStd(time, curveMeans, curveStds, curveNames, varargin{:}, 'showStd', showStd);
......
......@@ -36,7 +36,7 @@ figure; hold on;
if (foundTitle), title(mytitle); end
if (foundxlabel), xlabel(myxlabel); end
if (foundylabel), ylabel(myylabel); end
if (foundshowStd), showStd = 1; end
if (~foundshowStd && ~isempty(yStd)), showStd = 1; end
color = distinguishable_colors(numCurves);
......
......@@ -74,7 +74,7 @@ if (~found || max_infected > N), max_infected = N + 1; end
% Initializes the strategy's internal state by providing the graph and infection seed
strategyInternalState = struct('graph', G, 'infectionSeed', infected, 'issymmetricG', issymmetric(G));
if (strategyInternalState.issymmetricG) % the function to find the nodes of G that are connected to node i (with in- or out-edges)
if (strategyInternalState.issymmetricG) % function which finds the nodes of G that are connected to node i (with in- or out-edges)
strategyInternalState.nghbs = @(G,i) G(:,i);
else strategyInternalState.nghbs = @(G,i) (G(:,i)~=0 | G(i, :)'~=0);
end
......@@ -127,10 +127,9 @@ while (currentTime <= T && ~stopSimulation && iter <= msz)
% then write results for all the time steps which are smaller than currentTime.
diffInTimes = currentTime - timeSteps(iter);
if (diffInTimes >= 0)
numStepsWithNoChange = ceil(diffInTimes / timeStep);
for i=1:numStepsWithNoChange,
% Good for frequent node state changes (usually numStepsWithNoChange == 1) and, actually,
% Proper for frequent node state changes (usually numStepsWithNoChange == 1) and, actually,
% this is the case: more than one node changes to happen among two recording steps.
numInfected(iter) = curNumInfected;
timeSteps(iter + 1) = timeSteps(iter) + timeStep;
......@@ -147,13 +146,13 @@ while (currentTime <= T && ~stopSimulation && iter <= msz)
if (numSteps >= k_steps && curNumInfected > 20) % k_steps begins from a large value, e.g. 100
steps = (floor(1/5 * iter):iter)';
stopSimulation = ~isNonStationaryTest(steps, double(numInfected(steps)'), double(curNumInfected)/10 * timeStep/noSlopeT, 0.01);
k_steps = k_steps * 1.5; % recheck for stationarity later, after so many steps
k_steps = k_steps * 1.5; % recheck for stationarity later, after that many steps
end
iter = iter + 1; % increase the iteration counter
end
end
% Create a transition event, given a vector with transition rates (by normalizing this vector we get a probabilit vector that sums to 1)
% Create a transition event, given a vector with transition rates (although they don't need to be normalized, if so, this vector becomes a probability vector)
idxVariation = rand_probv(probTransition);
idxWasHealthy = noninfected(idxVariation);
......
......@@ -58,15 +58,15 @@ if (~found), verbosityLevel = 1; end
%% 1. NETWORK SPECIFICATION
switch graphPars.graphType
case 'erdosRenyi'
G = erdosRenyiGraph(graphPars.N, graphPars.prob_arete_graph);
case 'preferentialAttachment'
G = preferentialAttachmentGraph(graphPars.N, graphPars.m_arete_graph, graphPars.delta);
case 'smallWorld'
G = smallWorldGraph(graphPars.N, graphPars.m_arete_graph, graphPars.prob_arete_graph);
case 'adjacencyMatrix'
case 'erdosRenyi'
G = erdosRenyiGraph(graphPars.N, graphPars.edge_prob);
case 'preferentialAttachment'
G = preferentialAttachmentGraph(graphPars.N, graphPars.edge_num, graphPars.delta);
case 'smallWorld'
G = smallWorldGraph(graphPars.N, graphPars.edge_num, graphPars.edge_prob);
case 'adjacencyMatrix'
G = graphPars.adjacencyMatrix;
otherwise
otherwise
error('The graphType field of graphPars should be either ''erdosRenyi'', ''preferentialAttachment'', ''smallWorld'', ''adjacencyMatrix''');
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment