Update Notes on using Lem's algorithm interface authored by Peter Menegay's avatar Peter Menegay
......@@ -46,4 +46,80 @@ digraph G {
}
```
Following the `straight_average_intermediate -- TEST1` in the snippet (XXX) we divide the calculation up into 3 sub-groups: 1,3,4 and 2,5,6 at the bottom level followed by 0,1,2 at the top level.
For sub-group 1,3,4 the opinion and components are defined according to the diagram, noting that Node 1 trusts itself fully (trust_factor = 1) and that `intermediate_results` is empty because we are at the bottom level.
```python
opinion1 = OpinionData([0.5,0.5], 1)
opinion3 = OpinionData([0.6,0.4], 1)
opinion4 = OpinionData([0.7,0.3], 1)
intermediate_results = []
comp1 = ComponentData(opinion1, 1.0, intermediate_results)
comp3 = ComponentData(opinion3, 0.9, intermediate_results)
comp4 = ComponentData(opinion4, 0.9, intermediate_results)
```
Next we define AlgorithmInput and perform the calculation for the sub-group:
```python
alginp134 = AlgorithmInput([comp1, comp3, comp4],{})
output134 = straight_average_intermediate(alginp134)
```
At this point we have intermediate results for the output of the sub-group which can be passed on as input to subsequent calculations. Since comp1 will be in those calculations, we set its `intermediate_results` as follows:
```python
comp1.intermediate_results = output134.intermediate_results
```
Next is the 2,5,6 sub-group which is essentially the same except with different probabilities:
```python
opinion2 = OpinionData([0.5,0.5], 1)
opinion5 = OpinionData([0.8,0.2], 1)
opinion6 = OpinionData([0.9,0.1], 1)
intermediate_results = []
comp2 = ComponentData(opinion2, 1.0, intermediate_results)
comp5 = ComponentData(opinion5, 0.9, intermediate_results)
comp6 = ComponentData(opinion6, 0.9, intermediate_results)
alginp256 = AlgorithmInput([comp2, comp5, comp6],{})
output256 = straight_average_intermediate(alginp256)
print('output256 = ', output256.opinion.pdf_points)
print('inter_results256 = ', output256.intermediate_results)
comp2.intermediate_results = output256.intermediate_results
```
Now we are ready to move a level up and do the 0,1,2 sub-group. Since we haven't defined `comp0` yet we do so as follows, noting that it starts with no `intermediate_results`:
```python
opinion0 = OpinionData([0.5,0.5], 1)
intermediate_results = []
comp0 = ComponentData(opinion0, 1.0, intermediate_results)
```
We also reset the `trust_factor` for `comp1` and `comp2` because now we are representing the trust Node 0 has for them, rather than the trust they have for themselves (as we did previously):
```python
comp1.trust_factor = 0.9
comp2.trust_factor = 0.9
```
Next is to define the AlgorithmInput and do the calculation:
```python
alginp012 = AlgorithmInput([comp0, comp1, comp2],{})
output012 = straight_average_intermediate(alginp012)
```
Since we're at the top node, this result contains the final average result of the calculation and is seen in the following print statement:
```python
print('output012 = ', output012.opinion.pdf_points)
```
We don't need to do this since we're done but if Node 0 were to require transmission of its results to yet a higher level node, we would have to:
```python
comp0.intermediate_results = output012.intermediate_results
```