quoracle/examples/plot_load_distribution.py

60 lines
2 KiB
Python
Raw Normal View History

2021-02-05 04:59:10 +00:00
from quoracle import *
2021-02-07 20:19:08 +00:00
import argparse
2021-01-29 08:01:47 +00:00
import matplotlib
import matplotlib.pyplot as plt
2021-02-07 20:19:08 +00:00
import os.path
2021-01-29 08:01:47 +00:00
2021-02-07 20:19:08 +00:00
def main(output_directory: str):
2021-01-29 08:01:47 +00:00
a = Node('a', capacity=100)
b = Node('b', capacity=200)
c = Node('c', capacity=100)
d = Node('d', capacity=200)
e = Node('e', capacity=100)
nodes = [a, b, c, d, e]
quorum_systems = {
'majority': QuorumSystem(reads=majority([a, b, c, d, e])),
'crumbling_walls': QuorumSystem(reads=a*b + c*d*e),
'paths': QuorumSystem(reads=a*b + a*c*e + d*e + d*c*b),
}
for name, qs in quorum_systems.items():
2021-02-07 20:19:08 +00:00
dist = {0.0: 1., 0.1: 1., 0.2: 1., 0.3: 1., 0.4: 1., 0.5: 1.,
0.6: 1., 0.7: 1., 0.8: 1., 0.9: 1., 1.0: 1.}
2021-02-01 06:59:02 +00:00
fig, axes = plt.subplots(3, 4, figsize=(6 * 2, 4 * 2), sharey='all')
2021-01-29 08:01:47 +00:00
axes_iter = (axes[row][col] for row in range(3) for col in range(4))
2021-02-07 20:19:08 +00:00
for fr in dist.keys():
2021-01-29 08:01:47 +00:00
sigma = qs.strategy(read_fraction=fr)
ax = next(axes_iter)
plot_load_distribution_on(ax, sigma, nodes)
2021-02-01 06:59:02 +00:00
ax.set_title(f'Optimized For\nRead Fraction = {fr}')
2021-01-29 08:01:47 +00:00
ax.set_xlabel('Read Fraction')
2021-02-01 06:59:02 +00:00
ax.grid()
2021-01-29 08:01:47 +00:00
2021-02-07 20:19:08 +00:00
sigma = qs.strategy(read_fraction=dist)
2021-01-29 08:01:47 +00:00
ax = next(axes_iter)
plot_load_distribution_on(ax, sigma, nodes)
2021-02-01 06:59:02 +00:00
ax.set_title('Optimized For\nUniform Read Fraction')
2021-01-29 08:01:47 +00:00
ax.set_xlabel('Read Fraction')
2021-02-01 06:59:02 +00:00
ax.grid()
2021-01-29 08:01:47 +00:00
axes[0][0].set_ylabel('Load')
axes[1][0].set_ylabel('Load')
axes[2][0].set_ylabel('Load')
fig.tight_layout()
2021-02-07 20:19:08 +00:00
output_filename = os.path.join(output_directory, f'{name}.pdf')
fig.savefig(output_filename)
print(f'Wrote figure to "{output_filename}".')
2021-01-29 08:01:47 +00:00
if __name__ == '__main__':
2021-02-07 20:19:08 +00:00
parser = argparse.ArgumentParser()
parser.add_argument('--output',
type=str,
default='.',
help='Output directory')
args = parser.parse_args()
main(args.output)