2
$\begingroup$

I am new to Mathematica. I created a code where I can evaluate a parameter (denoted by PoA) for a random instance of a directed acyclic graph. The code is below.

     an = 10;
     al = 1;
     nodes = 10;
     links = 25;
     s = DirectedGraph[RandomGraph[{nodes, links}], "Acyclic"];
     M = al*Transpose[AdjacencyMatrix[s]];
     id = an + al*VertexInDegree[s];
     od = al*VertexOutDegree[s];
     wd = -Log[1 - (od/id)];
     wd // MatrixForm
     x = (DiagonalMatrix[id] - M).wd;
     poa = N[od - x, 4]
     PoA = Total@poa;

I now want to generate plots of average PoA vs. nodes, by varying 'nodes' from 5 to 50 and finding average of 1000 instances of PoA. Similarly I want to set nodes to 35 and vary connectivity (defined as links/nodes) and get a plot to find average PoA. How to reuse and extend this code to do the same? Thanks!

$\endgroup$
6
$\begingroup$

Based on your code, we can first create a function PoAGen to generate mean PoA values as follows:

Clear[PoAGen]
PoAGen[nodes_, links_, n_: 1000] := Module[
{an = 10, al = 1, s, M, id, od, wd, x, poa, PoA},
Cases[_?NumericQ]@
Table[s = DirectedGraph[RandomGraph[{nodes, links}], "Acyclic"];
 M = al*Transpose[AdjacencyMatrix[s]];
 id = an + al*VertexInDegree[s];
 od = al*VertexOutDegree[s];
 wd = -Log[1 - (od/id)];
 x = (DiagonalMatrix[id] - M).wd;
 poa = N[od - x, 4];
 PoA = Total@poa, {n}] // Mean
]

Then we can produce the data to plot with Table as follows:

dataPlot = Table[{nodes, links, PoAGen[nodes, links]}, {nodes, 10, 20}, {links, 20, 25}];

Here we go the plot:

ListPlot[dataPlot[[All, 1, {1, 3}]], Frame -> True, FrameLabel -> {"No. of Nodes", "POA"}, PlotLabel -> "No. of Links: " ~~ ToString[dataPlot[[1, 1, 2]]]]

enter image description here

and

ListPlot[dataPlot[[2, All, {2, 3}]], Frame -> True, FrameLabel -> {"No. of Links", "POA"}, PlotLabel -> "No. of Nodes: " ~~ ToString[dataPlot[[1, 1, 1]]]]

enter image description here

Looks you are doing very interesting research! Just modify the dataPlot to produce more samples as you want.

Have fun in research with Mathematica!

$\endgroup$
  • $\begingroup$ Thanks. I have a small question: What does Cases[_?NumericQ]@ do? Especially the symbols _? and @ - what do they stand for? $\endgroup$ – Bravo Apr 20 '15 at 12:33
  • $\begingroup$ @Bravo, it seems the RandomGraph cannot be transformed to DirectredGraph sometimes, whereby PoA is not numeric all the time. As such, I put a Cases there to avoid the non-numeric values that might cause the Mean to fail. $\endgroup$ – sunt05 Apr 20 '15 at 12:36
  • $\begingroup$ As for the _?NumericQ, it is a pattern that restricts the selected elements to be numeric. You may refer to the documentation for a thorough explanation: reference.wolfram.com/language/tutorial/… $\endgroup$ – sunt05 Apr 20 '15 at 12:38
  • $\begingroup$ Thanks. Helped me get an understanding of how to write functions :) $\endgroup$ – Bravo Apr 20 '15 at 12:43
  • $\begingroup$ Enjoy Mathematica:) $\endgroup$ – sunt05 Apr 20 '15 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.